Jump to content

  • Log In with Google      Sign In   
  • Create Account

Last post about 2D in OpenGL (so please stop!)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
34 replies to this topic

#21 Ilici   Members   -  Reputation: 862

Like
Likes
Like

Posted 31 May 2004 - 09:30 PM

quote:
Original post by Puzzler183
Also, the OpenGL PixelCopy functions are ridiculously slow.



You mean glReadPixels? Yes, it''s slow, but it''s not necesseraly OpenGL''s fault. The AGP bus is optimized for writing to the Video memory, not retrieving data from it so reading back pixels, which requires the driver to lock the framebuffer in video memory and send data back on the bus to system memory is slow. If you want software rendering, i think MesaGL is what you are looking for.



Sponsor:

#22 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 03 June 2004 - 07:55 PM

lol, i have almost the same exact functions in my game for drawing 2D UI stuff

- Voxelz

#23 Xiachunyi   Members   -  Reputation: 556

Like
Likes
Like

Posted 04 June 2004 - 09:41 AM

Great Job on the sticky.

One thing though - On the implementation example, can you change the last "glVertex2D(50, 50);" to "glVertex2d(50, 50);" so newbies like me won''t have to use their brains.

#24 etothex   Members   -  Reputation: 728

Like
Likes
Like

Posted 07 June 2004 - 01:27 PM

The same, essentially, except in lwjgl/java.

Haven''t tested it yet, but it compiles, so here it is. Licenced the same as the original.

(I was working on my snippet library and wanted to add this back to where it came from)


import org.lwjgl.opengl.GL11;

public final class OpenGL2D extends java.lang.Object {
public static void glEnable2D() {
java.nio.IntBuffer vPort = java.nio.IntBuffer.allocate(4);
GL11.glGetInteger(GL11.GL_VIEWPORT, vPort);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPushMatrix();
GL11.glLoadIdentity();
GL11.glOrtho(0, vPort.get(2), 0, vPort.get(3), -1, 1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPushMatrix();
GL11.glLoadIdentity();
}
public static void glDisable2D() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPopMatrix();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPopMatrix();
}
}


#25 fyhuang   Members   -  Reputation: 253

Like
Likes
Like

Posted 11 June 2004 - 08:25 AM

Also, if you want to be able to access the screen in floating-point with the left side being -1.0 and the top being -1.0 (bottom and right still 1.0), you can do:

gluOrtho2D( 0.0, 0.0, 1.0, 1.0 );

[Edited by - fyhuang on August 31, 2005 11:25:03 AM]
- fyhuang [ site ]

#26 Dwarf with Axe   Members   -  Reputation: 277

Like
Likes
Like

Posted 14 June 2004 - 02:33 AM

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!

#27 vincoof   Members   -  Reputation: 514

Like
Likes
Like

Posted 18 June 2004 - 12:28 AM

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 :
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.375, 0.375, 0.);


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 :
void glEnable2D()
{
int vPort[4];

glGetIntegerv(GL_VIEWPORT, vPort);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glOrtho(vPort[0], vPort[0]+vPort[2], vPort[1], vPort[1]+vPort[3], -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.375, 0.375, 0.);

glPushAttrib(GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
}

void glDisable2D()
{
glPopAttrib();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}


Keep up the good work =)

#28 Eber Kain   Members   -  Reputation: 130

Like
Likes
Like

Posted 18 June 2004 - 06:52 AM

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.comThere it is, Television, Look Listen Kneel Pray.

#29 vincoof   Members   -  Reputation: 514

Like
Likes
Like

Posted 18 June 2004 - 11:32 AM

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.

#30 Eber Kain   Members   -  Reputation: 130

Like
Likes
Like

Posted 18 June 2004 - 12:17 PM

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.comThere it is, Television, Look Listen Kneel Pray.

#31 vincoof   Members   -  Reputation: 514

Like
Likes
Like

Posted 18 June 2004 - 12:35 PM

Quote:
Original post by Eber Kain

I would think that calling glLoadMatrix is faster than glOrtho or another function that has to re-calculate the matrix every frame.


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.

#32 benjamin bunny   GDNet+   -  Reputation: 838

Like
Likes
Like

Posted 18 June 2004 - 12:40 PM

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.

#33 Eber Kain   Members   -  Reputation: 130

Like
Likes
Like

Posted 18 June 2004 - 02:38 PM

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.comThere it is, Television, Look Listen Kneel Pray.

#34 Tube   Members   -  Reputation: 128

Like
Likes
Like

Posted 18 June 2004 - 02:56 PM

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)

#35 vincoof   Members   -  Reputation: 514

Like
Likes
Like

Posted 19 June 2004 - 12:38 AM

Quote:
Original post by Eber Kain
In the code I posted, you call Perspective#D once in your entire program.

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:
Original post by Tube
OKey GRATE so now how d0 I trun this into a gaeM?!?!?!????

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 ?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS