worked on 3D model creation

This commit is contained in:
k-a-z-u
2018-02-06 17:34:29 +01:00
parent 0bb1b707de
commit a35e043196
15 changed files with 1442 additions and 1091 deletions

View File

@@ -5,137 +5,141 @@
#include "../../../geo/Triangle3.h"
#include "../../../math/Matrix4.h"
class Cube {
namespace Ray3D {
private:
class Cube {
std::vector<Triangle3> trias;
private:
public:
std::vector<Triangle3> trias;
/** ctor */
Cube() {
unitCube(true);
}
public:
/** ctor with position, size and rotation */
Cube(Point3 pos, Point3 size, Point3 rot_deg, const bool topAndBottom = true) {
unitCube(topAndBottom);
const Matrix4 mRot = Matrix4::getRotationDeg(rot_deg.x, rot_deg.y, rot_deg.z);
const Matrix4 mSize = Matrix4::getScale(size.x, size.y, size.z);
const Matrix4 mPos = Matrix4::getTranslation(pos.x, pos.y, pos.z);
const Matrix4 mat = mPos * mRot * mSize;
transform(mat);
}
/** ctor */
Cube() {
unitCube(true);
}
/** get the cube's triangles */
const std::vector<Triangle3> getTriangles() const {
return trias;
}
/** ctor with position, size and rotation */
Cube(Point3 pos, Point3 size, Point3 rot_deg, const bool topAndBottom = true) {
unitCube(topAndBottom);
const Matrix4 mRot = Matrix4::getRotationDeg(rot_deg.x, rot_deg.y, rot_deg.z);
const Matrix4 mSize = Matrix4::getScale(size.x, size.y, size.z);
const Matrix4 mPos = Matrix4::getTranslation(pos.x, pos.y, pos.z);
const Matrix4 mat = mPos * mRot * mSize;
transform(mat);
}
void transform(const Matrix4& mat) {
/** get the cube's triangles */
const std::vector<Triangle3> getTriangles() const {
return trias;
}
for (Triangle3& tria : trias) {
void transform(const Matrix4& mat) {
Vector4 v1(tria.p1.x, tria.p1.y, tria.p1.z, 1);
Vector4 v2(tria.p2.x, tria.p2.y, tria.p2.z, 1);
Vector4 v3(tria.p3.x, tria.p3.y, tria.p3.z, 1);
for (Triangle3& tria : trias) {
v1 = mat*v1;
v2 = mat*v2;
v3 = mat*v3;
Vector4 v1(tria.p1.x, tria.p1.y, tria.p1.z, 1);
Vector4 v2(tria.p2.x, tria.p2.y, tria.p2.z, 1);
Vector4 v3(tria.p3.x, tria.p3.y, tria.p3.z, 1);
tria.p1 = Point3(v1.x, v1.y, v1.z);
tria.p2 = Point3(v2.x, v2.y, v2.z);
tria.p3 = Point3(v3.x, v3.y, v3.z);
v1 = mat*v1;
v2 = mat*v2;
v3 = mat*v3;
tria.p1 = Point3(v1.x, v1.y, v1.z);
tria.p2 = Point3(v2.x, v2.y, v2.z);
tria.p3 = Point3(v3.x, v3.y, v3.z);
}
}
}
/** get a transformed version */
Cube transformed(const Matrix4& mat) const {
Cube res = *this;
res.transform(mat);
return res;
}
private:
/** build unit-cube faces */
void unitCube(const bool topAndBottom) {
const float s = 1.0f;
// left?
addQuad(
Point3(+s, -s, -s),
Point3(+s, -s, +s),
Point3(-s, -s, +s),
Point3(-s, -s, -s)
);
// right?
addQuad(
Point3(-s, +s, -s),
Point3(-s, +s, +s),
Point3(+s, +s, +s),
Point3(+s, +s, -s)
);
// small side
if (1 == 1) {
// front
addQuad(
Point3(-s, -s, -s),
Point3(-s, -s, +s),
Point3(-s, +s, +s),
Point3(-s, +s, -s)
);
// read
addQuad(
Point3(+s, +s, -s),
Point3(+s, +s, +s),
Point3(+s, -s, +s),
Point3(+s, -s, -s)
);
/** get a transformed version */
Cube transformed(const Matrix4& mat) const {
Cube res = *this;
res.transform(mat);
return res;
}
private:
/** build unit-cube faces */
void unitCube(const bool topAndBottom) {
const float s = 1.0f;
if (topAndBottom) {
// top
addQuad(
Point3(+s, +s, +s),
Point3(-s, +s, +s),
Point3(-s, -s, +s),
Point3(+s, -s, +s)
);
// bottom
// left?
addQuad(
Point3(+s, -s, -s),
Point3(-s, -s, -s),
Point3(+s, -s, +s),
Point3(-s, -s, +s),
Point3(-s, -s, -s)
);
// right?
addQuad(
Point3(-s, +s, -s),
Point3(-s, +s, +s),
Point3(+s, +s, +s),
Point3(+s, +s, -s)
);
// small side
if (1 == 1) {
// front
addQuad(
Point3(-s, -s, -s),
Point3(-s, -s, +s),
Point3(-s, +s, +s),
Point3(-s, +s, -s)
);
// read
addQuad(
Point3(+s, +s, -s),
Point3(+s, +s, +s),
Point3(+s, -s, +s),
Point3(+s, -s, -s)
);
}
if (topAndBottom) {
// top
addQuad(
Point3(+s, +s, +s),
Point3(-s, +s, +s),
Point3(-s, -s, +s),
Point3(+s, -s, +s)
);
// bottom
addQuad(
Point3(+s, -s, -s),
Point3(-s, -s, -s),
Point3(-s, +s, -s),
Point3(+s, +s, -s)
);
}
}
}
void addQuad(Point3 p1, Point3 p2, Point3 p3, Point3 p4) {
trias.push_back( Triangle3(p1,p2,p3) );
trias.push_back( Triangle3(p1,p3,p4) );
}
void addQuad(Point3 p1, Point3 p2, Point3 p3, Point3 p4) {
trias.push_back( Triangle3(p1,p2,p3) );
trias.push_back( Triangle3(p1,p3,p4) );
}
};
};
}
#endif // QUBE_H