Results 1 to 5 of 5

Thread: Drawing a cube

  1. #1
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Drawing a cube

    Hi all,

    I have this code to draw a cube:

    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2012-05-28T13:03:04
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core gui opengl
    8.  
    9. TARGET = opengl
    10. TEMPLATE = app
    11.  
    12.  
    13. SOURCES += main.cpp \
    14. cube.cpp
    15.  
    16. HEADERS += cube.h
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "cube.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. if (!QGLFormat::hasOpenGL())
    8. qFatal("This system has no OpenGL support");
    9. Cube cube;
    10. cube.resize(300, 300);
    11. cube.show();
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef GLWIDGET_H
    2. #define GLWIDGET_H
    3.  
    4. #include <QGLWidget>
    5. #include <QMouseEvent>
    6.  
    7. class Cube : public QGLWidget
    8. {
    9. public:
    10. Cube(QWidget *parent = 0);
    11. protected:
    12. void initializeGL();
    13. void resizeGL(int width, int height);
    14. void paintGL();
    15. void mousePressEvent(QMouseEvent *event);
    16. void mouseMoveEvent(QMouseEvent *event);
    17. void mouseDoubleClickEvent(QMouseEvent *event);
    18. private:
    19. void draw();
    20. int faceAtPosition(const QPoint &pos);
    21. GLfloat rotationX;
    22. GLfloat rotationY;
    23. GLfloat rotationZ;
    24. QColor faceColors[6];
    25. QPoint lastPos;
    26. };
    27.  
    28. #endif // GLWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "cube.h"
    2. #include <QColorDialog>
    3. #include <GL/glu.h>
    4.  
    5. Cube::Cube(QWidget *parent)
    6. : QGLWidget(parent)
    7. {
    8. setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer));
    9. rotationX = 0;
    10. rotationY = 0;
    11. rotationZ = 0;
    12. faceColors[0] = Qt::red;
    13. faceColors[1] = Qt::green;
    14. faceColors[2] = Qt::blue;
    15. faceColors[3] = Qt::cyan;
    16. faceColors[4] = Qt::yellow;
    17. faceColors[5] = Qt::magenta;
    18. }
    19.  
    20. void Cube::initializeGL()
    21. {
    22. qglClearColor(Qt::black);
    23. glShadeModel(GL_FLAT);
    24. glEnable(GL_DEPTH_TEST);
    25. glEnable(GL_CULL_FACE);
    26. }
    27.  
    28. void Cube::resizeGL(int width, int height)
    29. {
    30. glViewport(0, 0, width, height);
    31. glMatrixMode(GL_PROJECTION);
    32. glLoadIdentity();
    33. GLfloat x = (GLfloat)width / height;
    34. glFrustum(-x, x, -1.0, 1.0, 4.0, 15.0);
    35. glMatrixMode(GL_MODELVIEW);
    36. }
    37.  
    38. void Cube::paintGL()
    39. {
    40. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    41. draw();
    42. }
    43.  
    44. void Cube::draw()
    45. {
    46. static const GLfloat coords[6] [4] [3] = {
    47. { { +1.0, -1.0, +1.0 }, { +1.0, -1.0, -1.0 },
    48. { +1.0, +1.0, -1.0 }, { +1.0, +1.0, +1.0 } },
    49. { { -1.0, -1.0, -1.0 }, { -1.0, -1.0, +1.0 },
    50. { -1.0, +1.0, +1.0 }, { -1.0, +1.0, -1.0 } },
    51. { { +1.0, -1.0, -1.0 }, { -1.0, -1.0, -1.0 },
    52. { -1.0, +1.0, -1.0 }, { +1.0, +1.0, -1.0 } },
    53. { { -1.0, -1.0, +1.0 }, { +1.0, -1.0, +1.0 },
    54. { +1.0, +1.0, +1.0 }, { -1.0, +1.0, +1.0 } },
    55. { { -1.0, -1.0, -1.0 }, { +1.0, -1.0, -1.0 },
    56. { +1.0, -1.0, +1.0 }, { -1.0, -1.0, +1.0 } },
    57. { { -1.0, +1.0, +1.0 }, { +1.0, +1.0, +1.0 },
    58. { +1.0, +1.0, -1.0 }, { -1.0, +1.0, -1.0 } }
    59. };
    60. glMatrixMode(GL_MODELVIEW);
    61. glLoadIdentity();
    62. glTranslatef(0.0, 0.0, -10.0);
    63. glRotatef(rotationX, 1.0, 0.0, 0.0);
    64. glRotatef(rotationY, 0.0, 1.0, 0.0);
    65. glRotatef(rotationZ, 0.0, 0.0, 1.0);
    66. for (int i = 0; i < 6; ++i) {
    67. glLoadName(i);
    68. glBegin(GL_QUADS);
    69. qglColor(faceColors[i]);
    70. for (int j = 0; j < 4; ++j) {
    71. glVertex3f(coords[i] [j] [0], coords[i] [j] [1],
    72. coords[i] [j] [2]);
    73. }
    74. glEnd();
    75. }
    76. }
    77.  
    78. void Cube::mousePressEvent(QMouseEvent *event)
    79. {
    80. lastPos = event->pos();
    81. }
    82.  
    83. void Cube::mouseMoveEvent(QMouseEvent *event)
    84. {
    85. GLfloat dx = (GLfloat) (event->x() - lastPos.x()) / width();
    86. GLfloat dy = (GLfloat) (event->y() - lastPos.y()) / height();
    87. if (event->buttons() & Qt::LeftButton) {
    88. rotationX += 180 * dy;
    89. rotationY += 180 * dx;
    90. updateGL();
    91. } else if (event->buttons() & Qt::RightButton) {
    92. rotationX += 180 * dy;
    93. rotationZ += 180 * dx;
    94. updateGL();
    95. }
    96. lastPos = event->pos();
    97. }
    98.  
    99. void Cube::mouseDoubleClickEvent(QMouseEvent *event)
    100. {
    101. int face = faceAtPosition(event->pos());
    102. if (face != -1) {
    103. QColor color = QColorDialog::getColor(faceColors[face],
    104. this);
    105. if (color.isValid()) {
    106. faceColors[face] = color;
    107. updateGL();
    108. }
    109. }
    110. }
    111.  
    112. int Cube::faceAtPosition(const QPoint &pos)
    113. {
    114. const int MaxSize = 512;
    115. GLuint buffer[MaxSize];
    116. GLint viewport[4];
    117. glGetIntegerv(GL_VIEWPORT, viewport);
    118. glSelectBuffer(MaxSize, buffer);
    119. glRenderMode(GL_SELECT);
    120. glInitNames();
    121. glPushName(0);
    122. glMatrixMode(GL_PROJECTION);
    123. glPushMatrix();
    124. glLoadIdentity();
    125. gluPickMatrix((GLdouble)pos.x(),
    126. (GLdouble) (viewport[3] - pos.y()),
    127. 5.0, 5.0, viewport);
    128. GLfloat x = (GLfloat)width() / height();
    129. glFrustum(-x, x, -1.0, 1.0, 4.0, 15.0);
    130. draw();
    131. glMatrixMode(GL_PROJECTION);
    132. glPopMatrix();
    133. if (!glRenderMode(GL_RENDER))
    134. return -1;
    135. return buffer[3];
    136. }
    To copy to clipboard, switch view to plain text mode 

    There are any native implementation in Qt C++ that encapsulates all of this to simplify the 3D usage? I think that the above code is a very low level code to draw a cube. I have used Ogre in Qt and it's simplify a bit the 3D usage.

    Regards.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Drawing a cube

    Take a look at Qt3D.

  3. #3
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Drawing a cube

    I want use Qt C++, is it possible?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Drawing a cube

    Take a look at Qt3D. It even has examples.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing a cube

    Qt3D is Qt C++.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to design a 3D cube by using QPqinter?
    By anupam in forum General Programming
    Replies: 6
    Last Post: 12th April 2011, 13:17
  2. cube with widgets
    By mkind in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2011, 07:51
  3. 2D drawing
    By WXNSNW in forum Qt Programming
    Replies: 3
    Last Post: 6th October 2010, 00:01
  4. how to redraw cube to rectangle
    By wagmare in forum Qt Programming
    Replies: 6
    Last Post: 11th February 2009, 06:26
  5. Drawing
    By chethana in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2007, 07:29

Tags for this Thread

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.