-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphere.h
322 lines (269 loc) · 10.2 KB
/
sphere.h
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//
// sphere.h
// test
//
// Created by Nazirul Hasan on 26/9/23.
//
#ifndef sphere_h
#define sphere_h
#include <glad/glad.h>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader.h"
# define PI 3.1416
using namespace std;
const int MIN_SECTOR_COUNT = 3;
const int MIN_STACK_COUNT = 2;
class Sphere
{
public:
glm::vec3 ambient;
glm::vec3 diffuse;
glm::vec3 specular;
float shininess;
// ctor/dtor
Sphere(float radius = 1.0f, int sectorCount = 36, int stackCount = 18, glm::vec3 amb = glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3 diff = glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3 spec = glm::vec3(0.5f, 0.5f, 0.5f), float shiny = 32.0f) : verticesStride(24)
{
set(radius, sectorCount, stackCount, amb, diff, spec, shiny);
buildCoordinatesAndIndices();
buildVertices();
glGenVertexArrays(1, &sphereVAO);
glBindVertexArray(sphereVAO);
// create VBO to copy vertex data to VBO
unsigned int sphereVBO;
glGenBuffers(1, &sphereVBO);
glBindBuffer(GL_ARRAY_BUFFER, sphereVBO); // for vertex data
glBufferData(GL_ARRAY_BUFFER, // target
this->getVertexSize(), // data size, # of bytes
this->getVertices(), // ptr to vertex data
GL_STATIC_DRAW); // usage
// create EBO to copy index data
unsigned int sphereEBO;
glGenBuffers(1, &sphereEBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sphereEBO); // for index data
glBufferData(GL_ELEMENT_ARRAY_BUFFER, // target
this->getIndexSize(), // data size, # of bytes
this->getIndices(), // ptr to index data
GL_STATIC_DRAW); // usage
// activate attrib arrays
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
// set attrib arrays with stride and offset
int stride = this->getVerticesStride(); // should be 24 bytes
glVertexAttribPointer(0, 3, GL_FLOAT, false, stride, (void*)0);
glVertexAttribPointer(1, 3, GL_FLOAT, false, stride, (void*)(sizeof(float) * 3));
// unbind VAO and VBOs
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
~Sphere() {}
// getters/setters
void set(float radius, int sectors, int stacks, glm::vec3 amb, glm::vec3 diff, glm::vec3 spec, float shiny)
{
if (radius > 0)
this->radius = radius;
this->sectorCount = sectors;
if (sectors < MIN_SECTOR_COUNT)
this->sectorCount = MIN_SECTOR_COUNT;
this->stackCount = stacks;
if (stacks < MIN_STACK_COUNT)
this->stackCount = MIN_STACK_COUNT;
this->ambient = amb;
this->diffuse = diff;
this->specular = spec;
this->shininess = shiny;
}
void setRadius(float radius)
{
if (radius != this->radius)
set(radius, sectorCount, stackCount, ambient, diffuse, specular, shininess);
}
void setSectorCount(int sectors)
{
if (sectors != this->sectorCount)
set(radius, sectors, stackCount, ambient, diffuse, specular, shininess);
}
void setStackCount(int stacks)
{
if (stacks != this->stackCount)
set(radius, sectorCount, stacks, ambient, diffuse, specular, shininess);
}
// for interleaved vertices
unsigned int getVertexCount() const
{
return (unsigned int)coordinates.size() / 3; // # of vertices
}
unsigned int getVertexSize() const
{
return (unsigned int)vertices.size() * sizeof(float); // # of bytes
}
int getVerticesStride() const
{
return verticesStride; // should be 24 bytes
}
const float* getVertices() const
{
return vertices.data();
}
unsigned int getIndexSize() const
{
return (unsigned int)indices.size() * sizeof(unsigned int);
}
const unsigned int* getIndices() const
{
return indices.data();
}
unsigned int getIndexCount() const
{
return (unsigned int)indices.size();
}
// draw in VertexArray mode
void drawSphere(Shader& lightingShader, glm::mat4 model) const // draw surface
{
lightingShader.use();
lightingShader.setVec3("material.ambient", this->ambient);
lightingShader.setVec3("material.diffuse", this->diffuse);
lightingShader.setVec3("material.specular", this->specular);
lightingShader.setFloat("material.shininess", this->shininess);
lightingShader.setMat4("model", model);
// draw a sphere with VAO
glBindVertexArray(sphereVAO);
glDrawElements(GL_TRIANGLES, // primitive type
this->getIndexCount(), // # of indices
GL_UNSIGNED_INT, // data type
(void*)0); // offset to indices
// unbind VAO
glBindVertexArray(0);
}
private:
// member functions
void buildCoordinatesAndIndices()
{
float x, y, z, xz; // vertex position
float nx, ny, nz, lengthInv = 1.0f / radius; // vertex normal
float sectorStep = 2 * PI / sectorCount;
float stackStep = PI / stackCount;
float sectorAngle = -sectorStep;
float stackAngle = PI / 2 + stackStep;
for (int i = 0; i <= stackCount; ++i)
{
stackAngle -= stackStep; // starting from pi/2 to -pi/2
xz = radius * cosf(stackAngle);
y = radius * sinf(stackAngle);
// add (sectorCount+1) vertices per stack
// first and last vertices have same position and normal, but different tex coords
for (int j = 0; j <= sectorCount; ++j)
{
sectorAngle += sectorStep; // starting from 0 to 2pi
// vertex position (x, y, z)
z = xz * cosf(sectorAngle);
x = xz * sinf(sectorAngle);
coordinates.push_back(x);
coordinates.push_back(y);
coordinates.push_back(z);
// normalized vertex normal (nx, ny, nz)
nx = x * lengthInv;
ny = y * lengthInv;
nz = z * lengthInv;
normals.push_back(nx);
normals.push_back(ny);
normals.push_back(nz);
}
}
// generate index list of sphere triangles
// k1--k1+1
// | / |
// | / |
// k2--k2+1
int k1, k2;
for (int i = 0; i < stackCount; ++i)
{
k1 = i * (sectorCount + 1); // beginning of current stack
k2 = k1 + sectorCount + 1; // beginning of next stack
for (int j = 0; j < sectorCount; ++j, ++k1, ++k2)
{
// 2 triangles per sector excluding first and last stacks
if (i != 0 && i != (stackCount - 1))
{
// k1 => k2 => k1+1
indices.push_back(k1);
indices.push_back(k2);
indices.push_back(k1 + 1);
// k1+1 => k2 => k2+1
indices.push_back(k1 + 1);
indices.push_back(k2);
indices.push_back(k2 + 1);
}
// 2 triangles per sector excluding first and last stacks
else if (i == 0)
{
indices.push_back(k1 + 1);
indices.push_back(k2);
indices.push_back(k2 + 1);
}
else if (i == (stackCount - 1))
{
indices.push_back(k1);
indices.push_back(k2);
indices.push_back(k1 + 1);
}
}
}
}
void buildVertices()
{
size_t i, j;
size_t count = coordinates.size();
for (i = 0, j = 0; i < count; i += 3, j += 2)
{
vertices.push_back(coordinates[i]);
vertices.push_back(coordinates[i + 1]);
vertices.push_back(coordinates[i + 2]);
vertices.push_back(normals[i]);
vertices.push_back(normals[i + 1]);
vertices.push_back(normals[i + 2]);
}
}
vector<float> computeFaceNormal(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3)
{
const float EPSILON = 0.000001f;
vector<float> normal(3, 0.0f); // default return value (0,0,0)
float nx, ny, nz;
// find 2 edge vectors: v1-v2, v1-v3
float ex1 = x2 - x1;
float ey1 = y2 - y1;
float ez1 = z2 - z1;
float ex2 = x3 - x1;
float ey2 = y3 - y1;
float ez2 = z3 - z1;
// cross product: e1 x e2
nx = ey1 * ez2 - ez1 * ey2;
ny = ez1 * ex2 - ex1 * ez2;
nz = ex1 * ey2 - ey1 * ex2;
// normalize only if the length is > 0
float length = sqrtf(nx * nx + ny * ny + nz * nz);
if (length > EPSILON)
{
// normalize
float lengthInv = 1.0f / length;
normal[0] = nx * lengthInv;
normal[1] = ny * lengthInv;
normal[2] = nz * lengthInv;
}
return normal;
}
// memeber vars
unsigned int sphereVAO;
float radius;
int sectorCount; // longitude, # of slices
int stackCount; // latitude, # of stacks
vector<float> vertices;
vector<float> normals;
vector<unsigned int> indices;
vector<float> coordinates;
int verticesStride; // # of bytes to hop to the next vertex (should be 24 bytes)
};
#endif /* sphere_h */