PDA

View Full Version : Drawing a cube



qt_developer
28th May 2012, 14:54
Hi all,

I have this code to draw a cube:



#-------------------------------------------------
#
# Project created by QtCreator 2012-05-28T13:03:04
#
#-------------------------------------------------

QT += core gui opengl

TARGET = opengl
TEMPLATE = app


SOURCES += main.cpp \
cube.cpp

HEADERS += cube.h




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

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
if (!QGLFormat::hasOpenGL())
qFatal("This system has no OpenGL support");
Cube cube;
cube.resize(300, 300);
cube.show();
return app.exec();
}




#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QGLWidget>
#include <QMouseEvent>

class Cube : public QGLWidget
{
public:
Cube(QWidget *parent = 0);
protected:
void initializeGL();
void resizeGL(int width, int height);
void paintGL();
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseDoubleClickEvent(QMouseEvent *event);
private:
void draw();
int faceAtPosition(const QPoint &pos);
GLfloat rotationX;
GLfloat rotationY;
GLfloat rotationZ;
QColor faceColors[6];
QPoint lastPos;
};

#endif // GLWIDGET_H




#include "cube.h"
#include <QColorDialog>
#include <GL/glu.h>

Cube::Cube(QWidget *parent)
: QGLWidget(parent)
{
setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer));
rotationX = 0;
rotationY = 0;
rotationZ = 0;
faceColors[0] = Qt::red;
faceColors[1] = Qt::green;
faceColors[2] = Qt::blue;
faceColors[3] = Qt::cyan;
faceColors[4] = Qt::yellow;
faceColors[5] = Qt::magenta;
}

void Cube::initializeGL()
{
qglClearColor(Qt::black);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
}

void Cube::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat x = (GLfloat)width / height;
glFrustum(-x, x, -1.0, 1.0, 4.0, 15.0);
glMatrixMode(GL_MODELVIEW);
}

void Cube::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw();
}

void Cube::draw()
{
static const GLfloat coords[6] [4] [3] = {
{ { +1.0, -1.0, +1.0 }, { +1.0, -1.0, -1.0 },
{ +1.0, +1.0, -1.0 }, { +1.0, +1.0, +1.0 } },
{ { -1.0, -1.0, -1.0 }, { -1.0, -1.0, +1.0 },
{ -1.0, +1.0, +1.0 }, { -1.0, +1.0, -1.0 } },
{ { +1.0, -1.0, -1.0 }, { -1.0, -1.0, -1.0 },
{ -1.0, +1.0, -1.0 }, { +1.0, +1.0, -1.0 } },
{ { -1.0, -1.0, +1.0 }, { +1.0, -1.0, +1.0 },
{ +1.0, +1.0, +1.0 }, { -1.0, +1.0, +1.0 } },
{ { -1.0, -1.0, -1.0 }, { +1.0, -1.0, -1.0 },
{ +1.0, -1.0, +1.0 }, { -1.0, -1.0, +1.0 } },
{ { -1.0, +1.0, +1.0 }, { +1.0, +1.0, +1.0 },
{ +1.0, +1.0, -1.0 }, { -1.0, +1.0, -1.0 } }
};
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -10.0);
glRotatef(rotationX, 1.0, 0.0, 0.0);
glRotatef(rotationY, 0.0, 1.0, 0.0);
glRotatef(rotationZ, 0.0, 0.0, 1.0);
for (int i = 0; i < 6; ++i) {
glLoadName(i);
glBegin(GL_QUADS);
qglColor(faceColors[i]);
for (int j = 0; j < 4; ++j) {
glVertex3f(coords[i] [j] [0], coords[i] [j] [1],
coords[i] [j] [2]);
}
glEnd();
}
}

void Cube::mousePressEvent(QMouseEvent *event)
{
lastPos = event->pos();
}

void Cube::mouseMoveEvent(QMouseEvent *event)
{
GLfloat dx = (GLfloat) (event->x() - lastPos.x()) / width();
GLfloat dy = (GLfloat) (event->y() - lastPos.y()) / height();
if (event->buttons() & Qt::LeftButton) {
rotationX += 180 * dy;
rotationY += 180 * dx;
updateGL();
} else if (event->buttons() & Qt::RightButton) {
rotationX += 180 * dy;
rotationZ += 180 * dx;
updateGL();
}
lastPos = event->pos();
}

void Cube::mouseDoubleClickEvent(QMouseEvent *event)
{
int face = faceAtPosition(event->pos());
if (face != -1) {
QColor color = QColorDialog::getColor(faceColors[face],
this);
if (color.isValid()) {
faceColors[face] = color;
updateGL();
}
}
}

int Cube::faceAtPosition(const QPoint &pos)
{
const int MaxSize = 512;
GLuint buffer[MaxSize];
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glSelectBuffer(MaxSize, buffer);
glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix((GLdouble)pos.x(),
(GLdouble) (viewport[3] - pos.y()),
5.0, 5.0, viewport);
GLfloat x = (GLfloat)width() / height();
glFrustum(-x, x, -1.0, 1.0, 4.0, 15.0);
draw();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
if (!glRenderMode(GL_RENDER))
return -1;
return buffer[3];
}


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.

ChrisW67
29th May 2012, 01:00
Take a look at Qt3D (http://labs.qt.nokia.com/2012/04/11/qt-3d-and-qt5-qt4-news-and-releases/).

qt_developer
30th May 2012, 10:28
I want use Qt C++, is it possible?

ChrisW67
30th May 2012, 10:52
:confused: Take a look at Qt3D. It even has examples (http://doc-snapshot.qt-project.org/qt3d-1.0/qt3d-examples.html).

wysota
30th May 2012, 11:59
Qt3D is Qt C++.