worked on android port
opengl1 -> es
This commit is contained in:
23
mapview/3D/misc/Camera.h
Normal file
23
mapview/3D/misc/Camera.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef CAMERA_H
|
||||
#define CAMERA_H
|
||||
|
||||
#include <QMatrix4x4>
|
||||
#include <QMatrix3x3>
|
||||
|
||||
class Camera {
|
||||
|
||||
QMatrix4x4 V;
|
||||
QMatrix4x4 P;
|
||||
|
||||
public:
|
||||
|
||||
Camera() {
|
||||
|
||||
}
|
||||
|
||||
QMatrix4x4 getV() const {return V;}
|
||||
QMatrix4x4 getP() const {return P;}
|
||||
|
||||
};
|
||||
|
||||
#endif // CAMERA_H
|
||||
259
mapview/3D/misc/Cube.cpp
Normal file
259
mapview/3D/misc/Cube.cpp
Normal file
@@ -0,0 +1,259 @@
|
||||
#include "Cube.h"
|
||||
#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, 0,
|
||||
+1, -1, 0,
|
||||
0, +1, 0,
|
||||
|
||||
// -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.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,
|
||||
|
||||
};
|
||||
|
||||
//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.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.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.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.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;
|
||||
|
||||
Cube::Cube(Point3 pos, float size) : pos(pos), size(size,size,size), rot(0,0,0) {
|
||||
|
||||
}
|
||||
|
||||
Cube::Cube(Point3 pos, Point3 size, Point3 rot) : pos(pos), size(size), rot(rot) {
|
||||
|
||||
}
|
||||
|
||||
void Cube::setColor(float r, float g, float b) {
|
||||
this->color = Point3(r,g,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.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);
|
||||
rs.shader->unsetVertices();
|
||||
rs.shader->unsetNormals();
|
||||
|
||||
rs.shader->release();
|
||||
|
||||
};
|
||||
@@ -2,9 +2,11 @@
|
||||
#define CUBE_H
|
||||
|
||||
#include <Indoor/geo/Point3.h>
|
||||
#include <QtOpenGL>
|
||||
#include "Renderable3D.h"
|
||||
|
||||
class Cube {
|
||||
class Shader;
|
||||
|
||||
class Cube : public Renderable3D {
|
||||
|
||||
private:
|
||||
|
||||
@@ -12,20 +14,28 @@ private:
|
||||
Point3 size;
|
||||
Point3 rot;
|
||||
|
||||
Point3 color;
|
||||
|
||||
public:
|
||||
|
||||
Cube(Point3 pos, float size) : pos(pos), size(size,size,size), rot(0,0,0) {
|
||||
Cube(Point3 pos, float size);
|
||||
|
||||
}
|
||||
Cube(Point3 pos, Point3 size, Point3 rot);
|
||||
|
||||
Cube(Point3 pos, Point3 size, Point3 rot) : pos(pos), size(size), rot(rot) {
|
||||
void setColor(float r, float g, float b);
|
||||
|
||||
}
|
||||
void render(const RenderSettings& rs) override;
|
||||
|
||||
void paintGL() {
|
||||
|
||||
float s = 1;
|
||||
|
||||
|
||||
/*
|
||||
|
||||
void paintGL(Shader* shader);
|
||||
|
||||
TODO_GL
|
||||
|
||||
glColor3f(color.x, color.y, color.z);
|
||||
|
||||
glPushMatrix();
|
||||
|
||||
@@ -90,7 +100,7 @@ public:
|
||||
|
||||
glPopMatrix();
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
};
|
||||
|
||||
|
||||
20
mapview/3D/misc/Renderable3D.h
Normal file
20
mapview/3D/misc/Renderable3D.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef RENDERABLE3D_H
|
||||
#define RENDERABLE3D_H
|
||||
|
||||
class Shader;
|
||||
|
||||
struct RenderSettings {
|
||||
Shader* shader = nullptr;
|
||||
RenderSettings(Shader* shader) : shader(shader) {;}
|
||||
};
|
||||
|
||||
|
||||
class Renderable3D {
|
||||
|
||||
public:
|
||||
|
||||
virtual void render(const RenderSettings& rs) = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif // RENDERABLE3D_H
|
||||
88
mapview/3D/misc/Shader.cpp
Normal file
88
mapview/3D/misc/Shader.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#include "Shader.h"
|
||||
#include <iostream>
|
||||
|
||||
Shader::Shader() {
|
||||
|
||||
addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, R"(
|
||||
attribute highp vec3 a_vertex;
|
||||
attribute highp vec3 a_normal;
|
||||
uniform highp mat4 M;
|
||||
uniform highp mat4 V;
|
||||
uniform highp mat4 P;
|
||||
varying highp vec3 normal;
|
||||
void main() {
|
||||
gl_Position = vec4(a_vertex, 1.0);
|
||||
normal = normalize( V*M*vec4(a_normal, 0.0) );
|
||||
}
|
||||
)");
|
||||
|
||||
addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, R"(
|
||||
uniform vec4 color;
|
||||
varying highp vec3 normal;
|
||||
void main() {
|
||||
float intensity = dot( normal, normalize(vec3(-1,-1,-3)) );
|
||||
gl_FragColor.rgb = color.rgb * intensity;
|
||||
gl_FragColor.a = color.a;
|
||||
}
|
||||
)");
|
||||
|
||||
//bindAttributeLocation("vertices", 0);
|
||||
if (!link()) {
|
||||
std::cout << log().toStdString() << std::endl;
|
||||
throw std::runtime_error("shader link error");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Shader::setModelMatrix(const QMatrix4x4& m) {
|
||||
//setUniformValue(getUniform("M"), m);
|
||||
}
|
||||
|
||||
void Shader::setViewMatrix(const QMatrix4x4& m) {
|
||||
//setUniformValue(getUniform("V"), m);
|
||||
}
|
||||
|
||||
void Shader::setProjectionMatrix(const QMatrix4x4& m) {
|
||||
//setUniformValue(getUniform("P"), m);
|
||||
}
|
||||
|
||||
int Shader::getUniform(const char* name) {
|
||||
int loc = uniformLocation(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");}
|
||||
return loc;
|
||||
}
|
||||
|
||||
|
||||
void Shader::setColor(const float r, const float g, const float b) {
|
||||
setUniformValue(getUniform("color"), QVector4D(r,g,b,1));
|
||||
}
|
||||
void Shader::setColor(const float r, const float g, const float b, const float a) {
|
||||
setUniformValue(getUniform("color"), QVector4D(r,g,b,a));
|
||||
}
|
||||
|
||||
void Shader::setVertices(const float* values) {
|
||||
const int loc = getAttribute("a_vertex");
|
||||
enableAttributeArray(loc);
|
||||
setAttributeArray(loc, GL_FLOAT, values, 3);
|
||||
}
|
||||
void Shader::unsetVertices() {
|
||||
const int loc = getAttribute("a_vertex");
|
||||
disableAttributeArray(loc);
|
||||
}
|
||||
|
||||
void Shader::setNormals(const float* values) {
|
||||
const int loc = getAttribute("a_normal");
|
||||
enableAttributeArray(loc);
|
||||
setAttributeArray(loc, GL_FLOAT, values, 3);
|
||||
}
|
||||
void Shader::unsetNormals() {
|
||||
const int loc = getAttribute("a_normal");
|
||||
disableAttributeArray(loc);
|
||||
}
|
||||
33
mapview/3D/misc/Shader.h
Normal file
33
mapview/3D/misc/Shader.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef SHADER_H
|
||||
#define SHADER_H
|
||||
|
||||
#include <QOpenGLShaderProgram>
|
||||
|
||||
class Shader : public QOpenGLShaderProgram {
|
||||
|
||||
public:
|
||||
|
||||
Shader();
|
||||
|
||||
void setModelMatrix(const QMatrix4x4& m);
|
||||
|
||||
void setViewMatrix(const QMatrix4x4& m);
|
||||
|
||||
void setProjectionMatrix(const QMatrix4x4& m);
|
||||
|
||||
void setColor(const float r, const float g, const float b);
|
||||
void setColor(const float r, const float g, const float b, const float a);
|
||||
|
||||
void setVertices(const float*);
|
||||
void unsetVertices();
|
||||
|
||||
void setNormals(const float*);
|
||||
void unsetNormals();
|
||||
|
||||
int getUniform(const char*);
|
||||
int getAttribute(const char*);
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // SHADER_H
|
||||
87
mapview/3D/misc/Window.cpp
Normal file
87
mapview/3D/misc/Window.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "Window.h"
|
||||
#include "Shader.h"
|
||||
|
||||
#include <Indoor/geo/Point3.h>
|
||||
#include <Indoor/math/Math.h>
|
||||
|
||||
static float window_vertices[] = {
|
||||
|
||||
+1, 0, +1,
|
||||
-1, 0, +1,
|
||||
-1, 0, -1,
|
||||
|
||||
-1, 0, -1,
|
||||
+1, 0, -1,
|
||||
+1, 0, +1,
|
||||
|
||||
-1, 0, +1,
|
||||
+1, 0, +1,
|
||||
-1, 0, -1,
|
||||
|
||||
+1, 0, -1,
|
||||
-1, 0, -1,
|
||||
+1, 0, +1,
|
||||
|
||||
};
|
||||
|
||||
static float window_normals[] = {
|
||||
|
||||
0,-1,0,
|
||||
0,-1,0,
|
||||
0,-1,0,
|
||||
|
||||
0,-1,0,
|
||||
0,-1,0,
|
||||
0,-1,0,
|
||||
|
||||
0,+1,0,
|
||||
0,+1,0,
|
||||
0,+1,0,
|
||||
|
||||
0,+1,0,
|
||||
0,+1,0,
|
||||
0,+1,0,
|
||||
|
||||
};
|
||||
|
||||
Window::Window(const Point2 from, const Point2 to, float atHeight, float height) :
|
||||
from(from), to(to), atHeight(atHeight), height(height) {
|
||||
|
||||
const Point2 cen = (from+to)/2;
|
||||
|
||||
const float sx = from.getDistance(to) / 2.0f;
|
||||
const float sz = height / 2.0f;
|
||||
|
||||
const float rad = -std::atan2(to.y - from.y, to.x - from.x);
|
||||
const float deg = rad * 180 / M_PI;
|
||||
|
||||
mat.translate(cen.x, cen.y, atHeight + height/2);
|
||||
mat.rotate(deg, 0, 0, 1);
|
||||
mat.scale(sx, 1, sz);
|
||||
|
||||
}
|
||||
|
||||
void Window::render(const RenderSettings& rs) {
|
||||
|
||||
rs.shader->bind();
|
||||
|
||||
rs.shader->setColor(0.75, 0.85, 1.0, 0.35);
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
//glDisable(GL_CULL_FACE);
|
||||
|
||||
rs.shader->setModelMatrix(mat);
|
||||
|
||||
rs.shader->setVertices(window_vertices);
|
||||
rs.shader->setNormals(window_normals);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 12);
|
||||
rs.shader->unsetVertices();
|
||||
rs.shader->unsetNormals();
|
||||
|
||||
//glEnable(GL_CULL_FACE);
|
||||
glDisable(GL_BLEND);
|
||||
|
||||
rs.shader->release();
|
||||
|
||||
}
|
||||
26
mapview/3D/misc/Window.h
Normal file
26
mapview/3D/misc/Window.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef WINDOW_H
|
||||
#define WINDOW_H
|
||||
|
||||
#include "Renderable3D.h"
|
||||
#include <Indoor/geo/Point2.h>
|
||||
|
||||
#include <QMatrix4x4>
|
||||
|
||||
class Window : public Renderable3D {
|
||||
|
||||
Point2 from;
|
||||
Point2 to;
|
||||
float atHeight;
|
||||
float height;
|
||||
|
||||
QMatrix4x4 mat;
|
||||
|
||||
public:
|
||||
|
||||
Window(const Point2 from, const Point2 to, float atHeight, float height);
|
||||
|
||||
virtual void render(const RenderSettings& rs) override;
|
||||
|
||||
};
|
||||
|
||||
#endif // WINDOW_H
|
||||
Reference in New Issue
Block a user