gluLookAt not working

I’m learning OpenGL principles from Edward Angel’s book, “OpenGL: A Primer”, translating from C into Java/jogl as I go. His first example for working with the camera looks like:

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glutWireCube(0.5);
glutSwapBuffers();
}

Here’s my translation:

public void display(GLDrawable drawable) { 
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT ); 
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
   
              glu.gluLookAt(1.0, 1.0, 1.0, //eye
                               0.0, 0.0, 0.0,  //object
                               0.0, 1.0, 0.0); //up

    glut.glutWireCube(gl, 0.5f);
    gl.glFlush();
} 

In fact, I’m getting a black screen. No image. Although I can get images in other 3D examples, where I’m not using gluLookAt. Where could I be going wrong? BTW, is there a jogl equivalent for glutSwapBuffers()? It certainly doesn’t seem to be in the GLUT interface.

My init code is below, in case that’s the issue:

public void init(GLDrawable drawable) {
this.gl = drawable.getGL();
this.glDrawable = drawable;

        drawable.setGL( new DebugGL(drawable.getGL() ));
        glu = drawable.getGLU();
        drawable.addMouseListener(this);
        drawable.addMouseMotionListener(this);

  float pos[] = { 20.0f, 5.0f, 10.0f, 0.0f };
  gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, pos);
  gl.glEnable(GL.GL_CULL_FACE);
  gl.glEnable(GL.GL_LIGHTING);
  gl.glEnable(GL.GL_LIGHT0);
  gl.glEnable(GL.GL_DEPTH_TEST);
  
  gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  gl.glColor3d(1.0, 1.0, 1.0);

}

Re swapBuffers:

There are these two methods in GLCanvas class, so I presume you use these instead:

void setAutoSwapBufferMode(boolean onOrOff)

  • Enables or disables automatic buffer swapping for this drawable.

void swapBuffers()

  • Swaps the front and back buffers of this drawable.

By default I’m pretty sure the buffers are swapped automatically when needed so you dont have to worry about it, but I’m no expert :).

As for seeing only a black screen, its probably getting rid of the box as its two far away. What is your code for the reshape method. You should have something like:

glu.gluPerspective(40.0f, (width/height), 1.0, 200.0);

where 200 is the culling distance. Your code worked OK for me :).

Typically after doing a lookAt calculation, you need to invert the resulting matrix. Check to see if you need that in there.

[quote]As for seeing only a black screen, its?Vc?VV?rid of the box as its two far away. What is your code for the reshape method. You should have something like:

glu.gluPerspective(40.0f, (width/height), 1.0, 200.0);

where 200 is the culling distance. Your code worked OK for me :).
[/quote]
My reshape method had been empty (and I hadn’t been reshaping the window). I tried yours, and I tried reshaping - still black.

…Oh! I found Angel’s reshape method, here it is:

public void reshape(GLDrawable drawable, int x, int y, int width, int height)    {
              gl.glViewPort(0, 0, width, height);
    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
              gl.glOrtho(-4.0f, 4.0f, -4.0f, 4.0f, -4.0f, 4.0f);        
} 

PROBLEM: glViewPort doesn’t seem to exist in jogl. Or where is it?

I might be completely overanalyzing what you have there, but…

gl.glViewport instead of gl.glViewPort =p

For answers to things like this you need to download the javadoc documentation for JOGL

Hi ! Try to set the view frustum right, everything should work fine then. I had problem with polygons just disappearing - and the source of the problem was gluPerspective() call with near clipping pane set to 0. With the wrong parameter in gluPerspective gluLookAt didn’t work for me either.

I’m also having this problem and have played with the glu.gluPerspective function and still get a black screen with no objects

public void reshape(GLDrawable glDrawable, int x, int y, int width, int height)
    {
        GL gl = glDrawable.getGL();
        GLU glu = glDrawable.getGLU();
        
        if (height <= 0)
            height = 1;
        float h = ((float)width / (float)height);
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL_PROJECTION);
        gl.glLoadIdentity();
        camera.positionCamera(0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -6.0f, 0.0f, 1.0f, 0.0f);
        glu.gluPerspective(45.0f, h, 0.5f, 150.0f);
        gl.glMatrixMode(GL_MODELVIEW);
        gl.glLoadIdentity();
    }
glu.gluLookAt(((double)camera.getCameraLocationVector().getPointX()),
                      ((double)camera.getCameraLocationVector().getPointY()),
                      ((double)camera.getCameraLocationVector().getPointZ()),
                      ((double)camera.getViewVector().getPointX()), 
                      ((double)camera.getViewVector().getPointY()), 
                      ((double)camera.getViewVector().getPointZ()),
                      ((double)camera.getUpVector().getPointX()), 
                      ((double)camera.getUpVector().getPointY()),
                      ((double)camera.getUpVector().getPointZ()));

when i use the gluLookAt method in GLU no objects appear, without it…they work…
if you would like i can post my main class but i don’t know what help that would be.

Any help would be appreciated. Thanks!

  • nc

Revisited: I think I made a really naive mistake here:):):slight_smile: first glLoadIdentity then glLookAt… i simply misplaced them, now the problem is set. really a good lesson
[i]
as a newbie for JoGL, i also envounters this problem…

in OpenGL, without manually setting gluLookAt(), the viewing camera should be placed at (0,0,-1) and look at (0,0,0) by default;

but even if i set the parameters as gluLookAt( 0, 0, -1, 0, 0, 0, 0, 1, 0), there is a black screen for me;

just don’t know why… played with it till early in the morning…

PS: i previously use GLUT, is this a problem?[/i]

code here:



// as for this code i am just drawing the 3D axis
public void display(GLDrawable drawable)
	{
	      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
	      
	      gl.glMatrixMode(GL.GL_PROJECTION );
	      gl.glLoadIdentity();
	      glu.gluPerspective(45.0, screenW/screenH*4/3, 0.1, 1000.0);
	      
	      gl.glMatrixMode(GL.GL_MODELVIEW);
	      glu.gluLookAt( 0, 0, -100, 0, 0, 0, 0, 1, 0 );
	      gl.glLoadIdentity();
	      
	      // dealing with viewing position and direction
	      /*float xTrans = -xpos;
	      float yTrans = -walkbias - 0.43f;
	      float zTrans = -zpos;
	      float sceneroty = 360.0f - yrot;
	      gl.glRotatef(lookupdown, 1.0f, 0.0f, 0.0f);
	      gl.glRotatef(sceneroty, 0.0f, 1.0f, 0.0f);
	      gl.glTranslatef(xTrans, yTrans, zTrans);*/
	      
	      
	      // draw the axis
	      gl.glBegin(GL.GL_LINES);
	      	gl.glLineWidth( 2 );
	      	gl.glColor3f( 1.0f, 0.0f, 0.0f );
	      	gl.glVertex3f( -1000.0f, 0.0f, 0.0f);
	      	gl.glVertex3f( 1000.0f, 0.0f, 0.0f);
	      	gl.glColor3f( 0.0f, 1.0f, 0.0f );
	      	gl.glVertex3f( 0.0f, -1000.0f, 0.0f);
	      	gl.glVertex3f( 0.0f, 1000.0f, 0.0f);
	      	gl.glColor3f( 0.0f, 0.0f, 1.0f );
	      	gl.glVertex3f( 0.0f, 0.0f, -1000.0f);
	      	gl.glVertex3f( 0.0f, 0.0f, 1000.0f);
	      gl.glEnd();
        gl.glFlush()
}

this maybe because you put the gluLookAt code under the PROJECTION matrix model;