Iphone OpengL:编辑MODELVIEW MATRIX

时间:2011-01-20 17:40:54

标签: iphone opengl-es matrix

我正在研究一个旋转的3D立方体(glFrustumf设置),它将当前矩阵乘以前一个矩阵,以便立方体继续旋转。见下文

/* save current rotation state */
GLfloat matrix[16]; 
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);

/* re-center cube, apply new rotation */
glLoadIdentity(); 

glRotatef(self.angle, self.dy,self.dx,0);

glMultMatrixf(matrix);

问题是我需要退后一步(好像我有一台相机)。 我试图编辑矩阵,这种工作,但拾取噪音。立方体跳了起来。

   matrix[14] = -5.0;
   matrix[13] = 0;
   matrix[12] =0; 

有没有办法编辑当前的Modelview Matrix,以便我可以设置立方体的位置,并将其乘以另一个矩阵?

1 个答案:

答案 0 :(得分:1)

您不应该将OpenGL作为场景图或数学库。这意味着:不要回读矩阵,然后任意地将其相乘。而是每次执行渲染过程时重建整个矩阵堆栈。我想我应该指出,在OpenGL-4中,所有矩阵函数都已被删除。相反,你应该将矩阵作为制服提供。

由于@ Burf2000的评论编辑: 典型的渲染处理程序看起来像这样(伪代码):

draw_object():
    # bind VBO or plain VertexArrays (you might even use immediate mode, but that's deprecated)
    # draw the stuff using glDrawArrays or better yet glDrawElements

render_subobject(object, parent_transform):
    modelview = parent_tranform * object.transform
    if OPENGL3_CORE:
        glUniformMatrix4fv(object.shader.uniform_location[modelview], 1, 0, modelview)
    else:
        glLoadMatrixf(modelview)
    draw_object(object)
    for subobject in object.subobjects:
        render_subobject(subobject, modelview)

render(deltaT, window, scene):
    if use_physics:
        PhysicsSimulateTimeStep(deltaT, scene.objects)
    else:
        for o in scene.objects:
            o.animate(deltaT)

    glClearColor(...)
    glClearDepth(...)
    glViewport(0, 0, window.width, window.height)
    glDisable(GL_SCISSOR_TEST);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT)

    # ...

    # now _some_ objects' render pass - others may precede or follow, like for creating reflection cubemaps or water refractions.

    glViewport(0, 0, window.width, window.height)
    glEnable(GL_DEPTH_TEST)
    glDepthMask(1)
    glColorMask(1,1,1,1)

    if not OPENGL3_CORE:
        glMatrixMode(GL_PROJECTION)
        glLoadMatrixf(scene.projection.matrix)

    for object in scene.objects:
        bind_shader(object.shader)
        if OPENGL3_CORE:
            glUniformMatrix4fv(scene.projection_uniform, 1, 0, scene.projection.matrix)

    # other render passes

    glViewport(window.HUD.x, window.HUD.y, window.HUD.width, window.HUD.height)
    glStencil(window.HUD.x, window.HUD.y, window.HUD.width, window.HUD.height)
    glEnable(GL_STENCIL_TEST)
    glDisable(GL_DEPTH_TEST)

    if not OPENGL3_CORE:
        glMatrixMode(GL_PROJECTION)
        glLoadMatrixf(scene.HUD.projection.matrix)
    render_HUD(...)

等等。我希望你能得到一般的想法。 OpenGL既不是场景图,也不是矩阵操作库。