refactoring

wireframe display
worked on 3d-model display
This commit is contained in:
2018-02-08 21:38:51 +01:00
parent bce771d6d6
commit 839401edb7
16 changed files with 395 additions and 236 deletions

View File

@@ -7,56 +7,71 @@
class RenderTriangle {
std::vector<float> vertices;
std::vector<float> normals;
std::vector<float> rgba;
struct Vertex{
float x,y,z;
Vertex(const float x, const float y, const float z) : x(x), y(y), z(z) {;}
} __attribute__((packed));
struct Normal {
float x,y,z;
Normal(const float x, const float y, const float z) : x(x), y(y), z(z) {;}
} __attribute__((packed));
struct RGBA {
float r,g,b,a;
RGBA(const float r, const float g, const float b, const float a) : r(r), g(g), b(b), a(a) {;}
} __attribute__((packed));
std::vector<Vertex> vertices;
std::vector<Normal> normals;
std::vector<RGBA> rgba;
public:
void addTriangle(Point3 p1, Point3 p2, Point3 p3) {
vertices.insert(vertices.end(), {p1.x, p1.y, p1.z});
vertices.insert(vertices.end(), {p2.x, p2.y, p2.z});
vertices.insert(vertices.end(), {p3.x, p3.y, p3.z});
vertices.push_back(Vertex(p1.x, p1.y, p1.z));
vertices.push_back(Vertex(p2.x, p2.y, p2.z));
vertices.push_back(Vertex(p3.x, p3.y, p3.z));
}
void addTriangle(Point3 p1, Point3 p2, Point3 p3, const Point3 n) {
vertices.insert(vertices.end(), {p1.x, p1.y, p1.z});
vertices.insert(vertices.end(), {p2.x, p2.y, p2.z});
vertices.insert(vertices.end(), {p3.x, p3.y, p3.z});
vertices.push_back(Vertex(p1.x, p1.y, p1.z));
vertices.push_back(Vertex(p2.x, p2.y, p2.z));
vertices.push_back(Vertex(p3.x, p3.y, p3.z));
normals.insert(normals.end(), {n.x, n.y, n.z});
normals.insert(normals.end(), {n.x, n.y, n.z});
normals.insert(normals.end(), {n.x, n.y, n.z});
normals.push_back(Normal(n.x, n.y, n.z));
normals.push_back(Normal(n.x, n.y, n.z));
normals.push_back(Normal(n.x, n.y, n.z));
}
void addTriangle(Point3 p1, Point3 p2, Point3 p3, const Point3 n, const float r, const float g, const float b, const float a) {
vertices.insert(vertices.end(), {p1.x, p1.y, p1.z});
vertices.insert(vertices.end(), {p2.x, p2.y, p2.z});
vertices.insert(vertices.end(), {p3.x, p3.y, p3.z});
vertices.push_back(Vertex(p1.x, p1.y, p1.z));
vertices.push_back(Vertex(p2.x, p2.y, p2.z));
vertices.push_back(Vertex(p3.x, p3.y, p3.z));
normals.insert(normals.end(), {n.x, n.y, n.z});
normals.insert(normals.end(), {n.x, n.y, n.z});
normals.insert(normals.end(), {n.x, n.y, n.z});
normals.push_back(Normal(n.x, n.y, n.z));
normals.push_back(Normal(n.x, n.y, n.z));
normals.push_back(Normal(n.x, n.y, n.z));
rgba.insert(rgba.end(), {r,g,b,a});
rgba.insert(rgba.end(), {r,g,b,a});
rgba.insert(rgba.end(), {r,g,b,a});
rgba.push_back(RGBA(r,g,b,a));
rgba.push_back(RGBA(r,g,b,a));
rgba.push_back(RGBA(r,g,b,a));
}
void addLine(Point3 p1, Point3 p2, const float r, const float g, const float b, const float a) {
vertices.insert(vertices.end(), {p1.x, p1.y, p1.z});
vertices.insert(vertices.end(), {p2.x, p2.y, p2.z});
vertices.push_back( Vertex(p1.x, p1.y, p1.z));
vertices.push_back(Vertex(p2.x, p2.y, p2.z));
rgba.insert(rgba.end(), {r,g,b,a});
rgba.insert(rgba.end(), {r,g,b,a});
rgba.push_back( RGBA(r,g,b,a));
rgba.push_back(RGBA(r,g,b,a));
}
@@ -66,11 +81,49 @@ public:
rgba.clear();
}
const std::vector<float>& getVertices() const {return vertices;}
const float* getVertices() const {return (float*) vertices.data();}
const std::vector<float>& getNormals() const {return normals;}
const float* getNormals() const {return (float*) normals.data();}
const std::vector<float>& getRGBA() const {return rgba;}
const float* getRGBA() const {return (float*) rgba.data();}
size_t count() const {return vertices.size();}
RenderTriangle toWireframe(const bool withVertexColors) const {
RenderTriangle res;
for (size_t i = 0; i < vertices.size(); i += 3) {
res.vertices.push_back(vertices[i+0]);
res.vertices.push_back(vertices[i+1]);
res.vertices.push_back(vertices[i+1]);
res.vertices.push_back(vertices[i+2]);
res.vertices.push_back(vertices[i+2]);
res.vertices.push_back(vertices[i+0]);
}
if (withVertexColors) {
for (size_t i = 0; i < rgba.size(); i += 3) {
res.rgba.push_back(rgba[i+0]);
res.rgba.push_back(rgba[i+1]);
res.rgba.push_back(rgba[i+1]);
res.rgba.push_back(rgba[i+2]);
res.rgba.push_back(rgba[i+2]);
res.rgba.push_back(rgba[i+0]);
}
}
return res;
}
};