diff options
author | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-05-01 01:41:12 (GMT) |
---|---|---|
committer | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-05-01 01:41:53 (GMT) |
commit | 79bc8f778f0b7b935ce3d808d8c91c3730f886bb (patch) | |
tree | ffe8356a7c1038af0c104fee1516ce4d84defcef /demos/boxes/scene.cpp | |
parent | 1092ef425b9ed5835e4863c5ec32f9fc21af8d4d (diff) | |
download | Qt-79bc8f778f0b7b935ce3d808d8c91c3730f886bb.zip Qt-79bc8f778f0b7b935ce3d808d8c91c3730f886bb.tar.gz Qt-79bc8f778f0b7b935ce3d808d8c91c3730f886bb.tar.bz2 |
Port boxes demo to use math3d throughout
Patch originally by Ian Walters.
Diffstat (limited to 'demos/boxes/scene.cpp')
-rw-r--r-- | demos/boxes/scene.cpp | 80 |
1 files changed, 38 insertions, 42 deletions
diff --git a/demos/boxes/scene.cpp b/demos/boxes/scene.cpp index cd9671e..db32928 100644 --- a/demos/boxes/scene.cpp +++ b/demos/boxes/scene.cpp @@ -39,7 +39,10 @@ ** ****************************************************************************/ +#include <QDebug> #include "scene.h" +#include <QtGui/qmatrix4x4.h> +#include <QtGui/qvector3d.h> #include "3rdparty/fbm.h" @@ -484,9 +487,9 @@ Scene::Scene(int width, int height, int maxTextureSize) { setSceneRect(0, 0, width, height); - m_trackBalls[0] = TrackBall(0.0005f, gfx::Vector3f::vector(0, 1, 0), TrackBall::Sphere); - m_trackBalls[1] = TrackBall(0.0001f, gfx::Vector3f::vector(0, 0, 1), TrackBall::Sphere); - m_trackBalls[2] = TrackBall(0.0f, gfx::Vector3f::vector(0, 1, 0), TrackBall::Plane); + m_trackBalls[0] = TrackBall(0.05f, QVector3D(0, 1, 0), TrackBall::Sphere); + m_trackBalls[1] = TrackBall(0.005f, QVector3D(0, 0, 1), TrackBall::Sphere); + m_trackBalls[2] = TrackBall(0.0f, QVector3D(0, 1, 0), TrackBall::Plane); m_renderOptions = new RenderOptionsDialog; m_renderOptions->move(20, 120); @@ -648,9 +651,9 @@ void Scene::initGL() // If one of the boxes should not be rendered, set excludeBox to its index. // If the main box should not be rendered, set excludeBox to -1. -void Scene::renderBoxes(const gfx::Matrix4x4f &view, int excludeBox) +void Scene::renderBoxes(const QMatrix4x4 &view, int excludeBox) { - gfx::Matrix4x4f invView = view.inverse(); + QMatrix4x4 invView = view.inverted(); // If multi-texturing is supported, use three saplers. if (glActiveTexture) { @@ -666,11 +669,11 @@ void Scene::renderBoxes(const gfx::Matrix4x4f &view, int excludeBox) glDisable(GL_LIGHTING); glDisable(GL_CULL_FACE); - gfx::Matrix4x4f viewRotation(view); + QMatrix4x4 viewRotation(view); viewRotation(3, 0) = viewRotation(3, 1) = viewRotation(3, 2) = 0.0f; viewRotation(0, 3) = viewRotation(1, 3) = viewRotation(2, 3) = 0.0f; viewRotation(3, 3) = 1.0f; - glLoadMatrixf(viewRotation.bits()); + glLoadMatrixf(viewRotation.data()); glScalef(20.0f, 20.0f, 20.0f); // Don't render the environment if the environment texture can't be set for the correct sampler. @@ -685,7 +688,7 @@ void Scene::renderBoxes(const gfx::Matrix4x4f &view, int excludeBox) m_environment->unbind(); } - glLoadMatrixf(view.bits()); + glLoadMatrixf(view.data()); glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); @@ -695,9 +698,11 @@ void Scene::renderBoxes(const gfx::Matrix4x4f &view, int excludeBox) continue; glPushMatrix(); - gfx::Matrix4x4f m; - m_trackBalls[1].rotation().matrix(m); - glMultMatrixf(m.bits()); + QMatrix4x4 m; + m.rotate(m_trackBalls[1].rotation()); + m = m.transposed(); + + glMultMatrixf(m.data()); glRotatef(360.0f * i / m_programs.size(), 0.0f, 0.0f, 1.0f); glTranslatef(2.0f, 0.0f, 0.0f); @@ -713,12 +718,8 @@ void Scene::renderBoxes(const gfx::Matrix4x4f &view, int excludeBox) m_programs[i]->setUniformValue("tex", 0); m_programs[i]->setUniformValue("env", 1); m_programs[i]->setUniformValue("noise", 2); - QMatrix4x4 mview; - QMatrix4x4 minvview; - memcpy(mview.data(), view.bits(), sizeof(float) * 16); - memcpy(minvview.data(), invView.bits(), sizeof(float) * 16); - m_programs[i]->setUniformValue("view", mview); - m_programs[i]->setUniformValue("invView", minvview); + m_programs[i]->setUniformValue("view", view); + m_programs[i]->setUniformValue("invView", invView); m_box->draw(); m_programs[i]->disable(); @@ -732,9 +733,10 @@ void Scene::renderBoxes(const gfx::Matrix4x4f &view, int excludeBox) } if (-1 != excludeBox) { - gfx::Matrix4x4f m; - m_trackBalls[0].rotation().matrix(m); - glMultMatrixf(m.bits()); + QMatrix4x4 m; + m.rotate(m_trackBalls[0].rotation()); + m = m.transposed(); + glMultMatrixf(m.data()); if (glActiveTexture) { if (m_dynamicCubemap) @@ -747,12 +749,8 @@ void Scene::renderBoxes(const gfx::Matrix4x4f &view, int excludeBox) m_programs[m_currentShader]->setUniformValue("tex", 0); m_programs[m_currentShader]->setUniformValue("env", 1); m_programs[m_currentShader]->setUniformValue("noise", 2); - QMatrix4x4 mview; - QMatrix4x4 minvview; - memcpy(mview.data(), view.bits(), sizeof(float) * 16); - memcpy(minvview.data(), invView.bits(), sizeof(float) * 16); - m_programs[m_currentShader]->setUniformValue("view", mview); - m_programs[m_currentShader]->setUniformValue("invView", minvview); + m_programs[m_currentShader]->setUniformValue("view", view); + m_programs[m_currentShader]->setUniformValue("invView", invView); m_box->draw(); m_programs[m_currentShader]->disable(); @@ -839,31 +837,32 @@ void Scene::renderCubemaps() // To speed things up, only update the cubemaps for the small cubes every N frames. const int N = (m_updateAllCubemaps ? 1 : 3); - gfx::Matrix4x4f mat; + QMatrix4x4 mat; GLRenderTargetCube::getProjectionMatrix(mat, 0.1f, 100.0f); glMatrixMode(GL_PROJECTION); glPushMatrix(); - glLoadMatrixf(mat.bits()); + glLoadMatrixf(mat.data()); glMatrixMode(GL_MODELVIEW); glPushMatrix(); - gfx::Vector3f center; + QVector3D center; for (int i = m_frame % N; i < m_cubemaps.size(); i += N) { if (0 == m_cubemaps[i]) continue; float angle = 2.0f * PI * i / m_cubemaps.size(); - center = m_trackBalls[1].rotation().transform(gfx::Vector3f::vector(cos(angle), sin(angle), 0)); + + center = m_trackBalls[1].rotation().rotateVector(QVector3D(cos(angle), sin(angle), 0.0f)); for (int face = 0; face < 6; ++face) { m_cubemaps[i]->begin(face); GLRenderTargetCube::getViewMatrix(mat, face); - gfx::Vector4f v = gfx::Vector4f::vector(-center[0], -center[1], -center[2], 1.0); - mat[3] = v * mat; + QVector4D v = QVector4D(-center.x(), -center.y(), -center.z(), 1.0); + mat.setColumn(3, v * mat); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); renderBoxes(mat, i); @@ -907,12 +906,9 @@ void Scene::drawBackground(QPainter *painter, const QRectF &) glMatrixMode(GL_MODELVIEW); - //gfx::Matrix4x4f view = gfx::Matrix4x4f::identity(); - //view(3, 2) -= 2.0f * exp(m_distExp / 1200.0f); - - gfx::Matrix4x4f view; - m_trackBalls[2].rotation().matrix(view); - view(3, 2) -= 2.0f * exp(m_distExp / 1200.0f); + QMatrix4x4 view; + view.rotate(m_trackBalls[2].rotation()); + view(2, 3) -= 2.0f * exp(m_distExp / 1200.0f); renderBoxes(view); defaultStates(); @@ -946,10 +942,10 @@ void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) } if (event->buttons() & Qt::MidButton) { - m_trackBalls[2].move(pixelPosToViewPos(event->scenePos()), gfx::Quaternionf::identity()); + m_trackBalls[2].move(pixelPosToViewPos(event->scenePos()), QQuaternion()); event->accept(); } else { - m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), gfx::Quaternionf::identity()); + m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), QQuaternion()); } } @@ -970,7 +966,7 @@ void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event) } if (event->buttons() & Qt::MidButton) { - m_trackBalls[2].push(pixelPosToViewPos(event->scenePos()), gfx::Quaternionf::identity()); + m_trackBalls[2].push(pixelPosToViewPos(event->scenePos()), QQuaternion()); event->accept(); } } @@ -992,7 +988,7 @@ void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) } if (event->button() == Qt::MidButton) { - m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), gfx::Quaternionf::identity()); + m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), QQuaternion()); event->accept(); } } |