title: OpenGL: Depth buffering working backwards, is it in the winding order?
body: <p>I have been working on my opengl model viewer and have run into a problem. When I render my scene it seemed like the models were see through. I looked into it and thought maybe my depth buffer wasn't working but I had the code in place for it:</p>
  
  <pre><code>glEnable(GL_DEPTH_TEST)
  glDepthFunc(GL_LEQUAL)
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  </code></pre>
  
  <p>and the test for the depth buffer</p>
  
  <pre><code>glGetIntegerv(GL_DEPTH_BITS)
  </code></pre>
  
  <p>returns a non 0 value (it returns 24). So I went deeper into testing it, and found that it wasn't that the depth buffer didn't work, it's that it works in reverse. It shows the back faces over the front ones. If I call glCullFace(GL_FRONT), the model's render normally. This is great but it poses an issue of why that happens, and I should probably get to the root of the problem. By looking around it seems like my winding order may be the issue, I built my own matrix stack for the modelviewprojectionmatrix and I think that may be the problem. If I render my scene with the camera moved backwards 20 units, with a horizontal fov of .785 rads, aspect of 1.333, clipplaneNear of 1.000, and clipplaneFar of 30.000 my matrices look like this:</p>
  
  <pre><code>model: 
  [ 1 0 0 0 ]
  [ 0 1 0 0 ]
  [ 0 0 1 0 ]
  [ 0 0 0 1 ]
  view: 
  [ 1   0   0   0 ]
  [ 0   1   0   0 ]
  [ 0   0   1   0 ]
  [ 0   0   -20 1 ]
  proj:  
  [ 2.414214  0         0         0  ]
  [ 0         -3.218951 0         0  ]
  [ 0         0         -1.068966 -1 ]
  [ 0         0         -2.068966 0  ]
  </code></pre>
  
  <p>When I have cullface on front this renders exactly how I want it to, with the object properly rendered 20 units away from the camera. I can spin it and move it like I want to and everything works. But without cullface the depth is reversed, is my z value messed up somewhere? Everything looks similar to the resources I've been working with so far. And if it's not that then where is this backwards problem coming from?</p>