Qt + OpenGL Code Example
Aug/090
I wanted to develop a game using OpenGL but I was having trouble deciding on a windowing library. Somebody suggested I try Qt, so I decided to give it another shot. I heard it was good, but the editor it came with was so foreign and unintuitive. Unfortunately, this overshadowed what a beautiful library Qt really is! It takes a little getting used to, but it really is well designed.
Turns out Netbeans 6.8 (and perhaps earlier versions) have support for Qt, so it’s pretty easy to get set up. It’s probably way too early to be posting any code, but I’m going to anyways because I’m way too excited about this (yeah, I live a sad life).
main.cpp
#include <QtOpenGL/QGLWidget>
#include "GLWidget.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
GLWidget window;
window.resize(800,600);
window.show();
return app.exec();
}
GLWidget.h
#define _GLWIDGET_H
#include <QtOpenGL/QGLWidget>
class GLWidget : public QGLWidget {
Q_OBJECT // must include this if you use Qt signals/slots
public:
GLWidget(QWidget *parent = NULL);
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
};
#endif /* _GLWIDGET_H */
All those functions you see above will get called for you automatically. No need to register any callbacks!
GLWidget.cpp
#include "GLWidget.h"
GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) {
setMouseTracking(true);
}
void GLWidget::initializeGL() {
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glEnable(GL_POLYGON_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0, 0, 0, 0);
}
void GLWidget::resizeGL(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h); // set origin to bottom left corner
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glBegin(GL_POLYGON);
glVertex2f(0,0);
glVertex2f(100,500);
glVertex2f(500,100);
glEnd();
}
void GLWidget::mousePressEvent(QMouseEvent *event) {
}
void GLWidget::mouseMoveEvent(QMouseEvent *event) {
printf("%d, %d\n", event->x(), event->y());
}
void GLWidget::keyPressEvent(QKeyEvent* event) {
switch(event->key()) {
case Qt::Key_Escape:
close();
break;
default:
event->ignore();
break;
}
}
It just draws a red triangle and tracks your mouse movement. You need setMouseTracking(true); in order for the mouse move callback to fire when you aren’t holding a button down. I need this for my game.
Anyway, that’s all I’ve got so far, but that should get you up and running with Qt + OpenGL anyway, the rest is native OpenGL and your typical game layout; you shouldn’t need too much more Qt-specific stuff. Although, Qt does seem to have some nice OpenGL helpers and other neat stuff I have to dig into yet
Leave a comment if you need some help getting set up on Netbeans/Ubuntu: there are a couple quirks but nothing too terrible.
No comments yet.
Leave a comment
No trackbacks yet.