Define world

Hi,

I have a problem with defining the “world” on opengl. As far as I understand glViewport is the “camera” in my world, and gluPerspective defines that world. Now please look at this code:
void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0,0,0);
glVertex3f(10,0,0);
glVertex3f(10,10,0);
glVertex3f(0,10,0);

	glColor3f(1.0f, 1.0f, 0.0f);
	glVertex3f(10,0,-10);
	glVertex3f(10,10,-10);
	glVertex3f(10,10,0);
	glVertex3f(10,0,0);

	glColor3f(0.0f, 0.0f, 1.0f);
	glVertex3f(0,0,0);
	glVertex3f(0,0,-10);
	glVertex3f(0,10,-10);
	glVertex3f(0,10,0);

	glColor3f(0.0f, 1.0f, 1.0f);
	glVertex3f(10,10,-10);
	glVertex3f(0,10,-10);
	glVertex3f(0,0,-10);
	glVertex3f(10,0,-10);
glEnd();

glFlush();

}

and:

void ChangeSize(int w, int h)
{
glViewport(0,0,w/2,h/2);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(60.0f, (GLfloat)(w/h), 0, 100.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(20, 1,1,1);

}

The object is clipped at the top, so I guess my world is not “big” enough. But how do I, say, increase its width in the x and y axis?
I’m using VC6 with NVIDIA Drivers, OGL 1.2 + GLUT.

gluPerspective’s first argument sets an angle that makes the view frustrum smaller/bigger in Y dimension (here 60 degrees) and the second argument changes the aspect between X and Y (it means that 1.0 is the same angle in both X and Y), with this you can change the X dimension.

Have a look at the documentation (VC 6 should have a complete one on its MSDN CD)

Originally posted by Moonspell:
[b]Hi,

I have a problem with defining the “world” on opengl. As far as I understand glViewport is the “camera” in my world, and gluPerspective defines that world. Now please look at this code:
//…
//…
[/b]

How about:
gluLookAt(0, 0, 15, 0, 0, 0, 0, 1, 0);
or:
gluPerspective(60, w/h, 1, 50);
or maybe:
glViewport(0, 0, w, h);

Originally posted by Moonspell:
[b]void ChangeSize(int w, int h)
{
glViewport(0,0,w/2,h/2);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(60.0f, (GLfloat)(w/h), 0, 100.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(20, 1,1,1);
}
[/b]

Moonspell,

You can do several things to bring the object into better view:

  • move away from the origin before drawing geometry (glTranslatef())
  • increase the fov (now 60)

Also: to prevent problems later on you should NEVER use a near value of zero. The ratio between the far and near values determines the accuracy of the depth-buffer. You should try to keep ‘near’ as large as possible and ‘far’ as small as possible, so the ratio far/near is low.

And another tip: do not perform the initial glRotatef in your ChangeSize() method, but do this in the RenderScene() method. (unless the rotate is part of your ‘camera’ position, in which case you should introduce a new method like PlaceCamera() and perform the rotate/translate there; or use gluLookAt to control the ‘camera’ position).

HTH and good luck with OpenGL

Jean-Marc.