glVertex3f quesion

Started by
4 comments, last by V-man 15 years, 5 months ago
glVertex3f(50, 15, 0); How would I know where the point would be by just looking at those numbers? Is those coordinates in pixels or in what? Am pretty sure those aren't pixels, but Is it possible to make the numbers represent pixels from down left side of the screen? My goal is to make a Quad 800x600 pixels that fills up the whole window(the window is 800x600). Thanks!
Advertisement
When you use glVertex3f(...), the position will be in model space. Then, it gets transformed to world space, then view space, then NDC space, then after the perspective division, it's transformed to screen(2D) space. To get a screen sized quad, here's what I do:


[source lang = "cpp"]void RenderNormalizedQuad(){	glBegin(GL_QUADS);	glTexCoord2f(0.0f, 0.0f);	glVertex3f(-0.5f, -0.5f, 0.0f);		glTexCoord2f(1.0f, 0.0f);	glVertex3f( 0.5f, -0.5f, 0.0f);		glTexCoord2f(1.0f, 1.0f);	glVertex3f( 0.5f,  0.5f, 0.0f);		glTexCoord2f(0.0f, 1.0f);	glVertex3f(-0.5f,  0.5f, 0.0f);	glEnd();}



Now, in order for the quad to fill the screen, you have to do a few things. Here is an example:

[source lang = "cpp"]void RenderHUD(){	// Begin rendering by changing the projection	// to orthographic	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	glOrtho(0.0f, 800, 0.0f, 600, 1.0f, 10000.0f);	// Disable Depth Testing, and Enable alpha blending	glDisable(GL_DEPTH_TEST);	glEnable( GL_BLEND );	glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );	glDisable(GL_LIGHTING); // Do this only if you don't need lighting        // Prepare modelview matrix stack for HUD rendering	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();	glPushMatrix();	        glScale3f(800, 600, 1);	RenderNormalizedQuad();	glPopMatrix();	        // End rendering by resetting the projection to	// the camera's projection matrix and enabling the	// states we disabled	glDisable(GL_BLEND);	glEnable(GL_DEPTH_TEST);	glEnable(GL_LIGHTING);	SetProjection(&CAMERA);}




So, what is going on here? Setting the projection to ortho effectively changes the look of an object. It will no longer look smaller the further it is from the camera. Turning of depth testing makes sure that nothing will be drawn in front of the quad. This is useful for hud stuff. Enabling the blending is just my own preference. I like to alpha-blend stuff. Disable lighting unless you want your quad lit. You must provide normals for your verts if you want lighting. Then I change to the modelview matrix and do glLoadIdentity(). Since we're on the modelview stack, doing glLoadIdentity() places the camera at (0,0,0) and faces it along(0,0,-1). Then we push matrix to maintain state after drawing, scale the quad by the screen size, and then render it. Finally, we pop the state maintaining matrix and re-enable/disable the states we changed. Don't forget to set your projection back to perspective afterwards.
First of all Thanks a lot for the answerer.

Secondly, so you mean that the glLoadIdentity() command sets the camera at position 0,0,0 facing it at 0,0,-1? That would be the same as gluLookAt(0, 0, 0, 0, 0, -1, 0, 1, 0)?

That glScale3f(800, 600, 1); was really smart, never thought of that.

So something like this would work? This is going to be 3D so I cant use orthographic.

        glViewport(0, 0, 800, 600);		glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective(45,(800 / 600), 1, 1000);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();        glScale3f(800, 600, 1);	RenderNormalizedQuad();


Sorry for these noob questions but have to learn it someday. :)
depending on how you have your window coords setup, your projection(parallel or perspective) using glVertex3f will be different

The x,y could be pixel units if you have your window coordinates setup that way (i.e. full screen).

Now take for instance glVertex3f(1, 1, -1);

this means we will plot the position right to x axis by 1, up y axis by 1 (like in your old algebra classes) and z coordinate in this example is negative, so i like to imaging diving into the screen by 1. If the z coord is 0 just imaging it being right on the screen, and if its > 0 then i imagine it being outside the screen coming towards your face.

this is the standard coordinate system for a window, but opengl allows you to specify whatever type of coordinate system you want. You can have gl treat the window as if the z axis is the y axis, y axis the x axis, and the x axis the z axis. Its all up to you really.

i hope this has helped you get a clear picture of where a vertex would be plotted using glVertex3f.
glVertex3f(50, 15, 0);

The last question: Is it possible to make the numbers represent pixel on the screen instead of the standard OpenGL units?
If its possible how?

Thanks a bunch.
Yes, it is possible.

import org.lwjgl.opengl.GL11;public final class OpenGL2D extends java.lang.Object {	public static void glEnable2D() {		java.nio.IntBuffer vPort = java.nio.IntBuffer.allocate(4);		GL11.glGetInteger(GL11.GL_VIEWPORT, vPort);		GL11.glMatrixMode(GL11.GL_PROJECTION);		GL11.glPushMatrix();		GL11.glLoadIdentity();		GL11.glOrtho(0, vPort.get(2), 0, vPort.get(3), -1, 1);		GL11.glMatrixMode(GL11.GL_MODELVIEW);		GL11.glPushMatrix();		GL11.glLoadIdentity();	}	public static void glDisable2D() {		GL11.glMatrixMode(GL11.GL_PROJECTION);		GL11.glPopMatrix();   		GL11.glMatrixMode(GL11.GL_MODELVIEW);		GL11.glPopMatrix();		}}
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement