2

I'm trying to create a camera to move around a 3d space and am having some problems setting it up. I'm doing this is Java, and apparently using gluPerspective and gluLookAt together creates a conflict (the screen starts flickering like mad).

gluPerspective is set like this:

gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(50.0f, h, 1.0, 1000.0);
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);

I then create a camera matrix, making use of eye coordinates, forward and up vectors (http://people.freedesktop.org/~idr/glu3/form_4.png) (lets assume the code for the camera is correct.

Lastly, before I draw any thing I have:

gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glMultMatrixf(camera.matrix);

And then I call my drawing routines (which do some translation/rotation on their own by calling glRotatef and glTranslatef).

Without the call to glMultMatrixf the camera shows the items I need to see in the centre of the screen as it should. With glMulMatrixf however, all I get is a black screen. I tried using glLoadMatrixf instead and it didn't work either. Am I doing something wrong? Am I putting something out of place? If not, and this is how it should be done let me know and I'll post some of the camera code that might be creating the conflicts.

EDIT: Here is the camera matrix creation code:

private void createMatrix()
{
    float[] f = new float[3]; //forward (centre-eye)
    float[] s = new float[3]; //side (f x up)
    float[] u = new float[3]; //'new up' (s x f)        
    for(int i=0;i<3;i++){
        f[i] = centre[i]-eye[i];
    }
    f = Maths.normalize(f);
    s = Maths.crossProduct(f,upVec);
    u = Maths.crossProduct(s,f);

    float[][] mtx = new float[4][4];
    float[][] mtx2 = new float[4][4];   
            //initializing matrices to all 0s   
    for (int i = 0; i < mtx.length; i++) {
        for (int j = 0; j < mtx[0].length; j++) {
            mtx[i][j] = 0;
            mtx2[i][j] = 0;
        }
    }

            //mtx =  [ [s] 0,[u] 0,[-f] 0, 0 0 0 1]
            //mtx2 = [1 0 0 -eye(x), 0 1 0 -eye(y), 0 0 1 -eye(z), 0 0 0 1]
    for(int i=0;i<3;i++){
        mtx[0][i] = s[i];
        mtx[1][i] = u[i];
        mtx[2][i] = -f[i];

        mtx2[i][3]=-eye[i];
        mtx2[i][3]=-eye[i];
        mtx2[i][3]=-eye[i];
    }
    mtx[3][3] = 1;
    mtx2[0][0]=1;mtx2[1][1] = 1;mtx2[2][2] = 1;mtx2[3][3] = 1;

    mtx = Maths.matrixMultiply(mtx,mtx2);
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
                            // this.mtx is a float[16] for glMultMatrixf
            this.mtx[i*4+j] = mtx[i][j];
        }
    }

}

I'm hopping the error is somewhere in this piece of code, if not, I'll have a look at my maths functions to see whats going on..

EDIT2: Though I should mention that at least the initial vectors (eye,centre,up) are correct and do put teh camera where it should be (worked with gluLookAt but had teh flickering issue).

Zepee
  • 1,640
  • 3
  • 20
  • 40
  • 1
    There's nothing wrong with the code you have posted (except for the second argument to gluPerspective, which expects the ratio w/h). I cannot think of any other explanation than the camera matrix being wrong. – Kos Nov 28 '10 at 00:22
  • yea, the 'h' variable is width/height. Why someone named it 'h' I don't know (group work). Anyway, I posted the code that's supposed to create the matrix. Hope someoen can spot what I'm doing wrong. – Zepee Nov 28 '10 at 00:48
  • Why not use ol' good `gluLookAt` instead of a custom camera matrix? – Kos Nov 28 '10 at 01:26
  • 'gluPerspective and gluLookAt together creates a conflict (the screen starts flickering like mad)' Im using Java+JOGL (don't even ask) and by using both the screen flickers like mad for some reason, and I don't really want that (I think its understandable why). Must be something with the threads Java and AWT decide to create to run JOGL or something. So I'm trying to use it with my own camera matrix – Zepee Nov 28 '10 at 01:39
  • There shouldn't be any conflict- `gluPerspective` and `gluLookAt` are both nothing more than specific cases of `glMultMatrix`, no magic involoved - you can google the equations for both. If your screen was flickering, then I sincerely believe that something must have been wrong in the code. Can you post a minimalistic application with the flickering you're mentioning? That's what we should focus on, IMHO. – Kos Nov 28 '10 at 13:52
  • I would, but unfortunatly I'm running on a tight schedule and have to focus on this. The only difference to the code posted before was that instead of having glMultMatrix I had gluLookAt. The camera code was all the same, the only thing I added afterwards was the creation of the matrix. I know what gluLookAt does is basically call glMultMatrix itself, but I would not be surprised if there were conflicts as JOGL, at least from self experience, is a mess and doesn't work like it should most of the time.. Anyway, thanks for the input and for wanting to keep digging into it, I appreciate it. – Zepee Nov 28 '10 at 16:07

2 Answers2

5

It might be simpler to use glRotatef, glTranslatef, and glFrustum to create the camera, although your math seems fine to me (just as long as UpVec is actually defined). In most of the 3D graphics that I have done, you didn't really have a defined object that you wanted to track. I went through various implementations of a 3D camera using gluLookAt before I finally settled on this.

Here is how I tend to define my cameras:

When I create or initialize my camera, I set up the projection matrix with glFrustum. You can use glPerspecive if you prefer:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(left, right, down, up, near, far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

After I clear the color and depth buffers for a render pass, then I call

glLoadIdentity();
glRotated(orientation.x, 1.0, 0.0, 0.0);
glRotated(orientation.y, 0.0, 1.0, 0.0);
glRotated(orientation.z, 0.0, 0.0, 1.0);
glTranslatef(position.x, position.y, position.z);

To position and orient the camera. Initially, you set position and orientation both to {0}, then add or subtract from position when a key is pressed, and add or subtract from orientation.x and orientation.y when the mouse is moved... (I generally don't mess with orientation.z)

Cheers.

Ned Bingham
  • 2,749
  • 18
  • 23
  • The only reason I try to avoid directly using glRotate/glTranslate to control the camera is that I heard it is kind of a bad hack and tens to slow down the program. But yea, I can see where you are going with that, and it should definitly work as well. – Zepee Dec 03 '10 at 18:17
1

Fixed it kind of. The problem was using glMultMatrix(float[] matrix,int ?ofset?)... for some reason if I just use glMultMatrix(FloatBuffer matrix) it works fine..

There are some issues with the transformations I'm making but I should be able to deal with those... Thank you for your input though guys.

Zepee
  • 1,640
  • 3
  • 20
  • 40