-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphere.cpp
More file actions
108 lines (99 loc) · 2.92 KB
/
Sphere.cpp
File metadata and controls
108 lines (99 loc) · 2.92 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
#include "Sphere.h"
#include <cstdio>
#include <cmath>
#include <glm/gtc/constants.hpp>
Sphere::Sphere(QOpenGLShaderProgram* prog, float radius, GLuint nSlices, GLuint nStacks) : QuadMesh(prog, "Sphere")
{
int nVerts = (nSlices+1) * (nStacks + 1);
int elements = (nSlices * 2 * (nStacks-1) ) * 3;
// Verts
std::vector<GLfloat> p(3 * nVerts);
// Normals
std::vector<GLfloat> n(3 * nVerts);
// Tex coords
std::vector<GLfloat> tex(2 * nVerts);
// Elements
std::vector<GLuint> el(elements);
// Generate positions and normals
GLfloat theta, phi;
GLfloat thetaFac = glm::two_pi<float>() / nSlices;
GLfloat phiFac = glm::pi<float>() / nStacks;
GLfloat nx, ny, nz, s, t;
GLuint idx = 0, tIdx = 0;
for( GLuint i = 0; i <= nSlices; i++ ) {
theta = i * thetaFac;
s = (GLfloat)i / nSlices;
for( GLuint j = 0; j <= nStacks; j++ ) {
phi = j * phiFac;
t = (GLfloat)j / nStacks;
nx = sinf(phi) * cosf(theta);
ny = sinf(phi) * sinf(theta);
nz = cosf(phi);
p[idx] = radius * nx; p[idx+1] = radius * ny; p[idx+2] = radius * nz;
n[idx] = nx; n[idx+1] = ny; n[idx+2] = nz;
idx += 3;
tex[tIdx] = s;
tex[tIdx+1] = t;
tIdx += 2;
}
}
// Generate the element list
idx = 0;
/*for( GLuint i = 0; i < nSlices; i++ ) {
GLuint stackStart = i * (nStacks + 1);
GLuint nextStackStart = (i+1) * (nStacks+1);
for( GLuint j = 0; j < nStacks; j++ ) {
if( j == 0 ) {
el[idx] = stackStart;
el[idx+1] = stackStart + 1;
el[idx+2] = nextStackStart + 1;
idx += 3;
} else if( j == nStacks - 1) {
el[idx] = stackStart + j;
el[idx+1] = stackStart + j + 1;
el[idx+2] = nextStackStart + j;
idx += 3;
} else {
el[idx] = stackStart + j;
el[idx+1] = stackStart + j + 1;
el[idx+2] = nextStackStart + j + 1;
el[idx+3] = nextStackStart + j;
el[idx+4] = stackStart + j;
el[idx+5] = nextStackStart + j + 1;
idx += 6;
}
}
}*/
for (GLuint i = 0; i < nSlices; i++) {
GLuint stackStart = i * (nStacks + 1);
GLuint nextStackStart = (i + 1) * (nStacks + 1);
for (GLuint j = 0; j < nStacks; j++) {
if (j == 0) {
el[idx] = stackStart;
el[idx + 1] = stackStart + 1;
el[idx + 2] = nextStackStart + 1;
el[idx + 3] = stackStart;
idx += 4;
}
else if (j == nStacks - 1) {
el[idx] = stackStart + j;
el[idx + 1] = stackStart + j + 1;
el[idx + 2] = nextStackStart + j;
el[idx + 3] = stackStart + j;
idx += 4;
}
else {
el[idx] = stackStart + j;
el[idx + 1] = stackStart + j + 1;
el[idx + 2] = nextStackStart + j + 1;
el[idx + 3] = nextStackStart + j;
//el[idx + 4] = stackStart + j;
//el[idx + 5] = nextStackStart + j + 1;
idx += 4;
}
}
}
initBuffers(&el, &p, &n, &tex);
_boundingSphere.setCenter(0, 0, 0);
_boundingSphere.setRadius(radius);
}