34. Orthogonal Projections
Welcome to another in my slowly building long line of OpenGL tutorials.
Here we will be looking at how to implement orthogonal projections to
incorporate 2D into our program. This will draw our shape in a space that
will not move when our 3D scene is rotated. This is perfect when
implementing a display system into your game that displays health, ammo
etc. So lets get started now.
————————————————————————–
Section 1: Variables
The only global variables I am going to be using in this are:
int w1;
int h1;
These will simply hold our window width and height as gathered in our
reshape function.
————————————————————————-
Section 2: Starting the orthogonal projection.
This call, no matter when it is called, will start our orthogonal
projection, I have called it orthogonalStart:
void orthogonalStart (void) {
First of all, we need to switch to our projection matrix
glMatrixMode(GL_PROJECTION);
Start our projection modifications
glPushMatrix();
Then we need to clear it of all previous information
glLoadIdentity();
Now I am calling: gluOrtho2D instead of the previous gluPerspective
which was in our reshape function. This takes our parameters which
set the view space from 0,0 in the window, to the width and height of the
window which we collect in our reshape function
gluOrtho2D(0, w1, 0, h1);
Now we need to flip our scene upside down
glScalef(1, -1, 1);
And translate it to display our scene correctly
glTranslatef(0, -h1, 0);
Now we switch back to our model matrix so we can draw our 2D shapes
glMatrixMode(GL_MODELVIEW);
}
———————————————————————
Section 3: Ending the orthogonal projection and restoring our perspective
projection
To end our orthogonal projection, I am calling the function, which I have
called orthogonalEnd which will set our scene back to how it was
void orthogonalEnd (void) {
Switch back to our projection mode
glMatrixMode(GL_PROJECTION);
Finish our calls above
glPopMatrix();
Switch back to our model matrix to continue with out 3D scene
glMatrixMode(GL_MODELVIEW);
}
———————————————————————
Section 4: Display
Now to draw our information, I have called the following from within
my display function:
Call our function to start our orthogonal projections
orthogonalStart();
Begin drawing a quad, note here that in orthogonal space, we work with
pixels as opposed to units in 3D space which have no length of measurement
glBegin(GL_QUADS);
Now I am drawing our vertices in 2D as opposed to 3D vertices which
are called with glVertex3f. The following coordinates will draw a square right
in the middle of our 500×500 window.
glVertex2f(125, 125);
glVertex2f(125, 375);
glVertex2f(375, 375);
glVertex2f(375, 125);
glEnd();
Now we call our function to end our orthogonal projections
orthogonalEnd();
——————————————————————–
Section 5: Reshape
In our reshape function I am using the following lines:
w1 = w;
h1 = h;
Which I have called after our gluPerspective call. This will set
our variables to the width and the height of our window.
——————————————————————-
And there we have it, if you have any questions, please email me at
swiftless@gmail.com
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. |
#include <GL/gl.h> #include <GL/glut.h> #include <windows.h> #include <stdio.h> int w1; void orthogonalStart (void) { void orthogonalEnd (void) { void display (void) { orthogonalStart(); glBegin(GL_QUADS); orthogonalEnd(); glutSwapBuffers(); void reshape (int w, int h) { int main (int argc, char **argv) { |
Hey Swiftless, great job with the tutorial. I have one question though about this. (You did not mention it in the tutorial). How can I make it so that my 3D drawings in perspective mode show up underneath the 2D drawings in Orthogonal. Right know I have both 2D and 3D drawings, and when I change my view so that the 2D objects overlap with the 3D objects. The 2D objects hide underneath. I’m looking for the effect in games where I can store stats and other information on the screen at all times. Thanks in advance. -Nick.
Just kidding. I was drawing my 2D stuff before the 3D. I changed it so I draw the 2D after and now it works fine 😛
precompiled executables would be a massive benefit, as well as the source code in a project.
I keep finding myself having to create projects, sort out the source code, and compile. This takes time, especially on the longer tutorials.
Other than that, nice work!
Dale
Hey Dale,
I don’t know if you have noticed, but the new OpenGL 4.0 tutorials I provide include Visual C++ Projects, and once you have created a template project, most of my code should be copy and paste into one file.
However precompiled executables are generally not recommended to be placed online. A lot of people don’t trust them for starters, so I have no intention of placing these online at any stage.
Cheers,
Swiftless
Hey Swiftless, thanks for the reply.
All im saying is that some people (like myself) would prefer to have access to an executable to see the effects of the tutorial without having to compile it all first.
I dont want to get into an argument, but if people dont want to trust them, then surely that is their choice?
if your worried about security, zip it up and create a hask key. put the hask at the top of the tutorial. This way people can find out if it has been modified at all.
The main reason i suggested this in the first place is because sometime copying and pasting your tutorials, the code lines get messed up, and i have to sit there for a few minutes fixing it so the code will compile. i can imagine this would be worse on small netbooks though!
Hey man, its only a suggestion, you dont have to read a comment from me ever again, but you want to know how to make your site better, and that is my 2 pence worth.
Dale
Hey Dale,
I didn’t mean to come off rude, I apologize if I did.
For the very reason of showing off the effects of the tutorial, I moved to creating Youtube videos with the latest OpenGL 4.0 tutorials and am hoping to get time to do the same for these as well when I rewrite them.
I find it takes more effort to supply an executable in that manner than it does to film it for 10secs, add a title and upload it.
In the current rewrites of the tutorials, I have also switched to a better method of displaying code, which has a copy and paste friendly button 🙂
Feel free to leave any feedback, I read everything that people post on the site and have tried to cater for peoples requests thus far.
Cheers,
Swiftless
Hey Swiftless,
You didnt come across as rude, infact, i wondered if i was! but likewise, it wasnt intentional if i did.
Your solutions definately sound more user friendly than what is currently available.
keep up with the good articles, and if you ever need and help with web development give me a shout, i probably owe it to you for supplying these tutorials 😀
Dale
nice tutorials, but please add some images (applies for every tutorial on this site). a picture says more then a hundert words…
Hey a,
Thats a great idea, and with the new tutorials I am even trying to add YouTube videos to support them.
Cheers,
Swiftless
Great stuff! Just what I needed, much appreciated!