-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sphere.h
277 lines (241 loc) · 10.4 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
/*
* Aidan Fike and Russell Parker
* Comp 175 (Graphics) Homework 1
* February 11, 2018
*
* Sphere.h - Declaration and implementation of a class that is able to render
* a sphere onto the user's screen, as well as normals at each vertex of the
* sphere.
*/
#ifndef SPHERE_H
#define SPHERE_H
#include "Shape.h"
#define RADIUS 0.5
class Sphere : public Shape {
public:
// Create a default sphere - assume no user input for m_segmentsX/Y is
// given yet
Sphere() {
curr_segmentsX = 10;
curr_segmentsY = 10;
};
~Sphere() {};
/*
* This function will go through all the vertices calculated for the sphere
* and draw triangles between them in such a way that a fully filled sphere
* is rendered. If either m_segmentX or m_segment& changed since the last
* time this function was called, it will recalculate the vertices needed
* to draw the cube using the new values of m_segmentsX/Y
*/
void draw() {
//Recalculate vertices for the cube if m_segmentsX or Y changed since
//the last iteration of this function
if (m_segmentsX != curr_segmentsX || m_segmentsY != curr_segmentsY) {
barrelVertices.clear();
capVertices.clear();
barrelVertices.initVertexList(m_segmentsX * (m_segmentsY - 1));
capVertices.initVertexList((m_segmentsX + 2) * 2);
calcVertices();
curr_segmentsX = m_segmentsX;
curr_segmentsY = m_segmentsY;
}
// Loop through the vertices of the sphere and create the corresponding
// triangles needed to fill the surface excluding the caps of the
// sphere (The "north and south poles").
for (int y = 0; y < m_segmentsY - 2; y++) {
glBegin(GL_TRIANGLE_STRIP);
for (int x = 0; x < m_segmentsX; x++) {
Vertex currVertex;
barrelVertices.getIndex(y * (m_segmentsX) + x, &currVertex);
glNormal3f(currVertex.normal[0],
currVertex.normal[1],
currVertex.normal[2]);
glVertex3f(currVertex.point[0],
currVertex.point[1],
currVertex.point[2]);
/*
* After drawing a point, draw the point "underneath" it before
* returning ot the original level. This is needed to draw a
* triangle with height
*/
barrelVertices.getIndex(((y + 1) * (m_segmentsX)) + x,
&currVertex);
glNormal3f(currVertex.normal[0],
currVertex.normal[1],
currVertex.normal[2]);
glVertex3f(currVertex.point[0],
currVertex.point[1],
currVertex.point[2]);
/* At the final iteration of this loop, it is necessary to
* redraw the first points drawn. This is done to close the
* loop of the current stack. */
if (x == m_segmentsX - 1) {
barrelVertices.getIndex((y * (m_segmentsX)), &currVertex);
glNormal3f(currVertex.normal[0],
currVertex.normal[1],
currVertex.normal[2]);
glVertex3f(currVertex.point[0],
currVertex.point[1],
currVertex.point[2]);
barrelVertices.getIndex(((y + 1) * (m_segmentsX)),
&currVertex);
glNormal3f(currVertex.normal[0],
currVertex.normal[1],
currVertex.normal[2]);
glVertex3f(currVertex.point[0],
currVertex.point[1],
currVertex.point[2]);
}
}
glEnd();
}
/*
* Draw the "caps" (the "north and south poles") of the sphere. This is
* done using triangle fanning - first specifying the center of the
* cap, then the points surrounding it.
*/
for (int i = 0; i < 2; i++) {
glBegin(GL_TRIANGLE_FAN);
for (int j = 0; j < m_segmentsX + 2; j++) {
Vertex currVertex;
capVertices.getIndex((i * (m_segmentsX + 2)) + j, &currVertex);
glNormal3f(currVertex.normal[0],
currVertex.normal[1],
currVertex.normal[2]);
glVertex3f(currVertex.point[0],
currVertex.point[1],
currVertex.point[2]);
}
glEnd();
}
};
/*
* This function will go through all vertices calculate for the sphere and
* draw a line from each of them in the direction of the vertex's normal.
* If either m_segmentX or m_segment?Y changed since the last time this
* function was called, it will recalculate the vertices needed to draw the
* cube using the new values of m_segmentsX/Y
*/
void drawNormal() {
//Recalcuate vertices for the cube if m_segmentsX or Y changed since
//the last iteration
if (m_segmentsX != curr_segmentsX || m_segmentsY != curr_segmentsY) {
barrelVertices.clear();
capVertices.clear();
barrelVertices.initVertexList(m_segmentsX * (m_segmentsY - 1));
capVertices.initVertexList((m_segmentsX + 2) * 2);
calcVertices();
curr_segmentsX = m_segmentsX;
curr_segmentsY = m_segmentsY;
}
//Go through each vertex of the sphere excluding the sphere's "caps"
//and draw the appropriate normals at those points
glBegin(GL_LINES);
for(int i = 0; i < (m_segmentsX + 2) * 2; i++) {
Vertex currVertex;
capVertices.getIndex(i, &currVertex);
glVertex3f(currVertex.point[0],
currVertex.point[1],
currVertex.point[2]);
glVertex3f(currVertex.point[0] + (NORMAL_LEN * currVertex.normal[0]),
currVertex.point[1] + (NORMAL_LEN * currVertex.normal[1]),
currVertex.point[2] + (NORMAL_LEN * currVertex.normal[2]));
}
glEnd();
//Go through each vertex of the sphere's cap and draw the appropriate
//normals at those points
glBegin(GL_LINES);
for (int i = 0; i < m_segmentsX * (m_segmentsY - 1); i++) {
Vertex currVertex;
barrelVertices.getIndex(i, &currVertex);
glVertex3f(currVertex.point[0],
currVertex.point[1],
currVertex.point[2]);
glVertex3f(currVertex.point[0] + (NORMAL_LEN * currVertex.normal[0]),
currVertex.point[1] + (NORMAL_LEN * currVertex.normal[1]),
currVertex.point[2] + (NORMAL_LEN * currVertex.normal[2]));
}
glEnd();
};
private:
VertexList barrelVertices, capVertices;
//These variables hold m_segmentX and m_segmentY values that
//were used to calculate the current vertices in vertexList
int curr_segmentsX, curr_segmentsY;
/*
* This function will use the parameters m_segmentsX and m_segmentsY to
* calculate equidistant vertices on the surface of the sphere with a
* radius of 0.5. m_segmentsX/Y determine the angular distance betwen each
* of the vertices calculated. Normal vectors corresponding to each of
* these vertices will be calculated and stored alongside the position data
* itself in the barrelVertices and capVertices structures
*/
void calcVertices() {
unsigned i = 0; //Record the number of vertices that have been added
for (int y = 1; y < m_segmentsY; y++) {
float phi = PI * (y / (float)m_segmentsY);
for (int x = 0; x < m_segmentsX; x++) {
float theta = x * (2 * PI) / (float)m_segmentsX;
//Using m_segmentsY to calculate increments of phi, and
//m_segmentsX to calculate increments of theta, use the
//conversion from cylindrical coordinates to cartesian
//coordinates to find the appriopriate position and normal of
//the vertex in cartesian space
Point p(RADIUS * sin(phi) * cos(theta),
RADIUS * cos(phi),
RADIUS * sin(phi) * sin(theta));
Vector v(sin(phi) * cos(theta),
cos(phi),
sin(phi) * sin(theta));
Vertex vert(p, v);
barrelVertices.setIndex(i, vert);
i++;
}
}
// Calculate vertices for the cap of the sphere
//Vertex at the very top of the sphere
i = 0;
Point p(0, 0.5, 0);
Vector v(0, 1, 0);
Vertex vert(p, v);
capVertices.setIndex(i, vert);
i++;
//Vertices surrounding the top of sphere
for (int x = 0; x < m_segmentsX; x++) {
barrelVertices.getIndex(x, &vert);
capVertices.setIndex(i, vert);
i++;
//Duplicate the initial vertex to complete a circle around the top
//of the sphere
if (x == m_segmentsX - 1) {
barrelVertices.getIndex(0, &vert);
vert.normal = v;
capVertices.setIndex(i, vert);
i++;
}
}
//Vertex at the very bottom of the sphere
p[1] = -0.5;
v[1] = -1.0;
vert.point = p;
vert.normal = v;
capVertices.setIndex(i, vert);
i++;
//Vertices surrounding the bottom of the sphere
for (int x = 0; x < m_segmentsX; x++) {
barrelVertices.getIndex(x + (m_segmentsX * (m_segmentsY - 2)), &vert);
vert.normal = v;
capVertices.setIndex(i, vert);
i++;
//Duplicate the initial vertex to complete a circle around the
//bottom of the sphere
if (x == m_segmentsX - 1) {
barrelVertices.getIndex(m_segmentsX *(m_segmentsY - 2), &vert);
vert.normal = v;
capVertices.setIndex(i, vert);
i++;
}
}
}
};
#endif