简体   繁体   中英

View or perspective matrices messing with culling

I have this code here that almost works fine, but it is culling the front faces instead of the back faces:

auto scaleMatrix = glm::scale(glm::mat4(1.0f), glm::fvec3(0.5f, 0.5f, 0.5f));
auto rotateMatrix = glm::rotate(glm::mat4(1.0f), glm::degrees(rotation), glm::fvec3{.0f, 1.f, 0.f});
auto modelMatrix = rotateMatrix * scaleMatrix;

int w, h;
glfwGetWindowSize(window, &w, &h);
auto perspectiveMatrix = glm::perspective(90.f, w/(float)h, .1f, 100.f);

auto viewMatrix = glm::lookAt(
        glm::vec3(-1, 0, -1),
        glm::vec3(0, 0, 0),
        glm::vec3(0, 1, 0)
        );

auto mvp = perspectiveMatrix * viewMatrix * modelMatrix;

glUniformMatrix4fv(gMVPLocation, 1, GL_FALSE, &mvp[0][0]);

If I only pass the model matrix to the shader, the correct faces cull. But if I am passing the MVP matrix, the front faces are culled while the back faces are rendered. Not sure why this would cause this issue, but my vertices are defined in ccw winding order and work perfectly fine if I don't use a view or perspective matrix. I'm clueless as to why this doesn't work here.

When you don't have any transforms (ie, you supply coordinates directly in clip space) and a standard depth buffer, you have a left-handed coordinate system where the z-axis points away from the viewer. By applying a model matrix with positive determinant (like rotations or positive scalings), you do not change the orientation.

If you use the view-projection matrix from glm, you have a right-handed coordinate system where the z-axis points towards the viewer. Hence, your model will revert its orientation. To solve the problem, either revert the orientation of your faces or set the direction of your front sides using glFrontFace .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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