OpenGL 2D engine

Started by
3 comments, last by mikeman 18 years, 8 months ago
In an OpenGL 2D engine, what's a good way to draw a 2D circle on the screen? Filled and not filled. And drawing a 2D pixel, is this done by drawing a 3D point at such a location that it appears in the correct 2D pixel, or is there a better way?
Advertisement
Just draw a regular polygon with many sides. The more the merrier, just pick a number that looks good.

For drawing pixels you can use points, just offset the coordinates by 0.375f (to aim for the pixel's center).
If you want to draw onto a 2D surface, you can use these functions:
// A Fairly Straightforward Function That Pushes// A Projection Matrix That Will Make Object World // Coordinates Identical To Window Coordinates.inline void pushScreenCoordinateMatrix() {	glPushAttrib(GL_TRANSFORM_BIT);	GLint viewport[4];	glGetIntegerv(GL_VIEWPORT, viewport);	glMatrixMode(GL_PROJECTION);	glPushMatrix();	glLoadIdentity();	gluOrtho2D(viewport[0],viewport[2],viewport[1],viewport[3]);	glPopAttrib();}// Pops The Projection Matrix Without Changing The Current// MatrixMode.inline void pop_projection_matrix() {	glPushAttrib(GL_TRANSFORM_BIT);	glMatrixMode(GL_PROJECTION);	glPopMatrix();	glPopAttrib();}

Push the screen coordinate matrix, and do all your drawing. If you wish to go back to 3D, just pop_projection and itll be in 3D. This way, you can draw pixels with perfect accuracy, meaning if your window is 800x600, and you put a dot at 799, 599, it will be in the very corner. For drawing circles, you may want to look up some of the GLU library functions, I think they might have one for ya.
Quote:Original post by Ilici
For drawing pixels you can use points, just offset the coordinates by 0.375f (to aim for the pixel's center).

Why is 0.375 in the middle of a pixel?
<br><br>
Quote:Original post by krausest
Quote:Original post by Ilici
For drawing pixels you can use points, just offset the coordinates by 0.375f (to aim for the pixel's center).

Why is 0.375 in the middle of a pixel?
<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>Commands like glRect need integer coordinates. For instance, glRecti(0,0,1,1) must fill the lower-left pixel. &#79;n the other hand, drawing line or points require offsetting by 0.5(the center of the pixel).<br><br>Offseting by 0.375(mostly by translating the MV matrix before any rendering) is just a compromise if you want to handle both cases and always work with integer coordinates. That way polygon edges will be away from the center, but line vertices or points will be close enough.

This topic is closed to new replies.

Advertisement