PDA

View Full Version : [OpenGL] Drawing simple polygon crashes application



Doles
27th February 2009, 23:27
Hi there,
I`m new OpenGL programmer but I have some experience from programming with WinAPI+OpenGL or plain GLUT. Now I decided to move into Qt4 + OpenGL but I have plenty of troubles during getting first steps in this topic :) I was looking at example source code for OpenGL programs but there are too sophisicated (I don`t want to use any sliders or rotate something yet). I tried to write as simple app as it can be using source code from here:
http://doc.trolltech.com/4.4/opengl-hellogl.html
My programm compile (wrote in Qt Creator under Windows XP) without any errors but it crash and I just see Window`s error dialog with sending report about bug (i hope you know what i mean). I don`t know how to debuge OpenGL apps, so there is my whole source code:
File: window.h

#ifndef WINDOW_H
#define WINDOW_H

#include <QtGui/QWidget>
#include <QHBoxLayout>
#include "ui_window.h"
#include "glwidget.h"


class Window : public QWidget
{
Q_OBJECT

public:
Window(QWidget *parent = 0, Qt::WFlags flags = 0);
~Window();

private:
Ui::WindowClass ui;
GLWidget* glWidget;
};

#endif // WINDOW_H

File: window.cpp

#include "window.h"

Window::Window(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
{
ui.setupUi(this);
QHBoxLayout *mainLayout = new QHBoxLayout();
mainLayout->addWidget(glWidget);
setLayout(mainLayout);

setWindowTitle(tr("Hello OpenGL"));
}

Window::~Window()
{

}
and most important files, I guess...
File: glwidget.h

#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
#include <QtOpenGL>

class GLWidget: public QGLWidget
{
Q_OBJECT
public:
GLWidget(QWidget *parent = 0);
~GLWidget();


protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
};
#endif // GLWIDGET_H
File: glwidget.cpp

#include "glwidget.h"

GLWidget::GLWidget(QWidget* parent): QGLWidget(parent)
{
}

GLWidget::~GLWidget()
{
}

void GLWidget::initializeGL()
{
qglClearColor(QColor(255, 0, 0, 127));
glEnable(GL_DEPTH_TEST);
}

void GLWidget::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side)/2, side, side);
glMatrixMode(GL_PROJECTION);

glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}

void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_POLYGON);
//glColor3f(1.0f, 0.4f, 0.5f);
glVertex3f(-1.0f , 4.0f, 0.0f);
glVertex3f(2.0f, -1.0f, 0.0f);
glVertex3f(4.0f, -4.0f, -1.0f);
//glColor3f(0.4f, 0.4f, 0.4f);
glVertex3f(1.3f, -2.3f, -2.0f);
glVertex3f(0.0f, 4.0f, -2.0f);
glEnd();
}



Main.cpp is of course very simple:

#include <QtGui/QApplication>
#include "window.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Window *window = new Window();
window->show();
return app.exec();
}

Thank you for attention, I`m waiting for any help :)

stephanepoirier
2nd March 2009, 17:48
Hi,

I am trying to use glut.h along in a qt application under windows. My application won't build, I get 4 unresolved references: __glutInitWithExit, __glutCreateWindowWithExit, __glutCreateMenuWithExit and the only glut function I am explicitely calling in my code glutSolidCube.

Anyone know the trick to use glut.h functionality along with Qtcreator in Windows?

Stephane

ComaWhite
3rd March 2009, 05:23
stephanepoirier please don't hijack threads

spud
3rd March 2009, 12:24
It seems glWidget is an uninitialized variable.
You need the line

glWidget = new GlWidget;
somewhere in your code.