将QMatrix4x4与OpenGL函数一起使用 [英] Use QMatrix4x4 with OpenGL functions
问题描述
是否可以将QMatrix4x4
与OpenGL功能(特别是glMultMatrixf
)一起使用?
Is there an easy way to use QMatrix4x4
with OpenGL functions, specifically glMultMatrixf
?
如果我理解此权利,则必须对矩阵进行转置,并确保将qreal
(取决于基础系统,可以为float
或double
)转换为GLfloat
.
没有为我执行此功能的功能吗?
If I understand this right, I'd have to transpose the matrix, and be sure to convert qreal
(which can be either float
or double
depending on the underlying system) to GLfloat
.
Isn't there a function which does this for me?
我对QVector3D
也有同样的问题,我又需要在函数glVertex3fv
中将其作为GLfloat
数组.
I also have the same problem with QVector3D
, which again I need as GLfloat
array in the function glVertex3fv
.
推荐答案
首先,我要提到QMatrix4x4
具有用于矩阵乘法(带有第二个矩阵,向量或标量)的内置运算符.但是,您的问题仍然需要答案,因为您希望早晚将矩阵传递给OpenGL.
First I want to mention that QMatrix4x4
has built-in operators for matrix multiplication (with a second matrix, a vector or a scalar). However, your question still needs an answer, as you want to pass a matrix to OpenGL sooner or later.
QMatrix4x4
使用qreals
进行内部表示.虽然该类旨在直接与OpenGL一起使用(使用Bart在其答案中建议的constData()
),但是如果要保持平台兼容性,则可以确保根据类型将其传递给相应的OpenGL函数(在嵌入式设备上,qreal
是float
!):
QMatrix4x4
uses qreals
for internal representation. While the class is intended to be used with OpenGL directly (using constData()
as suggested by Bart in his answer), you can make sure you pass it to the corresponding OpenGL function depending on the type, if you want to keep platform compatibility (on embedded devices, qreal
is float
!):
// these are defined in the OpenGL headers:
void glMultMatrixf(const GLfloat *m);
void glMultMatrixd(const GLdouble *m);
// add overloaded functions which call the underlying OpenGL function
inline void glMultMatrix(const GLfloat *m) { glMultMatrixf(m); }
inline void glMultMatrix(const GLdouble *m) { glMultMatrixd(m); }
// add an overload for QMatrix4x4 for convenience
inline void glMultMatrix(const QMatrix4x4 &m) { glMultMatrix(m.constData()); }
您还可以将这种机制用于矢量,在这里是glVertex*
系列,在这里它更有意义,因为需要考虑原始指针"重载的组件数量,但是面向对象的重载可以自动实现你:
You can also use this mechanism for vectors, here the glVertex*
family, where it makes even more sense because of the number of components the "raw pointer" overloads need to consider, but the object oriented overloads can do automatically for you:
inline void glVertex2v(const GLfloat *v) { glVertex2fv(v); }
inline void glVertex2v(const GLdouble *v) { glVertex2dv(v); }
inline void glVertex3v(const GLfloat *v) { glVertex3fv(v); }
inline void glVertex3v(const GLdouble *v) { glVertex3dv(v); }
inline void glVertex4v(const GLfloat *v) { glVertex4fv(v); }
inline void glVertex4v(const GLdouble *v) { glVertex4dv(v); }
// Note that QVectorND use floats, but we check this during compile time...
Q_STATIC_ASSERT(sizeof(QVector2D) == 2*sizeof(float));
Q_STATIC_ASSERT(sizeof(QVector3D) == 3*sizeof(float));
Q_STATIC_ASSERT(sizeof(QVector4D) == 4*sizeof(float));
inline void glVertex(const QVector2D &v) { glVertex2v(reinterpret_cast<const float*>(&v)); }
inline void glVertex(const QVector3D &v) { glVertex3v(reinterpret_cast<const float*>(&v)); }
inline void glVertex(const QVector4D &v) { glVertex4v(reinterpret_cast<const float*>(&v)); }
// Even for QPointF we can do it!
Q_STATIC_ASSERT(sizeof(QPointF) == 2*sizeof(qreal));
inline void glVertex(const QPointF &v) { glVertex4v(reinterpret_cast<const qreal*>(&v)); }
因此,如果进行以下更改,您的代码将保持有效:
So your code keeps valid if the following changes are made:
- Qt将
QVector*
/QMatrix
的表示形式更改为分别使用浮点数/双精度数, - 您的代码更改了向量的分量数量
- Qt changes the representation of
QVector*
/QMatrix
to use floats / doubles respectively, - Your code changes the number of components of vectors
...,尤其是使用诸如glVertex3f
之类的原始OpenGL命令时,第二种情况并非如此.
... while especially the second isn't the case when using raw OpenGL commands like glVertex3f
.
以上代码中的Q_STATIC_ASSERT
离子仅自Qt 5.0起定义.如果您使用的是Qt4(或具有Qt4兼容性的代码),请在定义之前将其添加到一些全局头文件中: http://ideone.com/VDPUSg [来源:Qt5/qglobal.h]
The Q_STATIC_ASSERT
ions in the code above are only defined since Qt 5.0. If you are using Qt4 (or code with Qt4 compatibility), add this in some global header file / before your definitions: http://ideone.com/VDPUSg [source: Qt5 / qglobal.h]
这篇关于将QMatrix4x4与OpenGL函数一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!