0
votes

i drawn a cube and i can rotate it. I would want to print out the position of vertices after rotation. in some tutorials i found that i can use QMatrix4x4 to rotate my cube and the to get the new position of vertices. the i changed mu code:

void MyWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    QMatrix4x4 matrix;  // new matrice for transformation
    matrix.translate(0.0, 0.0, -10.0);
    matrix.Rotate(xRot / 16.0, 1.0, 0.0, 0.0);
    matrix.Rotate(yRot / 16.0, 0.0, 1.0, 0.0);
    matrix.Rotate(zRot / 16.0, 0.0, 0.0, 1.0);

    glBegin(GL_QUADS);  //1st quad
    qglColor(Qt::green);
    glNormal3f(0.0f, 0.0f, -1.0f);
    ---
    glEnd();
}

my question is how to apply the matrix to draw the vertice because glvertex3f can't take that matrix. Then i will print out the new position as follow: ( matrix * QVector3D(0.0f, 0.0f, -1.0f)) for example;

After Some days and thanks to the links you gave me, i had tried to read about Modern OpenGL: Here is my code now:

void GLWidget::paintGL() { -------- QMatrix4x4 matrixTransformation; matrixTransformation.rotate(alpha, 0,1,0); matrixTransformation.rotate(beta, 0, 1, 0);

 QVector3D cameraPosition = matrixTransformation * QVector3D(0,0,distance);
 QVector3D cameraUpDirection = matrixTransformation * QVector3D(0, 1, 0);

vMatrix.lookAt(cameraPosition, QVector3D(), cameraUpDirection);
// display the coordinates
 foreach(const QVector3D& p, cameraPosition) {
  qDebug() << "Rotated position =" << matrixTransformation * p;
}
 shaderProgram.bind();
 shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix *mMatrix);
 shaderProgram.setUniformValue("color", QColor(Qt::white));
 shaderProgram.setAttributeArray("vertex", vertices.constData());
 shaderProgram.enableAttributeArray("vertex");
 glDrawArrays(GL_TRIANGLES, 0,vertices.size());
 shaderProgram.disableAttributeArray("vertex");
 shaderProgram.release();}

} an error appeared : error C2039: ‘const_iterator’ : is not a member of ‘QVector3D’ error C2039: ‘i’ : is not a member of ‘QForeachContainer’

I had included and in my header but the error still there.

  1. Is the way i used correct to get the coordinates of vertices??
  2. the error is from my foreach loop code, and is maybe about the declaration of QVector3D in a qvector3d.h of Qt. How to solve it??
  3. if the use of transformation matrix , how can iuse a transform feedback object to pass the vertex positions that come out of the vertex shader back into another buffer object. I seen that its another solution to get coordinates of vertices after rotation.

Thanks and sory for my questions which are may be for beginners.

2
You'd need transform feedback for this to work. There is no way to read back the position of the vertices after transformation otherwise. In legacy OpenGL (which is what you are using), there was something called the "feedback buffer" that allowed you to do this, but it is quite complicated and it actually gives you the vertices after clipping; it's definitely not what you want.Andon M. Coleman
i think the easy way for me is to draw my cube with the modern OpenGL.i can find some example of it but is it really easy with Modern openGL to get the new coordinates of vertices after rotation. if you suggest me so, i can go with modern OpenGL even it's diffucult for me. Thankschinois

2 Answers

0
votes

You can upload a QMatrix4x4 to the OpenGL matrixstack. Uploading a matrix is done by using the glLoadMatrixf methode, where your data comes from the QMatrix4x4's constData() function. The code could look like the following:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
QMatrix4x4 matrix;  // new matrice for transformation
matrix.translate(0.0, 0.0, -10.0);
matrix.Rotate(xRot / 16.0, 1.0, 0.0, 0.0);
matrix.Rotate(yRot / 16.0, 0.0, 1.0, 0.0);
matrix.Rotate(zRot / 16.0, 0.0, 0.0, 1.0);
glLoadMatrixf(matrix.constData());

//Draw as already done before

More information about how qt's matrices and vectors can be used together with OpenGL can be found in this very good answer here

0
votes

I know that this might sound a bit too much especially in the beginning, but since you mentioned that you are new to OpenGL, I would suggest you to not learn OpenGL using the deprecated fixed function pipeline (glLoadMatrixf, glVertex3f, ...).

Rather than doing glVertex3f calls, you should consider using a Vertex Buffer Object (see VBO) and put in your vertices.

Next thing is writing a simple pass through shader which has a matrix defined in the vertex shader were you can upload your matrix before drawing.

The actual code to draw your object is then a single OpenGL API call.

As I said it might be a lot stuff to learn, but it is definitely worth it in case you really want to know and understand how opengl works without using the deprecated functions.

Here you can find a really good table showing the extensions and their state: OpenGL Extensions Core Availability

Here you can find a good website showign some tutorials based on OpenGL 4: OpenGL 4 based tutorials