2D in Opengl 3.2?

Started by
12 comments, last by Murdocki 12 years, 11 months ago
Hello all

I was wondering if there is a simple way of doing 2D in the new OpenGL

Ive traveled the whole web for tutorials and all of these guys just keen on using screen coordinates for their purposes (-1,-1, 1,1)

Yes ofcourse one could think to start dividing all your coords by your screen space but that is not a clean way of doing it specially if you want to render worlds bigger than your screen
(In other words I dont want to have it that way on my project)



So if anybody in here could teach me step by step on how to do this I would be thankful

I already know how to create shader programs and all that stuff, the part that is really messing me up is;

- where to set up my coordinate system
- how to set it up
Advertisement
I suggest that you do what other games are doing. Make the usual orthographic projection matrix.
They usually call glOrtho(0.0, windowWidth, 0.0, windowHeight, znear, zfar)

Some people invert the y axis because they want it to go from top (0.0) to bottom (windowHeight) so you just swap your values
glOrtho(0.0, windowWidth, windowHeight, 0.0, znear, zfar)
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
glOrtho? are you sure that works on 3.2?
Replicate the functionality of it instead if you cannot use it. The point is not that you should use that particular function, but that you should use the projection is produces. Just create the corresponding matrix and load it into your shader if that's how you want to do it. The idea is still the same; use an orthographic projection, and create it using the parameters corresponding to the call V-man showed. Any decent documentation or reference manual will show you what the matrix from glOrtho looks like.
You need to build an ortho projection matrix and get it to your shader as a uniform mat4.

You can either look up the what the function is on your own it is described here http://www.opengl.or...tml/glOrtho.xml but it looks like the syntax is kind of confusing.

An ortho projection function is available in the GLM library http://glm.g-truc.ne...cccf6eda05c8127

I haven't done any 2D games so I don't know how you want to handle your coordinates but I have used screen coordinates for my on-screen drawing with sampler2DRect in my fragment shader
code to build ortho matrix:

[font=Monaco,]
[color="#666600"]void [color=#660066]Ortho[color=#666600](float *m, [color=#000088]float[color=#000000] left[color=#666600],[color=#000000] [color=#000088]float[color=#000000] right[color=#666600],[color=#000000] [color=#000088]float[color=#000000] top[color=#666600],[color=#000000] [color=#000088]float[color=#000000] bottom[color=#666600],[color=#000000] [color=#000088]float[color=#000000] znear[color=#666600],[color=#000000] [color=#000088]float[color=#000000] zfar[color=#666600])[color=#000000]
[color=#666600]{[color=#000000]
[color=#000000] [color=#000088]float[color=#000000] tx [color=#666600]=[color=#000000] [color=#666600]-[color=#000000] [color=#666600]([color=#000000]right [color=#666600]+[color=#000000] left[color=#666600])[color=#000000] [color=#666600]/[color=#000000] [color=#666600]([color=#000000]right [color=#666600]-[color=#000000] left[color=#666600]);[color=#000000]
[color=#000000] [color=#000088]float[color=#000000] ty [color=#666600]=[color=#000000] [color=#666600]-[color=#000000] [color=#666600]([color=#000000]top [color=#666600]+[color=#000000] bottom[color=#666600])[color=#000000] [color=#666600]/[color=#000000] [color=#666600]([color=#000000]top [color=#666600]-[color=#000000] bottom[color=#666600]);[color=#000000]
[color=#000000] [color=#000088]float[color=#000000] tz [color=#666600]=[color=#000000] [color=#666600]-[color=#000000] [color=#666600]([color=#000000]zfar [color=#666600]+[color=#000000] znear[color=#666600])[color=#000000] [color=#666600]/[color=#000000] [color=#666600]([color=#000000]zfar [color=#666600]-[color=#000000] znear[color=#666600]);[color=#000000]
[color=#000000]
[color=#000000] Identity(m); //identity matrix[color=#000000]
[color=#000000]
[color=#000000] m[color=#666600][[color=#006666]0[color=#666600]][color=#000000] [color=#666600]=[color=#000000] [color=#006666]2.0f[color=#000000] [color=#666600]/[color=#000000] [color=#666600]([color=#000000]right [color=#666600]-[color=#000000] left[color=#666600]);[color=#000000]
[color=#000000] m[color=#666600][[color=#006666]12[color=#666600]][color=#000000] [color=#666600]=[color=#000000] tx[color=#666600];[color=#000000]
[color=#000000] m[color=#666600][[color=#006666]5[color=#666600]][color=#000000] [color=#666600]=[color=#000000] [color=#006666]2[color=#000000] [color=#666600]/[color=#000000] [color=#666600]([color=#000000]top [color=#666600]-[color=#000000] bottom[color=#666600]);[color=#000000]
[color=#000000] m[color=#666600][[color=#006666]13[color=#666600]][color=#000000] [color=#666600]=[color=#000000] ty[color=#666600];[color=#000000]
[color=#000000] m[color=#666600][[color=#006666]10[color=#666600]][color=#000000] [color=#666600]=[color=#000000] [color=#666600]-[color=#006666]2[color=#000000] [color=#666600]/[color=#000000] [color=#666600]([color=#000000]zfar [color=#666600]-[color=#000000] znear[color=#666600]);[color=#000000]
[color=#000000] m[color=#666600][[color=#006666]14[color=#666600]][color=#000000] [color=#666600]=[color=#000000] tz[color=#666600];[color=#000000]
[color=#000000]
[color=#000000] [color=#000088]return[color=#000000] m[color=#666600];[color=#000000]
[color=#666600]}[/quote][color=#666600][/font]
sorry for bad english
axelynx: http://likosoft.com
Okay got my ortho matrix now what?

Okay got my ortho matrix now what?


Now draw everything with glVertex2f and so on.
what is that? Im using glDrawArrays to draw my geometry


Im trying to use the core profile
in a shader u would transform your vertex positions as normal with the projection matrix.

also I would recomment NOT to use your screen size for creating your matrix. Just think about how many world-units the player should see in one direction and calculate the size of the other with your screen ratio.

for example you want the player to see 10 in width u have to set a height of 7.5 (ratio: 4/3), I think this is importandt because you wouldn't want the player to see more of the game depeding on their screen resolution.

Another thing I like to do is to set the center of the screen in the center of the projection, so instead of
ortho(0, 10, 0, 7.5 ...

I use
ortho(-5, 5, -3.75, 3.75 ...

This topic is closed to new replies.

Advertisement