2011-01-20 19 views
0

我在旋转3D立方体(glFrustumf设置)上工作,它将当前矩阵乘以前一个矩阵,以使立方体继续旋转。看下面Iphone OpengGL:编辑MODELVIEW_MATRIX

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

有没有办法编辑当前的模型视图矩阵,以便我可以设置立方体乘以另一个矩阵的位置?

+0

[Iphone OpenGL:glOrthof vs glFrustumf可能出现重复。是glOrthof不是3D?](http://stackoverflow.com/questions/4749786/iphone-opengl-glorthof-vs-glfrustumf-is-glorthof-not-3d) – genpfault 2011-01-20 18:52:32

回答

1

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

EDIT由于评论通过@ 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既不是场景图,也不是矩阵操作库。

+0

我不认为iPhone支持OpenGL 4. – genpfault 2011-01-20 18:53:03