Class QMatrix4x4
-
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 ?
-
@SGaist Okay, Here is my code:
GLWidget::GLWidget(QWidget *parent):QOpenGLWidget(parent)
{
quaternion = QQuaternion( w, x, y, z); // construction of the quaternion
w=1.0;
x=0.0;
y=0.0;
z=0.0;connect(&timer, SIGNAL(timeout()), this, SLOT(update())); timer.start(16);
}
//================================================================================================void GLWidget::initializeGL()
{glClearColor(0.0f,0.0f,0.0f,1.0f); //background
}
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); // Modelview matrix used to apply transformations on the object
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); // trnaslate the elements of the scene to the center // of the screen
quaternion.normalize();
GLfloat m[16];
glGetFloatv (GL_MODELVIEW_MATRIX, m);
QMatrix4x4 modelview(m);
modelview.rotate(quaternion); // applying the rotation: rotate function calculates
//the rotation matrix that corresponds to the quaternion
// then multiplies it by the modelview matrixglLoadMatrixf(modelview.constData());
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);glBegin(GL_QUADS);
[ Then I drew the object]
void GLWidget::resizeGL(int wi, int h)
{
glViewport(0,0,wi,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 45.0, (float)wi/h, 0.01, 100.0);update();
}
Logically the glLoadMatrix will replace the current matrix which is the old modelview matrix with the new modelview matrix after applying the rotation but that doesn't happen and when I run my code, the object disappears.
-
Did you check that the output of your multiplied matrices is what you expect ?
-
Let's take a step back.
Can you properly rotate it if you grab the matrix, manually apply the coefficients and then load it back ?
-
-
So if you grab the matrix, change its values and set it again in does not do anything ?
-
I tried to change the modelview matrix values manually but that didn't work. When I add glLoadMatrix to load it back, the object completely disappears from the scene.
Well, at first I could rotate the object without using the class QQuaternion.
I declared 4 variables which are w,x,y and z (quaternion components) and developped a function to normalize the quaternion and another one to calculate the rotation matrix. Then I used glMultMatrixf to apply the rotation and I could rotate the object.
But, the moment I started using QQuaternion and QMatrix, nothing seemed to work as it should.