openGL work and other parts
This commit is contained in:
155
map/MapView.cpp
155
map/MapView.cpp
@@ -1,7 +1,13 @@
|
||||
#include "MapView.h"
|
||||
|
||||
#include <QGLShaderProgram>
|
||||
#include "Geometry.h"
|
||||
|
||||
#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
|
||||
|
||||
@@ -10,31 +16,42 @@ 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() {
|
||||
|
||||
//qglClearColor(Qt::black);
|
||||
|
||||
//glEnable(GL_DEPTH_TEST);
|
||||
//glEnable(GL_CULL_FACE);
|
||||
//glShadeModel(GL_SMOOTH);
|
||||
//glEnable(GL_LIGHTING);
|
||||
//glEnable(GL_LIGHT0);
|
||||
|
||||
//static GLfloat lightPosition[4] = { 0, 0, 10, 1.0 };
|
||||
//glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
|
||||
|
||||
initializeOpenGLFunctions();
|
||||
|
||||
geo = new Geometry();
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/res/gl/vertex1.glsl")) {throw "1";}
|
||||
if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/res/gl/fragment1.glsl")) {throw "2";}
|
||||
if (!program.link()) {throw "3";}
|
||||
if (!program.bind()) {throw "4";}
|
||||
|
||||
|
||||
for (Renderable* r : elements) {
|
||||
r->initGL();
|
||||
}
|
||||
|
||||
timer.start(60, this);
|
||||
|
||||
@@ -42,11 +59,6 @@ void MapView::initializeGL() {
|
||||
|
||||
void MapView::paintGL() {
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
//glLoadIdentity();
|
||||
//glTranslatef(0.0, 0.0, -10.0);
|
||||
//glRotatef(20 / 16.0, 1.0, 0.0, 0.0);
|
||||
//glRotatef(30 / 16.0, 0.0, 1.0, 0.0);
|
||||
//glRotatef(60 / 16.0, 0.0, 0.0, 1.0);
|
||||
draw();
|
||||
}
|
||||
|
||||
@@ -55,75 +67,66 @@ void MapView::resizeGL(int w, int h) {
|
||||
// Calculate aspect ratio
|
||||
qreal aspect = qreal(w) / qreal(h ? h : 1);
|
||||
|
||||
// Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
|
||||
const qreal zNear = 3.0, zFar = 7.0, fov = 45.0;
|
||||
// viewing frustrum [0:50] meter
|
||||
const qreal zNear = 0.02, zFar = 50, fov = 50.0;
|
||||
|
||||
// Reset projection
|
||||
projection.setToIdentity();
|
||||
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);
|
||||
|
||||
// Set perspective projection
|
||||
projection.perspective(fov, aspect, zNear, zFar);
|
||||
}
|
||||
|
||||
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 += 0.5;
|
||||
//static float angularSpeed = 0;
|
||||
//angularSpeed += 1.5;
|
||||
|
||||
// Clear color and depth buffer
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
//texture->bind();
|
||||
//QVector3D rotationAxis(1,1,1);
|
||||
//QQuaternion rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed);
|
||||
|
||||
//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);
|
||||
|
||||
// Calculate model view transformation
|
||||
QMatrix4x4 matrix;
|
||||
matrix.translate(0.0, 0.0, -5.0);
|
||||
matrix.rotate(rotation);
|
||||
for (Renderable* r : elements) {
|
||||
|
||||
// Set modelview-projection matrix
|
||||
program.setUniformValue("mvp_matrix", projection * matrix);
|
||||
QOpenGLShaderProgram& program = r->getProgram();
|
||||
program.bind();
|
||||
|
||||
// Use texture unit 0 which contains cube.png
|
||||
program.setUniformValue("texture", 0);
|
||||
// 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);
|
||||
|
||||
// Draw cube geometry
|
||||
geo->drawCubeGeometry(&program);
|
||||
r->render();
|
||||
|
||||
//qglColor(Qt::red);
|
||||
/*glBegin(GL_QUADS);
|
||||
glNormal3f(0,0,-1);
|
||||
glVertex3f(-1,-1,0);
|
||||
glVertex3f(-1,1,0);
|
||||
glVertex3f(1,1,0);
|
||||
glVertex3f(1,-1,0);
|
||||
}
|
||||
|
||||
glEnd();
|
||||
glBegin(GL_TRIANGLES);
|
||||
glNormal3f(0,-1,0.707);
|
||||
glVertex3f(-1,-1,0);
|
||||
glVertex3f(1,-1,0);
|
||||
glVertex3f(0,0,1.2);
|
||||
glEnd();
|
||||
glBegin(GL_TRIANGLES);
|
||||
glNormal3f(1,0, 0.707);
|
||||
glVertex3f(1,-1,0);
|
||||
glVertex3f(1,1,0);
|
||||
glVertex3f(0,0,1.2);
|
||||
glEnd();
|
||||
glBegin(GL_TRIANGLES);
|
||||
glNormal3f(0,1,0.707);
|
||||
glVertex3f(1,1,0);
|
||||
glVertex3f(-1,1,0);
|
||||
glVertex3f(0,0,1.2);
|
||||
glEnd();
|
||||
glBegin(GL_TRIANGLES);
|
||||
glNormal3f(-1,0,0.707);
|
||||
glVertex3f(-1,1,0);
|
||||
glVertex3f(-1,-1,0);
|
||||
glVertex3f(0,0,1.2);
|
||||
glEnd();*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user