开发者

Iphone OpengGL : Editing the MODELVIEW_MATRIX

I working on a spinning 3D cube (glFrustumf setup) and it multiplies the current matrix by the previous one so that the cube continues to spin. See below

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

The problem is I need to开发者_JAVA技巧 step back from this (as if I had a camera). I tried to edit the matrix and that kind of works but picks up noise. The cube jumps around.

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

Is there a way to edit the current Modelview Matrix so that I can set the position of the cube with multiplying it by another matrix?


You should not mistreat OpenGL as a scene graph, nor a math library. That means: Don't read back the matrix, and multiply it arbitrarily back. Instead rebuild the whole matrix stack a new every time you do a render pass. I think I should point out, that in OpenGL-4 all the matrix functions have been removed. Instead you're expected to supply the matrices as uniforms.

EDIT due to comment by @Burf2000: Your typical render handler will look something like this (pseudocode):

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

and so on. I hope you get the general idea. OpenGL is neither a scene graph, nor a matrix manipulation library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