Added camera to OpenGLSharedState

This commit is contained in:
Michaël Lemaire 2013-12-22 17:30:48 +01:00
parent 666420bbb2
commit 645bada91b
10 changed files with 63 additions and 60 deletions

View file

@ -4,6 +4,9 @@
#include "basics_global.h"
#include "Vector3.h"
#ifdef QT_GUI_LIB
#include <QMatrix4x4>
#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;

View file

@ -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;}

View file

@ -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<QString, OpenGLShaderProgram*> i(shaders);
while (i.hasNext())
{
i.next();
i.value()->updateCamera(vlocation, projection * transform);
}
}
void OpenGLPart::updateScenery(bool onlyCommon)
{
// Let subclass do its own collecting

View file

@ -27,7 +27,6 @@ public:
virtual void render() = 0;
void postInitialize();
void updateCamera(CameraDefinition* camera);
void updateScenery(bool onlyCommon=false);
protected:

View file

@ -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 &)

View file

@ -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<QString, QPair<int, unsigned int> > iter(textures);
int i = 0;
while (iter.hasNext())

View file

@ -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;

View file

@ -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:

View file

@ -4,6 +4,7 @@
#include <QOpenGLShaderProgram>
#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)

View file

@ -5,6 +5,7 @@
#include <QColor>
#include <QVector3D>
#include <QMatrix4x4>
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;
};
}