简体   繁体   English

我应该以什么顺序将我的顶点发送到 OpenGL 进行剔除

[英]In What Order Should I Send My Vertices To OpenGL for Culling

I'm learning a spot of 3d opengl, and it's going rather well, I've got a nice camera moving about and some simple cube objects, at the moment.我正在学习一个 3d opengl 点,并且进展顺利,目前我有一个漂亮的相机和一些简单的立方体对象。 Currently using vertex arrays, but I'm swapping to VBOs pretty quick here.目前使用顶点数组,但我在这里很快切换到 VBO。 I'm just trying to enable culling, however I'm not sure what order in which I ought to specify my vertices, right now this is what I'm doing:我只是想启用剔除,但是我不确定我应该以什么顺序指定我的顶点,现在这就是我正在做的:

void cube::update_verts(){
GLushort cur=0;

///back face
verts[cur++]=x; verts[cur++]=y; verts[cur++]=z;
verts[cur++]=x+sx; verts[cur++]=y; verts[cur++]=z;
verts[cur++]=x+sx; verts[cur++]=y+sy; verts[cur++]=z;
verts[cur++]=x; verts[cur++]=y+sy; verts[cur++]=z;

///right face
verts[cur++]=x+sx; verts[cur++]=y+sy; verts[cur++]=z;
verts[cur++]=x+sx; verts[cur++]=y; verts[cur++]=z;
verts[cur++]=x+sx; verts[cur++]=y; verts[cur++]=z+sz;
verts[cur++]=x+sx; verts[cur++]=y+sy; verts[cur++]=z+sz;

///top face
verts[cur++]=x+sx; verts[cur++]=y+sy; verts[cur++]=z;
verts[cur++]=x; verts[cur++]=y+sy; verts[cur++]=z;
verts[cur++]=x; verts[cur++]=y+sy; verts[cur++]=z+sz;
verts[cur++]=x+sx; verts[cur++]=y+sy; verts[cur++]=z+sz;

///front face
verts[cur++]=x; verts[cur++]=y; verts[cur++]=z+sz;
verts[cur++]=x+sx; verts[cur++]=y; verts[cur++]=z+sz;
verts[cur++]=x+sx; verts[cur++]=y+sy; verts[cur++]=z+sz;
verts[cur++]=x; verts[cur++]=y+sy; verts[cur++]=z+sz;

///bottom face
verts[cur++]=x+sx; verts[cur++]=y; verts[cur++]=z;
verts[cur++]=x; verts[cur++]=y; verts[cur++]=z;
verts[cur++]=x; verts[cur++]=y; verts[cur++]=z+sz;
verts[cur++]=x+sx; verts[cur++]=y; verts[cur++]=z+sz;

///left face
verts[cur++]=x; verts[cur++]=y+sy; verts[cur++]=z;
verts[cur++]=x; verts[cur++]=y; verts[cur++]=z;
verts[cur++]=x; verts[cur++]=y; verts[cur++]=z+sz;
verts[cur++]=x; verts[cur++]=y+sy; verts[cur++]=z+sz;


}

///Drawing Code:

glVertexPointer(3,GL_FLOAT,0,object.verts);
glColorPointer(3,GL_UNSIGNED_BYTE,0,object.colors);
glDrawArrays(GL_QUADS,0,6*4);

However it's definitely quite wrong, because when I glEnable(GL_CULL_FACE);然而这绝对是错误的,因为当我glEnable(GL_CULL_FACE); my cubes don't show the correct faces (as seen below).我的立方体没有显示正确的面(如下所示)。

Normal普通的从顶部的常规视图

Problem Child问题少年从侧面看

With both of these images culling is enabled.启用这两个图像剔除。

In what order should I specify the vertices?我应该以什么顺序指定顶点?


(EDIT) Updated Working Function: (编辑)更新的工作功能:

void cube::update_verts(){
GLushort cur=0;

///top face
verts[cur++]=x; verts[cur++]=y+sy; verts[cur++]=z;
verts[cur++]=x; verts[cur++]=y+sy; verts[cur++]=z+sz;
verts[cur++]=x+sx; verts[cur++]=y+sy; verts[cur++]=z+sz;
verts[cur++]=x+sx; verts[cur++]=y+sy; verts[cur++]=z;


///bottom face
verts[cur++]=x; verts[cur++]=y; verts[cur++]=z;
verts[cur++]=x+sx; verts[cur++]=y; verts[cur++]=z;
verts[cur++]=x+sx; verts[cur++]=y; verts[cur++]=z+sz;
verts[cur++]=x; verts[cur++]=y; verts[cur++]=z+sz;

///left face
verts[cur++]=x; verts[cur++]=y; verts[cur++]=z;
verts[cur++]=x; verts[cur++]=y; verts[cur++]=z+sz;
verts[cur++]=x; verts[cur++]=y+sy; verts[cur++]=z+sz;
verts[cur++]=x; verts[cur++]=y+sy; verts[cur++]=z;

///right face
verts[cur++]=x+sx; verts[cur++]=y; verts[cur++]=z;
verts[cur++]=x+sx; verts[cur++]=y+sy; verts[cur++]=z;
verts[cur++]=x+sx; verts[cur++]=y+sy; verts[cur++]=z+sz;
verts[cur++]=x+sx; verts[cur++]=y; verts[cur++]=z+sz;

///front face
verts[cur++]=x; verts[cur++]=y; verts[cur++]=z+sz;
verts[cur++]=x+sx; verts[cur++]=y; verts[cur++]=z+sz;
verts[cur++]=x+sx; verts[cur++]=y+sy; verts[cur++]=z+sz;
verts[cur++]=x; verts[cur++]=y+sy; verts[cur++]=z+sz;


///back face
verts[cur++]=x; verts[cur++]=y; verts[cur++]=z;
verts[cur++]=x; verts[cur++]=y+sy; verts[cur++]=z;
verts[cur++]=x+sx; verts[cur++]=y+sy; verts[cur++]=z;
verts[cur++]=x+sx; verts[cur++]=y; verts[cur++]=z;

}

