Results 1 to 7 of 7

Thread: Opengl

  1. #1
    Join Date
    Nov 2008
    Posts
    39
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Opengl

    Hi to every one I am't a good programmer with the qt but I am trying to design a widget which has 3 Opengl widgets....

    can any one help me with any kind of information that will help to create this... widget...

    This widget has a main widget which will be expanding on the hoizontal way only... and then I would like to display 3 cylinders in all the 3 widgets....

    I was successful in drawing a single cylinder in the main widget... but after that I have used the same code in the other 2 widgets but there's no display on the screen....

    please help me out... I will post my code.... also if necessary....

  2. #2
    Join Date
    Nov 2008
    Posts
    39
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Opengl

    Here is the code in the zipped format....
    This program was compiled in Linux...
    I have created a cpp file and a header file for each of the three widgets so this might be big....
    Qt Code:
    1. //GLWidget1 this is the main window header file.....
    2. #ifndef GLWIDGET1_H
    3. #define GLWIDGET1_H
    4. #include <QGLWidget>
    5. class GLWidget : public QGLWidget
    6. {
    7. Q_OBJECT
    8. public:
    9. GLWidget(QWidget *parent = 0);
    10. protected:
    11. void initializeGL();
    12. void paintGL();
    13. void resizeGL(int width, int height);
    14. void mousePressEvent(QMouseEvent *event);
    15. void mouseMoveEvent(QMouseEvent *event);
    16. private:
    17. GLUquadricObj *pobj;
    18. QPoint lastPos;
    19. GLfloat rotationX, rotationY, rotationZ;
    20. };
    21. #endif
    To copy to clipboard, switch view to plain text mode 

    Main windows cpp file...
    Qt Code:
    1. #include <QtGui>
    2. #include <QtOpenGL>
    3. #include <math.h>
    4. #include "glwidget1.h"
    5. GLWidget::GLWidget(QWidget *parent)
    6. : QGLWidget(parent)
    7. { }
    8. void GLWidget::initializeGL()
    9. { qglClearColor(Qt::gray);
    10. glShadeModel(GL_SMOOTH);
    11. glEnable(GL_DEPTH_TEST);
    12. glEnable(GL_CULL_FACE); }
    13. void GLWidget::paintGL()
    14. { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    15. glMatrixMode(GL_MODELVIEW);
    16. glLoadIdentity();
    17. glTranslatef(0.0, 0.0, -15.0);
    18. glRotatef(rotationX, 1.0, 0.0, 0.0);
    19. glRotatef(rotationY, 0.0, 1.0, 0.0);
    20. glRotatef(rotationZ, 0.0, 0.0, 1.0);
    21. pobj = gluNewQuadric();
    22. gluQuadricDrawStyle(pobj, GLU_LINE);
    23. gluQuadricNormals(pobj, GLU_SMOOTH);
    24. qglColor(Qt::red);
    25. gluCylinder(pobj, 1, 1, 3, 30, 30); }
    26. void GLWidget::resizeGL(int width, int height)
    27. { glViewport(0, 0, width, height);
    28. glMatrixMode(GL_PROJECTION);
    29. glLoadIdentity();
    30. GLfloat x = GLfloat(width) / height;
    31. glFrustum(-x, +x, -1.0, +1.0, 4.0, 20.0);
    32. glMatrixMode(GL_MODELVIEW); }
    33. void GLWidget::mousePressEvent(QMouseEvent *event)
    34. { lastPos = event->pos(); }
    35. void GLWidget::mouseMoveEvent(QMouseEvent *event)
    36. { GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
    37. GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();
    38. if (event->buttons() & Qt::LeftButton) {
    39. rotationX += 180 * dy;
    40. rotationY += 180 * dx;
    41. updateGL();
    42. } else if (event->buttons() & Qt::RightButton) {
    43. rotationX += 180 * dy;
    44. rotationZ += 180 * dx;
    45. updateGL(); }
    46. lastPos = event->pos(); }
    To copy to clipboard, switch view to plain text mode 
    this code is for the other window............. //glwidget2.cpp file...
    Qt Code:
    1. #include "glwidget2.h"
    2. #include <QtGui>
    3. #include <QtOpenGL>
    4. #include <math.h>
    5. GLWidget_fin::GLWidget_fin(QWidget *parent)
    6. : QGLWidget(parent)
    7. { }
    8. void GLWidget_fin::initializeGL()
    9. { qglClearColor(Qt::gray);
    10. glShadeModel(GL_SMOOTH);
    11. glEnable(GL_DEPTH_TEST);
    12. glEnable(GL_CULL_FACE); }
    13. void GLWidget_fin::paintGL()
    14. { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    15. glMatrixMode(GL_MODELVIEW);
    16. glLoadIdentity();
    17. glTranslatef(0.0, 0.0, -15.0);
    18. glRotatef(rotationX, 1.0, 0.0, 0.0);
    19. glRotatef(rotationY, 0.0, 1.0, 0.0);
    20. glRotatef(rotationZ, 0.0, 0.0, 1.0);
    21. pobj = gluNewQuadric();
    22. gluQuadricDrawStyle(pobj, GLU_LINE);
    23. gluQuadricNormals(pobj, GLU_SMOOTH);
    24. qglColor(Qt::red);
    25. gluCylinder(pobj, 1, 1, 1, 30, 30); }
    26. void GLWidget_fin::resizeGL(int width, int height)
    27. { glViewport(0, 0, width, height);
    28. glMatrixMode(GL_PROJECTION);
    29. glLoadIdentity();
    30. GLfloat x = GLfloat(width) / height;
    31. glFrustum(-x, +x, -1.0, +1.0, 10.0, 40.0);
    32. glMatrixMode(GL_MODELVIEW); }
    33. /*void GLWidget_fin::mousePressEvent(QMouseEvent *event)
    34.  { lastPos = event->pos(); }
    35.  void GLWidget_fin::mouseMoveEvent(QMouseEvent *event)
    36.  { GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
    37.   GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();
    38.   if (event->buttons() & Qt::LeftButton) {
    39.   rotationX += 180 * dy;
    40.   rotationY += 180 * dx;
    41.   updateGL();
    42.   } else if (event->buttons() & Qt::RightButton) {
    43.   rotationX += 180 * dy;
    44.   rotationZ += 180 * dx;
    45.   updateGL(); }
    46.   lastPos = event->pos(); }*/
    To copy to clipboard, switch view to plain text mode 
    glwidget2.h header file...
    Qt Code:
    1. #ifndef GLWIDGET2_H
    2. #define GLWIDGET2_H
    3. #include <QGLWidget>
    4. class GLWidget_fin : public QGLWidget
    5. { Q_OBJECT
    6. public:
    7. GLWidget_fin(QWidget *parent = 0);
    8. protected:
    9. void initializeGL();
    10. void paintGL();
    11. void resizeGL(int width, int height);
    12. /* void mousePressEvent(QMouseEvent *event);
    13.   void mouseMoveEvent(QMouseEvent *event);*/
    14. private:
    15. GLUquadricObj *pobj;
    16. QPoint lastPos;
    17. GLfloat rotationX, rotationY, rotationZ; };
    18. #endif // GLWIDGET2_H
    To copy to clipboard, switch view to plain text mode 
    glwidget3.h header file for the third GLWidget
    Qt Code:
    1. #ifndef GLWIDGET3_H
    2. #define GLWIDGET3_H
    3. #include <QGLWidget>
    4. class GLWidget_rear : public QGLWidget
    5. { Q_OBJECT
    6. public:
    7. GLWidget_rear(QWidget *parent = 0);
    8. protected:
    9. void initializeGL();
    10. void paintGL();
    11. void resizeGL(int width, int height);
    12. /*void mousePressEvent(QMouseEvent *event);
    13.   void mouseMoveEvent(QMouseEvent *event);*/
    14. private:
    15. GLUquadricObj *pobj;
    16. QPoint lastPos;
    17. GLfloat rotationX, rotationY, rotationZ; };
    18. #endif // GLWIDGET3_H
    To copy to clipboard, switch view to plain text mode 
    glwidget3.cpp file ..... third widgets cpp file...
    Qt Code:
    1. #include "glwidget3.h"
    2. #include <QtGui>
    3. #include <QtOpenGL>
    4. #include <math.h>
    5. GLWidget_rear::GLWidget_rear(QWidget *parent)
    6. : QGLWidget(parent)
    7. { }
    8. void GLWidget_rear::initializeGL()
    9. { qglClearColor(Qt::gray);
    10. glShadeModel(GL_SMOOTH);
    11. glEnable(GL_DEPTH_TEST);
    12. glEnable(GL_CULL_FACE); }
    13. void GLWidget_rear::paintGL()
    14. { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    15. glMatrixMode(GL_MODELVIEW);
    16. glLoadIdentity();
    17. glTranslatef(0.0, 0.0, -15.0);
    18. glRotatef(rotationX, 1.0, 0.0, 0.0);
    19. glRotatef(rotationY, 0.0, 1.0, 0.0);
    20. glRotatef(rotationZ, 0.0, 0.0, 1.0);
    21. pobj = gluNewQuadric();
    22. gluQuadricDrawStyle(pobj, GLU_LINE);
    23. gluQuadricNormals(pobj, GLU_SMOOTH);
    24. qglColor(Qt::red);
    25. gluCylinder(pobj, 1, 1, 3, 30, 30); }
    26. void GLWidget_rear::resizeGL(int width, int height)
    27. { glViewport(0, 0, width, height);
    28. glMatrixMode(GL_PROJECTION);
    29. glLoadIdentity();
    30. GLfloat x = GLfloat(width) / height;
    31. glFrustum(-x, +x, -1.0, +1.0, 4.0, 10.0);
    32. glMatrixMode(GL_MODELVIEW); }
    33. /*void GLWidget_rear::mousePressEvent(QMouseEvent *event)
    34.  { lastPos = event->pos(); }
    35.  void GLWidget_rear::mouseMoveEvent(QMouseEvent *event)
    36.  { GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
    37.   GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();
    38.   if (event->buttons() & Qt::LeftButton) {
    39.   rotationX += 180 * dy;
    40.   rotationY += 180 * dx;
    41.   updateGL();
    42.   } else if (event->buttons() & Qt::RightButton) {
    43.   rotationX += 180 * dy;
    44.   rotationZ += 180 * dx;
    45.   updateGL(); }
    46.   lastPos = event->pos();
    47.  }*/
    To copy to clipboard, switch view to plain text mode 
    main.cpp file for the program...
    Qt Code:
    1. #include <QApplication>
    2. #include <QDesktopWidget>
    3. #include "window.h"
    4. int main(int argc, char *argv[])
    5. { QApplication app(argc, argv);
    6. Window window;
    7. window.show();
    8. return app.exec(); }
    To copy to clipboard, switch view to plain text mode 
    this 3 widgets are put in together into a window widgets whos code is
    window.h
    Qt Code:
    1. #ifndef WINDOW_H
    2. #define WINDOW_H
    3. #include <QWidget>
    4. #include <QDialog>
    5. class GLWidget;
    6. class Window : public QWidget
    7. { Q_OBJECT
    8. public:
    9. Window();
    10. private:
    11. GLWidget *glWidget1, //glWidget1 -> main Design Widget
    12. *glWidget2, //glWidget2 -> Fin Design Widget
    13. *glWidget3 ; //glWidget3 -> rear View Design Widget */
    14. };
    15. #endif
    To copy to clipboard, switch view to plain text mode 
    and the .cpp file of the main window is
    Qt Code:
    1. #include <QtGui>
    2. #include "glwidget1.h"
    3. #include "glwidget2.h"
    4. #include "glwidget3.h"
    5. #include "window.h"
    6. Window::Window()
    7. { glWidget1 = new GLWidget;
    8. glWidget2 = new GLWidget;
    9. glWidget3 = new GLWidget;
    10. QVBoxLayout *vLayout = new QVBoxLayout;
    11. vLayout->addWidget(glWidget2);
    12. vLayout->addWidget(glWidget3);
    13. QHBoxLayout *hLayout = new QHBoxLayout;
    14. hLayout->addWidget(glWidget1);
    15. hLayout->addLayout(vLayout);
    16. setLayout(hLayout);
    17. QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Expanding);
    18. glWidget1->setSizePolicy(sizePolicy1);
    19. QSizePolicy sizePolicy2(QSizePolicy::Fixed, QSizePolicy::Fixed);
    20. glWidget2->setSizePolicy(sizePolicy2);
    21. QSizePolicy sizePolicy3(QSizePolicy::Fixed, QSizePolicy::Fixed);
    22. glWidget2->setSizePolicy(sizePolicy3);
    23. glWidget1->setMaximumSize(QSize(1000, 1000));
    24. glWidget1->setMinimumSize(QSize(800, 800));
    25. glWidget2->setMaximumSize(QSize(400,400));
    26. glWidget2->setMinimumSize(QSize(400,400));
    27. glWidget2->resize(400,400);
    28. glWidget3->setMaximumSize(QSize(400,400));
    29. glWidget3->setMinimumSize(QSize(400,400));
    30. setWindowTitle(tr("Hello GL")); }
    To copy to clipboard, switch view to plain text mode 
    this the pro file that was generated to which I have added QT += opengl for running the opengl program...
    Qt Code:
    1. ######################################################################
    2. # Automatically generated by qmake (2.01a) Wed Feb 11 14:50:13 2009
    3. ######################################################################
    4. TEMPLATE = app
    5. TARGET =
    6. DEPENDPATH += .
    7. INCLUDEPATH += .
    8. QT += opengl
    9. # Input
    10. HEADERS += glwidget1.h glwidget2.h glwidget3.h window.h
    11. SOURCES += glwidget1.cpp glwidget2.cpp glwidget3.cpp main.cpp window.cpp
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 13th February 2009 at 17:32. Reason: missing [code] tags

  3. #3
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Opengl

    I've notice some issues in your code, you define 3 classes, GLWidget, GLWidget_fin, GLWidget_rear, but the 3 variables glWidget1, glWidget2, and glWidget3 are all from
    GLWidget type.
    Also don't see the point of declaring 3 classes with the same code.
    I compile the code of your program and the widget doesnt fit in my screen, is your intention to do the widget with fixed dimensions ?
    As far only one cilinder being show, sorry I also dond't know
    Gurus where are you ?

  4. #4
    Join Date
    Nov 2008
    Posts
    39
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Opengl

    Thanks dude for checking, but I have declared the same class because once I get the display then I will be changing the display of the widgets in a different way.....


    Any body please help...............

  5. #5
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Opengl

    Also you may checkout in the Qt Assistant the "GLWidget" and read it. I think there's a issue with the widget's OpenGL rendering context, witch has to be made current with the makeCurrent() function. This would explain why only the first cilinder is being draw.

  6. #6
    Join Date
    Nov 2008
    Posts
    39
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Opengl

    Thanks dude I have tried that too but there was no improvement.....

    Please help me yar....

    this will help me in developing a g8t app....

    Thanks for the help.....

  7. #7
    Join Date
    Nov 2008
    Posts
    39
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Opengl

    Hey dude is there any forum from where I could get help regarding this program please let me know......

    Thank's
    Mohan

Similar Threads

  1. OpenGL and Qt Question
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2009, 18:04
  2. Qt Embedded + OpenGL problem
    By EeroS in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 7th October 2008, 14:32
  3. QT + OpenGL + Thread => aaaahhhhh !
    By anthibug in forum Qt Programming
    Replies: 7
    Last Post: 26th July 2008, 13:36
  4. openGL in Qtopia core
    By sar_van81 in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 18th July 2008, 11:26
  5. Qtopia Core & OpenGL ES?
    By zelko in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 28th May 2007, 07:21

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.