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

chore: cylinder generator #12

Merged
merged 2 commits into from
Mar 5, 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
2 changes: 1 addition & 1 deletion engine/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void renderScene(void) {
// put drawing instructions here
drawAxis();

glPolygonMode(GL_FRONT, GL_LINE);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
for (std::string model : c.models) {
drawFile(model.data());
}
Expand Down
11 changes: 11 additions & 0 deletions generator/include/shapes/cylinder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef CYLINDER_HPP
#define CYLINDER_HPP

#include <string>

#include "cylinder.hpp"

bool generateCylinder(float radius, float height, int slices,
const char* filepath);

#endif // CYLINDER_HPP
1 change: 0 additions & 1 deletion generator/include/shapes/plane.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define SOLAR_SYSTEM_PLANE_HPP

#include <string>
#include <vector>

#include "../utils.hpp"

Expand Down
21 changes: 15 additions & 6 deletions generator/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "../include/shapes/cone.hpp"
#include "../include/shapes/cube.hpp"
#include "../include/shapes/cylinder.hpp"
#include "../include/shapes/plane.hpp"
#include "../include/shapes/sphere.hpp"
#include "../include/shapes/torus.hpp"
Expand All @@ -13,7 +14,7 @@ void generateFigure(int argc, char* argv[]) {
return;
}

char* figureType = argv[argc - 1];
char* fileName = argv[argc - 1];
std::string figureName = argv[1];

if (figureName == "sphere" && argc == 6) {
Expand All @@ -23,22 +24,22 @@ void generateFigure(int argc, char* argv[]) {
int slices = std::stoi(argv[3]);
int stacks = std::stoi(argv[4]);

generateSphere(radius, slices, stacks, figureType);
generateSphere(radius, slices, stacks, fileName);
} else if (figureName == "box" && argc == 5) {
// Generate Box
std::cout << "Generating Box\n";
float length = std::stof(argv[2]);
int divisions = std::stoi(argv[3]);

generateCube(length, divisions, figureType);
generateCube(length, divisions, fileName);
} else if (figureName == "plane" && argc == 5) {
// Generate Plane
std::cout << "Generating Plane\n"; // Added newline here
float length = std::stof(argv[2]);
int divisions = std::stoi(argv[3]);

generatePlane(length, divisions,
figureType); // Assuming saveToFile is available
fileName); // Assuming saveToFile is available
} else if (figureName == "cone" && argc == 7) {
// Generate Cone
std::cout << "Generating Cone\n";
Expand All @@ -47,7 +48,7 @@ void generateFigure(int argc, char* argv[]) {
int slices = std::stoi(argv[4]);
int stacks = std::stoi(argv[5]);

generateCone(radius, height, slices, stacks, figureType);
generateCone(radius, height, slices, stacks, fileName);

} else if (figureName == "torus" && argc == 7) {
// Generate Torus
Expand All @@ -57,7 +58,15 @@ void generateFigure(int argc, char* argv[]) {
int slices = std::stoi(argv[4]);
int stacks = std::stoi(argv[5]);

generateTorus(innerRadius, outerRadius, slices, stacks, figureType);
generateTorus(innerRadius, outerRadius, slices, stacks, fileName);
} else if (figureName == "cylinder" && argc == 6) {
// Generate Cylinder
std::cout << "Generating Cylinder\n";
float radius = std::stof(argv[2]);
float height = std::stof(argv[3]);
int slices = std::stoi(argv[4]);

generateCylinder(radius, height, slices, fileName);
} else {
std::cerr << "Invalid arguments\n";
}
Expand Down
72 changes: 72 additions & 0 deletions generator/src/shapes/cylinder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "../../include/shapes/cylinder.hpp"

#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>

#include "utils.hpp"

std::vector<Point> cylinderTriangles(const float radius, const float height,
const int slices) {
std::vector<Point> vertex;

float alpha = 2 * M_PI / slices;
float half = height / 2;

float r = 0;

for (int i = 0; i < slices; i++) {
// base
float px = radius * sin(r);
float pz = radius * cos(r);
vertex.push_back(Point(px, -half, pz));
vertex.push_back(Point(0, -half, 0));
r += alpha;
px = radius * sin(r);
pz = radius * cos(r);
vertex.push_back(Point(px, -half, pz));

// face
vertex.push_back(Point(px, -half, pz));
vertex.push_back(Point(px, half, pz));
r -= alpha;
px = radius * sin(r);
pz = radius * cos(r);
vertex.push_back(Point(px, half, pz));

vertex.push_back(Point(px, half, pz));
vertex.push_back(Point(px, -half, pz));
r += alpha;
px = radius * sin(r);
pz = radius * cos(r);
vertex.push_back(Point(px, -half, pz));

// topo
r += alpha;
px = radius * sin(r);
pz = radius * cos(r);
vertex.push_back(Point(px, half, pz));
vertex.push_back(Point(0, half, 0));
r -= alpha;
px = radius * sin(r);
pz = radius * cos(r);
vertex.push_back(Point(px, half, pz));
}

return vertex;
}

bool generateCylinder(float radius, float height, int slices,
const char* filepath) {
std::vector<Point> triangles = cylinderTriangles(radius, height, slices);

if (triangles.empty()) {
std::cerr << "Error: Empty vector of triangles.\n";
return false;
}

saveToFile(triangles, filepath);

return true;
}
Loading
Loading