Jump to content
  • Advertisement
Sign in to follow this  
TrueTom

Exact 2D pixel coordinates

This topic is 4877 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

What is the correct way to calculate pixel coordinates in 2D? For example drawing a box at 0,0-9,9 which should be 10 by 10 pixels?
glViewport(0,0,Width,Height);

glMatrixMode(GL_PROJECTION);						
glLoadIdentity();							

glOrtho(0, Width-1,Height-1,0.0f,-1.0f,1.0f);

This code should draw a triangle but fails to produce an one pixel per line increase:
glBegin(GL_QUADS);

for(int i = 0;i < 480;i++)
{
	glColor3f(1.0, 0.0, 0.0);			
	glVertex2f(i, 0);			
	
	glColor3f(1.0, 0.0, 0.0);			
	glVertex2f(i+1, 0);			
	
	glColor3f(1.0f, 0.0f, 0.0f);			
	glVertex2f(i+1, i);			

	glColor3f(1.0f, 0.0f, 0.0f);		
	glVertex2f(i, i);			
}
	
glEnd();	

I googled a bit and many people seem to have the same problem but without a satisfying solution. For example adding some small jitter which I tried but doesn't work reliable in all situations. The problem seems to be some rounding errors.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Brother Bob
You shouldn't have -1 after width and height when setting the orthographic projection.


Yes, that's right :)

I figured it out while using

glOrtho(0, 1, 1, 0, -1, 1);

and using coordinates from 0 to 1. (Which should have been wrong with my old thinking ;) )

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!