PDA

View Full Version : I don't understand what's the problem!



06-26-2001, 06:58 PM
I am trying to display a square in a window... something is messed up, the square is not centered... the dimensions need to be 128x128 for the square to be centered and not 256x256! can you tell me what I am doing wrong?

#include <GL/gl.h>
#include <GL/glut.h>

void InitGL();
void DrawScene();

const float Height(256), Width(256);

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(200,200);
glutInitWindowSize(int(Width)+20,int(Height)+20);
glutCreateWindow("Square");

InitGL();

glutDisplayFunc(DrawScene);

glutMainLoop();

return 0;
}

void InitGL()
{
glClearColor(0.2f, 0.1f, 0.5f, 1.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glShadeModel(GL_SMOOTH);

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glViewport(0,0,int(Width+20),int(Height+20));

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-10,Width+10,-10,Height+10);
glMatrixMode(GL_MODELVIEW);
}

void DrawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glColor3f(0.0,1.0f,0.0f);
glBegin(GL_QUADS);
glNormal3f(0.0f,0.0f,1.0f);
glVertex4f(0.0f,0.0f,0.0f,0.5f);
glVertex4f(Width,0.0f,0.0f,0.5f);
glVertex4f(Width,Height,0.0f,0.5f);
glVertex4f(0.0f,Height,0.0f,0.5f);
glEnd();

glutSwapBuffers();
glutPostRedisplay();
}

MrShoe
06-26-2001, 07:48 PM
Might be a rather stupid question, but what is glVertex4f?? I have only heard of 3f... Maybe you should try changing it to glVertex3f??

john
06-26-2001, 07:49 PM
gluOrtho2D(-10,Width+10,-10,Height+10);


is the problem. Well, I say problem loosly, because its a valid projection, but it isn't doing what you want to do.

Use:



gluOrtho2D(-5-Width/2, 5+Width/2, -5-Height/2, 5+Height/2)

the reason why: the left edge is the world coordinates that will map to the left of the screen, the right edge to the right of the screen, etc.

so, in the first ortho you had, you were mapping -10 in world coordinates to the left of the screen, and +10+width == 266 (or, whatever the size of yoru square is) to the right. this means that the centre of your window maps to (266--10)/2, which is != 0, and since you're centering your square at 0, then your square won't appear in the middle of the window

do you see how i've fixed the ortho command? the projection is symetric about the optical axis, ie. left == -right.

cheers,
John

john
06-26-2001, 07:54 PM
the 4f command specifies verticies in homogeneous coordinates.

x/w, y/w, z/w in cartesian coordinates is x, y, z, w in homogeneous coordinates. w defaults to 1.0 if glVertex3* is used. So, in his example, the following commands are identical:



glVertex4f(Width, 0.0, 0.0, 0.5);
glVertex3f(Width/2, 0.0, 0.0);
glVertex2f(Width/2, 0.0);
glVertex4f(Width/2, 0.0, 0.0, 1.0);
glVertex4f(Width*2, 0.0, 0.0, 2.0);

cheers,
John

06-26-2001, 10:49 PM
thank you john! I was dumb... and I accidently used the 4th parameter thinking it was something else!! (I thought it was used as an alpha value... but I was wrong, and I took out all of the other blending stuff out of this example to make it easier to understand. I should revisit that blending chapter http://www.opengl.org/discussion_boards/ubb/wink.gif.

Thanks agin John...

john
06-26-2001, 11:15 PM
<beams happily> no worries =)

you want to be checkin' out glColor4f for the alpha value =)

cheers,
John