132 lines
3.0 KiB
C++
132 lines
3.0 KiB
C++
#ifndef RENDERTRIANGLE_H
|
|
#define RENDERTRIANGLE_H
|
|
|
|
|
|
#include <vector>
|
|
#include <Indoor/geo/Point3.h>
|
|
|
|
class RenderTriangle {
|
|
|
|
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.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.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.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.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.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.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.push_back( Vertex(p1.x, p1.y, p1.z));
|
|
vertices.push_back(Vertex(p2.x, p2.y, p2.z));
|
|
|
|
rgba.push_back( RGBA(r,g,b,a));
|
|
rgba.push_back(RGBA(r,g,b,a));
|
|
|
|
}
|
|
|
|
void clear() {
|
|
vertices.clear();
|
|
normals.clear();
|
|
rgba.clear();
|
|
}
|
|
|
|
const float* getVertices() const {return (float*) vertices.data();}
|
|
|
|
const float* getNormals() const {return (float*) normals.data();}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
#endif // RENDERTRIANGLE_H
|