Drawing in absolute pixels

Started by
4 comments, last by Peaceman 22 years, 2 months ago
Hi, can I draw a quad by defining the pixels for the vertices instead of scaled opengl coordinates? thx
Advertisement
Sure. It's easy.

    // Set the size of the view portglViewport(0, 0, Width, Height);// Use orthographica projectionglMatrixMode(GL_PROJECTION);glLoadIdentity();   // Edit: Forgot to add thisglOrtho(0, Width, Height, 0, 0, 1);// Render the objectsglMatrixMode(GL_MODELVIEW);glLoadIdentity();// Now you can draw stuff in pixel coordinates.// The upper left is (0, 0)// The lower right is (Width, Height)  



Premature optimizations can only slow down your project even more.

Edited by - tangentz on February 16, 2002 3:27:01 PM
神はサイコロを振らない!
Thank you but your answer only works to 50%. I want a rectangle which is 50 pixels wide and 768 pixels long. It's supposed to cover the left side of the monitor (as part of an GUI). The code I used is:
	glPushMatrix();	glViewport(0, 0, WindowWidth, WindowHeight); // Set the size of the view port	glMatrixMode(GL_PROJECTION); // Use orthographica projection	glOrtho(0, WindowWidth, WindowHeight, 0, 0, 1);	// Render the objects	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();	glColor4f(1.0f, 0.0f, 1.0f, 1.0f);	glBegin(GL_QUADS);	{	glVertex3f(0.0f ,  0.0f, 0.0f);	glVertex3f(0.0f ,767.0f, 0.0f);	glVertex3f(50.0f,767.0f, 0.0f);	glVertex3f(50.0f,  0.0f, 0.0f);	}	glEnd();	glPopMatrix();  

The rectangle is drawn perfectly, but the scene is translated 128 pixels right, and the rectangle is only 37 pixels wide. Why???

Edited by - Peaceman on February 16, 2002 1:34:03 PM
Put a glLoadIdentity(); between glMatrixMode(GL_PROJECTION); and glOrtho(0, WindowWidth, WindowHeight, 0, 0, 1);

glOrtho, gluOrtho2D, gluPerspective, etc. are all matrix MULTIPLICATIONS. So if you want to replace the current matrix you should call glLoadIdentity first.

Also, it's probably better to change your glOrtho statement into this: glOrtho(0, WindowWidth, WindowHeight, 0, -1, 1); or this: glOrtho(0, WindowWidth, 0, WindowHeight, -1, 1);

Because otherwise drawing at depth 0 draws exactly at the near clipping plane. It's safer to draw between the near and far clipping planes.

Edited by - Scarab0 on February 16, 2002 3:09:26 PM
Dirk =[Scarab]= Gerrits
Thank you, it works fine now, but why is a near clipping value of 0 a bad idea?
quote:Original post by Peaceman
Thank you, it works fine now, but why is a near clipping value of 0 a bad idea?


Well it''s not really a bad idea in Ortho mode. In fact it should always work if you''re drawing at depth 0.
-1 is just safer because 0 is definately between -1 and 1, with 0 and 1 it''s not that clear.
So maybe it''s just paranoid of me, but I prefer [-1, 1].

BTW, another tip: you can just use:
glVertex2f(0.0f , 0.0f); instead of glVertex3f because glVertex2f is equivalent to calling glVertex3f with a Z of 0. It''s shorter so maybe it''ll save you some time.
Dirk =[Scarab]= Gerrits

This topic is closed to new replies.

Advertisement