Class QMatrix4x4
-
Hello.
I've been trying to rotate my object using quaternion.
I'm coding in c++ and I'm using Qt6 and OpenGL for object rendering.So after defining my quaternion, I wanted to use void QMatrix4x4::rotate(const QQuaternion &quaternion) to rotate the object. I know that this function multiplies the current matrix which is the modelview matrix in my case, with the rotation matrix.
So here's what I've tried to do:
QMatrix4x4 matrix ,model;GLfloat m[16]; glGetFloatv (GL_MODELVIEW_MATRIX, m); model=m*matrix;
I tried to retrieve the values of the modelview matrix using glgetfloatv which returns a 4x4 matrix.
then I wanted to multiply it by an identity matrix which is "matrix" to get another matrix which I called model and which is my modelview matrix.
Next I want to use this function : model.rotate(quaternion) to rotate the object where model is my modelview matrix.
The problem is that I get this error : invalid operands to binary expression ('GLfloat [16]' and 'QMatrix4x4') in this line : model= m*matrix;
Now I'm stuck and I don't know how to fix this.
Can someone help me please ? -
Hi,
Unless mistaking, you should build a QMatrix4x4 out of your model view matrix before multiplying it with your own matrix.
-
What did you do ?
What did not work ? -
My suggestion was to create a QMatrix4x4 out of your GLfloat array.
Something like
QMatrix4x4 modelview(m); prod = matrix * modelview;
Check the warning off the constructor.
-
@SGaist okay now I get no error but there is another little problem.
I'm trying to rotate my object using QQuaternion class: so here is what I did:
First I defined my quaternion:
GLWidget::GLWidget(QWidget *parent):QOpenGLWidget(parent){
quaternion= QQuaternion(w=1.0, x=0.0, y=0.0, z=0.0); quaternion.setScalar(w); quaternion.setX(x); quaternion.setY(y); quaternion.setZ(z); connect(&timer, SIGNAL(timeout()), this, SLOT(update())); timer.start(16);
}
then :
void GLWidget::paintGL()
{glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // camera transformation gluLookAt(6.0, 6.0, 6.0, 3.0,3.5,0.0, 0.0,1.0,0.0); glTranslatef(1.1, 2.1, -3.1);
QMatrix4x4 matrix, prod;
GLfloat m[16]; //GLfloat matrix[16]; glGetFloatv (GL_MODELVIEW_MATRIX, m); QMatrix4x4 modelview(m); prod = matrix * modelview; prod.rotate(quaternion);
then I drew the object I want to rotate which is a parallelepiped.
My mainwindow looks like this:
where the 4 first values are w,x,y,z.When I change these values, the object is supposed to rotate but it does not.
-
Did you apply the newly calculated matrix ?
-
@SGaist yes I applied it using the rotate function :
GLfloat m[16];
//GLfloat matrix[16];
glGetFloatv (GL_MODELVIEW_MATRIX, m);
QMatrix4x4 modelview(m);
modelview.rotate(quaternion);
update();Well the rotation will be applied on the modelview matrix and logically the object should rotate when I change the values of w,x,y,z but that doesn't happen.
-
Did you apply the newly calculated matrix ?
-
I think there is a misunderstanding here.
You are modifying the local object that you created out of the data returned by glGetFloatv. Changing that as no influence whatsoever on the model view matrix actually used.
You have to load it back.
It's like when you want to modify a font on a widget. You retrieve the font, modify it and they apply it on the widget.
-
@Erija said in Class QMatrix4x4:
GLfloat m[16]; glGetFloatv (GL_MODELVIEW_MATRIX, m); QMatrix4x4 modelview(m); modelview.rotate(quaternion); update();
You rotate "modelview" which is built out of "m" that you populated with glGetFloatv.
That variable is decoupled from the OpenGL pipeline. -
@SGaist Modelview is built out of m and m contains the values of the modelview matrix.
In this line : QMatrix4x4 modelview(m); creates a 4x4 matrix from "m" so modelview(m) is the same as the modelview matrix. Am I right ??By decoupled, you mean that OpenGL doesn't consider it as The modelview matrix, I mean the one used at the beginning for applying transformation such as rotation, translation and scaling ?
-
@Erija said in Class QMatrix4x4:
@SGaist Yesterday you told me that I should load it back ? does it mean that I should use glLoadMatrix ?
Yes, the way the OpenGL pipeline works is closer to my QFont example. The data you fetch from it is a copy of what is used not a reference to it.
-
Can you show your code ?