Jump to content
  • Advertisement
Sign in to follow this  
CirdanValen

OpenGL Can't figure out vertex winding

This topic is 1380 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a project with OpenGL and am trying to figure out vertex winding. I understand that, by default OpenGL considers faces where the vertices are "wound" counter-clock wise are front facing. I've built a cube out of individual faces, however, OpenGL keeps culling them away...which I have narrowed down to being a vertex winding issue.
 
Currently, for the front face (pointing towards the +z axis, camera is pointing down the -z axis) is wound as such:
0-----2
|     /
|   /
1 /
      0
    / |
   /  |
  /   |
1------2
Which, to me....looks counter clockwise. However, the triangles are only visible when I turn on glCullFace(GL_FRONT). Does it have something to do with the direction that the final edge connection is going?
 
Is there something I'm not understanding about what is considered front-facing or something with counter-clock wise winding?
 
Note: I'm rendering with GL_TRIANGLES Edited by CirdanValen

Share this post


Link to post
Share on other sites
Advertisement

Currently, for the front face (pointing towards the +z axis, camera is pointing down the -z axis) is wound as such:

if the triangle is pointing towards the +z axis, then the vertex order from the camera view is:

2-------0
 \      |
   \    |
     \  |
        1

Therefor clockwise. Could this be the fault ?

Edited by Ashaman73

Share this post


Link to post
Share on other sites

This is the result with default culling settings (remove back faces, frontface = CCW)
 

kTQo89l.png

 

With this setup:

void addPlaneSouth(const glm::vec3& offset = glm::vec3(0.f),
                       const glm::vec3& rotation = glm::vec3(0.f),
                       float scale = 1.f)
    {
            growVertexArray(6);
            uint32 index = _currentMesh->vertexCount;
 
             // Top right
            _currentMesh->vertexArray[index++] = Vertex((0.5f*scale) + offset.x, 
                                                        (-0.5f*scale) + offset.y,   
                                                        (0.5f*scale) + offset.z, 
                                                        1.f, 0.f); 
         
 
            // Bottom right 
            _currentMesh->vertexArray[index++] = Vertex((0.5f*scale) + offset.x, 
                                                        (0.5f*scale) + offset.y,   
                                                        (0.5f*scale) + offset.z, 
                                                        0.f, 1.f); 
 
            // Top left
            _currentMesh->vertexArray[index++] = Vertex((-0.5f*scale) + offset.x, 
                                                        (-0.5f*scale) + offset.y,   
                                                        (0.5f*scale) + offset.z, 
                                                        0.f, 0.f); 
 
 
          
            // Triangle 2
 
            // Top left 
            _currentMesh->vertexArray[index++] = Vertex((-0.5f*scale) + offset.x, 
                                                        (-0.5f*scale) + offset.y,   
                                                        (0.5f*scale) + offset.z, 
                                                        1.f, 0.f); 
 
            // Bottom left
            _currentMesh->vertexArray[index++] = Vertex((-0.5f*scale) + offset.x, 
                                                        (0.5f*scale) + offset.y,   
                                                        (0.5f*scale) + offset.z, 
                                                        0.f, 1.f); 
 
            // Bottom right
            _currentMesh->vertexArray[index++] = Vertex((0.5f*scale) + offset.x, 
                                                        (0.5f*scale) + offset.y,   
                                                        (0.5f*scale) + offset.z, 
                                                        1.f, 1.f); 
 
            
           _currentMesh->vertexCount += 6;
 
    }

Share this post


Link to post
Share on other sites

Ok, first analyse your both tris in world space (do not consider the camera):

Tri1 (according to your corner naming)
2-----0
 \   |
  \  |    CW
     1



Tri 2
0
| \    CCW !
|   \
1----2

Now view it from the camera (looking along the negative z-axis:

Tri 1
0---- 2
|   /
|  /     CCW !
1

Tri 2
    0
  / |    CW 
 /  |
2---1

You use two different vertex orders, therefor only one tri is visible, and that should be Tri 1(front face is CCW and backface is CW , which get culled)

 

But, isn't your up-vector looking along the positive y-axis ?

Then your naming would be wrong and the tris would look like this (world space):

Tri1
    1
  / |  CCW
 /  |
2---0

Tri 2
1----2
|   /  CW
| /
0

In this case, if your camera would look along the positive z axis, you would  see the Tri 1.

 

So, double check your camera setup, for each corner and tri use unique corner colors to track the issue down.

Share this post


Link to post
Share on other sites

Okay, thanks for the tips...I think I figured it out. Instead of thinking about winding around the center of the triangle, it helps to think about it winding around the quad. By default -y was up, so I ended up rotating the view matrix.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!