1

Hello guys I am beginner to opengl and trying to understand the concept gluPerspective() funtion. I went through this post gluPerspective parameters- what do they mean? post and I wrote this code

glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_DEPTH_BUFFER_BIT);
glViewport(0,0,500,500);
gluPerspective(45,16/9,1.0,3.0);
glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
    glVertex3f(100.0f,0.0f,2.0f);
    glVertex3f(0.0f,0.0f,2.0f);
    glVertex3f(0.0f,100.0f,2.0f);
    glVertex3f(100.0f,100.0f,2.0f);
glEnd();
glFlush();

This is the dispay function but black screen is rendered.What is wrong in my code

Community
  • 1
  • 1

2 Answers2

3

Call glMatrixMode(GL_PROJECTION); before calling gluPerspective.
After that switch back with glMatrixMode(GL_MODELVIEW);.

Axalo
  • 2,953
  • 4
  • 25
  • 39
  • What do the negative values mean –  Jan 31 '15 at 18:58
  • 1
    @Goutam OpenGL's coordinate system is right handed. That means that negative values mean the object is farther away. See [here](https://www.evl.uic.edu/ralph/508S98/coordinates.html). – Axalo Jan 31 '15 at 19:01
  • Ok, But I didn't understand one concept,I specified zNear =1.0 and zFar = 3.0,It means frustum is between 1.0 and 3.0,and how does -2.0 lies in the frustum –  Jan 31 '15 at 19:05
  • @Goutam: it doesn't mean that. The functions are defined in such a way that _positive_ parameter values refer to _negative_ z positions. – derhass Jan 31 '15 at 19:09
  • @Axalo: `zNear=1` is fine. for depth buffer precision, setting the near plane as _far_ as possible is the best you can do – derhass Jan 31 '15 at 19:10
  • @Goutam Have you called glViewport? – Axalo Jan 31 '15 at 19:12
  • I called glViewport(0,0,500,500); where 500 is with hieght of the window, I missed that in question,I have edited it now –  Jan 31 '15 at 19:40
  • @Goutam rereading your question I realized that you forgot to switch the matrix mode. I updated my answer. – Axalo Jan 31 '15 at 19:46
  • I dont know that concept,What does that actually mean,Square is visible even I dont call it –  Jan 31 '15 at 19:48
  • @Goutam I suggest you read a good tutorial about [OpenGL](http://nehe.gamedev.net/). If you want to understand why you need to call glMatrixMode, read [this](http://en.wikibooks.org/wiki/OpenGL_Programming/GLStart/Tut4). – Axalo Jan 31 '15 at 19:51
0

gluPerspective() sets up a projection matrix under the assumption that your eye point is at the origin, and you're looking down the negative z-axis. The zNear and zFar parameters specify the range of distances along the negative z-axis that will be contained within the view volume.

Therefore, with zNear set to 1.0 and zFar to 3.0, z-values within a range of -1.0 to -3.0 will be within the view volume.

To make your quad visible, you will have to use coordinates within that range. Changing all your z-coordinates from 2.0 to -2.0 would work. The more typical approach is to apply a view transformation to place the geometry where it is needed for the projection transformation.

For example, in your case, you could place the quad around the origin, and then use either gluLookAt(), or a simple translation, to move the geometry down the negative z-axis.

You also need to watch out for the matrix modes. The projection matrix should normally be set in the corresponding matrix mode.

The whole thing will then look like this:

glMatrixMode(GL_PROJECTION);
gluPerspective(45,16/9,1.0,3.0);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0f, 0.0f, -2.0f);
glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
glVertex3f(100.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 100.0f, 0.0f);
glVertex3f(100.0f, 100.0f, 0.0f);
glEnd();

Note that the range of your x and y coordinates is far beyond the default coordinate range of [-1.0, 1.0]. You will either want to use additional translation/scaling, or use much smaller values.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133