OpenGL:什么是MatrixMode?

use*_*652 12 opengl

有几种模式可供选择:

Modelview
Projection
Texture
Color
Run Code Online (Sandbox Code Playgroud)

他们的意思是什么?什么是最常用的?您对初学者有什么简单的读数吗?

dat*_*olf 16

OpenGL使用几个矩阵来转换几何和相关数据.那些矩阵是:

  • 模型视图 -地点在全球,物体几何非投影空间
  • 投影 - 将全局坐标投影到剪辑空间; 你可能会认为它是一种镜头
  • 纹理 - 之前调整纹理坐标; 主要用于实现纹理投影(即投影纹理,就好像它是投影仪中的幻灯片一样)
  • 颜色 - 调整顶点颜色.很少有人感动

所有这些矩阵都一直在使用.由于他们都遵循相同的规则的OpenGL只有一组矩阵操作函数:glPushMatrix,glPopMatrix,glLoadIdentity,glLoadMatrix,glMultMatrix,glTranslate,glRotate,glScale,glOrtho,glFrustum.

glMatrixMode选择那些操作作用于哪个矩阵.假设您想编写一些C++命名空间包装器,它可能如下所示:

namespace OpenGL {
  // A single template class for easy OpenGL matrix mode association
  template<GLenum mat> class Matrix
  {
  public:
    void LoadIdentity() const 
        { glMatrixMode(mat); glLoadIdentity(); }

    void Translate(GLfloat x, GLfloat y, GLfloat z) const
        { glMatrixMode(mat); glTranslatef(x,y,z); }
    void Translate(GLdouble x, GLdouble y, GLdouble z) const
        { glMatrixMode(mat); glTranslated(x,y,z); }

    void Rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) const
        { glMatrixMode(mat); glRotatef(angle, x, y, z); }
    void Rotate(GLdouble angle, GLdouble x, GLdouble y, GLdouble z) const
        { glMatrixMode(mat); glRotated(angle, x, y, z); }

    // And all the other matrix manipulation functions
    // using overloading to select proper OpenGL variant depending on
    // function parameters, and all the other C++ whiz.
    // ...
  };

  // 
  const Matrix<GL_MODELVIEW>  Modelview;
  const Matrix<GL_PROJECTION> Projection;
  const Matrix<GL_TEXTURE>    Texture;
  const Matrix<GL_COLOR>      Color;
}
Run Code Online (Sandbox Code Playgroud)

稍后在C++程序中,您可以编写

void draw_something()
{
    OpenGL::Projection::LoadIdentity();
    OpenGL::Projection::Frustum(...);

    OpenGL::Modelview::LoadIdentity();
    OpenGL::Modelview::Translate(...);

    // drawing commands
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,C++不能模板命名空间,或者在实例上应用using(或with)(其他语言都有这个),否则我会写一些像(无效的C++)

void draw_something_else()
{
    using namespace OpenGL;

    with(Projection) {    // glMatrixMode(GL_PROJECTION);
        LoadIdentity();   // glLoadIdentity();
        Frustum(...);     // glFrustum(...);
    }

    with(Modelview) {     // glMatrixMode(GL_MODELVIEW);
        LoadIdentity();   // glLoadIdentity();
        Translate(...);   // glTranslatef(...);
    }

}
Run Code Online (Sandbox Code Playgroud)

我认为这最后一段(伪)代码清楚地说明了:glMatrixMode是一种withOpenGL声明.