Jump to content
  • Advertisement
Sign in to follow this  
ahmadka

OpenGL Why isnt gluPerspective working for me ??

This topic is 4135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys....I am trying to read up Nehe's wonderful tutorials....He sure has done an excellent job at them...the only thing is, the non-OpenGL code is too complex for me :P...My C++ is not *VERY* advanced ya know :P... anyways, I am trying to add perspective into a scene I created....as soon as I switch from glOrtho to gluPerspective, I end up seeing nothing in output window...in glOrtho (0.0, 400.0, 0.0, 225.0,-10,10); everything is OK, when I change this line to gluPerspective(100,0.6,-200,200); I see nothing; only the Clear Color (set to Black) is visible.... I have tried tweaking with the arguments, and I have tried moving the objects about, but there is no effect.... Please tell me what the problem could be... I know posting codes altogether is not preferred here, but this is a small code, so I thought maybe you guys can have a look....all the code draws is a simple triangle :P.... #include "stdafx.h" #include <GL/glut.h> void init (void) { glClearColor (1.0, 1.0, 1.0, 1.0); glMatrixMode (GL_PROJECTION); glLoadIdentity(); // Reset The Projection Matrix glOrtho (0.0, 400.0, 0.0, 225.0,-10,10); // <--- SWITCH BETWEEN THESE TWO //gluPerspective(100,0.6,-200,200); // <--- SWITCH BETWEEN THESE TWO glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix glLoadIdentity(); // Reset The Modelview Matrix glShadeModel(GL_SMOOTH); // Enable Smooth Shading glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background glClearDepth(1.0f); // Depth Buffer Setup glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations } void symTriangle (void) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glBegin (GL_TRIANGLE_STRIP); glColor3f (1.0, 0.0, 0.0); glVertex2i (50, 50); glColor3f (0.0, 1.0, 0.0); glVertex2i (150, 50); glColor3f (0.0, 0.0, 1.0); glVertex2i (50, 150); glEnd ( ); glFlush ( ); } void main (int argc, char** argv) { glutInit (&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition (100, 100); glutInitWindowSize (400, 400); glutCreateWindow ("An Example OpenGL Program"); init ( ); glutDisplayFunc (symTriangle); glutMainLoop ( ); }

Share this post


Link to post
Share on other sites
Advertisement
Hi,

firstly you only need to call gluPerspective once, when you resize the window.
this should be the function in the NeHE tutorial


GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}

glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(52.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}




gluPerspective(52.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
first parameter is field of view, which in your case 100, seems a little big, try something between 45 & 60.
Next is the aspect say your window is 800x600 your aspect would be 800 / 600 (=1.333) how did you get 0.6 ?

Just use the height & width to calculate this, don't hard code it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!