feat: Part 1
Some checks failed
CMake / build (zip, zip, [self-hosted Linux], normals, obj, quad_subdivision, ux) (push) Has been cancelled
CMake / build (zip, zip, [self-hosted Windows], Release/normals.exe, Release/obj.exe, Release/quad_subdivision.exe, win) (push) Has been cancelled

Without sphere implementation
This commit is contained in:
Tibo De Peuter 2024-11-08 14:55:32 +01:00
parent 56144466fd
commit fa6881fb01
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
2 changed files with 97 additions and 11 deletions

View file

@ -8,15 +8,67 @@ void cube(
Eigen::MatrixXd & NV,
Eigen::MatrixXi & NF)
{
////////////////////////////////////////////////////////////////////////////
// Add your code here:
////////////////////////////////////////////////////////////////////////////
// Construct default Blender cube.
////Hint:
// V.resize(8,3);
// F.resize(6,4);
// UV.resize(14,2);
// UF.resize(6,4);
// NV.resize(6,3);
// NF.resize(6,4);
V.resize(8,3);
F.resize(6,4);
// for (int i = 0; i < 8; i++) {
// V(i,0) = 1 - (i&4) / 2;
// V(i,1) = 1 - 2 * (i&1);
// V(i,2) = -1 + 1 * (i&2);
// V.row(i) = Eigen::RowVector3d(1 - (i&4) / 2, 1 - 2 * (i&1), -1 + 1 * (i&2));
/* Add Vertex to the faces it belongs to */
// F(0 + (i&4) / 2, i % 4) = i;
// F(1 + 2 * (i&1), i / 2) = i;
// F(5 - (i&2) / 2, (i % 4) % 2 + (i / 4) * 2) = i;
// }
V.row(0) = Eigen::RowVector3d( 1, 1, -1);
V.row(1) = Eigen::RowVector3d( 1, -1, -1);
V.row(2) = Eigen::RowVector3d( 1, 1, 1);
V.row(3) = Eigen::RowVector3d( 1, -1, 1);
V.row(4) = Eigen::RowVector3d(-1, 1, -1);
V.row(5) = Eigen::RowVector3d(-1, -1, -1);
V.row(6) = Eigen::RowVector3d(-1, 1, 1);
V.row(7) = Eigen::RowVector3d(-1, -1, 1);
F.row(0) = Eigen::RowVector4i(1, 2, 4, 3);
F.row(1) = Eigen::RowVector4i(5, 1, 3, 7);
F.row(2) = Eigen::RowVector4i(6, 5, 7, 8);
F.row(3) = Eigen::RowVector4i(2, 6, 8, 4);
F.row(4) = Eigen::RowVector4i(3, 4, 8, 7);
F.row(5) = Eigen::RowVector4i(5, 6, 2, 1);
NV.resize(6,3);
NV.row(0) = Eigen::RowVector3d(1, 0, 0);
NV.row(1) = Eigen::RowVector3d(0, 1, 0);
NV.row(2) = Eigen::RowVector3d(-1, 0, 0);
NV.row(3) = Eigen::RowVector3d(0, -1, 0);
NV.row(4) = Eigen::RowVector3d(0, 0, 1);
NV.row(5) = Eigen::RowVector3d(0, 0, -1);
NF.resize(6,4);
for (int i = 1; i <= 6; i++) {
NF.row(i - 1) = Eigen::RowVector4i(i, i, i, i);
}
UV.resize(14,2);
UV.row(0) = Eigen::RowVector2d(0.25, 0);
UV.row(1) = Eigen::RowVector2d(0.5, 0);
for (int i = 0; i < 5; i++) {
UV.row(2 + i) = Eigen::RowVector2d(0.25 * i, 0.25);
UV.row(7 + i) = Eigen::RowVector2d(0.25 * i, 0.5);
}
UV.row(12) = Eigen::RowVector2d(0.25, 0.75);
UV.row(13) = Eigen::RowVector2d(0.5, 0.75);
UF.resize(6,4);
UF.row(0) = Eigen::RowVector4i(4, 5, 10, 9);
UF.row(1) = Eigen::RowVector4i(5, 6, 11, 10);
UF.row(2) = Eigen::RowVector4i(6, 7, 12, 11);
UF.row(3) = Eigen::RowVector4i(3, 4, 9, 8);
UF.row(4) = Eigen::RowVector4i(9, 10, 14, 13);
UF.row(5) = Eigen::RowVector4i(1, 2, 5, 4);
}

View file

@ -16,5 +16,39 @@ bool write_obj(
printf("ERROR in write_obj: F must have 3 or 4 columns");
exit(-1);
}
return false;
/* Write to file */
std::ofstream out(filename.c_str());
if (!out) {
printf("ERROR in write_obj: can't write to %s\n", filename.c_str());
return false;
}
/* Geometric vertices .*/
for (int i = 0; i < V.rows(); i++) {
out << "v " << V(i, 0) << " " << V(i, 1) << " " << V(i, 2) << std::endl;
}
/* Vertex normals */
for (int i = 0; i < NV.rows(); i++) {
out << "vn " << NV(i, 0) << " " << NV(i, 1) << " " << NV(i, 2) << std::endl;
}
/* Vertex texture coordinates */
for (int i = 0; i < UV.rows(); i++) {
out << "vt " << UV(i, 0) << " " << UV(i, 1) << std::endl;
}
/* Face elements */
for (int i = 0; i < F.rows(); i++) {
out << "f";
for (int j = 0; j < F.cols(); j++) {
out << " " << F(i, j) << "/" << UF(i, j) << "/" << NF(i, j);
}
out << std::endl;
}
out.close();
return true;
}