简体   繁体   English

使用GL_TRIANGLES的OpenGL奇怪的渲染

[英]OpenGL strange render using GL_TRIANGLES

I'm getting a strange effect rendering these spheres/cylinders I coded myself. 我在渲染自己编写的这些球体/圆柱体时得到一种奇怪的效果。

The vertices and indices are correct, since when I use GL_LINE_STRIP in glDrawElements instead of GL_TRIANGLES, my objects are rendering as intended, so there's no duplicated vertices, but when I invert the indices order, I get the same effect. 顶点和索引是正确的,因为当我在glDrawElements中使用GL_LINE_STRIP而不是GL_TRIANGLES时,我的对象正按预期进行渲染,因此没有重复的顶点,但是当我反转索引顺序时,会得到相同的效果。

Here arethe objects rendered with GL_TRIANGLES, and you can see these inner circles being rendered within them: 以下是使用GL_TRIANGLES渲染的对象,您可以看到其中正在渲染的这些内部圆圈:

http://imgur.com/4ocO2Bt http://imgur.com/4ocO2Bt

Here are the objects with GL_LINE_STRIP, and the geometry looks fine to me: 这是带有GL_LINE_STRIP的对象,其几何形状对我来说看起来不错:

http://imgur.com/f0wGNC1 http://imgur.com/f0wGNC1

My vertex shader code is a standard MVP * vertex position transform, and pixel shader code is just outputting a color with nothing else done. 我的顶点着色器代码是标准的MVP *顶点位置变换,而像素着色器代码只是输出一种颜色,而无其他操作。

And just to be clear, even if I invert the indices, I still get the exact same image with GL_TRIANGLES, I still see through the object, with the same exact circles inside the mesh. 而且要清楚一点,即使我颠倒了索引,我仍然可以使用GL_TRIANGLES获得完全相同的图像,仍然可以看到对象,并且网格内部具有相同的圆形。

If glCullFace ( GL_FRONT); 如果是glCullFace ( GL_FRONT); fixes your problem and you intend to render the geometry with lighting then you should do one of these: 解决了您的问题,并且打算使用照明渲染几何图形,则应执行以下操作之一:

Your triangles are drawn in CW(clockwise) fashion thus OpenGL calculates that the triangles closer to you are facing backwards and culls them since default is glCullFace ( GL_BACK); 您的三角形是以CW(顺时针)方式绘制的,因此OpenGL计算得出,距离您最近的三角形朝后,并将其剔除,因为默认情况下是glCullFace ( GL_BACK); You can simply call it with GL_FRONT and fix the problem. 您可以简单地使用GL_FRONT调用它并解决问题。 However, when you are going to calculate the normal for the triangle the normal will be at the opposite direction. 但是,当您要计算三角形的法线时,法线将处于相反的方向。 You can invert the normal or fix it with drawing triangles in CCW. 您可以反转法线或使用CCW中的绘制三角形对其进行修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM