1
\$\begingroup\$

I just want to draw a simple triangle using GL_LINES primitive with orthographic projection matrix.

This is my vertices...

float_t vertices[]{
    -0.5f, -0.5f, 0.0f,     0.0f, 0.0f, 1.0f,
     0.5f, -0.5f, 0.0f,     0.0f, 0.0f, 1.0f,
     0.5f, -0.5f, 0.0f,     0.0f, 0.0f, 1.0f,
     0.0f,  0.5f, 0.0f,     0.0f, 0.0f, 1.0f,
     0.0f,  0.5f, 0.0f,     0.0f, 0.0f, 1.0f,
    -0.5f, -0.5f, 0.0f,     0.0f, 0.0f, 1.0f
};

And this is my matrices(model, view, projection)...

glm::mat4 model = glm::mat4(1.0f);
prog.setMat4fv1("model", model);

glm::mat4 view = glm::mat4(1.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
prog.setMat4fv1("view", view);

glm::mat4 projection = glm::mat4(1.0f);
projection = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f, 0.1f, 100.0f);
prog.setMat4fv1("projection", projection);

And this is the code in the while loop (game loop) that draws a triangle...

prog.useProgram();
glBindVertexArray(vao);
glDrawArrays(GL_LINES, 0, 6);

I see nothing. But when I change the code for projection matrix from glm::ortho() to glm::perspective(), it draws a triangle which is obviously smaller than the original one because of the code view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f)); which moves all the objects a little bit away from the camera.(I am not coding for the camera class at this time.)

So this is the code for projection matrix which works fine...

projection = glm::perspective(glm::radians(45.0f), (float_t)win_width / (float_t)win_height, 0.1f, 100.0f);

Am I doing something wrong with glm::ortho()? I can only create a model matrix and it's just fine. But this the first time I am using glm::ortho() and I don't see any examples on the internet. How can I solve the issuse with glm::ortho()?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ The ortho matrix extents are very big, that will make the triangle very small. Try with projection = glm::ortho(0.0f, 4.0f, 0.0f, 3.0f, 0.1f, 100.0f);. After that, you might notice that the triangle is not centered, try with this projection = glm::ortho(-2.0f, +2.0f, -1.5f, +1.5f, 0.1f, 100.0f); \$\endgroup\$
    – tuket
    Commented Aug 10, 2021 at 6:49
  • \$\begingroup\$ @tuket Wow. Thanks man! Now it works. I also forget the fact that when we use orthographic projection, all the vertex coordinates are directly mapped to our screen so they becomes pixel coordinates. That's why my triangle was very small and I can't see it. I aslo want to know that when I want to make a simple 2d game with using projection = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f, 0.1f, 100.0f); and scale my triangle with model matix, is it fine? Or should I put smaller values in the glm::ortho()? Thanks. \$\endgroup\$ Commented Aug 10, 2021 at 7:13
  • \$\begingroup\$ Glad it was useful! I have made a post answering that question \$\endgroup\$
    – tuket
    Commented Aug 10, 2021 at 8:31

1 Answer 1

4
\$\begingroup\$

The triangle appears very small because the ortho projection is quite big. Try this

projection = glm::ortho(0.0f, 4.0f, 0.0f, 3.0f, 0.1f, 100.0f)

And if you want the triangle to be centered:

projection = glm::ortho(-2.0f, +2.0f, -1.5f, +1.5f, 0.1f, 100.0f);

The OP also wanted to know whether it's better to use a projection such as projection = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f, 0.1f, 100.0f); and make objects bigger. Or use a smaller projection, and keep the objects small.

The 800x600projection sounds like a window resolution. It might be okay to use pixels as the unit of measure (i.e. this chacter is 36x40 pixels). However, take into account that, if you always match the the proection with the window resolution, the bigger the window, the more content you will be able to see. I don't know if that is desirable for your game, but it could work handy if your game was fixed resolution.

I'm going to give some examples of how I like to approach it depending on the game (just my opinion :) ).

If you wanted to make a game such as tetris

enter image description here

I would assume one square is 1x1 units. So the visible region will be, in this example, 12x25 (if I counted the squares correctly). Also I will take the bottom left square as (0,0) coordinate because I find it convenient in this case. So the ortho matrix will be glm::ortho(0.0f, 12.0f, 0.0f, 25.0f)

For a 2d adventure game

enter image description here

I would take something remarkable as the unit of measurement of the game, for example, the main character, or the size of the tiles. For example, I could say the main chacter is going to be 4x4 units.

Then the ortho matrix, will define how much of the game you can see in the whole screen. If the ortho matrix is 4x4, I will only be able to see the main chacter, so you should adjust the dimensions to whatever you like (60x48 could be a good number in this example).

Also, for this kind of game, I would make the ortho matrix centered. i.e the (0,0) corrdinate is the center of the screen, instead of the bottom-left corner.

So the ortho matrix will be glm::ortho(-30.0f, +30.0f, -24.0f, +24.0f).

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thanks for explaining the another topic. I became to realize better of how the orthographic projection matrix works. I am about to make a 2d game with fixed resolution(800x600). Now I can move on with your help! \$\endgroup\$ Commented Aug 10, 2021 at 9:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .