简体   繁体   English

OpenGL背面剔除导致奇怪的结果

[英]OpenGL Back Face Culling Causing Weird Results

I'm trying to create a simple cell shader effect, and it involves back and front face culling. 我正在尝试创建一个简单的单元格着色器效果,它涉及背面和正面剔除。 But so far, my culling results are rather odd. 但到目前为止,我的淘汰结果相当奇怪。

Setup OpenGL source code: 设置OpenGL源代码:

// init matrices
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 800 / 600, 0.01f, 150);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// enable opengl caps
glEnable(GL_NORMALIZE);
glEnable(GL_CULL_FACE);
// cell shading effects
glLineWidth(3.5f);
// setup display list
pDList = glGenLists(1);
glNewList(pDList, GL_COMPILE);
    glBegin(GL_QUADS);
        // front
        glNormal3f(0, 0, 1);
        glVertex3f(-0.25f, -0.5f, -3);
        glVertex3f(0.25f, -0.5f, -3);
        glVertex3f(0.25f, -1, -3);
        glVertex3f(-0.25f, -1, -3);
        // back
        glNormal3f(0, 0, -1);
        glVertex3f(-0.25f, -0.5f, -3.5f);
        glVertex3f(0.25f, -0.5f, -3.5f);
        glVertex3f(0.25f, -1, -3.5f);
        glVertex3f(-0.25f, -1, -3.5f);
        // top 
        glNormal3f(0, 1, 0);
        glVertex3f(-0.25f, -0.5f, -3.5f);
        glVertex3f(0.25f, -0.5f, -3.5f);
        glVertex3f(0.25f, -0.5f, -3);
        glVertex3f(-0.25f, -0.5f, -3);
        // bottom
        glNormal3f(0, -1, 0);
        glVertex3f(-0.25f, -1, -3.5f);
        glVertex3f(0.25f, -1, -3.5f);
        glVertex3f(0.25f, -1, -3);
        glVertex3f(-0.25f, -1, -3);
        // right
        glNormal3f(1, 0, 0);
        glVertex3f(0.25f, -0.5f, -3);
        glVertex3f(0.25f, -0.5f, -3.5f);
        glVertex3f(0.25f, -1, -3.5f);
        glVertex3f(0.25f, -1, -3);
        // left
        glNormal3f(-1, 0, 0);
        glVertex3f(-0.25f, -0.5f, -3);
        glVertex3f(-0.25f, -0.5f, -3.5f);
        glVertex3f(-0.25f, -1, -3.5f);
        glVertex3f(-0.25f, -1, -3);
    glEnd();
glEndList();

Render source code: 渲染源代码:

// clear
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.75f, 0.75f, 0.75f, 1);
// reset matrix
glLoadIdentity();
// draw cube
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glCullFace(GL_BACK);
glColor3i(0, 0, 0);
glCallList(pDList);

This is what I got: 这就是我得到的:

在此输入图像描述

For some reason it's only drawing the right side of the cube. 出于某种原因,它只是绘制了立方体的右侧。 Weird. 奇怪的。 I changed the glCullFace(GL_BACK); 我更改了glCullFace(GL_BACK); to glCullFace(GL_FRONT); 到glCullFace(GL_FRONT); in my render function. 在我的渲染功能中。 I got this: 我懂了:

在此输入图像描述

Now it's drawing every side? 现在它正在吸引每一方? It seems to think that the right side of the cube is both back and front facing, and the rest of the cube is facing back. 它似乎认为立方体的右侧是背面和正面,而立方体的其余部分面向后面。 I also get this same result shown in the picture above when i have culling disabled. 当我禁用剔除时,我也得到了上图所示的相同结果。

Any other relevant info I can think of is that I'm using SDL, and that I'm sure this isn't a graphics card issue, as I've ran this on two different computers, each yielding the same result. 我能想到的任何其他相关信息都是我正在使用SDL,而且我确定这不是一个显卡问题,因为我在两台不同的计算机上运行它们,每一台都产生相同的结果。

I just started to learn how to set normals for a primitive so chances are this is a big n00b mistake, or some dumb error. 我刚刚开始学习如何为原语设置法线,所以很可能这是一个很大的错误,或者是一些愚蠢的错误。 I'd be a millionaire if I got a penny every time I did the most thoughtless obvious mistake and have been stuck on it for hours. 如果每次我犯了一个最轻率的明显错误并且被困在它上好几个小时,我就会成为一个百万富翁。

So, in a nutshell, what have I done wrong, and how to I get the proper faced sides to cull or not cull in the proper way? 所以,简而言之,我做错了什么,以及如何以适当的方式剔除或不剔除正确的方面?

If you look at each triangle from the vector of its minus-normal you can define the order in which your vertices are specified (it is called winding ): clockwise or counterclockwise. 如果从它的负法向量的矢量中查看每个三角形,您可以定义顶点的指定顺序(称为winding ):顺时针或逆时针。

You have to be consistent with it and specify all vertices in either CW or CCW winding to make the culling work correctly. 您必须与它保持一致并指定CWCCW绕组中的所有顶点,以使剔除工作正确。

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

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