fast pixel routines

Whats the best (fastest) way to draw a px in openGL ? all i need to do is output an rgb pixel to an x,y screen coordinate.

Either use glDrawPixels, or, if you want to draw more than one pixel, draw a poing using an orthographic projection like this.

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, winXSize, 0, winYSize, -1, 1);
glBegin(GL_POINTS);
glVertex2f(pointX, pointY);
glEnd();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

If you wan to draw more than one point, you just draw then all at once between glBegin/glEnd. pointX/pointY is the point coordinate in windowspace, and I hope you understand what winXSize/winYSize is.

thanks, worked fine when drawing a few pixels at a time but if i say try to fill the whole screen ( in 800x600 ) it only fills halfway up. Is there a vertex limit i need to set or did i mess up the code:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, 800, 0, 600, -1, 1);
glBegin(GL_POINTS);

for (float x=0;x<800;x++)
{
for (float y=0;y<600;y++)
{
glVertex2f(x, y);
}
}

glEnd();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

[ edit - formatting :stuck_out_tongue: ]

[This message has been edited by mali (edited 01-10-2001).]

nm i got it to work ( forgot about perspective :stuck_out_tongue: )

WARNING !!

from Bob/mali
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

You should ALWAYS send vertices in GL_MODELVIEW matrix mode !! The correct way is :
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

This prevent you from modify the current matrix thinking you’re in modelview matrix mode while you’re in projection matrix mode (this can save lot of debug time :slight_smile: )…

On the flip side of that, don’t do glOrtho(…) in GL_MODELVIEW mode, because that would really screw things up. Also, drawing that many vertices will be slow if you are planning to do the whole screen that way.

j

is there a faster - like a win api call ( the thought that windows could be faster? )
sorry if i sound sutpid

If you want to draw points like an image, use glDrawPixels only. My code was for if you wanted to draw points spread all over the screen, like stars in the sky.

But if you ONLY want to draw images, you shouldn’t use OpenGL at all. Go for a special API that is designed for 2D only. Got no names of any, so can’t help you with that, sorry.

And also, noticed I forgot to put a glPushMatrix(); before first call to glMatrixMode(GL_MODELVIEW);

Haust: Of course, you are right. In general, you should always activate the modelview matrix. But in this case I see no reasons to do so. You only set a new projection, draw some points, and then go back to the original state. You never leave the function, and enter a new one where misunderstandings can arise. But anyway, I DO agree with you.

hmm 2d api’s … directdraw? hmm no last time i played with directX i rendered all my games inoperatable. Just have to hack up some asm to do it :stuck_out_tongue: thanks for your help guys

To draw images in openGl:
—glDrawPixels works, but its generally very slow
—Creating a texture and drawing a quad, is pretty fast but you have to deal with the power-of-2 limit of opengl. You can break the image in tiles, or you can create a texture that contains the whole screen (like a 1024x1024 texture for 800x600), and the use glTexSubImage2d and draw the quad mapping texcoords to the 800x600 portion of the texture (this is the way to go I believe).
—You can simulate an overlay with the previous method, using alpha test. enable GL_ALPHA_TEST, set alphafunc to (GL_GREATER,0.5), and create a RGBA texture as described above (1024x1024 for 800x600, etc). Then, for each visible pixel, draw the pixel in the texture and set its alpha to 255, while undrawn pixels could be set to alpha 0.
—If you only what to draw a few pixels, you can use the glvertex method described in previous posts.