By default?默认情况下? In counter-clockwise order.按逆时针顺序。

Consider a triangle facing the camera:考虑一个面向相机的三角形:

A
|\
| \
|  \
B---C

A->B->C would be front facing (counter-clockwise order), A->C->B would be rear-facing (clockwise order). A->B->C 为正面(逆时针顺序),A->C->B 为背面(顺时针顺序)。

You can change which way OpenGL considers "front facing" via glFrontFace() :您可以通过glFrontFace()更改 OpenGL 考虑“正面”的方式:

The projection of a polygon to window coordinates is said to have clockwise winding if an imaginary object following the path from its first vertex, its second vertex, and so on, to its last vertex, and finally back to its first vertex, moves in a clockwise direction about the interior of the polygon.如果一个假想的对象沿着从其第一个顶点、第二个顶点等到最后一个顶点,最后回到第一个顶点的路径,在一个围绕多边形内部顺时针方向。 The polygon's winding is said to be counterclockwise if the imaginary object following the same path moves in a counterclockwise direction about the interior of the polygon.如果沿着相同路径的假想物体绕多边形内部沿逆时针方向移动,则多边形的缠绕称为逆时针方向。 glFrontFace specifies whether polygons with clockwise winding in window coordinates, or counterclockwise winding in window coordinates, are taken to be front-facing. glFrontFace指定窗口坐标中顺时针缠绕的多边形或窗口坐标中逆时针缠绕的多边形是否被视为正面。 Passing GL_CCW to mode selects counterclockwise polygons as front-facing;GL_CCW传递给mode选择逆时针多边形作为正面; GL_CW selects clockwise polygons as front-facing. GL_CW选择顺时针多边形作为正面。

By default, counterclockwise polygons are taken to be front-facing.默认情况下,逆时针多边形被视为正面。

For ordering your vertices, consider an ideal cube:要对顶点进行排序,请考虑一个理想的立方体:

  6---7
 /|  /|
2---3 |
| 4-|-5
|/  |/ 
0---1

For each face mentally rotate it to face the camera (your mind's eye):对于每张脸,在精神上旋转它以面对相机(你的心灵之眼):

Sides:
2---3  3---7  7---6  6---2
|   |  |   |  |   |  |   |
|   |  |   |  |   |  |   |
0---1  1---5  5---4  4---0

Bottom/Top
0---1  6---7
|   |  |   |
|   |  |   |
4---5  2---3

Then you can just visually read off the quads or triangle pairs in the right counter-clockwise order:然后,您可以以正确的逆时针顺序直观地读出四边形或三角形对:

2---3                3         2---3 
|   |  becomes      /|   and   |  / 
|   |             /  |         |/ 
0---1            0---1         0 

Triangles 0-1-3 and 0-3-2
Quad 0-1-3-2

It doesn't matter which vertex you start the triangle/quad with, for example with the first triangle 0-1-3 , 1-3-0 , and 3-0-1 are all equally valid, front-facing triangles.您从哪个顶点开始三角形/四边形并不重要,例如第一个三角形0-1-31-3-03-0-1都是同样有效的正面三角形。

I learned another rule of thumb (literally) for determining the vertex order known as the "right hand rule".我学到了另一个用于确定顶点顺序的经验法则(字面意思),称为“右手法则”。
Imagine your open hand (right) inside the cube with your thumb pointing towards the center of the cube.想象你张开的手(右)在立方体内,拇指指向立方体的中心。 If you then curl your hand into a fist, your fingers will pass the vertices in the correct order.如果您随后将手握成拳头,您的手指将以正确的顺序通过顶点。 Since you are using your right hand for this it is called the "right hand rule".由于您为此使用右手,因此称为“右手法则”。

Conversely, if you start with your left hand and point your thumb away from the center of the cube, your fingers will again sweep the vertices in the correct order.相反,如果您从左手开始并将拇指指向远离立方体中心的方向,您的手指将再次以正确的顺序扫过顶点。 This is known as the "left hand rule" (surprise).这被称为“左手规则”(惊喜)。

Both methods work to give you counter-clockwise ordering.这两种方法都可以为您提供逆时针排序。 For clockwise ordering, just use the opposite hand.对于顺时针排序,只需使用相反的手。

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

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