OpenGL: creating your own camera

I'm trying to create a camera to navigate through 3D space, and I am having problems setting it up. I am doing this Java and apparently using gluPerspective and gluLookAt together creates a conflict (the screen starts to flicker like crazy).

gluPerspective is installed as follows:

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

Then I create a camera matrix using the coordinates of the eyes, forward and upward along the vectors ( http://people.freedesktop.org/~idr/glu3/form_4.png ) (allows you to take the code for the camera is correct.

Finally, before I draw everything I have:

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

And then I call my drawing routines (which do some translations / rotations on their own, calling glRotatef and glTranslatef).

glMultMatrixf , , . glMulMatrixf , , . glLoadMatrixf , . - ? - ? , , , , .

EDIT: :

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];
        }
    }

}

, - , , , , .

EDIT2: , (, , ) , ( gluLookAt, ).

+2
2

. glMultMatrix (float [] matrix, int? Ofset?)... - , glMultMatrix ( FloatBuffer), .

, , ... , .

+1

glRotatef, glTranslatef glFrustum , ( , UpVec ). 3D-, , , . 3D- gluLookAt, , , .

:

, glFrustum. glPerspecive, :

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

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

. {0}, .x orientation.y, ... ( , t . z)

.

+5

Source: https://habr.com/ru/post/1779216/


All Articles