Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Textures #36

Merged
merged 2 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
289 changes: 145 additions & 144 deletions generator/src/shapes/cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,161 +5,162 @@
#include "save3d.hpp"
#include "utils.hpp"

std::pair<std::pair<std::vector<Point>, std::vector<Point>>,
std::vector<Point2D>>
cubeAllPoints(float length, int divisions) {
float halfSize = length / 2.0f;
float step = length / divisions;

std::vector<Point> points;
std::vector<Point> normals;
std::vector<Point2D> textures;

for (int i = 0; i < divisions; ++i) {
for (int j = 0; j < divisions; ++j) {
float v1 = -halfSize + i * step;
float u1 = -halfSize + j * step;
float v2 = v1 + step;
float u2 = u1 + step;

// Front Face
points.push_back(Point(v1, u1, halfSize));
points.push_back(Point(v2, u1, halfSize));
points.push_back(Point(v1, u2, halfSize));

points.push_back(Point(v1, u2, halfSize));
points.push_back(Point(v2, u1, halfSize));
points.push_back(Point(v2, u2, halfSize));

Point n1 = Point(0.0f, 0.0f, 1.0f);
for (int i = 0; i < 6; i++) {
normals.push_back(n1);
}

textures.push_back(Point2D(u1 / length, v1 / length));
textures.push_back(Point2D(u2 / length, v1 / length));
textures.push_back(Point2D(u1 / length, v2 / length));
textures.push_back(Point2D(u1 / length, v2 / length));
textures.push_back(Point2D(u2 / length, v1 / length));
textures.push_back(Point2D(u2 / length, v2 / length));

// Back Face
points.push_back(Point(v1, u1, -halfSize));
points.push_back(Point(v1, u2, -halfSize));
points.push_back(Point(v2, u1, -halfSize));

points.push_back(Point(v1, u2, -halfSize));
points.push_back(Point(v2, u2, -halfSize));
points.push_back(Point(v2, u1, -halfSize));

Point n2 = Point(0.0f, 0.0f, -1.0f);
for (int i = 0; i < 6; i++) {
normals.push_back(n2);
}

textures.push_back(Point2D(1.0f - u1 / length, v1 / length));
textures.push_back(Point2D(1.0f - u1 / length, v2 / length));
textures.push_back(Point2D(1.0f - u2 / length, v1 / length));
textures.push_back(Point2D(1.0f - u1 / length, v2 / length));
textures.push_back(Point2D(1.0f - u2 / length, v2 / length));
textures.push_back(Point2D(1.0f - u2 / length, v1 / length));

// Left Face
points.push_back(Point(-halfSize, v1, u1));
points.push_back(Point(-halfSize, v1, u2));
points.push_back(Point(-halfSize, v2, u1));

points.push_back(Point(-halfSize, v1, u2));
points.push_back(Point(-halfSize, v2, u2));
points.push_back(Point(-halfSize, v2, u1));

Point n3 = Point(-1.0f, 0.0f, 0.0f);
for (int i = 0; i < 6; i++) {
normals.push_back(n3);
}

textures.push_back(Point2D(v1 / length, 1.0f - u1 / length));
textures.push_back(Point2D(v2 / length, 1.0f - u1 / length));
textures.push_back(Point2D(v1 / length, 1.0f - u2 / length));
textures.push_back(Point2D(v1 / length, 1.0f - u2 / length));
textures.push_back(Point2D(v2 / length, 1.0f - u1 / length));
textures.push_back(Point2D(v2 / length, 1.0f - u2 / length));

// Right Face
points.push_back(Point(halfSize, v1, u1));
points.push_back(Point(halfSize, v2, u1));
points.push_back(Point(halfSize, v1, u2));

points.push_back(Point(halfSize, v1, u2));
points.push_back(Point(halfSize, v2, u1));
points.push_back(Point(halfSize, v2, u2));

Point n4 = Point(1.0f, 0.0f, 0.0f);
for (int i = 0; i < 6; i++) {
normals.push_back(n4);
}

textures.push_back(Point2D(v1 / length, u1 / length));
textures.push_back(Point2D(v2 / length, u1 / length));
textures.push_back(Point2D(v1 / length, u2 / length));
textures.push_back(Point2D(v1 / length, u2 / length));
textures.push_back(Point2D(v2 / length, u1 / length));
textures.push_back(Point2D(v2 / length, u2 / length));

// Top Face
points.push_back(Point(v1, halfSize, u1));
points.push_back(Point(v1, halfSize, u2));
points.push_back(Point(v2, halfSize, u1));

points.push_back(Point(v1, halfSize, u2));
points.push_back(Point(v2, halfSize, u2));
points.push_back(Point(v2, halfSize, u1));

Point n5 = Point(0.0f, 1.0f, 0.0f);
for (int i = 0; i < 6; i++) {
normals.push_back(n5);
}

textures.push_back(Point2D(u1 / length, 1.0f - v1 / length));
textures.push_back(Point2D(u2 / length, 1.0f - v1 / length));
textures.push_back(Point2D(u1 / length, 1.0f - v2 / length));
textures.push_back(Point2D(u1 / length, 1.0f - v2 / length));
textures.push_back(Point2D(u2 / length, 1.0f - v1 / length));
textures.push_back(Point2D(u2 / length, 1.0f - v2 / length));

// Bottom Face
points.push_back(Point(v1, -halfSize, u1));
points.push_back(Point(v2, -halfSize, u1));
points.push_back(Point(v1, -halfSize, u2));

points.push_back(Point(v1, -halfSize, u2));
points.push_back(Point(v2, -halfSize, u1));
points.push_back(Point(v2, -halfSize, u2));

Point n6 = Point(0.0f, -1.0f, 0.0f);
for (int i = 0; i < 6; i++) {
normals.push_back(n6);
}

textures.push_back(Point2D(u1 / length, v1 / length));
textures.push_back(Point2D(u2 / length, v1 / length));
textures.push_back(Point2D(u1 / length, v2 / length));
textures.push_back(Point2D(u1 / length, v2 / length));
textures.push_back(Point2D(u2 / length, v1 / length));
textures.push_back(Point2D(u2 / length, v2 / length));
std::pair<std::pair<std::vector<Point>, std::vector<Point>>, std::vector<Point2D>> cubeAllPoints(float length, int divisions) {
float halfSize = length / 2.0f;
float step = length / divisions;

std::vector<Point> points;
std::vector<Point> normals;
std::vector<Point2D> textures;

for (int i = 0; i < divisions; ++i) {
for (int j = 0; j < divisions; ++j) {
float v1 = -halfSize + i * step;
float u1 = -halfSize + j * step;
float v2 = v1 + step;
float u2 = u1 + step;

// Front Face
points.push_back(Point(v1, u1, halfSize));
points.push_back(Point(v2, u1, halfSize));
points.push_back(Point(v1, u2, halfSize));

points.push_back(Point(v1, u2, halfSize));
points.push_back(Point(v2, u1, halfSize));
points.push_back(Point(v2, u2, halfSize));

Point n1 = Point(0.0f, 0.0f, 1.0f);
for (int k = 0; k < 6; k++) {
normals.push_back(n1);
}

textures.push_back(Point2D((v1 + halfSize) / length, (u1 + halfSize) / length));
textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length));
textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length));
textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length));
textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length));
textures.push_back(Point2D((v2 + halfSize) / length, (u2 + halfSize) / length));

