首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >摄像机旋转

摄像机旋转
EN

Stack Overflow用户
提问于 2012-11-20 05:25:17
回答 1查看 1K关注 0票数 1

my previous question一样,我在opengl和jogl中创建了一个小太阳系:)现在我想旋转相机,从不同的角度显示它。

我设法旋转了立方体,但它们的运动平面保持不变。

GL3 gl = drawable.getGL().getGL3();

代码语言:javascript
复制
    gl.glClear(GL3.GL_COLOR_BUFFER_BIT | GL3.GL_DEPTH_BUFFER_BIT);
    //Model View Matrix

    //Mat4 mv = new Mat4();
    Mat4 mv = MatrixMath.lookAt(this.eyeX,this.eyeY,this.eyeZ,this.at,this.up);
    mv = mv.mul(MatrixMath.rotationZ(satellite));
    mv.set(2, 2, 0.0f);
    mv.set(2, 3, 0.0f);

    cubeprogram.passUniformMatrix(gl, "model_view", mv);

    gl.glDrawArrays( GL3.GL_TRIANGLES, 0, numVertices );

    angle += 2.0f;
    satellite = angle + 8.0f;
    if (angle > 360.0f)
    angle -= 360.0f;

    float positionX = setMovementX(angle, 0.8f);
    float positionZ = setMovementZ(angle, 0.8f);
   // mv = MatrixMath.lookAt(this.eyeX,this.eyeY,this.eyeZ,this.at,this.up);
    mv = mv.mul(MatrixMath.rotationZ(satellite));
    mv.set(0, 3, positionX);
    mv.set(2, 3, positionZ);
    cubeprogram.passUniformMatrix(gl, "model_view", mv);
    gl.glDrawArrays( GL3.GL_TRIANGLES, 0, numVertices );
    positionX = setMovementX(satellite, 0.2f);
    positionZ = setMovementZ(satellite, 0.2f);
    Mat4 sm = new Mat4( 1.0f,    0.0f,  0.0f,   positionX, 
            0.0f,    1.0f,  0.0f,   0.0f, 
            0.0f,    0.0f,  1.0f,   positionZ, 
            0.0f,    0.0f,  0.0f,   1.0f);
    mv = mv.mul(sm);
    mv = mv.mul(MatrixMath.rotationZ(angle));
    cubeprogram.use(gl);
    cubeprogram.passUniformMatrix(gl, "model_view", mv);
    gl.glDrawArrays( GL3.GL_TRIANGLES, 0, numVertices );
    cubeprogram.setUniformMatrix("projection", projection);`

//定义

代码语言:javascript
复制
public static Mat4 lookAt(Vec4 eye, Vec4 at, Vec4 up) {
    final Vec4 eyeneg = eye.neg();

    final Vec4 n = VectorMath.normalize(eye.sub(at));
    final Vec4 u = VectorMath.normalize(VectorMath.cross(up, n));
    final Vec4 v = VectorMath.normalize(VectorMath.cross(n, u));
    final Vec4 t = new Vec4(0, 0, 0, 1);
    final Mat4 c = new Mat4(u, v, n, t);

    return c.mul(MatrixMath.translate(eyeneg));
}
public static Mat4 lookAt(float x, float y, float z, Vec4 at, Vec4 up) {
    final Vec4 eye = new Vec4(x, y, z, 1.0f);
    return lookAt(eye, at, up);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-21 10:58:43

检查jogl的pmvMatrix

对于摄像头:

代码语言:javascript
复制
pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
pmvMatrix.glLoadIdentity();
pmvMatrix.gluPerspective(fov, aspect, zNear, zFar);
pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
pmvMatrix.glLoadIdentity();
pmvMatrix.glRotatef(-pitch, 1, 0, 0);
pmvMatrix.glRotatef(-yaw, 0, 1, 0);
pmvMatrix.glRotatef(-roll, 0, 0, 1);
pmvMatrix.glTranslatef(-position.x, -position.y, -position.z);
pmvMatrix.update();

固定功能:

代码语言:javascript
复制
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadMatrixf(pmvMatrix.glGetPMatrixf());
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
gl.glLoadMatrixf(pmvMatrix.glGetMvMatrixf());

着色器:

代码语言:javascript
复制
GLUniformData pmvMatrixUniform;

初始化代码...

代码语言:javascript
复制
PMVMatrix pmvMatrix = ...;
state.attachObject("pmvMatrix", pmvMatrix);
pmvMatrixUniform = new GLUniformData("pmvMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf());
state.ownUniform(pmvMatrixUniform);
state.uniform(gl, pmvMatrixUniform);

显示代码...

代码语言:javascript
复制
state.uniform(gl, pmvMatrixUniform);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13462516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档