View or perspective matrices messing with culling












1














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.










share|improve this question






















  • You should visualize in your mind (or paper) where a triangle is (its coordinates) after scaling & rotating, as seen from the camera you set with lookAt. I guess you'll see the back-face. Notice z-coord will be < 0.
    – Ripi2
    Nov 22 at 19:26


















1














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.










share|improve this question






















  • You should visualize in your mind (or paper) where a triangle is (its coordinates) after scaling & rotating, as seen from the camera you set with lookAt. I guess you'll see the back-face. Notice z-coord will be < 0.
    – Ripi2
    Nov 22 at 19:26
















1












1








1


1





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.










share|improve this question













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.







c++ opengl glfw






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 18:17









Potato

264




264












  • You should visualize in your mind (or paper) where a triangle is (its coordinates) after scaling & rotating, as seen from the camera you set with lookAt. I guess you'll see the back-face. Notice z-coord will be < 0.
    – Ripi2
    Nov 22 at 19:26




















  • You should visualize in your mind (or paper) where a triangle is (its coordinates) after scaling & rotating, as seen from the camera you set with lookAt. I guess you'll see the back-face. Notice z-coord will be < 0.
    – Ripi2
    Nov 22 at 19:26


















You should visualize in your mind (or paper) where a triangle is (its coordinates) after scaling & rotating, as seen from the camera you set with lookAt. I guess you'll see the back-face. Notice z-coord will be < 0.
– Ripi2
Nov 22 at 19:26






You should visualize in your mind (or paper) where a triangle is (its coordinates) after scaling & rotating, as seen from the camera you set with lookAt. I guess you'll see the back-face. Notice z-coord will be < 0.
– Ripi2
Nov 22 at 19:26














1 Answer
1






active

oldest

votes


















3














When you don't have any transforms (i.e., 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.






share|improve this answer





















  • Just had my z coords of my model wrong, didn't realize -z is away from camera and was storing vertices as if further away was +z.
    – Potato
    Nov 22 at 21:33











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436372%2fview-or-perspective-matrices-messing-with-culling%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














When you don't have any transforms (i.e., 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.






share|improve this answer





















  • Just had my z coords of my model wrong, didn't realize -z is away from camera and was storing vertices as if further away was +z.
    – Potato
    Nov 22 at 21:33
















3














When you don't have any transforms (i.e., 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.






share|improve this answer





















  • Just had my z coords of my model wrong, didn't realize -z is away from camera and was storing vertices as if further away was +z.
    – Potato
    Nov 22 at 21:33














3












3








3






When you don't have any transforms (i.e., 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.






share|improve this answer












When you don't have any transforms (i.e., 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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 18:35









Nico Schertler

25.1k42350




25.1k42350












  • Just had my z coords of my model wrong, didn't realize -z is away from camera and was storing vertices as if further away was +z.
    – Potato
    Nov 22 at 21:33


















  • Just had my z coords of my model wrong, didn't realize -z is away from camera and was storing vertices as if further away was +z.
    – Potato
    Nov 22 at 21:33
















Just had my z coords of my model wrong, didn't realize -z is away from camera and was storing vertices as if further away was +z.
– Potato
Nov 22 at 21:33




Just had my z coords of my model wrong, didn't realize -z is away from camera and was storing vertices as if further away was +z.
– Potato
Nov 22 at 21:33


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436372%2fview-or-perspective-matrices-messing-with-culling%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu