Home > front end >  How to rotate scene OpenGL with Qt
How to rotate scene OpenGL with Qt

Time:08-01

I've got couple of methods to get reaction on some Qt events. In one of those methods i'm drawing point in OpenGl widget. And in another I want to rotate them on some angle. The scene is plane scene. Here are those methods:

#include "GLMap.h"
GLMap::GLMap{}

void GLMap::initializeGL(){
    glClearColor(r,g,b,alpha);
}

void GLMap::resizeGL(int w, int h){
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glViewport(0,0,(GLint)w,(GLint)h);
    glOrtho(0,1024,720,0,-1,1);
    glPushMatrix();
}

void GLMap::paintGL(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    if(pointsVec.size()!=0){
         glPushMatrix();
         foreach(QPoint pt, pointsVec){
              draw(pt.x(),pt.y(),GL_POINTS);
         }

         glPopMatrix();
    }
}

void GLMap::draw(int x, int y, GLenum type){
     glPointsSize(5);
     glBegin(type)
         glColor3f(0,0,0);
         glVertex2i(x,y);
    glEnd();
 }

 void GLMap::mouseDoubleClickEvent(QMouseEvent *event){
    cutPt.setX(event->x());
    curPt.setY(event->y());
    pointsVec.push_back(curPt);
    repaint();
 }

 void GLMap::wheelEvent(QWheelEvent *event){ 
   anlge =0.9;
   if(ang le>360.0){
        angle = 0.0f;
   }

   glPopMatrix();
   glLoadIdentity();
   glRotatef(angle,0.0,0.0,1.0f);
   glPushMatrix();
 }

The rotation don't want to be done. What is wrong in my code.

CodePudding user response:

The glRotatef() function applies a rotation to the matrix currently at the top of the matrix stack. When you draw, it does so using the matrix currently at the top. And when you glPopMatrix(), you revert the matrix stack to the one below.

So the pattern while issuing commands in a painting function will typically look more like:

glPushMatrix();
glRotatef( angle, vector );
drawMyStuff();
glPopMatrix();

In your case, you are not applying the rotation when you draw in your paint function.

The gl code in your wheel event handler doesn't look like it belongs there. There is no drawing happening there currently, (there shouldn't be, you should call updateGL() after changing the angle so that the paint() function subsequently handles it.) and you have push/pop reversed... You want to match you push and pop commands carefully. If you are just drawing one thing, you might not need them, as they are typically used for maintaining a tree of transforms where child objects rotate with their parents.

Also, you will want to make sure you are working with the right stack. You will need to use glMatrixMode( GL_PROJECTION ) and glMatrixMode( GL_MODELVIEW ) to make the transform functions work on the right stack at the right time.

CodePudding user response:

Thanks for reply.

In my case I just needed to rotate in paintGL function. I am not using push and pop matrix, at this time, because I just rotate one matrix at the top. The rotation should be implied over z-axe at the center of the screen, so i used translateGL("center.x","center.y",0.0); and after rotation used translateGL(-"center.x",-"center.y",0.0);

  • Related