有几种可用的模式:
Modelview
Projection
Texture
Color
它们是什么意思?哪一个最常用?你知道适合初学者的简单读物吗?
有几种可用的模式:
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
有点像OpenGL的with
语句。所有这些矩阵都是由OpenGL在内部使用的,但是你是否需要更改它们取决于你的应用程序。
您总是希望设置投影矩阵以确定您的视野和所查看的空间的范围。通常,您将设置模型视图矩阵来选择您的“相机”方向,并定位场景中的对象。
纹理矩阵和颜色矩阵不常用。在我的当前项目中,我使用纹理矩阵来翻转我的位图中的Y轴。我个人从未使用过颜色矩阵。
你可以在这里找到答案 http://www.opengl.org/sdk/docs/man/xhtml/glMatrixMode.xml
modelview 用于建模。Projection 用于投影,如 3D 物品。Texture 用于纹理。Color 用于着色。但还有更多的内容。只需阅读我给你的链接即可。干杯。