-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
787f549
commit 854af3d
Showing
3 changed files
with
654 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
#ifndef SOLAR_SYSTEM_SPHERE_HPP | ||
#define SOLAR_SYSTEM_SPHERE_HPP | ||
|
||
|
||
#include <vector> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "../utils.hpp" | ||
|
||
bool generateSphere(float radius, int slices, int stacks, const char* filepath); | ||
|
||
#endif //SOLAR_SYSTEM_SPHERE_HPP | ||
#endif // SOLAR_SYSTEM_SPHERE_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,66 @@ | ||
#include <cmath> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <vector> | ||
#include <cmath> | ||
|
||
#include "utils.hpp" | ||
|
||
std::vector<Point> sphereTriangles(float radius, int slices, int stacks) { | ||
std::vector<Point> points; | ||
|
||
for (int i = 0; i < slices; ++i) { | ||
float theta1 = static_cast<float>(i) * static_cast<float>(M_PI) / static_cast<float>(slices); | ||
float theta2 = static_cast<float>(i + 1) * static_cast<float>(M_PI) / static_cast<float>(slices); | ||
|
||
for (int j = 0; j < stacks; ++j) { | ||
float phi1 = static_cast<float>(j) * 2.0f * static_cast<float>(M_PI) / static_cast<float>(stacks); | ||
float phi2 = static_cast<float>(j + 1) * 2.0f * static_cast<float>(M_PI) / static_cast<float>(stacks); | ||
|
||
// Vertices | ||
float x1 = radius * std::sin(theta1) * std::cos(phi1); | ||
float y1 = radius * std::sin(theta1) * std::sin(phi1); | ||
float z1 = radius * std::cos(theta1); | ||
|
||
float x2 = radius * std::sin(theta1) * std::cos(phi2); | ||
float y2 = radius * std::sin(theta1) * std::sin(phi2); | ||
float z2 = radius * std::cos(theta1); | ||
|
||
float x3 = radius * std::sin(theta2) * std::cos(phi1); | ||
float y3 = radius * std::sin(theta2) * std::sin(phi1); | ||
float z3 = radius * std::cos(theta2); | ||
|
||
float x4 = radius * std::sin(theta2) * std::cos(phi2); | ||
float y4 = radius * std::sin(theta2) * std::sin(phi2); | ||
float z4 = radius * std::cos(theta2); | ||
|
||
// Push vertices in counter-clockwise order | ||
points.push_back(Point(x1, y1, z1)); | ||
points.push_back(Point(x2, y2, z2)); | ||
points.push_back(Point(x4, y4, z4)); | ||
|
||
points.push_back(Point(x1, y1, z1)); | ||
points.push_back(Point(x4, y4, z4)); | ||
points.push_back(Point(x3, y3, z3)); | ||
} | ||
std::vector<Point> points; | ||
|
||
for (int i = 0; i < slices; ++i) { | ||
float theta1 = static_cast<float>(i) * static_cast<float>(M_PI) / | ||
static_cast<float>(slices); | ||
float theta2 = static_cast<float>(i + 1) * static_cast<float>(M_PI) / | ||
static_cast<float>(slices); | ||
|
||
for (int j = 0; j < stacks; ++j) { | ||
float phi1 = static_cast<float>(j) * 2.0f * static_cast<float>(M_PI) / | ||
static_cast<float>(stacks); | ||
float phi2 = static_cast<float>(j + 1) * 2.0f * static_cast<float>(M_PI) / | ||
static_cast<float>(stacks); | ||
|
||
// Vertices | ||
float x1 = radius * std::sin(theta1) * std::cos(phi1); | ||
float y1 = radius * std::sin(theta1) * std::sin(phi1); | ||
float z1 = radius * std::cos(theta1); | ||
|
||
float x2 = radius * std::sin(theta1) * std::cos(phi2); | ||
float y2 = radius * std::sin(theta1) * std::sin(phi2); | ||
float z2 = radius * std::cos(theta1); | ||
|
||
float x3 = radius * std::sin(theta2) * std::cos(phi1); | ||
float y3 = radius * std::sin(theta2) * std::sin(phi1); | ||
float z3 = radius * std::cos(theta2); | ||
|
||
float x4 = radius * std::sin(theta2) * std::cos(phi2); | ||
float y4 = radius * std::sin(theta2) * std::sin(phi2); | ||
float z4 = radius * std::cos(theta2); | ||
|
||
// Push vertices in counter-clockwise order | ||
points.push_back(Point(x1, y1, z1)); | ||
points.push_back(Point(x2, y2, z2)); | ||
points.push_back(Point(x4, y4, z4)); | ||
|
||
points.push_back(Point(x1, y1, z1)); | ||
points.push_back(Point(x4, y4, z4)); | ||
points.push_back(Point(x3, y3, z3)); | ||
} | ||
} | ||
|
||
return points; | ||
return points; | ||
} | ||
|
||
bool generateSphere(float radius, int slices, int stacks, const char* filepath) { | ||
std::vector<Point> triangles = sphereTriangles(radius, slices, stacks); | ||
bool generateSphere(float radius, int slices, int stacks, | ||
const char* filepath) { | ||
std::vector<Point> triangles = sphereTriangles(radius, slices, stacks); | ||
|
||
if (triangles.empty()) { | ||
std::cerr << "Error: Empty vector of triangles.\n"; | ||
return false; | ||
} | ||
if (triangles.empty()) { | ||
std::cerr << "Error: Empty vector of triangles.\n"; | ||
return false; | ||
} | ||
|
||
saveToFile(triangles, filepath); | ||
saveToFile(triangles, filepath); | ||
|
||
return true; | ||
return true; | ||
} |
Oops, something went wrong.