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"
{
Q_OBJECT
public:
Window
(QWidget *parent
= 0, Qt
::WFlags flags
= 0);
~Window();
private:
Ui::WindowClass ui;
GLWidget* glWidget;
};
#endif // 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
To copy to clipboard, switch view to plain text mode
File: window.cpp
#include "window.h"
Window
::Window(QWidget *parent, Qt
::WFlags flags
){
ui.setupUi(this);
mainLayout->addWidget(glWidget);
setLayout(mainLayout);
setWindowTitle(tr("Hello OpenGL"));
}
Window::~Window()
{
}
#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()
{
}
To copy to clipboard, switch view to plain text mode
and most important files, I guess...
File: glwidget.h
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
#include <QtOpenGL>
{
Q_OBJECT
public:
~GLWidget();
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
};
#endif // 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
To copy to clipboard, switch view to plain text mode
File: glwidget.cpp
#include "glwidget.h"
{
}
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Main.cpp is of course very simple:
#include <QtGui/QApplication>
#include "window.h"
int main(int argc, char *argv[])
{
Window *window = new Window();
window->show();
return app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Thank you for attention, I`m waiting for any help
Bookmarks