// Back Face
points.push_back(Point(v1, u1, -halfSize));
points.push_back(Point(v1, u2, -halfSize));
points.push_back(Point(v2, u1, -halfSize));

points.push_back(Point(v1, u2, -halfSize));
points.push_back(Point(v2, u2, -halfSize));
points.push_back(Point(v2, u1, -halfSize));

Point n2 = Point(0.0f, 0.0f, -1.0f);
for (int k = 0; k < 6; k++) {
normals.push_back(n2);
}

textures.push_back(Point2D((v1 + halfSize) / length, (u1 + halfSize) / length));
textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length));
textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length));
textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length));
textures.push_back(Point2D((v2 + halfSize) / length, (u2 + halfSize) / length));
textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length));

// Left Face
points.push_back(Point(-halfSize, v1, u1));
points.push_back(Point(-halfSize, v1, u2));
points.push_back(Point(-halfSize, v2, u1));

points.push_back(Point(-halfSize, v1, u2));
points.push_back(Point(-halfSize, v2, u2));
points.push_back(Point(-halfSize, v2, u1));

Point n3 = Point(-1.0f, 0.0f, 0.0f);
for (int k = 0; k < 6; k++) {
normals.push_back(n3);
}

textures.push_back(Point2D((i + 0.0f) / divisions, (j + 0.0f) / divisions));
textures.push_back(Point2D((i + 1.0f) / divisions, (j + 0.0f) / divisions));
textures.push_back(Point2D((i + 0.0f) / divisions, (j + 1.0f) / divisions));
textures.push_back(Point2D((i + 1.0f) / divisions, (j + 0.0f) / divisions));
textures.push_back(Point2D((i + 1.0f) / divisions, (j + 1.0f) / divisions));
textures.push_back(Point2D((i + 0.0f) / divisions, (j + 1.0f) / divisions));


