From 645bada91b2b85b72037b3a1815ca910895c9180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 22 Dec 2013 17:30:48 +0100 Subject: [PATCH] Added camera to OpenGLSharedState --- src/basics/Matrix4.h | 12 +++++++++ src/definition/CameraDefinition.h | 1 + src/render/opengl/OpenGLPart.cpp | 30 ----------------------- src/render/opengl/OpenGLPart.h | 1 - src/render/opengl/OpenGLRenderer.cpp | 22 +++++++++++++++-- src/render/opengl/OpenGLShaderProgram.cpp | 20 --------------- src/render/opengl/OpenGLShaderProgram.h | 5 ---- src/render/opengl/OpenGLSharedState.h | 2 ++ src/render/opengl/OpenGLVariable.cpp | 26 ++++++++++++++++++-- src/render/opengl/OpenGLVariable.h | 4 +++ 10 files changed, 63 insertions(+), 60 deletions(-) diff --git a/src/basics/Matrix4.h b/src/basics/Matrix4.h index a53d3f3..42954c7 100644 --- a/src/basics/Matrix4.h +++ b/src/basics/Matrix4.h @@ -4,6 +4,9 @@ #include "basics_global.h" #include "Vector3.h" +#ifdef QT_GUI_LIB +#include +#endif namespace paysages { namespace basics { @@ -35,6 +38,15 @@ public: double getDeterminant() const; +#ifdef QT_GUI_LIB + inline QMatrix4x4 toQMatrix() const { + return QMatrix4x4(a, b, c, d, + e, f, g, h, + i, j, k, l, + m, n, o, p); + } +#endif + private: double a; double b; diff --git a/src/definition/CameraDefinition.h b/src/definition/CameraDefinition.h index e5dc512..162e38a 100644 --- a/src/definition/CameraDefinition.h +++ b/src/definition/CameraDefinition.h @@ -36,6 +36,7 @@ public: inline double getRoll() const {return roll;} inline Vector3 getDirection() const {return Vector3(direction);} inline Vector3 getDirectionNormalized() const {return forward;} + inline const Matrix4 &getTransformationMatrix() const {return projector;} inline VectorSpherical getDirectionSpherical() const {return direction;} inline CameraPerspective getPerspective() const {return perspective;} diff --git a/src/render/opengl/OpenGLPart.cpp b/src/render/opengl/OpenGLPart.cpp index 4c184d6..2e313d5 100644 --- a/src/render/opengl/OpenGLPart.cpp +++ b/src/render/opengl/OpenGLPart.cpp @@ -49,36 +49,6 @@ void OpenGLPart::postInitialize() } } -void OpenGLPart::updateCamera(CameraDefinition* camera) -{ - // Get camera info - Vector3 location = camera->getLocation(); - Vector3 target = camera->getTarget(); - Vector3 up = camera->getUpVector(); - CameraPerspective perspective = camera->getPerspective(); - - QVector3D vlocation(location.x, location.y, location.z); - - // Compute matrix - QMatrix4x4 transform; - transform.setToIdentity(); - transform.lookAt(vlocation, - QVector3D(target.x, target.y, target.z), - QVector3D(up.x, up.y, up.z)); - - QMatrix4x4 projection; - projection.setToIdentity(); - projection.perspective(perspective.yfov * 180.0 / M_PI, perspective.xratio, perspective.znear, perspective.zfar); - - // Set in shaders - QMapIterator i(shaders); - while (i.hasNext()) - { - i.next(); - i.value()->updateCamera(vlocation, projection * transform); - } -} - void OpenGLPart::updateScenery(bool onlyCommon) { // Let subclass do its own collecting diff --git a/src/render/opengl/OpenGLPart.h b/src/render/opengl/OpenGLPart.h index b7ae211..84ef432 100644 --- a/src/render/opengl/OpenGLPart.h +++ b/src/render/opengl/OpenGLPart.h @@ -27,7 +27,6 @@ public: virtual void render() = 0; void postInitialize(); - void updateCamera(CameraDefinition* camera); void updateScenery(bool onlyCommon=false); protected: diff --git a/src/render/opengl/OpenGLRenderer.cpp b/src/render/opengl/OpenGLRenderer.cpp index 1b326bf..0600be9 100644 --- a/src/render/opengl/OpenGLRenderer.cpp +++ b/src/render/opengl/OpenGLRenderer.cpp @@ -75,8 +75,26 @@ void OpenGLRenderer::paint() void OpenGLRenderer::cameraChangeEvent(CameraDefinition *camera) { - skybox->updateCamera(camera); - water->updateCamera(camera); + // Get camera info + Vector3 location = camera->getLocation(); + Vector3 target = camera->getTarget(); + Vector3 up = camera->getUpVector(); + CameraPerspective perspective = camera->getPerspective(); + + // Compute matrix + QMatrix4x4 transform; + transform.setToIdentity(); + transform.lookAt(QVector3D(location.x, location.y, location.z), + QVector3D(target.x, target.y, target.z), + QVector3D(up.x, up.y, up.z)); + + QMatrix4x4 projection; + projection.setToIdentity(); + projection.perspective(perspective.yfov * 180.0 / M_PI, perspective.xratio, perspective.znear, perspective.zfar); + + // Set in shaders + shared_state->set("cameraLocation", location); + shared_state->set("viewMatrix", projection * transform); } double OpenGLRenderer::getPrecision(const Vector3 &) diff --git a/src/render/opengl/OpenGLShaderProgram.cpp b/src/render/opengl/OpenGLShaderProgram.cpp index cf34e66..9023b4d 100644 --- a/src/render/opengl/OpenGLShaderProgram.cpp +++ b/src/render/opengl/OpenGLShaderProgram.cpp @@ -44,12 +44,6 @@ void OpenGLShaderProgram::compile() } } -void OpenGLShaderProgram::updateCamera(const QVector3D& location, const QMatrix4x4& view) -{ - this->camera_location = location; - this->view = view; -} - void OpenGLShaderProgram::addTexture(QString sampler_name, Texture2D* texture) { GLuint texid; @@ -187,20 +181,6 @@ void OpenGLShaderProgram::bind() renderer->getSharedState()->apply(this); - // TODO Keep locations in cache - - int viewMatrix = program->uniformLocation("viewMatrix"); - if (viewMatrix >= 0) - { - program->setUniformValue(viewMatrix, view); - } - - int cameraLocation = program->uniformLocation("cameraLocation"); - if (cameraLocation >= 0) - { - program->setUniformValue(cameraLocation, camera_location); - } - QMapIterator > iter(textures); int i = 0; while (iter.hasNext()) diff --git a/src/render/opengl/OpenGLShaderProgram.h b/src/render/opengl/OpenGLShaderProgram.h index 7cf9e6b..0f23c2a 100644 --- a/src/render/opengl/OpenGLShaderProgram.h +++ b/src/render/opengl/OpenGLShaderProgram.h @@ -25,8 +25,6 @@ public: void addFragmentSource(QString path); void compile(); - void updateCamera(const QVector3D& location, const QMatrix4x4& view); - void addTexture(QString sampler_name, Texture2D* texture); void addTexture(QString sampler_name, Texture3D* texture); void addTexture(QString sampler_name, Texture4D* texture); @@ -44,9 +42,6 @@ private: OpenGLRenderer* renderer; - QMatrix4x4 view; - QVector3D camera_location; - QString name; QOpenGLShaderProgram* program; QOpenGLFunctions_3_2_Core* functions; diff --git a/src/render/opengl/OpenGLSharedState.h b/src/render/opengl/OpenGLSharedState.h index 050f68d..8ce3d00 100644 --- a/src/render/opengl/OpenGLSharedState.h +++ b/src/render/opengl/OpenGLSharedState.h @@ -33,7 +33,9 @@ public: inline void set(const std::string &name, const Texture4D *texture) {get(name)->set(texture);} inline void set(const std::string &name, float value) {get(name)->set(value);} inline void set(const std::string &name, const Vector3 &vector) {get(name)->set(vector);} + inline void set(const std::string &name, const QVector3D &vector) {get(name)->set(vector);} inline void set(const std::string &name, const Matrix4 &matrix) {get(name)->set(matrix);} + inline void set(const std::string &name, const QMatrix4x4 &matrix) {get(name)->set(matrix);} inline void set(const std::string &name, const Color &color) {get(name)->set(color);} private: diff --git a/src/render/opengl/OpenGLVariable.cpp b/src/render/opengl/OpenGLVariable.cpp index 5724399..b7d864e 100644 --- a/src/render/opengl/OpenGLVariable.cpp +++ b/src/render/opengl/OpenGLVariable.cpp @@ -4,6 +4,7 @@ #include #include "OpenGLShaderProgram.h" #include "Vector3.h" +#include "Matrix4.h" #include "Color.h" OpenGLVariable::OpenGLVariable(const std::string &name): @@ -27,6 +28,9 @@ void OpenGLVariable::apply(OpenGLShaderProgram *program) case TYPE_VECTOR3: pr->setUniformValue(name.c_str(), value_vector3); break; + case TYPE_MATRIX4: + pr->setUniformValue(name.c_str(), value_matrix4); + break; case TYPE_NONE: break; } @@ -42,10 +46,28 @@ void OpenGLVariable::set(float value) void OpenGLVariable::set(const Vector3 &vector) { - assert(type == TYPE_NONE or type == TYPE_COLOR); + set(QVector3D(vector.x, vector.y, vector.z)); +} + +void OpenGLVariable::set(const QVector3D &vector) +{ + assert(type == TYPE_NONE or type == TYPE_VECTOR3); type = TYPE_VECTOR3; - value_vector3 = QVector3D(vector.x, vector.y, vector.z); + value_vector3 = vector; +} + +void OpenGLVariable::set(const Matrix4 &matrix) +{ + set(matrix.toQMatrix()); +} + +void OpenGLVariable::set(const QMatrix4x4 &matrix) +{ + assert(type == TYPE_NONE or type == TYPE_MATRIX4); + + type = TYPE_MATRIX4; + value_matrix4 = matrix; } void OpenGLVariable::set(const Color &color) diff --git a/src/render/opengl/OpenGLVariable.h b/src/render/opengl/OpenGLVariable.h index 102c1ea..0fc633c 100644 --- a/src/render/opengl/OpenGLVariable.h +++ b/src/render/opengl/OpenGLVariable.h @@ -5,6 +5,7 @@ #include #include +#include namespace paysages { namespace opengl { @@ -36,7 +37,9 @@ public: void set(const Texture4D *texture); void set(float value); void set(const Vector3 &vector); + void set(const QVector3D &vector); void set(const Matrix4 &matrix); + void set(const QMatrix4x4 &matrix); void set(const Color &color); private: @@ -46,6 +49,7 @@ private: float value_float; QColor value_color; QVector3D value_vector3; + QMatrix4x4 value_matrix4; }; }