-1

I am trying to test gluLookAt using this code. But I can see only a black screen. What is wrong with this code ? Is there any basic concept about glulookAt (or opengl camera) that I need to understand.

glViewport(0,0,640,480);
glEnable(GL_DEPTH_TEST);
glClearColor(0,0,0,1);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluLookAt(0,0,5,0,0,0,0,0,1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_POLYGON);
glColor3f(1, 0, 0);
glVertex2d(0.25, 0.25);
glVertex2d(-0.25, 0.25);
glVertex2d(-0.25, -0.25);
glVertex2d(0.25, -0.25);
glEnd();
Sudesh
  • 100
  • 4
  • a word of advice about OpenGL (whatever you are doing): start simple, if you are trying to understand gluLookAt, first make absolutely sure that everything else in your program works *using visual feedback*. In this case, I suggest to **a)** create a simple program with **ortographic projection** and make sure that you can see something (a point, a line, whatever...) in the -1,1 cube, **b)** then try a basic projection **c)** and make sure it works, then test gluLookAt. I'm sort of a newbie on OpenGL myself, and this approach did helped me immensely – Rick77 Oct 15 '14 at 09:15
  • and another word of advice forget about the legacy openGL (any tutorial that uses glBegin/glEnd) but instead look for 3.2+ tutorials to learn it, it forces you to manage your own matrices and will teach you more about how everything actually works – ratchet freak Oct 15 '14 at 09:20
  • Another word of advice: gluLookAt, like most else goes into the *modelview* matrix (gluLookAt provides the *view* part). The projection matrix should be used only for setting the "lens" parameters (field of view, lens shift) but left alone for anything else. Only place a "pure" orthographic or perspective projection in the projection matrix but nothing else. – datenwolf Oct 16 '14 at 07:37

1 Answers1

0

One issue is the up vector of gluLookAt is in the same direction as the look direction.

All you need to do is set +Y up and it should work...

gluLookAt(0, 0, 0.5, //position is +0.5 along Z (NOTE: 0.5, not 5. see below),
          0, 0, 0,   //looking at a 0.5x0.5 X/Y quad at the origin
          0, 1, 0    //rotated such that +Y is up
         );

The other issue is that gluLookAt shouldn't be applied to the projection matrix. It'll work for now but will break lighting later. Move it to the modelview matrix:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(... as before ...);

Assuming the projection matrix hasn't been set, changing the from position of gluLookAt back to your 5 will make the quad disappear. This is because the default projection gives a viewing volume of an orthographic -1 to 1 cube. With the "camera" now too far away it won't see anything. This is where you'll want to investigate changing the projection matrix. Maybe increase the size of the orthographic projection with glOrtho(), or look into the more complex but natural gluPerspective().

jozxyqk
  • 16,424
  • 12
  • 91
  • 180