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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With