-1
\$\begingroup\$

I'm trying to use an orthographic projection to allow me to specify the dimensions and position of rendered objects using exact pixel coordinates (like in this tutorial: https://learnopengl.com/#!In-Practice/2D-Game/Rendering-Sprites)

So I have this vertex data, and I setup the ortho projection matrix like this:

glm::vec2 vertices[] = {
    // X, Y
    glm::vec2(0.0f, 1.0f),
    glm::vec2(1.0f, 0.0f),
    glm::vec2(0.0f, 0.0f),

    glm::vec2(0.0f, 1.0f),
    glm::vec2(1.0f, 1.0f),
    glm::vec2(1.0f, 0.0f),
};

glm::mat4 projection = glm::ortho(0.0f, 1024.0f, 768.0f, 0.0f, -1.0f, 1.0f);

and I set my shader's projection uniform to that value.

The problem is that I can't see any rendered objects if I use the projection matrix. I know that the rest of my program works, because I can see the rendered square if I remove the projection matrix from my shader:

gl_Position = model * view * vec4(position.xy, 0.0, 1.0);

Then the triangles are rendered as you would expect. But in the tutorial, they use integers to specify the exact size and position (in screen pixels) to render the object, which doesn't work without the orthographic projection matrix.

My full code is here: https://github.com/Oddity/basic-2D-opengl (sorry about the hand-made Python 2 build script)

\$\endgroup\$

closed as off-topic by Gnemlock, DMGregory, Vaillancourt, Tyyppi_77, doppelgreener Jul 21 '17 at 18:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem must provide a minimal, complete, verifiable example of the issue so that readers can diagnose it without needing to guess, read all of your code, or engage in extensive back-and-forth dialog. For more information, see this meta thread." – Gnemlock, DMGregory, Vaillancourt, Tyyppi_77, doppelgreener
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ If your having a problem that needs to be debugged, you need to provide us with a minimal and verifiable example of the code. We can not be expected to dig through your github repo to find the solution, sorry. \$\endgroup\$ – Gnemlock Jul 11 '17 at 6:51
  • \$\begingroup\$ The github repo is a minimal and verifiable example of the code. \$\endgroup\$ – Terran Jul 11 '17 at 9:10
  • \$\begingroup\$ Maybe so; I have not looked. We don't include links when judging questions; mostly because links die. If your github contains the minimum example, you need to copy it into the body of your question. \$\endgroup\$ – Gnemlock Jul 11 '17 at 11:24
  • \$\begingroup\$ Seems like a projection 1 pix = 1 unit, and since the triangles are half a square unit, I wouldn't be surprised if they were too small to be noticed when rendered. \$\endgroup\$ – wondra Jul 11 '17 at 13:03
3
\$\begingroup\$

I believe you're multiplying in GLSL in the wrong order:

gl_Position = projection * LookAt * model * vec4(position.xy, 0.0, 1.0);

Remember that matrix multiplication is not a commutative operation.

I feel I should emphasize that the Projection matrix must be the last in the order of multiplication(the order should be read in the reverse order; i.e.: First comes the model * vertex multiplication, then the result is multiplied by the LookAt matrix, and the last one is the Projection matrix;).

\$\endgroup\$
  • \$\begingroup\$ You were right. Thanks! I'd upvote but my account is too new. I had no idea that the order of multiplication could matter... \$\endgroup\$ – Terran Jul 11 '17 at 6:14
  • \$\begingroup\$ No worries, I'm here to help and be helped, not earn points. \$\endgroup\$ – curious student Jul 11 '17 at 7:58
  • \$\begingroup\$ @Terran mark this answer as correct (green tick) if it solved your problem \$\endgroup\$ – Bálint Jul 11 '17 at 9:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.