Sign in to follow this  
TrueTom

Exact 2D pixel coordinates

Recommended Posts

TrueTom    110
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
TrueTom    110
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this