worked on 3D display

some ui changes
refactoring
new icons
This commit is contained in:
2018-02-03 23:30:55 +01:00
parent e5e19779d5
commit 3b62f23c0e
21 changed files with 1054 additions and 765 deletions

View File

@@ -2,217 +2,52 @@
#include "Shader.h"
#include <QtOpenGL>
//static float cube_vertices[] = {
//1 -1.0000, -1.0000, -1.0000,
//2 -1.0000, 1.0000, -1.0000,
//3 1.0000, 1.0000, -1.0000,
//4 1.0000, -1.0000, -1.0000,
//5 -1.0000, -1.0000, 1.0000,
//6 1.0000, -1.0000, 1.0000,
//7 1.0000, 1.0000, 1.0000,
//8 -1.0000, 1.0000, 1.0000,
//};
//static int cube_vertex_indices[] = {
// 1, 2, 3,
// 3, 4, 1,
// 5, 6, 7,
// 7, 8, 5,
// 1, 4, 6,
// 6, 5, 1,
// 4, 3, 7,
// 7, 6, 4,
// 3, 2, 8,
// 8, 7, 3,
// 2, 1, 5,
// 5, 8, 2,
//};
static float cube_vertices[] = {
-1, -1, -1, -1, +1, -1, +1, +1, -1,
+1, +1, -1, +1, -1, -1, -1, -1, -1,
-1, -1, 0,
+1, -1, 0,
0, +1, 0,
-1, -1, +1, +1, -1, +1, +1, +1, +1,
+1, +1, +1, -1, +1, +1, -1, -1, +1,
// -1.0000, -1.0000, -1.0000, -1.0000, 1.0000, -1.0000, 1.0000, 1.0000, -1.0000,
// 1.0000, 1.0000, -1.0000, 1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000,
-1, -1, -1, +1, -1, -1, +1, -1, +1,
+1, -1, +1, -1, -1, +1, -1, -1, -1,
// -1.0000, -1.0000, 1.0000, 1.0000, -1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
// 1.0000, 1.0000, 1.0000, -1.0000, 1.0000, 1.0000, -1.0000, -1.0000, 1.0000,
+1, -1, -1, +1, +1, -1, +1, +1, +1,
+1, +1, +1, +1, -1, +1, +1, -1, -1,
// -1.0000, -1.0000, -1.0000, 1.0000, -1.0000, -1.0000, 1.0000, -1.0000, 1.0000,
// 1.0000, -1.0000, 1.0000, -1.0000, -1.0000, 1.0000, -1.0000, -1.0000, -1.0000,
+1, +1, -1, -1, +1, -1, -1, +1, +1,
-1, +1, +1, +1, +1, +1, +1, +1, -1,
// 1.0000, -1.0000, -1.0000, 1.0000, 1.0000, -1.0000, 1.0000, 1.0000, 1.0000,
// 1.0000, 1.0000, 1.0000, 1.0000, -1.0000, 1.0000, 1.0000, -1.0000, -1.0000,
// 1.0000, 1.0000, -1.0000, -1.0000, 1.0000, -1.0000, -1.0000, 1.0000, 1.0000,
// -1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, -1.0000,
// -1.0000, 1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, 1.0000,
// -1.0000, -1.0000, 1.0000, -1.0000, 1.0000, 1.0000, -1.0000, 1.0000, -1.0000,
-1, +1, -1, -1, -1, -1, -1, -1, +1,
-1, -1, +1, -1, +1, +1, -1, +1, -1,
};
//static float cube_normals[] = {
// 0.0000, 0.0000, -1.0000,
// 0.0000, 0.0000, 1.0000,
// 0.0000, -1.0000, 0.0000,
// 1.0000, 0.0000, 0.0000,
// 0.0000, 1.0000, 0.0000,
// -1.0000, 0.0000, 0.0000,
//};
static float cube_normals[] = {
0.0000, 0.0000, -1.0000, 0.0000, 0.0000, -1.0000, 0.0000, 0.0000, -1.0000,
0.0000, 0.0000, -1.0000, 0.0000, 0.0000, -1.0000, 0.0000, 0.0000, -1.0000,
0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 1.0000,
0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 1.0000,
0, 0, -1, 0, 0, -1, 0, 0, -1,
0, 0, -1, 0, 0, -1, 0, 0, -1,
0.0000, -1.0000, 0.0000, 0.0000, -1.0000, 0.0000, 0.0000, -1.0000, 0.0000,
0.0000, -1.0000, 0.0000, 0.0000, -1.0000, 0.0000, 0.0000, -1.0000, 0.0000,
0, 0, +1, 0, 0, +1, 0, 0, +1,
0, 0, +1, 0, 0, +1, 0, 0, +1,
1.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000,
1.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000,
0, -1, 0, 0, -1, 0, 0, -1, 0,
0, -1, 0, 0, -1, 0, 0, -1, 0,
0.0000, 1.0000, 0.0000,0.0000, 1.0000, 0.0000,0.0000, 1.0000, 0.0000,
0.0000, 1.0000, 0.0000,0.0000, 1.0000, 0.0000,0.0000, 1.0000, 0.0000,
1, 0, 0, 1, 0, 0, 1, 0, 0,
1, 0, 0, 1, 0, 0, 1, 0, 0,
0, +1, 0, 0, +1, 0, 0, +1, 0,
0, +1, 0, 0, +1, 0, 0, +1, 0,
-1, 0, 0, -1, 0, 0, -1, 0, 0,
-1, 0, 0, -1, 0, 0, -1, 0, 0,
-1.0000, 0.0000, 0.0000,-1.0000, 0.0000, 0.0000,-1.0000, 0.0000, 0.0000,
-1.0000, 0.0000, 0.0000,-1.0000, 0.0000, 0.0000,-1.0000, 0.0000, 0.0000,
};
//static int cube_normal_indices[] = {
// 1, 1, 1,
// 1, 1, 1,
// 2, 2, 2,
// 2, 2, 2,
// 3, 3, 3,
// 3, 3, 3,
// 4, 4, 4,
// 4, 4, 4,
// 5, 5, 5,
// 5, 5, 5,
// 6, 6, 6,
// 6, 6, 6,
//};
//static float cube_vertices[] = {
// // bottom
// +1, -1, -1,
// +1, -1, +1,
// -1, -1, +1,
// -1, -1, +1,
// -1, -1, -1,
// +1, -1, -1,
// // top
// -1, +1, -1,
// -1, +1, +1,
// +1, +1, +1,
// +1, +1, +1,
// +1, +1, -1,
// -1, +1, -1,
// // left
// -1, -1, -1,
// -1, -1, +1,
// -1, +1, +1,
// -1, +1, +1,
// -1, +1, -1,
// -1, -1, -1,
// // right
// +1, +1, -1,
// +1, +1, +1,
// +1, -1, +1,
// +1, -1, +1,
// +1, -1, -1,
// +1, +1, -1,
// // front
// +1, +1, +1,
// -1, +1, +1,
// -1, -1, +1,
// -1, -1, +1,
// +1, -1, +1,
// +1, +1, +1,
// // rear
// +1, -1, -1,
// -1, -1, -1,
// -1, +1, -1,
// -1, +1, -1,
// +1, +1, -1,
// +1, -1, -1,
//};
//static float cube_normals[] = {
// // bottom
// 0,-1,0,
// 0,-1,0,
// 0,-1,0,
// 0,-1,0,
// 0,-1,0,
// 0,-1,0,
// // top
// 0,+1,0,
// 0,+1,0,
// 0,+1,0,
// 0,+1,0,
// 0,+1,0,
// 0,+1,0,
// // left
// -1,0,0,
// -1,0,0,
// -1,0,0,
// -1,0,0,
// -1,0,0,
// -1,0,0,
// // right
// +1,0,0,
// +1,0,0,
// +1,0,0,
// +1,0,0,
// +1,0,0,
// +1,0,0,
// // front
// 0,0,+1,
// 0,0,+1,
// 0,0,+1,
// 0,0,+1,
// 0,0,+1,
// 0,0,+1,
// // rear
// 0,0,-1,
// 0,0,-1,
// 0,0,-1,
// 0,0,-1,
// 0,0,-1,
// 0,0,-1,
//};
static Shader* shader = nullptr;
//static Shader* shader = nullptr;
Cube::Cube(Point3 pos, float size) : pos(pos), size(size,size,size), rot(0,0,0) {
@@ -228,29 +63,23 @@ void Cube::setColor(float r, float g, float b) {
void Cube::render(const RenderSettings& rs) {
rs.shader->bind();
QMatrix4x4 mat;
mat.translate(pos.x, pos.y, pos.z);
mat.rotate(rot.x, 1, 0, 0);
mat.rotate(rot.y, 0, 1, 0);
mat.rotate(rot.z, 0, 0, 1);
mat.rotate(rot.x, +1, 0, 0);
mat.rotate(rot.y, 0, +1, 0);
mat.rotate(rot.z, 0, 0, +1);
mat.scale(size.x, size.y, size.z);
// mat.scale(0.1, 0.1, 0.1);
rs.shader->setModelMatrix(mat);
// shader->setViewMatrix(V);
// shader->setProjectionMatrix(P);
rs.shader->setColor(color.x, color.y, color.z);
rs.shader->setVertices(cube_vertices);
rs.shader->setNormals(cube_normals);
glDrawArrays(GL_TRIANGLES, 0, 1*3);
//glDrawElements(GL_TRIANGLES, 12, GL_INT, cube_vertex_indices);
glDrawArrays(GL_TRIANGLES, 0, 12*3);
//glDrawElements(GL_TRIANGLES, +12, GL_INT, cube_vertex_indices);
rs.shader->unsetVertices();
rs.shader->unsetNormals();

View File

@@ -0,0 +1,101 @@
#include "Handrail.h"
#include <Indoor/geo/Point3.h>
#include "Shader.h"
Handrail::Handrail(const Point2 from, const Point2 to, float atHeight, float height) :
from(from), to(to), atHeight(atHeight), height(height) {
;
}
void Handrail::render(const RenderSettings& rs) {
rs.shader->bind();
rs.shader->setColor(0.8, 0.8, 0.8);
rs.shader->setModelMatrix(QMatrix4x4());
const float z1 = atHeight;
const float z2 = atHeight + height;
// polygon edges
Point3 p1 = Point3(from.x, from.y, z1);
Point3 p2 = Point3(to.x, to.y, z1);
Point3 p3 = Point3(from.x, from.y, z2);
Point3 p4 = Point3(to.x, to.y, z2);
std::vector<float> vertices;
// top
vertices.insert( vertices.end(), {p3.x, p3.y, p3.z} );
vertices.insert( vertices.end(), {p4.x, p4.y, p4.z} );
// start bar
vertices.insert( vertices.end(), {p1.x, p1.y, p1.z} );
vertices.insert( vertices.end(), {p3.x, p3.y, p3.z} );
// end bar
vertices.insert( vertices.end(), {p2.x, p2.y, p2.z} );
vertices.insert( vertices.end(), {p4.x, p4.y, p4.z} );
// intermediate bars
const Point3 d1 = p2-p1;
const Point3 d2 = p4-p3;
const int numBars = d2.length() / 1;
for (int i = 1; i < numBars; ++i) {
const Point3 s = p1 + d1 * i / numBars;
const Point3 e = p3 + d2 * i / numBars;
vertices.insert( vertices.end(), {s.x, s.y, s.z} );
vertices.insert( vertices.end(), {e.x, e.y, e.z} );
}
rs.shader->setVertices(vertices.data());
glDrawArrays(GL_LINES, 0, vertices.size() / 3);
rs.shader->unsetVertices();
rs.shader->release();
/*
TODO_GL
glDisable(GL_LIGHTING);
glBegin(GL_LINES);
glColor3f(0.9, 0.9, 0.9);
// top
glVertex3f(p3.x, p3.y, p3.z);
glVertex3f(p4.x, p4.y, p4.z);
// start bar
glVertex3f(p1.x, p1.y, p1.z);
glVertex3f(p3.x, p3.y, p3.z);
// end bar
glVertex3f(p2.x, p2.y, p2.z);
glVertex3f(p4.x, p4.y, p4.z);
glColor3f(0.6, 0.6, 0.6);
// intermediate bars
const Point3 d1 = p2-p1;
const Point3 d2 = p4-p3;
const int numBars = d2.length() / 1;
for (int i = 1; i < numBars; ++i) {
const Point3 s = p1 + d1 * i / numBars;
const Point3 e = p3 + d2 * i / numBars;
glVertex3f(s.x, s.y, s.z);
glVertex3f(e.x, e.y, e.z);
}
glEnd();
glEnable(GL_LIGHTING);
*/
}

View File

@@ -0,0 +1,25 @@
#ifndef HANDRAIL_H
#define HANDRAIL_H
#include "Renderable3D.h"
#include <Indoor/geo/Point2.h>
class Handrail : public Renderable3D {
private:
Point2 from;
Point2 to;
float atHeight;
float height;
public:
Handrail(const Point2 from, const Point2 to, float atHeight, float height);
void render(const RenderSettings& rs) override;
};
#endif // HANDRAIL_H

View File

@@ -0,0 +1,73 @@
#include "Outline.h"
#include <QMatrix4x4>
#include <Indoor/geo/Point3.h>
#include <QOpenGLWidget>
#include "Shader.h"
Outline::Outline() {
}
void Outline::render(const RenderSettings& rs) {
rs.shader->bind();
// identity
QMatrix4x4 mat;
rs.shader->setModelMatrix(mat);
// show both sides
//glDisable(GL_CULL_FACE);
glEnable(GL_CULL_FACE);
rs.shader->setColor(color.x, color.y, color.z);
rs.shader->setVertices(vertices.data());
rs.shader->setNormals(normals.data());
glDrawArrays(GL_TRIANGLES, 0, vertices.size() / 3);
rs.shader->unsetVertices();
rs.shader->unsetNormals();
rs.shader->release();
}
void Outline::setColor(float r, float g, float b) {
color = Point3(r,g,b);
}
void Outline::clear() {
normals.clear();
vertices.clear();
}
void Outline::add(std::vector<std::vector<Point3>>& triangles) {
for (const std::vector<Point3>& tria : triangles) {
for (size_t i = 2; i < tria.size(); ++i) {
const Point3 p1 = tria[i-2];
const Point3 p2 = tria[i-1];
const Point3 p3 = tria[i-0];
const Point3 n = cross(p2-p1, p3-p1);
if (n.z < 0) {
vertices.push_back(p1.x); vertices.push_back(p1.y); vertices.push_back(p1.z);
vertices.push_back(p3.x); vertices.push_back(p3.y); vertices.push_back(p3.z);
vertices.push_back(p2.x); vertices.push_back(p2.y); vertices.push_back(p2.z);
} else {
vertices.push_back(p1.x); vertices.push_back(p1.y); vertices.push_back(p1.z);
vertices.push_back(p2.x); vertices.push_back(p2.y); vertices.push_back(p2.z);
vertices.push_back(p3.x); vertices.push_back(p3.y); vertices.push_back(p3.z);
}
normals.push_back(0); normals.push_back(0); normals.push_back(1);
normals.push_back(0); normals.push_back(0); normals.push_back(1);
normals.push_back(0); normals.push_back(0); normals.push_back(1);
}
}
}

33
mapview/3D/misc/Outline.h Normal file
View File

@@ -0,0 +1,33 @@
#ifndef OUTLINE_H
#define OUTLINE_H
#include "Renderable3D.h"
#include <vector>
#include <Indoor/geo/Point3.h>
class Outline : public Renderable3D {
private:
std::vector<float> vertices;
std::vector<float> normals;
Point3 color;
public:
Outline();
virtual void render(const RenderSettings& rs) override;
void setColor(float r, float g, float b);
void clear();
void add(std::vector<std::vector<Point3>>&);
};
#endif // OUTLINE_H

View File

@@ -6,27 +6,41 @@ Shader::Shader() {
addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, R"(
attribute highp vec3 a_vertex;
attribute highp vec3 a_normal;
attribute lowp vec4 a_color;
uniform highp mat4 M;
uniform highp mat4 V;
uniform highp mat4 P;
varying highp vec3 normal;
varying highp vec3 v_normal;
varying lowp vec4 v_color;
varying lowp vec4 v_vertex;
void main() {
gl_Position = vec4(a_vertex, 1.0);
normal = normalize( V*M*vec4(a_normal, 0.0) );
gl_Position = P * V * M * vec4(a_vertex, 1.0);
v_normal = (V * M * vec4(a_normal, 0.0)).xyz;
v_color = a_color;
v_vertex = V * M * vec4(a_vertex, 1.0);
}
)");
addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, R"(
uniform vec4 color;
varying highp vec3 normal;
uniform bool useNormal;
uniform bool useVertexColor;
varying highp vec3 v_normal;
varying lowp vec4 v_color;
varying lowp vec4 v_vertex;
void main() {
float intensity = dot( normal, normalize(vec3(-1,-1,-3)) );
gl_FragColor.rgb = color.rgb * intensity;
gl_FragColor.a = color.a;
vec3 lightPos = vec3(0,0,0); // camera
//vec3 lightVec = normalize(vec3(0,0,1));
vec3 lightVec = normalize(lightPos - v_vertex.xyz);
float intensity = useNormal ? 0.3 + 0.7 * dot( normalize(v_normal), lightVec ) : 1.0; // light at camera pos
vec4 col = useVertexColor ? v_color : color;
gl_FragColor.rgb = col.rgb * intensity;
gl_FragColor.a = col.a;
}
)");
//bindAttributeLocation("vertices", 0);
if (!link()) {
std::cout << log().toStdString() << std::endl;
throw std::runtime_error("shader link error");
@@ -36,15 +50,25 @@ Shader::Shader() {
void Shader::setModelMatrix(const QMatrix4x4& m) {
//setUniformValue(getUniform("M"), m);
setUniformValue(getUniform("M"), m);
}
void Shader::setViewMatrix(const QMatrix4x4& m) {
//setUniformValue(getUniform("V"), m);
setUniformValue(getUniform("V"), m);
}
void Shader::setProjectionMatrix(const QMatrix4x4& m) {
//setUniformValue(getUniform("P"), m);
setUniformValue(getUniform("P"), m);
}
void Shader::setUseNormals(bool use) {
int loc = getUniform("useNormal");
setUniformValue(loc, use);
}
void Shader::setUseVertexColor(bool use) {
int loc = getUniform("useVertexColor");
setUniformValue(loc, use);
}
int Shader::getUniform(const char* name) {
@@ -52,7 +76,6 @@ int Shader::getUniform(const char* name) {
if (loc == -1) {throw std::runtime_error("error");}
return loc;
}
int Shader::getAttribute(const char* name) {
int loc = attributeLocation(name);
if (loc == -1) {throw std::runtime_error("error");}
@@ -78,11 +101,25 @@ void Shader::unsetVertices() {
}
void Shader::setNormals(const float* values) {
setUseNormals(true);
const int loc = getAttribute("a_normal");
enableAttributeArray(loc);
setAttributeArray(loc, GL_FLOAT, values, 3);
}
void Shader::unsetNormals() {
setUseNormals(false);
const int loc = getAttribute("a_normal");
disableAttributeArray(loc);
}
void Shader::setVertexColor(const float* values) {
setUseVertexColor(true);
const int loc = getAttribute("a_color");
enableAttributeArray(loc);
setAttributeArray(loc, GL_FLOAT, values, 4); // RGBA!!!
}
void Shader::unsetVertexColor() {
setUseVertexColor(false);
const int loc = getAttribute("a_color");
disableAttributeArray(loc);
}

View File

@@ -21,9 +21,14 @@ public:
void setVertices(const float*);
void unsetVertices();
void setUseNormals(bool use);
void setNormals(const float*);
void unsetNormals();
void setVertexColor(const float*);
void unsetVertexColor();
void setUseVertexColor(bool use);
int getUniform(const char*);
int getAttribute(const char*);