// Right Face
points.push_back(Point(halfSize, v1, u1));
points.push_back(Point(halfSize, v2, u1));
points.push_back(Point(halfSize, v1, u2));

points.push_back(Point(halfSize, v1, u2));
points.push_back(Point(halfSize, v2, u1));
points.push_back(Point(halfSize, v2, u2));

Point n4 = Point(1.0f, 0.0f, 0.0f);
for (int k = 0; k < 6; k++) {
normals.push_back(n4);
}

textures.push_back(Point2D((u1 + halfSize) / length, (v1 + halfSize) / length));
textures.push_back(Point2D((u2 + halfSize) / length, (v1 + halfSize) / length));
textures.push_back(Point2D((u1 + halfSize) / length, (v2 + halfSize) / length));
textures.push_back(Point2D((u1 + halfSize) / length, (v2 + halfSize) / length));
textures.push_back(Point2D((u2 + halfSize) / length, (v1 + halfSize) / length));
textures.push_back(Point2D((u2 + halfSize) / length, (v2 + halfSize) / length));

// Top Face
points.push_back(Point(v1, halfSize, u1));
points.push_back(Point(v1, halfSize, u2));
points.push_back(Point(v2, halfSize, u1));

points.push_back(Point(v1, halfSize, u2));
points.push_back(Point(v2, halfSize, u2));
points.push_back(Point(v2, halfSize, u1));

Point n5 = Point(0.0f, 1.0f, 0.0f);
for (int k = 0; k < 6; k++) {
normals.push_back(n5);
}

textures.push_back(Point2D((v1 + halfSize) / length, (u1 + halfSize) / length));
textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length));
textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length));
textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length));
textures.push_back(Point2D((v2 + halfSize) / length, (u2 + halfSize) / length));
textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length));

// Bottom Face
points.push_back(Point(v1, -halfSize, u1));
points.push_back(Point(v2, -halfSize, u1));
points.push_back(Point(v1, -halfSize, u2));

points.push_back(Point(v1, -halfSize, u2));
points.push_back(Point(v2, -halfSize, u1));
points.push_back(Point(v2, -halfSize, u2));

Point n6 = Point(0.0f, -1.0f, 0.0f);
for (int k = 0; k < 6; k++) {
normals.push_back(n6);
}

textures.push_back(Point2D((v1 + halfSize) / length, (u1 + halfSize) / length));
textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length));
textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length));
textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length));
textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length));
textures.push_back(Point2D((v2 + halfSize) / length, (u2 + halfSize) / length));
}
}
}

return std::pair(std::pair(points, normals), textures);
return std::pair(std::pair(points, normals), textures);
}


bool generateCube(float length, int divisions, const char* filepath,
bool advanced) {
std::pair<std::pair<std::vector<Point>, std::vector<Point>>,
std::vector<Point2D>>
cube = cubeAllPoints(length, divisions);

if (advanced) {
printf("Saving advanced cube to %s\n", filepath);
save3DAdvancedfile(cube.first.first, cube.first.second, cube.second,
filepath);
} else {
Expand Down
33 changes: 17 additions & 16 deletions generator/src/shapes/sphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,23 @@ sphereAllPoints(float radius, int slices, int stacks) {
normals.push_back(p3.normalize());
normals.push_back(p4.normalize());

float u1 = static_cast<float>(i) / static_cast<float>(slices);
float u2 = static_cast<float>(i + 1) / static_cast<float>(slices);
float v1 = static_cast<float>(j) / static_cast<float>(stacks);
float v2 = static_cast<float>(j + 1) / static_cast<float>(stacks);

Point2D t1(u1, v1);
Point2D t2(u2, v1);
Point2D t3(u1, v2);
Point2D t4(u2, v2);

textures.push_back(t1);
textures.push_back(t4);
textures.push_back(t2);
textures.push_back(t1);
textures.push_back(t3);
textures.push_back(t4);
// Texture coordinates
float u1 = phi1 / (2.0f * static_cast<float>(M_PI));
float u2 = phi2 / (2.0f * static_cast<float>(M_PI));
float v1 = theta1 / static_cast<float>(M_PI);
float v2 = theta2 / static_cast<float>(M_PI);

Point2D t1(u1, v1);
Point2D t2(u2, v1);
Point2D t3(u1, v2);
Point2D t4(u2, v2);

textures.push_back(t1);
textures.push_back(t4);
textures.push_back(t2);
textures.push_back(t1);
textures.push_back(t3);
textures.push_back(t4);
}
}

Expand Down
Loading
Loading