diff options
author | Samuel Rødal <sroedal@trolltech.com> | 2008-06-23 10:01:08 +0200 |
---|---|---|
committer | Samuel Rødal <sroedal@trolltech.com> | 2008-06-23 10:01:08 +0200 |
commit | 3984206b94c0cf6ddf332caf2a0679fe28e7f865 (patch) | |
tree | c32939358e2221986cb3d7b2ef628a54d2046de6 /main.cpp | |
parent | 8b44c875279603745a02ac3b80f83806a4bfbfc6 (diff) |
Added some more controls.
Diffstat (limited to 'main.cpp')
-rw-r--r-- | main.cpp | 100 |
1 files changed, 88 insertions, 12 deletions
@@ -17,10 +17,21 @@ public: public slots: void setModel(Model *model); + void enableAutoRotate(bool enabled); void enableWireframe(bool enabled); + void enableNormals(bool enabled); + void setLightPosition(int pos); + void setColor(QRgb color); private: bool m_wireframeEnabled; + bool m_normalsEnabled; + bool m_autoRotate; + + float m_angle; + float m_lightPos; + + QRgb m_color; Model *m_model; }; @@ -35,19 +46,24 @@ public: private slots: void loadModel(const QString &model); void modelLoaded(); + void setColor(bool showDialog = true); private: GraphicsScene *m_scene; QFutureWatcher<Model *> m_modelLoader; QComboBox *m_models; + QRgb m_color; }; Controls::Controls(GraphicsScene *scene) : m_scene(scene) , m_models(new QComboBox) + , m_color(qRgb(180, 100, 255)) { QVBoxLayout *layout = new QVBoxLayout(this); + layout->addWidget(new QLabel("Model:")); + QDir dir("models"); dir.setNameFilters(QStringList() << "*.obj"); m_models->addItems(dir.entryList()); @@ -56,14 +72,35 @@ Controls::Controls(GraphicsScene *scene) if (m_models->count() > 0) loadModel(m_models->currentText()); - layout->addWidget(m_models); - QCheckBox *wireframe = new QCheckBox("Wireframe"); + QCheckBox *autoRotate = new QCheckBox("Auto-rotate"); + autoRotate->setChecked(true); + connect(autoRotate, SIGNAL(toggled(bool)), m_scene, SLOT(enableAutoRotate(bool))); + layout->addWidget(autoRotate); + + QCheckBox *wireframe = new QCheckBox("Render as wireframe"); wireframe->setChecked(true); connect(wireframe, SIGNAL(toggled(bool)), m_scene, SLOT(enableWireframe(bool))); - layout->addWidget(wireframe); + + QCheckBox *normals = new QCheckBox("Display normals vectors"); + wireframe->setChecked(false); + connect(normals, SIGNAL(toggled(bool)), m_scene, SLOT(enableNormals(bool))); + layout->addWidget(normals); + + layout->addWidget(new QLabel("Light position:")); + QSlider *lightPosition = new QSlider(Qt::Horizontal); + lightPosition->setRange(-1000, 1000); + connect(lightPosition, SIGNAL(valueChanged(int)), m_scene, SLOT(setLightPosition(int))); + layout->addWidget(lightPosition); + + m_scene->setLightPosition(lightPosition->value()); + + QPushButton *colorButton = new QPushButton("Set model color"); + connect(colorButton, SIGNAL(pressed()), this, SLOT(setColor())); + layout->addWidget(colorButton); + setColor(false); } Model *loadModel(const QString &filename) @@ -85,15 +122,26 @@ void Controls::modelLoaded() QApplication::restoreOverrideCursor(); } +void Controls::setColor(bool showDialog) +{ + if (showDialog) + m_color = QColorDialog::getRgba(m_color); + + m_scene->setColor(m_color); +} + GraphicsScene::GraphicsScene() : m_wireframeEnabled(true) + , m_normalsEnabled(false) + , m_autoRotate(true) + , m_angle(0) , m_model(0) { Controls *controls = new Controls(this); controls->setWindowOpacity(0.8); addWidget(controls)->translate(10, 10); - setSceneRect(QRect(0, 0, 800, 600)); + setSceneRect(QRect(0, 0, 1024, 768)); } void GraphicsScene::drawBackground(QPainter *painter, const QRectF &) @@ -101,7 +149,7 @@ void GraphicsScene::drawBackground(QPainter *painter, const QRectF &) painter->save(); glClearColor(1, 1, 1, 1); - glClear(GL_COLOR_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); bool useMultisample = static_cast<QGLWidget *>(painter->device())->format().sampleBuffers(); @@ -109,21 +157,26 @@ void GraphicsScene::drawBackground(QPainter *painter, const QRectF &) glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); - glOrtho(-1, 1, 1, -1, -10, 10); + gluPerspective(70, painter->device()->width() / float(painter->device()->height()), 0.01, 1000); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); + float pos[] = { m_lightPos, 5, 2, 0 }; + glLightfv(GL_LIGHT0, GL_POSITION, pos); + glColor4f(qRed(m_color)/255.0f, qGreen(m_color)/255.0f, qBlue(m_color)/255.0f, 1.0f); + if (useMultisample) glEnable(GL_MULTISAMPLE); - static float rot = 0; - glRotatef(rot, 0, 1, 0); - rot += 0.1; + glTranslatef(0, 0, -1.5); + glRotatef(m_angle, 0.5, 1, 0.1); + + if (m_autoRotate) + m_angle += 0.4; - glColor3f(0, 0, 0); - m_model->render(m_wireframeEnabled); + m_model->render(m_wireframeEnabled, m_normalsEnabled); if (useMultisample) glDisable(GL_MULTISAMPLE); @@ -136,7 +189,7 @@ void GraphicsScene::drawBackground(QPainter *painter, const QRectF &) painter->restore(); - update(); + QTimer::singleShot(20, this, SLOT(update())); } void GraphicsScene::setModel(Model *model) @@ -146,11 +199,34 @@ void GraphicsScene::setModel(Model *model) update(); } +void GraphicsScene::enableAutoRotate(bool enabled) +{ + m_autoRotate = enabled; + update(); +} + void GraphicsScene::enableWireframe(bool enabled) { m_wireframeEnabled = enabled; } +void GraphicsScene::enableNormals(bool enabled) +{ + m_normalsEnabled = enabled; +} + +void GraphicsScene::setLightPosition(int pos) +{ + m_lightPos = pos * 0.005; + update(); +} + +void GraphicsScene::setColor(QRgb color) +{ + m_color = color; + update(); +} + int main(int argc, char **argv) { QApplication app(argc, argv); |