# Orthographic projection not showing a triangle

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()?

• 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); Aug 10 '21 at 6:49
• @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. Aug 10 '21 at 7:13
• Glad it was useful! I have made a post answering that question Aug 10 '21 at 8:31

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

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)

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