Results 1 to 4 of 4

Thread: [OpenGL] Drawing simple polygon crashes application

  1. #1
    Join Date
    Jun 2008
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [OpenGL] Drawing simple polygon crashes application

    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
    Qt Code:
    1. #ifndef WINDOW_H
    2. #define WINDOW_H
    3.  
    4. #include <QtGui/QWidget>
    5. #include <QHBoxLayout>
    6. #include "ui_window.h"
    7. #include "glwidget.h"
    8.  
    9.  
    10. class Window : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. Window(QWidget *parent = 0, Qt::WFlags flags = 0);
    16. ~Window();
    17.  
    18. private:
    19. Ui::WindowClass ui;
    20. GLWidget* glWidget;
    21. };
    22.  
    23. #endif // WINDOW_H
    To copy to clipboard, switch view to plain text mode 
    File: window.cpp
    Qt Code:
    1. #include "window.h"
    2.  
    3. Window::Window(QWidget *parent, Qt::WFlags flags)
    4. : QWidget(parent, flags)
    5. {
    6. ui.setupUi(this);
    7. QHBoxLayout *mainLayout = new QHBoxLayout();
    8. mainLayout->addWidget(glWidget);
    9. setLayout(mainLayout);
    10.  
    11. setWindowTitle(tr("Hello OpenGL"));
    12. }
    13.  
    14. Window::~Window()
    15. {
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 
    and most important files, I guess...
    File: glwidget.h
    Qt Code:
    1. #ifndef GLWIDGET_H
    2. #define GLWIDGET_H
    3. #include <QGLWidget>
    4. #include <QtOpenGL>
    5.  
    6. class GLWidget: public QGLWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. GLWidget(QWidget *parent = 0);
    11. ~GLWidget();
    12.  
    13.  
    14. protected:
    15. void initializeGL();
    16. void paintGL();
    17. void resizeGL(int width, int height);
    18. };
    19. #endif // GLWIDGET_H
    To copy to clipboard, switch view to plain text mode 
    File: glwidget.cpp
    Qt Code:
    1. #include "glwidget.h"
    2.  
    3. GLWidget::GLWidget(QWidget* parent): QGLWidget(parent)
    4. {
    5. }
    6.  
    7. GLWidget::~GLWidget()
    8. {
    9. }
    10.  
    11. void GLWidget::initializeGL()
    12. {
    13. qglClearColor(QColor(255, 0, 0, 127));
    14. glEnable(GL_DEPTH_TEST);
    15. }
    16.  
    17. void GLWidget::resizeGL(int width, int height)
    18. {
    19. int side = qMin(width, height);
    20. glViewport((width - side) / 2, (height - side)/2, side, side);
    21. glMatrixMode(GL_PROJECTION);
    22.  
    23. glLoadIdentity();
    24. glMatrixMode(GL_MODELVIEW);
    25. }
    26.  
    27. void GLWidget::paintGL()
    28. {
    29. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    30. glLoadIdentity();
    31. glBegin(GL_POLYGON);
    32. //glColor3f(1.0f, 0.4f, 0.5f);
    33. glVertex3f(-1.0f , 4.0f, 0.0f);
    34. glVertex3f(2.0f, -1.0f, 0.0f);
    35. glVertex3f(4.0f, -4.0f, -1.0f);
    36. //glColor3f(0.4f, 0.4f, 0.4f);
    37. glVertex3f(1.3f, -2.3f, -2.0f);
    38. glVertex3f(0.0f, 4.0f, -2.0f);
    39. glEnd();
    40. }
    To copy to clipboard, switch view to plain text mode 
    Main.cpp is of course very simple:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "window.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. Window *window = new Window();
    8. window->show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    Thank you for attention, I`m waiting for any help

  2. #2

    Default Re: [OpenGL] Drawing simple polygon crashes application

    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

  3. #3
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: [OpenGL] Drawing simple polygon crashes application

    stephanepoirier please don't hijack threads
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  4. #4
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [OpenGL] Drawing simple polygon crashes application

    It seems glWidget is an uninitialized variable.
    You need the line
    Qt Code:
    1. glWidget = new GlWidget;
    To copy to clipboard, switch view to plain text mode 
    somewhere in your code.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.