OpenGL:创建我自己的相机 [英] OpenGL: creating my own camera

查看:29
本文介绍了OpenGL:创建我自己的相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个在 3d 空间中移动的相机,但在设置时遇到了一些问题.我正在做的是 Java,显然将 gluPerspective 和 gluLookAt 一起使用会产生冲突(屏幕开始像疯了一样闪烁).

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 设置如下:

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

然后我创建了一个相机矩阵,利用眼睛坐标、前向和向上向量(http://people.freedesktop.org/~idr/glu3/form_4.png)(假设相机的代码是正确的.

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

然后我调用我的绘图程序(它们通过调用 glRotatef 和 glTranslatef 自己做一些平移/旋转).

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

如果没有调用 glMultMatrixf,相机会在屏幕中央显示我需要看到的项目.但是,使用 glMulMatrixf,我得到的只是黑屏.我尝试使用 glLoadMatrixf 代替,但它也不起作用.难道我做错了什么?我是不是把东西放错了地方?如果没有,应该这样做,请告诉我,我会发布一些可能会造成冲突的相机代码.

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.

这是相机矩阵创建代码:

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

虽然我应该提到至少初始向量(眼睛、中心、向上)是正确的,并且确实将相机放在它应该放置的位置(与 gluLookAt 一起使用但有闪烁问题).

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

推荐答案

修复了它.问题是使用 glMultMatrix(float[] matrix,int ?ofset?)...出于某种原因,如果我只使用 glMultMatrix(FloatBuffer matrix) 它工作正常..

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.

这篇关于OpenGL:创建我自己的相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