Is there an easy way to use QMatrix4x4 with OpenGL functions, specifically glMultMatrixf?
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?
I also have the same problem with QVector3D, which again I need as GLfloat array in the function glVertex3fv.
First I want to mention that
QMatrix4x4has 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.QMatrix4x4usesqrealsfor internal representation. While the class is intended to be used with OpenGL directly (usingconstData()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,qrealisfloat!):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:So your code keeps valid if the following changes are made:
QVector*/QMatrixto use floats / doubles respectively,… while especially the second isn’t the case when using raw OpenGL commands like
glVertex3f.The
Q_STATIC_ASSERTions 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]