java OpenGL:创建我自己的相机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4294596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenGL: creating my own camera
提问by Zepee
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).
我正在尝试创建一个相机来在 3d 空间中移动,但在设置时遇到了一些问题。我正在做的是 Java,显然将 gluPerspective 和 gluLookAt 一起使用会产生冲突(屏幕开始像疯了一样闪烁)。
gluPerspective is set like this:
gluPerspective 设置如下:
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.
然后我创建了一个相机矩阵,利用眼睛坐标、前向和向上向量(http://people.freedesktop.org/~idr/glu3/form_4.png)(假设相机的代码是正确的。
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).
然后我调用我的绘图程序(它们通过调用 glRotatef 和 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.
如果没有调用 glMultMatrixf,相机会在屏幕中央显示我需要看到的项目。但是,使用 glMulMatrixf,我得到的只是黑屏。我尝试使用 glLoadMatrixf 代替,但它也不起作用。难道我做错了什么?我是不是把东西放错了地方?如果没有,这就是应该如何做的让我知道,我会发布一些可能会造成冲突的相机代码。
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).
EDIT2:虽然我应该提到至少初始向量(眼睛,中心,向上)是正确的,并且确实将相机放在它应该放置的位置(与 gluLookAt 一起使用但有闪烁问题)。
采纳答案by Zepee
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..
修好了。问题是使用 glMultMatrix(float[] matrix,int ?ofset?)... 出于某种原因,如果我只使用 glMultMatrix(FloatBuffer matrix) 它工作正常..
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.
我正在做的转换存在一些问题,但我应该能够处理这些问题......尽管伙计们,感谢您的投入。
回答by Ned Bingham
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.
使用 glRotatef、glTranslatef 和 glFrustum 来创建相机可能更简单,尽管你的数学对我来说似乎很好(只要实际定义了 UpVec)。在我制作的大多数 3D 图形中,您并没有真正想要跟踪的已定义对象。在我最终确定这个之前,我使用 gluLookAt 经历了 3D 相机的各种实现。
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:
当我创建或初始化我的相机时,我用 glFrustum 设置了投影矩阵。如果您愿意,可以使用 glPerspecive:
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)
定位和定向相机。最初,您将位置和方向都设置为 {0},然后在按下某个键时添加或减去位置,并在移动鼠标时添加或减去orientation.x 和orientation.y...(我通常不t 搞乱方向。z)
Cheers.
干杯。