21did21
5th July 2011, 11:54
Hello world,
i use openGL widget and i want to use keybord to rotate my object but it don't works...
i don't know why, because i think that my programm is good...
can you see this please? :
MAIN.CPP
#include <iostream>
#include <QApplication>
#include "myGLWidget.h"
#include <QGridLayout>
#include "FenPrincipale.h"
using namespace std;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FenPrincipale fenetre;
fenetre.showMaximized();
fenetre.setWindowTitle("blabla");
return app.exec();
}
FenPrincipale.f
#ifndef HEADER_FENPRINCIPALE
#define HEADER_FENPRINCIPALE
#include <QtGui>
class FenPrincipale : public QMainWindow
{
public:
FenPrincipale();
~FenPrincipale();
private:
};
#endif
FenPrincipale.CPP
#include "FenPrincipale.h"
#include "myGLWidget.h"
FenPrincipale::FenPrincipale()
{
QMdiArea *zoneCentrale = new QMdiArea;
myGLWidget *plotFenetre = new myGLWidget;
QMdiSubWindow *sousFenetre1 = zoneCentrale->addSubWindow(plotFenetre);
setCentralWidget(zoneCentrale);
}
FenPrincipale::~FenPrincipale()
{
}
MAIN.CPP
#include <iostream>
#include <QApplication>
#include "myGLWidget.h"
#include <QGridLayout>
#include "FenPrincipale.h"
using namespace std;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FenPrincipale fenetre;
fenetre.showMaximized();
fenetre.setWindowTitle("blabla");
return app.exec();
}
myGLWidget.H
#ifndef MYGLWIDGET_H
#define MYGLWIDGET_H
#include <QtOpenGL>
#include <QGLWidget>
#include <QKeyEvent>
class myGLWidget : public QGLWidget //this class herite from QGLWidget
{
Q_OBJECT
public:
myGLWidget();
~myGLWidget();
void initializeGL();
void resizeGL(int,int);
void paintGL();
void keyPressEvent( QKeyEvent *keyEvent );
public slots:
private:
float rotateX;
float rotateY;
float rotateZ;
};
#endif // MYGLWIDGET_H
myGLWidget.CPP
#include <iostream>
#include "myGLWidget.h"
#include <GL/gl.h>
#include <GL/glu.h>
using namespace std;
myGLWidget::myGLWidget() : QGLWidget()
{
}
myGLWidget::~myGLWidget()
{
}
void myGLWidget::keyPressEvent(QKeyEvent *keyEvent)
{
cout << "clavier" << endl;
if (keyEvent->key())
{
if (Qt::Key_Up)
{
rotateX=rotateX+2;
repaint();
updateGL();
cout << "UP" << endl;
}
}
else
{
}
}
void myGLWidget::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 myGLWidget::resizeGL(int width, int height)
{
if(height == 0)
height = 1;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void myGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0f,1.0f,1.0f,1.0f);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
gluLookAt( 1.2, 1.7, 1.0, 0, 0, 0, 0, 0, 1);
glPushMatrix( );
glRotatef(rotateX,1.0f,0.0f,0.0f);
glRotatef(rotateY,0.0f,1.0f,0.0f);
glRotatef(rotateZ,0.0f,0.0f,1.0f);
glBegin(GL_LINES);
//i draw
glEnd();
}
EDIT:
2°) i have a second question:
you can see in this programm that i never tell the methode : "paintGL()"
but openGL nevertheless draw when i compile my project, why?
i use openGL widget and i want to use keybord to rotate my object but it don't works...
i don't know why, because i think that my programm is good...
can you see this please? :
MAIN.CPP
#include <iostream>
#include <QApplication>
#include "myGLWidget.h"
#include <QGridLayout>
#include "FenPrincipale.h"
using namespace std;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FenPrincipale fenetre;
fenetre.showMaximized();
fenetre.setWindowTitle("blabla");
return app.exec();
}
FenPrincipale.f
#ifndef HEADER_FENPRINCIPALE
#define HEADER_FENPRINCIPALE
#include <QtGui>
class FenPrincipale : public QMainWindow
{
public:
FenPrincipale();
~FenPrincipale();
private:
};
#endif
FenPrincipale.CPP
#include "FenPrincipale.h"
#include "myGLWidget.h"
FenPrincipale::FenPrincipale()
{
QMdiArea *zoneCentrale = new QMdiArea;
myGLWidget *plotFenetre = new myGLWidget;
QMdiSubWindow *sousFenetre1 = zoneCentrale->addSubWindow(plotFenetre);
setCentralWidget(zoneCentrale);
}
FenPrincipale::~FenPrincipale()
{
}
MAIN.CPP
#include <iostream>
#include <QApplication>
#include "myGLWidget.h"
#include <QGridLayout>
#include "FenPrincipale.h"
using namespace std;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FenPrincipale fenetre;
fenetre.showMaximized();
fenetre.setWindowTitle("blabla");
return app.exec();
}
myGLWidget.H
#ifndef MYGLWIDGET_H
#define MYGLWIDGET_H
#include <QtOpenGL>
#include <QGLWidget>
#include <QKeyEvent>
class myGLWidget : public QGLWidget //this class herite from QGLWidget
{
Q_OBJECT
public:
myGLWidget();
~myGLWidget();
void initializeGL();
void resizeGL(int,int);
void paintGL();
void keyPressEvent( QKeyEvent *keyEvent );
public slots:
private:
float rotateX;
float rotateY;
float rotateZ;
};
#endif // MYGLWIDGET_H
myGLWidget.CPP
#include <iostream>
#include "myGLWidget.h"
#include <GL/gl.h>
#include <GL/glu.h>
using namespace std;
myGLWidget::myGLWidget() : QGLWidget()
{
}
myGLWidget::~myGLWidget()
{
}
void myGLWidget::keyPressEvent(QKeyEvent *keyEvent)
{
cout << "clavier" << endl;
if (keyEvent->key())
{
if (Qt::Key_Up)
{
rotateX=rotateX+2;
repaint();
updateGL();
cout << "UP" << endl;
}
}
else
{
}
}
void myGLWidget::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 myGLWidget::resizeGL(int width, int height)
{
if(height == 0)
height = 1;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void myGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0f,1.0f,1.0f,1.0f);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
gluLookAt( 1.2, 1.7, 1.0, 0, 0, 0, 0, 0, 1);
glPushMatrix( );
glRotatef(rotateX,1.0f,0.0f,0.0f);
glRotatef(rotateY,0.0f,1.0f,0.0f);
glRotatef(rotateZ,0.0f,0.0f,1.0f);
glBegin(GL_LINES);
//i draw
glEnd();
}
EDIT:
2°) i have a second question:
you can see in this programm that i never tell the methode : "paintGL()"
but openGL nevertheless draw when i compile my project, why?