Problem in the perspective projection

Hi guys,

i’m a newbie in OpenGl. I am trying to render a scene(essentially just a flat polygon square) using perspective projection. I am using gluPerspective() after loading the projection matrix. And using the gluLookAt() after the model view matrix. I just cant figure out why the polygon is not getting rendered. Please guys, tell me know what is wrong with my code.

The following is my code…

GLdouble WindowWidth ;
GLdouble WindowHeight ;

void Init()
{
glShadeModel(GL_SMOOTH) ;
glClearDepth(1.0f) ;
glEnable(GL_DEPTH_TEST) ;
glDisable(GL_CULL_FACE) ;
glFrontFace(GL_CCW) ;
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glClearColor(0.5f , 0.5f , 0.5f , 1.0f) ;

}

void ReShape(int Width , int Height)
{
WindowWidth = (GLdouble)Width ;
WindowHeight = (GLdouble)Height ;

if(Height == 0)
	Height = 1 ;

glMatrixMode(GL_PROJECTION) ;
glLoadIdentity() ;

glViewport(0 , 0 , (GLsizei)Width , (GLsizei)Height) ;

gluPerspective(60.0 , (WindowWidth / WindowHeight) , 1.0f , 20.0f) ;


glMatrixMode(GL_MODELVIEW) ;
glLoadIdentity() ;

gluLookAt(0.0f , 0.0f , -5.0f , 2.0f , 2.0f , 0.0f , 0.0f , 1.0f , 0.0f) ;

}

void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;

// gluLookAt(0.0f , 0.0f , -5.0f , 2.0f , 2.0f , 0.0f , 0.0f , 1.0f , 0.0f) ;

glTranslatef(0.0f , 0.0f , 0.0f) ;
glColor3f(0.1f , 0.2f , 0.5f) ;
glBegin(GL_POLYGON) ;
	glVertex3f(1.0f , 0.0f , 0.0f) ;
	glVertex3f(1.0f , 4.0f , 0.0f) ;
	glVertex3f(5.0f , 4.0f , 0.0f) ;
	glVertex3f(5.0f , 0.0f , 0.0f) ;
glEnd() ;
glFlush() ;

glutSwapBuffers() ;

}

void main()
{
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_SINGLE) ;
glutCreateWindow(“Hello”) ;
glutInitWindowPosition(100 , 100) ;
glutInitWindowSize(500 , 500) ;
Init() ;
glutDisplayFunc(RenderScene) ;
glutReshapeFunc(ReShape) ;
glutMainLoop() ;
}

thanx in advance

yours

Siddharth

Hi,

maybe the square is just not visible. :cool: Move it along z.

Just a guess…
cheers

hi, check out your draw order vs the cull face… hope this helps

Hey guys,

yeah I got it to work. Its just that the camera wasnt focussing the square. Now I want to move the camera back and forth and strafe left and right using the arrow keys. Also rotate the camera by first pressing a key…say “R” and then use the same arrow keys to rotate along the axis. For example, pressing R and then pressing up arrow key shoudl rotate the camer along the x axis…

So how do i go about it?

Please let me know if there are codes that do this with gluLookAt().

Thanx guys

http://www.mevis.de/opengl/gluLookAt.html

Looks like you can adjust the values that go into gluLookAt to get to different positions.
Keep in mind you’ll always be looking at the center, unless you change that. This is designed for looking “at” somthing, afteral.
But, that could be an interesting way to view a game anyway. Stuff in an arena, for example.
Try storing eyex,eyey,eyez outside in the game loop object, then iterate from one value to another to get a smooth tranistion. You could also just jump to a new view, if you want.(say, each corner.)