This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
YASMIN/map/MapView.cpp
2016-09-11 12:11:54 +02:00

133 lines
2.9 KiB
C++

#include "MapView.h"
#include <QGLShaderProgram>
#include "elements/Walls.h"
#include "elements/Ground.h"
#include "elements/Handrails.h"
#include "elements/Stairs.h"
#include "elements/Doors.h"
#include "elements/Path.h"
// http://doc.qt.io/qt-5/qtopengl-cube-example.html
MapView::MapView(QWidget* parent) : QOpenGLWidget(parent) {
};
void MapView::setMap(Floorplan::IndoorMap* map) {
for (Floorplan::Floor* floor : map->floors) {
elements.push_back(new Ground(floor));
elements.push_back(new Walls(floor));
elements.push_back(new Handrails(floor));
elements.push_back(new Stairs(floor));
elements.push_back(new Doors(floor));
}
this->path = new Path();
elements.push_back(this->path);
}
void MapView::setPath(const std::vector<Point3>& path) {
this->path->set(path);
}
void MapView::timerEvent(QTimerEvent *) {
update();
}
void MapView::initializeGL() {
initializeOpenGLFunctions();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
for (Renderable* r : elements) {
r->initGL();
}
timer.start(60, this);
}
void MapView::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw();
}
void MapView::resizeGL(int w, int h) {
// Calculate aspect ratio
qreal aspect = qreal(w) / qreal(h ? h : 1);
// viewing frustrum [0:50] meter
const qreal zNear = 0.02, zFar = 50, fov = 50.0;
// Reset projection
matProject.setToIdentity();
matProject.scale(-1, 1, 1);
glCullFace(GL_FRONT);
//matProject.scale(0.05, 0.05, 0.05);
matProject.perspective(fov, aspect, zNear, zFar);
//matProject.scale(-0.01, 0.01, 0.01);
}
void MapView::setLookAt(const Point3 pos_m, const Point3 dir) {
QVector3D qDir(dir.x, dir.z, dir.y);
QVector3D lookAt = QVector3D(pos_m.x, pos_m.z, pos_m.y);
QVector3D eye = lookAt + qDir * 0.1;
QVector3D up = QVector3D(0,1,0);
matView.setToIdentity();
//matView.scale(0.01, 0.01, 0.01);
matView.lookAt(eye, lookAt, up);
//matView.scale(0.99, 1, 1);
//matView.translate(0.7, 0, 0);
lightPos = eye + QVector3D(0.0, 4.0, 0.0);
eyePos = eye;
}
void MapView::draw() {
// clear everything
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//static float angularSpeed = 0;
//angularSpeed += 1.5;
//texture->bind();
//QVector3D rotationAxis(1,1,1);
//QQuaternion rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed);
// Calculate model view transformation
QMatrix4x4 matModel;
//matModel.setToIdentity();
//matModel.translate(0.0, 0.0, 0.0);
//matModel.rotate(rotation);
for (Renderable* r : elements) {
QOpenGLShaderProgram& program = r->getProgram();
program.bind();
// set the matrices
program.setUniformValue("m_matrix", matModel);
program.setUniformValue("mv_matrix", matView * matModel);
program.setUniformValue("mvp_matrix", matProject * matView * matModel);
program.setUniformValue("lightWorldPos", lightPos);
program.setUniformValue("eyeWorldPos", eyePos);
r->render();
}
}