|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic Page: «« 1 2 |
Last Thread Next Thread ![]() |
| Last post about 2D in OpenGL (so please stop!) |
|
![]() Dwarf with Axe Member since: 12/21/2001 From: Northridge, CA, United States |
||||
|
|
||||
| I love you guys. All of you. You know that? I haven't been here for over two years, and you still have that post. :tears in eyes: =D You guys rock! |
||||
|
||||
![]() vincoof Member since: 1/10/2002 From: Toulouse, France |
||||
|
|
||||
| Nice tip. May I point few enhancements ? 1- don't mandate viewport[0] and viewport[1] are null. In the generic case, it's better to call that instead : glOrtho(vPort[0], vPort[0]+vPort[2], vPort[1], vPort[1]+vPort[3], -1, 1); 2- for pixel-exact positioning, the usage of a slight translation is mandatory, as explained in the opengl.org's FAQ :
3- disable depth testing while rendering 2D. But this means 2D stuff has to be rendered AFTER all 3D stuff, in case 2D represents some kind of HUD (acronym of Heads Up Display). So the new code becomes :
Keep up the good work =) |
||||
|
||||
![]() Eber Kain Member since: 2/19/2001 |
||||
|
|
||||
| For me the important thing is speed, I have 2D and 3D elements every frame and it is important that the framerate not suffer to bad. I came up with the idea of saving the matrix and reloading it, instead of recalculating it, I have not tested for a speed difference, but it seems as though it would have to be faster. Basically, you would call both the Perspective#D functions at the start of the program and then call the Render#D function before you draw depending on what you want to draw.
#define RENDER_2D 2030
#define RENDER_3D 2031
double* matrix2D = new float[16];
double* matrix3D = new float[16];
short renderMode;
void Perspective3D(float p_fov, float p_near, float p_far, float width, float height) {
renderMode = RENDER_3D;
float lw = (float)width, lh = (float)height;
if(height == 0) height = 1;
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(p_fov,lw/lh,p_near,p_far);
glMatrixMode(GL_MODELVIEW);
glGetDoublev(GL_PROJECTION_MATRIX , matrix3D);
glLoadIdentity();
}
void Render3D() {
if(renderMode==RENDER_3D) return;
renderMode = RENDER_3D;
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(matrix3D);
glMatrixMode(GL_MODELVIEW);
}
void Perspective2D(RECT size) {
renderMode = RENDER_2D;
glViewport(size.left, size.bottom, size.right, size.top);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(size.left, size.right, size.bottom, size.top);
glMatrixMode(GL_MODELVIEW);
glGetDoublev(GL_PROJECTION_MATRIX, matrix2D);
glLoadIdentity();
}
void Render2D() {
if(renderMode==RENDER_2D) return;
renderMode = RENDER_2D;
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(matrix2D);
glMatrixMode(GL_MODELVIEW);
}www.EberKain.com There it is, Television, Look Listen Kneel Pray. |
||||
|
||||
![]() vincoof Member since: 1/10/2002 From: Toulouse, France |
||||
|
|
||||
| my guess is that push/pop is faster than get/load, and it also saves memory. The only problem with push and pop is that you're limited in some (quite high, yet limited) stack depth. |
||||
|
||||
![]() Eber Kain Member since: 2/19/2001 |
||||
|
|
||||
| Push / Pop would be faster, but thats not all you would be doing. Any way you look at it you have to get the new Matrix into the projection matrix stack. I would think that calling glLoadMatrix is faster than glOrtho or another function that has to re-calculate the matrix every frame. Memory... its 16 doubles, thats like 128 bytes, I dont think thats to much to sacrifice for a possible speed gain. www.EberKain.com There it is, Television, Look Listen Kneel Pray. |
||||
|
||||
![]() vincoof Member since: 1/10/2002 From: Toulouse, France |
||||
|
|
||||
Quote: Sure, but in the method you posted you call glOrtho as often as I do. :) So where's the performance loss ? And while you're pointing correctly that glLoadMatrix may be faster than glOrtho, I'd like to point that glGetDoublev is much slower than any kind of matrix operation. In general, anything read back from the server side should be discarded when you look for the highest performance. |
||||
|
||||
![]() benjamin bunny GDNet+ Member since: 11/26/1999 From: Norwich, England |
||||
|
|
||||
| Optimising this for speed is a complete waste of time. Unless you're switching between 2D and 3D thousands of times per frame, you won't see any noticeable increase in frame rate. |
||||
|
||||
![]() Eber Kain Member since: 2/19/2001 |
||||
|
|
||||
| In the code I posted, you call Perspective#D once in your entire program. You then call Render#D before drawing to specify if you want 2D or 3D. www.EberKain.com There it is, Television, Look Listen Kneel Pray. |
||||
|
||||
![]() Tube Member since: 6/9/2004 From: USA |
||||
|
|
||||
| OKey GRATE so now how d0 I trun this into a gaeM?!?!?!???? (P.S. Thanks, that's awesome, and makes me almost wish I used OpenGL) |
||||
|
||||
![]() vincoof Member since: 1/10/2002 From: Toulouse, France |
||||
|
|
||||
Quote: Yes it is called thousands of time during the game, but what's important is that it's called once PER FRAME. It's true that optimizing every part of the engine is always a gain, but it's also true that if the performance gain makes almost no difference you'd better spend your time on other optimization things that will significantly improve frame rate, and come back later (probably in the end) on such negligible improvements. Quote: Sure, I have the three lines of code solution that makes anything a great game everyone would love. Mind me if I post it later ? |
||||
|
||||
|
Page: «« 1 2 All times are ET (US) ![]() |
Last Thread Next Thread ![]() |
|