Jump to content
  • Advertisement
Sign in to follow this  
Lode

OpenGL OpenGL 2D engine

This topic is 4837 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?
[I thought you'd set the ortho projection from (0,0)-(window_width,window_height) and use an offset of 0.5 - but .375 sounds so weird that it must be right...]

Share this post


Link to post
Share on other sites
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?
[I thought you'd set the ortho projection from (0,0)-(window_width,window_height) and use an offset of 0.5 - but .375 sounds so weird that it must be right...]


Commands like glRect need integer coordinates. For instance, glRecti(0,0,1,1) must fill the lower-left pixel. On the other hand, drawing line or points require offsetting by 0.5(the center of the pixel).

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!