gpt4 book ai didi

OpenGL:什么是矩阵模式?

转载 作者:行者123 更新时间:2023-12-04 00:40:26 25 4
gpt4 key购买 nike

有几种模式可用:

Modelview
Projection
Texture
Color

他们的意思是什么?最常用的是哪一种?有没有适合初学者的简单读物?

最佳答案

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;
    }

    稍后在 C++ 程序中,您可以编写
    void draw_something()
    {
    OpenGL::Projection::LoadIdentity();
    OpenGL::Projection::Frustum(...);

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

    // drawing commands
    }

    不幸的是,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(...);
    }

    }

    我认为最后一段(伪)代码很清楚: glMatrixMode有点像 with OpenGL 的声明。

    关于OpenGL:什么是矩阵模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5367361/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com