OpenGL: creating my own camera

11,712

Solution 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.

Solution 2

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.

Share:
11,712
Zepee
Author by

Zepee

Updated on June 04, 2022

Comments

  • Zepee
    Zepee almost 2 years

    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).