-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphere.cpp
More file actions
162 lines (137 loc) · 6.14 KB
/
Copy pathSphere.cpp
File metadata and controls
162 lines (137 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// Created by tztz8 on 5/16/22.
//
#include "Sphere.h"
//#include <random>
#include <vector>
#include <cstdio>
#include <GL/glew.h>
#define GLM_FORCE_RADIANS
#include <glm/mat4x4.hpp> // glm::mat4
#include <glm/gtc/matrix_transform.hpp> // glm::translate, glm::rotate, glm::scale, glm::perspective
#include "OpenGLHelperMethods.h"
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
#include <spdlog/spdlog.h>
const double kPI = 3.1415926535897932384626433832795;
Sphere::Sphere(int step) :
step(step),
numVertices((step + 1) * (step + 1)),
numTriangles((2 * step * (step - 1))),
numIndices((6 * step * step)),
sphere_vao(0) // setting 0 so it is initialized
{
// TODO: Debug
SPDLOG_DEBUG(spdlog::fmt_lib::format("make using step: {}, numVertices: {}, numTriangles: {}, numIndices: {}, do not forget to call create",
this->step,
this->numVertices,
this->numTriangles,
this->numIndices));
// fprintf(stdout,
// "Debug: Sphere: Class make using isBasket: %s, step: %d, numVertices: %d, numTriangles: %d, numIndices: %d, do not forget to call create\n",
// this->isBasket ? "true" : "false",
// this->step,
// this->numVertices,
// this->numTriangles,
// this->numIndices);
}
void Sphere::create() {
std::vector<GLushort> indices(this->numIndices);
std::vector<glm::vec4> points(this->numVertices);
std::vector<glm::vec3> normals(this->numVertices);
std::vector<glm::vec2> TexCoord(this->numVertices);
double theta = 0.0;
double phi = 0.0;
size_t i = 0;
// Generate Vertices (Points)
for (double b = -this->step / 2.0; b <= this->step / 2.0; b++) {
theta = (1.0 * b / this->step) * kPI;
for (int a = 0; a <= this->step; ++a) {
phi = (1.0 * a / this->step) * 2 * kPI;
points[i] = glm::vec4(
cos(theta) * sin(phi),
sin(theta),
cos(theta) * cos(phi),
1.0
);
TexCoord[i] = glm::vec2(
1.0 * a / this->step,
((sin(theta) / 2.0) + 0.5)
);
i++;
}
}
// TODO: Debug
SPDLOG_DEBUG(spdlog::fmt_lib::format("NumVertices: {}, i: {}", this->numVertices, i));
size_t size_t_numVertices = static_cast<size_t>(this->numVertices);
for (size_t j = 0; j < size_t_numVertices; ++j) {
// Generate Vertices (normal)
normals[j] = glm::normalize(glm::vec3(points[j]));
}
size_t index = 0;
for (int j = 0; j < (this->numVertices-this->step-1); j += this->step + 1) {
for (int k = j; k < (j + this->step); ++k) {
indices[index++] = k;
indices[index++] = k + this->step + 1;
indices[index++] = (k + 1) + this->step + 1;
indices[index++] = k;
indices[index++] = (k + 1) + this->step + 1;
indices[index++] = k + 1;
}
}
std::vector<glm::vec4> tangents(this->numVertices);
updateVertexTangents(points.data(), normals.data(), tangents.data(),
this->numVertices, this->numIndices,
indices.data(), TexCoord.data());
// TODO: Debug
// fprintf(stdout, "Debug: Sphere Class: NumVertices: %d, tangents size: %zu\n", this->numVertices, tangents.size());
// for (int j = 0; j < tangents.size(); ++j) {
// fprintf(stdout, "Debug: Sphere Class: tangents%d: <%f, %f, %f, %f>\n", j,
// tangents[j].x, tangents[j].y, tangents[j].z, tangents[j].w);
// }
// TODO: Debug
SPDLOG_DEBUG(spdlog::fmt_lib::format("NumIndices: {}, index: {}", this->numIndices, index));
glGenVertexArrays(1, &this->sphere_vao);
glBindVertexArray(this->sphere_vao);
unsigned int handle[5];
glGenBuffers(5, handle);
glBindBuffer(GL_ARRAY_BUFFER, handle[0]);
glBufferData(GL_ARRAY_BUFFER, points.size() * sizeof(glm::vec4), points.data(), GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)0, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(0); // Vertex position
glBindBuffer(GL_ARRAY_BUFFER, handle[1]);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), normals.data(), GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(1); // Vertex normal
glBindBuffer(GL_ARRAY_BUFFER, handle[2]);
glBufferData(GL_ARRAY_BUFFER, TexCoord.size() * sizeof(glm::vec2), TexCoord.data(), GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)2, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(2); // Vertex texture
glBindBuffer(GL_ARRAY_BUFFER, handle[3]);
glBufferData(GL_ARRAY_BUFFER, tangents.size() * sizeof(glm::vec4), tangents.data(), GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)3, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(3); // Vertex tangents
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[4]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLushort), indices.data(), GL_STATIC_DRAW);
glBindVertexArray(0);
}
void Sphere::draw() {
glBindVertexArray(this->sphere_vao);
glDrawElements(GL_TRIANGLES, this->numIndices, GL_UNSIGNED_SHORT, 0);
// glDrawArrays(GL_POINTS, 0, NUMVERTICES);
}
void Sphere::updateStep(int step) {
glDeleteVertexArrays(1, &this->sphere_vao);
this->step = step,
this->numVertices = ((step + 1) * (step + 1));
this->numTriangles = ((2 * step * (step - 1)));
this->numIndices = ((6 * step * step));
SPDLOG_DEBUG(spdlog::fmt_lib::format("make using step: {}, numVertices: {}, numTriangles: {}, numIndices: {}, do not forget to call create",
this->step,
this->numVertices,
this->numTriangles,
this->numIndices));
this->create();
}
int Sphere::getStep() {
return this->step;
}