# How to use traditional pixel coordinates system in modern OpenGL? (3.1) (with C++/GLFW)

This question is very similar to my other question, however the solution provided there does not work when using OpenGL 3.1. glOrtho seems to do absolutely nothing no matter where I call it in OpenGL 3.1. Googling this didn't give me any decent results. Should I be doing something in the fragment/vertex shaders, or?

• With OpenGL 3, lots of stuff got removed (Like the matrix stack you mentioned). If you want to use a matrix in OpenGL 3+, you have to supply it to your shader manually. – Tara Dec 2 '16 at 4:19
• And how would I do that? – Accumulator Dec 2 '16 at 5:03
• You search the Internet for an OpenGL 3 tutorial, then you follow the tutorial, then once you've learned how OpenGL 3 works you go back to your game and you hopefully know how to make it work. – user253751 Dec 2 '16 at 10:20

In modern OpenGL (> 2.0 actually) the matrix stack has been removed . The matrix stack was part of the fixed pipeline. The fixed pipeline (for example, glBegin, glEnd) is deprecated and has been replaced in favour of a programmable pipeline.

This requires you to use shaders and supply your own transform matrices. I recommend reading this for a great tutorial set on modern OpenGL. It covers everything from the basic setup of drawing a triangle to the matrices you need.

Keep in mind, comparing to older versions (with glOrtho for example) this new pipeline is much harder to learn and understand. There is not really an easy way out.

EDIT: Fixed my statement about the removed pipeline (as Dudeson pointed out). The older OpenGL code should still work on newer platforms, but you can't use both in modern OpenGL (its bad practice to use the fixed pipeline eitherway)

• 1. The matrix stack is still present in OpenGL 2.1 (maybe you're confusing it with OpenGL ES 2.0). In order to still use it in OpenGL 2.1 you have to use built-in vertex shader variables like gl_ModelViewProjectionMatrix and functions like ftransform(). 2. I would argue that the new pipeline is much harder to learn and understand. In my opinion it's easier because it does less stuff behind the scenes which can be quite confusing (at least for me it was). – Tara Dec 2 '16 at 22:21

Yes, you should do something in the shader. A simple vertex shader would be something like:

#version 140

in vec3 position;

uniform mat4 mvp;

void main() {
gl_Position = mvp * vec4(position, 1.0);
}


GLSL's versions

Where mvp is your full matrix (projection, view, model) that you pass from your code. In your case, you want your projection to be orthogonal matrix by either making one yourself or use a library like glm.

This is how a usual orthographic projection matrix looks like. r, l, t, b, f, and n are the positions of the right, left, top, bottom, far and near clipping planes respectively. To achieve your desired effect:

r should be the width of the display in pixels
l is 0
t is 0,
b should be the height og the display in pixels
f should be something like 1000
n should be something like -1000


The last 2 doesn't matter, but f should be bigger than n.

After you created this matrix, you need to create a shader, load in this as the projection matrix and multiply the eye-space position of the vertex with it.

• If I'm reading this correctly, the first one is 2/r-l, or am I wrong? I can't read the last one one the first row either (excuse my noobiness) – Accumulator Dec 2 '16 at 23:00
• @Omega This is a matrix, those values are simple fractions. It could be translated to 2 / (r - l) – Bálint Dec 3 '16 at 16:22