engine/test/src/meshgen.cpp

155 lines
5.3 KiB
C++
Raw Normal View History

2022-11-07 20:15:26 +00:00
#include "meshgen.hpp"
#include "resources/mesh.hpp"
2022-11-07 20:15:26 +00:00
#include <glm/gtc/constants.hpp>
#include <glm/ext.hpp>
#include <glm/trigonometric.hpp>
2023-01-06 16:45:39 +00:00
#include <stdexcept>
2022-11-07 20:15:26 +00:00
2023-01-06 16:45:39 +00:00
std::unique_ptr<engine::resources::Mesh> genSphereMesh(engine::GFXDevice* gfx, float r, int detail, bool windInside, bool flipNormals)
2022-11-07 20:15:26 +00:00
{
using namespace glm;
2023-01-06 16:45:39 +00:00
std::vector<engine::Vertex> vertices{};
2022-11-07 20:15:26 +00:00
float angleStep = two_pi<float>() / (float)detail;
for (int i = 0; i < detail; i++) {
// theta goes north-to-south
float theta = i * angleStep;
float theta2 = theta + angleStep;
for (int j = 0; j < detail/2; j++) {
// phi goes west-to-east
float phi = j * angleStep;
float phi2 = phi + angleStep;
vec3 top_left{ r * sin(phi) * cos(theta),
r * cos(phi),
r * sin(phi) * sin(theta) };
vec3 bottom_left{ r * sin(phi) * cos(theta2),
r * cos(phi),
r * sin(phi) * sin(theta2) };
vec3 top_right{ r * sin(phi2) * cos(theta),
r * cos(phi2),
r * sin(phi2) * sin(theta) };
vec3 bottom_right{ r * sin(phi2) * cos(theta2),
r * cos(phi2),
r * sin(phi2) * sin(theta2) };
if (windInside == false) {
// tris are visible from outside the sphere
// triangle 1
vertices.push_back({ top_left, {}, {0.0f, 0.0f} });
vertices.push_back({ bottom_left, {}, {0.0f, 1.0f} });
vertices.push_back({ bottom_right, {}, {1.0f, 1.0f} });
// triangle 2
vertices.push_back({ top_right, {}, {1.0f, 0.0f} });
vertices.push_back({ top_left, {}, {0.0f, 0.0f} });
vertices.push_back({ bottom_right, {}, {1.0f, 1.0f} });
}
else {
// tris are visible from inside the sphere
// triangle 1
vertices.push_back({ bottom_right, {}, {1.0f, 1.0f} });
vertices.push_back({ bottom_left, {}, {0.0f, 1.0f} });
vertices.push_back({ top_left, {}, {0.0f, 0.0f} });
// triangle 2
vertices.push_back({ bottom_right, {}, {1.0f, 1.0f} });
vertices.push_back({ top_left, {}, {0.0f, 0.0f} });
vertices.push_back({ top_right, {}, {1.0f, 0.0f} });
}
2023-01-06 16:45:39 +00:00
vec3 vector1 = (vertices.end() - 1)->pos - (vertices.end() - 2)->pos;
vec3 vector2 = (vertices.end() - 2)->pos - (vertices.end() - 3)->pos;
2023-01-26 21:54:20 +00:00
vec3 norm = normalize(cross(vector2, vector1));
2022-11-07 20:15:26 +00:00
2023-01-26 21:54:20 +00:00
// NORMALS HAVE BEEN FIXED
2022-11-07 20:15:26 +00:00
2023-01-06 16:45:39 +00:00
if (flipNormals)
norm = -norm;
2023-01-16 11:37:46 +00:00
if (j == (detail / 2) - 1)
norm = -norm;
2022-11-07 20:15:26 +00:00
for (auto it = vertices.end() - 6; it != vertices.end(); it++) {
it->norm = norm;
}
}
}
2023-01-06 16:45:39 +00:00
return std::make_unique<engine::resources::Mesh>(gfx, vertices);
2022-11-07 20:15:26 +00:00
}
2023-01-06 16:45:39 +00:00
std::unique_ptr<engine::resources::Mesh> genCuboidMesh(engine::GFXDevice* gfx, float x, float y, float z)
2022-11-07 20:15:26 +00:00
{
// x goes ->
// y goes ^
// z goes into the screen
2023-01-06 16:45:39 +00:00
using namespace glm;
std::vector<engine::Vertex> v{};
2023-01-16 11:37:46 +00:00
const float tiling = 128.0f;
2023-01-06 16:45:39 +00:00
// front
2023-01-16 11:37:46 +00:00
v.push_back({{x, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {tiling, 0.0f}});
2023-01-06 16:45:39 +00:00
v.push_back({{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}});
2023-01-16 11:37:46 +00:00
v.push_back({{0.0f, y, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, tiling}});
v.push_back({{x, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {tiling, 0.0f}});
v.push_back({{0.0f, y, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, tiling}});
v.push_back({{x, y, 0.0f}, {0.0f, 0.0f, -1.0f}, {tiling, tiling}});
2023-01-06 16:45:39 +00:00
// back
v.push_back({{0.0f, 0.0f, z}, {0.0f, 0.0f, 1.0f}, {0.0f, 0.0f}});
2023-01-16 11:37:46 +00:00
v.push_back({{x, 0.0f, z}, {0.0f, 0.0f, 1.0f}, {tiling, 0.0f}});
v.push_back({{0.0f, y, z}, {0.0f, 0.0f, 1.0f}, {0.0f, tiling}});
v.push_back({{x, 0.0f, z}, {0.0f, 0.0f, 1.0f}, {tiling, 0.0f}});
v.push_back({{x, y, z}, {0.0f, 0.0f, 1.0f}, {tiling, tiling}});
v.push_back({{0.0f, y, z}, {0.0f, 0.0f, 1.0f}, {0.0f, tiling}});
2023-01-06 16:45:39 +00:00
// left
v.push_back({{0.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}});
2023-01-16 11:37:46 +00:00
v.push_back({{0.0f, 0.0f, x}, {-1.0f, 0.0f, 0.0f}, {0.0f, tiling}});
v.push_back({{0.0f, y, 0.0f}, {-1.0f, 0.0f, 0.0f}, {tiling, 0.0f}});
v.push_back({{0.0f, 0.0f, x}, {-1.0f, 0.0f, 0.0f}, {0.0f, tiling}});
v.push_back({{0.0f, y, x}, {-1.0f, 0.0f, 0.0f}, {tiling, tiling}});
v.push_back({{0.0f, y, 0.0f}, {-1.0f, 0.0f, 0.0f}, {tiling, 0.0f}});
2023-01-06 16:45:39 +00:00
// right
2023-01-16 11:37:46 +00:00
v.push_back({{x, y, 0.0f}, {1.0f, 0.0f, 0.0f}, {tiling, 0.0f}});
v.push_back({{x, 0.0f, x}, {1.0f, 0.0f, 0.0f}, {0.0f, tiling}});
2023-01-06 16:45:39 +00:00
v.push_back({{x, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}});
2023-01-16 11:37:46 +00:00
v.push_back({{x, y, 0.0f}, {1.0f, 0.0f, 0.0f}, {tiling, 0.0f}});
v.push_back({{x, y, x}, {1.0f, 0.0f, 0.0f}, {tiling, tiling}});
v.push_back({{x, 0.0f, x}, {1.0f, 0.0f, 0.0f}, {0.0f, tiling}});
2023-01-06 16:45:39 +00:00
// bottom
2023-01-16 11:37:46 +00:00
v.push_back({{0.0f, 0.0f, z}, {0.0f, -1.0f, 0.0f}, {0.0f, tiling}});
2023-01-06 16:45:39 +00:00
v.push_back({{0.0f, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f}});
2023-01-16 11:37:46 +00:00
v.push_back({{x, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {tiling, 0.0f}});
v.push_back({{x, 0.0f, z}, {0.0f, -1.0f, 0.0f}, {tiling, tiling}});
v.push_back({{0.0f, 0.0f, z}, {0.0f, -1.0f, 0.0f}, {0.0f, tiling}});
v.push_back({{x, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {tiling, 0.0f}});
2023-01-06 16:45:39 +00:00
// top
2023-01-16 11:37:46 +00:00
v.push_back({{x, y, 0.0f}, {0.0f, 1.0f, 0.0f}, {tiling, 0.0f}});
2023-01-06 16:45:39 +00:00
v.push_back({{0.0f, y, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}});
2023-01-16 11:37:46 +00:00
v.push_back({{0.0f, y, z}, {0.0f, 1.0f, 0.0f}, {0.0f, tiling}});
v.push_back({{x, y, 0.0f}, {0.0f, 1.0f, 0.0f}, {tiling, 0.0f}});
v.push_back({{0.0f, y, z}, {0.0f, 1.0f, 0.0f}, {0.0f, tiling}});
v.push_back({{x, y, z}, {0.0f, 1.0f, 0.0f}, {tiling, tiling}});
2023-01-06 16:45:39 +00:00
return std::make_unique<engine::resources::Mesh>(gfx, v);
2022-11-07 20:15:26 +00:00
}