Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL - problem in rotating - Qt

I am trying to learn||Code openGL in Qt. I made one application which shows two figures. One is a triangle "A", and the other triangle "B" is just the same as "A" except it got rotated -90 degree about the z-Axis(z axis is perpendicular to the computer screen). Now, the problem is rotation makes the change in the dimension. I am posting the "main.cpp" below,

#include <QApplication>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QtOpenGL/QGLWidget>
#include <QWidget>

class MyOpenGL:public QGLWidget
{
public:
    MyOpenGL(QWidget * parent);
    ~MyOpenGL();
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();
};

MyOpenGL::MyOpenGL(QWidget *parent):QGLWidget(QGLFormat(QGL::SampleBuffers),parent)
{
    setAutoFillBackground(false);
}

MyOpenGL::~MyOpenGL()
{

}

void MyOpenGL::initializeGL()
{
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f,0.0f,0.0f,0.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
}

void MyOpenGL::resizeGL(int w, int h)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f,width()/height(),10.0f,100.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void MyOpenGL::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    //glTranslatef(0.0,0.0,-10.0);

    glBegin(GL_TRIANGLES);
    glColor3f(1.0f,0.0f,0.0f);
    glVertex3f(-1.0f,0.0f,-10.0f);
    glColor3f(0.0f,1.0f,0.0f);
    glVertex3f(1.0f,0.0f,-10.0f);
    glColor3f(0.0f,0.0f,1.0f);
    glVertex3f(0.0f,4.0f,-10.0f);
    glEnd();

    glRotatef(90.0,0.0f,0.0f,-1.0f);

    glBegin(GL_TRIANGLES);
    glColor3f(1.0f,0.0f,0.0f);
    glVertex3f(-1.0f,0.0f,-10.0f);
    glColor3f(0.0f,1.0f,0.0f);
    glVertex3f(1.0f,0.0f,-10.0f);
    glColor3f(0.0f,0.0f,1.0f);
    glVertex3f(0.0f,4.0f,-10.0f);
    glEnd();

    glLoadIdentity();
}

int main(int argc,char * argv[])
{
    QApplication app(argc,argv);

    MyOpenGL * f = new MyOpenGL(NULL);
    f->show();

    return app.exec();
}

This is the pro file

SOURCES += \
    main.cpp

QT += opengl

This is the resulting app's screen shot

enter image description here

As for as I know rotation won't do any change in dimension. But here the dimension is changing .If anybody clear my doubt in this issue, I will be very thankful to him/her.

like image 394
prabhakaran Avatar asked Oct 25 '22 08:10

prabhakaran


1 Answers

I can't say for certain what is going on, but I do see that there may be some problems.

gluPerspective(45.0f,width()/height(),10.0f,100.0f);

If width and height both return integers (and they probably do), then dividing one by the other will result in an integer. That's not going to give you a proper aspect ratio. Cast one of them to a float to get a real aspect ratio.

Next, try putting a glLoadIdentity() call at the top of the paintGL function.

like image 134
Nicol Bolas Avatar answered Oct 27 '22 10:10

Nicol Bolas