Skip to content

Commit

Permalink
bezier patches normals
Browse files Browse the repository at this point in the history
  • Loading branch information
gramosomi committed May 21, 2024
1 parent 6d6393f commit cddbd93
Show file tree
Hide file tree
Showing 7 changed files with 19,562 additions and 19,363 deletions.
8 changes: 6 additions & 2 deletions common/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ typedef struct Point {
return x == other.x && y == other.y && z == other.z;
}

auto normalize() {
Point normalize() {
float norm = sqrt(x * x + y * y + z * z);
return Point(x / norm, y / norm, z / norm);
}

auto cross(const Point& other) {
Point cross(const Point& other) {
return Point(y * other.z - z * other.y, z * other.x - x * other.z,
x * other.y - y * other.x);
}
Expand All @@ -48,6 +48,10 @@ struct Point2D {
bool operator==(const Point2D& other) const {
return x == other.x && y == other.y;
}

auto cross(const Point2D& other) {
return x * other.y - y * other.x;
}
};

struct PointHash {
Expand Down
2 changes: 2 additions & 0 deletions generator/include/shapes/patches.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ float bernstein(int i, float t);

Point bezierPatch(const std::vector<Point>& controlPoints, float u, float v);

float bernsteinDerivative(int i, float t);

Point calculateNormal(const Point& p1, const Point& p2, const Point& p3);

#endif // PATCHES_HPP
138 changes: 91 additions & 47 deletions generator/src/shapes/cone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "utils.hpp"



std::pair<std::pair<std::vector<Point>, std::vector<Point>>,
std::vector<Point2D>>
coneTriangles(const float radius, const float height, const size_t slices,
Expand All @@ -17,6 +19,9 @@ coneTriangles(const float radius, const float height, const size_t slices,
float sliceAngle = (float) (2 * M_PI) / slices; //angulo interno de cada slice
float stackSize = (float) height / stacks; //diferença de y entre cada stack
float rAux = (float) radius / stacks; //diferença do valor do raio entre stacks
printf("stackSize: %f\n", stackSize);
printf("rAux: %f\n", rAux);
printf("sliceAngle: %f\n", sliceAngle);

//base
for(float a = 0; a < (2 * M_PI); a += sliceAngle){ //divide a base pelo numero de slices
Expand All @@ -26,107 +31,146 @@ coneTriangles(const float radius, const float height, const size_t slices,
float z1 = 0;

//pontos contidos na circunferência da base
float x3 = (float) radius * sin(a);
float x3 = (float) radius * std::sin(a);
float y3 = 0;
float z3 = (float)radius * cos(a);
float z3 = (float)radius * std::cos(a);

float x2 = (float) radius * sin(a + sliceAngle);
float x2 = (float) radius * std::sin(a + sliceAngle);
float y2 = 0;
float z2 =(float) radius * cos(a + sliceAngle);
float z2 =(float) radius * std::cos(a + sliceAngle);

points.push_back(Point(x3, y3, z3));
points.push_back(Point(x1, y1, z1));
points.push_back(Point(x2, y2, z2));

Point normal = {0, -1, 0};
normals.push_back(Point(0, -1, 0));
normals.push_back(Point(0, -1, 0));
normals.push_back(Point(0, -1, 0));

points.push_back({x3, y3, z3});
points.push_back({x1, y1, z1});
points.push_back({x2, y2, z2});
textures.push_back(Point2D(0.5 + 0.5 * sin(a), 0.5 + 0.5 * cos(a)));
textures.push_back(Point2D(0.5, 0.5));
textures.push_back(Point2D(0.5 + 0.5 * sin(a + sliceAngle), 0.5 + 0.5 * cos(a + sliceAngle)));

normals.push_back(normal);
normals.push_back(normal);
normals.push_back(normal);

Point2D texture = {0, 0};
textures.push_back(texture);
textures.push_back(texture);
textures.push_back(texture);

}

//lados

float sliceNormals[slices * 2][3];
int filled = 0;
int count = 0;
for(int i = 0; i<stacks; i++){ //divide a altura pelo numero de stacks
for(float a = 0; a < (2 * M_PI); a += sliceAngle){ //divide cada stack pelo numero de slices
float yBaixo = i * stackSize; //y da margem inferior da stack
float yCima = (i + 1) * stackSize; //y da margem inferior da stack

float rBaixo = (float) radius - (i * rAux); //raio da margem inferior da stack
float rCima = (float) radius - ((i + 1) * rAux); //raio da margem inferior da stack


float coneAngleBaixo = atan(rBaixo/(height-yBaixo));
//float coneAngleCima = atan(rCima/(height-yCima));

printf("a: %f rbaixo: %f\n", a, rBaixo);
float x2 = rBaixo * sin(a);
printf("x2: %f\n", x2);
float y2 = yBaixo;
float z2 = rBaixo * cos(a);
Point normal2 = Point(sin(coneAngleBaixo) * sin(a), sin(coneAngleBaixo), sin(coneAngleBaixo) * cos(a));
normal2.normalize();
Point normal2;
if (filled == 0) { // se for a primeira stack, calcula a normal para os pontos em comum com a base
normal2.x = std::sin(a);
normal2.y = (float)rBaixo / height;
normal2.z = std::cos(a);
normal2.normalize();
sliceNormals[count][0] = normal2.x;
sliceNormals[count][1] = normal2.y;
sliceNormals[count][2] = normal2.z;
} else {
normal2.x = sliceNormals[count][0];
normal2.y = sliceNormals[count][1];
normal2.z = sliceNormals[count][2];
}
count++;

float x5 = rBaixo * sin(a + sliceAngle);
float y5 = yBaixo;
float z5 = rBaixo * cos(a + sliceAngle);
Point normal5 = Point (sin(coneAngleBaixo) * sin(a + sliceAngle), sin(coneAngleBaixo), sin(coneAngleBaixo) * cos(a + sliceAngle));
normal5.normalize();

Point normal5;
if(filled == 0){//se for a primeira stack, calcula a normal para os pontos em comum com a base
normal5.x = sin(a + sliceAngle);
normal5.y = (float) rBaixo / height;
normal5.z = cos(a + sliceAngle);
normal5.normalize();

sliceNormals[count][0] = normal5.x;
sliceNormals[count][1] = normal5.y;
sliceNormals[count][2] = normal5.z;

}
else{
normal5.x = sliceNormals[count][0];
normal5.y = sliceNormals[count][1];
normal5.z = sliceNormals[count][2];
}
count++;

float x4 = rBaixo * sin(a + sliceAngle);
float y4 = yBaixo;
float z4 = rBaixo * cos(a + sliceAngle);
Point normal4 = Point(normal5.x, normal5.y, normal5.z);
normal4.normalize();
Point normal4;
normal4.x = sliceNormals[count-1][0];
normal4.y = sliceNormals[count-1][1];
normal4.z = sliceNormals[count-1][2];

float x1 = rCima * sin(a);
float y1 = yCima;
float z1 = rCima * cos(a);
Point normal1 = Point(normal2.x, normal2.y, normal2.z);
normal1.normalize();

Point normal1;
normal1.x = sliceNormals[count-2][0];
normal1.y = sliceNormals[count-2][1];
normal1.z = sliceNormals[count-2][2];


float x3 = rCima * sin(a);
float y3 = yCima;
float z3 = rCima * cos(a);
Point normal3 = Point(normal2.x, normal2.y, normal2.z);
normal3.normalize();
Point normal3;
normal3.x = sliceNormals[count-2][0];
normal3.y = sliceNormals[count-2][1];
normal3.z = sliceNormals[count-2][2];

float x6 = rCima * sin(a + sliceAngle);
float y6 = yCima;
float z6 = rCima * cos(a + sliceAngle);
Point normal6 = Point(normal5.x, normal5.y, normal5.z);
normal6.normalize();
Point normal6;
normal6.x = sliceNormals[count-1][0];
normal6.y = sliceNormals[count-1][1];
normal6.z = sliceNormals[count-1][2];

points.push_back({x1, y1, z1});
points.push_back({x2, y2, z2});
points.push_back({x5, y5, z5});
points.push_back({x3, y3, z3});
points.push_back({x4, y4, z4});
points.push_back({x6, y6, z6});
points.push_back(Point(x1, y1, z1));
points.push_back(Point(x2, y2, z2));
points.push_back(Point(x5, y5, z5));

points.push_back(Point(x3, y3, z3));
points.push_back(Point(x4, y4, z4));
points.push_back(Point(x6, y6, z6));

normals.push_back(normal1);
normals.push_back(normal2);
normals.push_back(normal5);

normals.push_back(normal3);
normals.push_back(normal4);
normals.push_back(normal6);

Point2D texture1 = {0, 0};
textures.push_back(Point2D(a / (2 * M_PI), yCima / height));
textures.push_back(Point2D(a / (2 * M_PI), yBaixo / height));
textures.push_back(Point2D((a + sliceAngle) / (2 * M_PI), yBaixo / height));

textures.push_back(texture1);
textures.push_back(texture1);
textures.push_back(texture1);
textures.push_back(texture1);
textures.push_back(texture1);
textures.push_back(texture1);
textures.push_back(Point2D(a / (2 * M_PI), yCima / height));
textures.push_back(Point2D((a + sliceAngle) / (2 * M_PI), yBaixo / height));
textures.push_back(Point2D((a + sliceAngle) / (2 * M_PI), yCima / height));

}
filled = 1;
count = 0;
}

return std::make_pair(std::make_pair(points, normals), textures);
Expand Down
Loading

0 comments on commit cddbd93

Please sign in to comment.