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

查看:252
本文介绍了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 )(让我们假设在code相机是正确的。

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来代替,而它也不能工作。难道我做错了什么?我是不是把一些出位?如果不是,这是它应该怎么做,让我知道,我会发布一些有可能被创建冲突相机code。

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.

编辑:这里是相机矩阵生成code:

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

}

我跳了错误的地方在这片code,如果没有,我会看看我的数学函数,看看怎么回事。

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:虽然我应该提到的是,至少初始向量(眼睛,中心,向上)是正确的,这样做把德的相机,它应该被(曾与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问题(浮动[]矩阵,INT?OFSET?)......由于某种原因,如果我只是用glMultMatrix(FloatBuffer矩阵),它工作得很好。

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天全站免登陆