-
Notifications
You must be signed in to change notification settings - Fork 0
/
osusphere.cpp
198 lines (165 loc) · 5.64 KB
/
osusphere.cpp
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include "vertexbufferobject.cpp"
#include <GL/gl.h>
#ifndef F_PI
#define F_PI ((float)(M_PI))
#define F_2_PI ((float)(2.f*F_PI))
#define F_PI_2 ((float)(F_PI/2.f))
#endif
inline
void
_DrawSphLatLng( float radius, float lat, float lng )
{
// lat is in radians between -F_PI_2 and +F_PI_2
// lng is in radians between -F_PI and +F_PI
float xz = cosf(lat);
float x = xz * sinf(lng);
float y = sinf(lat);
float z = xz * cosf(lng);
float nx = x; // for a *sphere only*, the normal is the unitized position
float ny = y; // for a *sphere only*, the normal is the unitized position
float nz = z; // for a *sphere only*, the normal is the unitized position
float s = ( lng + F_PI ) / F_2_PI;
float t = ( lat + F_PI_2 ) / F_PI;
glTexCoord2f( s, t );
glNormal3f( nx, ny, nz );
glVertex3f( x*radius, y*radius, z*radius );
}
void
OsuSphere( float radius, int slices, int stacks)
{
// sanity check:
radius = (float)fabs(radius);
if( slices < 4 ) slices = 4;
if( stacks < 4 ) stacks = 4;
glBegin( GL_TRIANGLES );
// south pole:
{
int istack = 0;
float north = -F_PI_2 + F_PI * (float)(istack + 1) / (float)stacks;
float south = -F_PI_2 + F_PI * (float)(istack + 0) / (float)stacks;
for (int islice = 0; islice < slices; islice++)
{
float west = -F_PI + F_2_PI * (float)(islice + 0) / (float)slices;
float east = -F_PI + F_2_PI * (float)(islice + 1) / (float)slices;
_DrawSphLatLng( radius, south, .5f * (east + west));
_DrawSphLatLng( radius, north, east);
_DrawSphLatLng( radius, north, west);
}
}
// north pole:
{
int istack = stacks - 1;
float north = -F_PI_2 + F_PI * (float)(istack + 1) / (float)stacks;
float south = -F_PI_2 + F_PI * (float)(istack + 0) / (float)stacks;
for (int islice = 0; islice < slices; islice++)
{
float west = -F_PI + F_2_PI * (float)(islice + 0) / (float)slices;
float east = -F_PI + F_2_PI * (float)(islice + 1) / (float)slices;
_DrawSphLatLng( radius, north, .5f*(east + west) );
_DrawSphLatLng( radius, south, west );
_DrawSphLatLng( radius, south, east );
}
}
// all the bands in between:
for (int istack = 1; istack < stacks-1; istack++)
{
float north = -F_PI_2 + F_PI * (float)(istack + 1) / (float)stacks;
float south = -F_PI_2 + F_PI * (float)(istack + 0) / (float)stacks;
for (int islice = 0; islice < slices; islice++)
{
float west = -F_PI + F_2_PI * (float)(islice + 0) / (float)slices;
float east = -F_PI + F_2_PI * (float)(islice + 1) / (float)slices;
_DrawSphLatLng( radius, north, west );
_DrawSphLatLng( radius, south, west );
_DrawSphLatLng( radius, north, east );
_DrawSphLatLng( radius, north, east );
_DrawSphLatLng( radius, south, west );
_DrawSphLatLng( radius, south, east );
}
}
glEnd( );
}
// -------------- draw sphere via VBO ------------//
inline
void
_DrawSphLatLngVBO(float radius, float lat, float lng, VertexBufferObject* vbo)
{
// lat is in radians between -F_PI_2 and +F_PI_2
// lng is in radians between -F_PI and +F_PI
float xz = cosf(lat);
float x = xz * sinf(lng);
float y = sinf(lat);
float z = xz * cosf(lng);
float nx = x; // for a *sphere only*, the normal is the unitized position
float ny = y; // for a *sphere only*, the normal is the unitized position
float nz = z; // for a *sphere only*, the normal is the unitized position
float s = (lng + F_PI) / F_2_PI;
float t = (lat + F_PI_2) / F_PI;
vbo->glTexCoord2f(s, t);
vbo->glNormal3f(nx, ny, nz);
vbo->glVertex3f(x * radius, y * radius, z * radius);
}
void
OsuSphereVBO(float radius, int slices, int stacks, VertexBufferObject* vbo)
{
// sanity check:
radius = (float)fabs(radius);
if (slices < 4) slices = 4;
if (stacks < 4) stacks = 4;
if (vbo == nullptr) {
fprintf(stderr, "\nVBO is null, cannot draw initial sphere!");
return;
}
vbo->glBegin(GL_TRIANGLES);
// south pole:
{
int istack = 0;
float north = -F_PI_2 + F_PI * (float)(istack + 1) / (float)stacks;
float south = -F_PI_2 + F_PI * (float)(istack + 0) / (float)stacks;
for (int islice = 0; islice < slices; islice++)
{
float west = -F_PI + F_2_PI * (float)(islice + 0) / (float)slices;
float east = -F_PI + F_2_PI * (float)(islice + 1) / (float)slices;
_DrawSphLatLngVBO(radius, south, .5f * (east + west), vbo);
_DrawSphLatLngVBO(radius, north, east, vbo);
_DrawSphLatLngVBO(radius, north, west, vbo);
}
}
// north pole:
{
int istack = stacks - 1;
float north = -F_PI_2 + F_PI * (float)(istack + 1) / (float)stacks;
float south = -F_PI_2 + F_PI * (float)(istack + 0) / (float)stacks;
for (int islice = 0; islice < slices; islice++)
{
float west = -F_PI + F_2_PI * (float)(islice + 0) / (float)slices;
float east = -F_PI + F_2_PI * (float)(islice + 1) / (float)slices;
_DrawSphLatLngVBO(radius, north, .5f * (east + west), vbo);
_DrawSphLatLngVBO(radius, south, west, vbo);
_DrawSphLatLngVBO(radius, south, east, vbo);
}
}
// all the bands in between:
for (int istack = 1; istack < stacks - 1; istack++)
{
float north = -F_PI_2 + F_PI * (float)(istack + 1) / (float)stacks;
float south = -F_PI_2 + F_PI * (float)(istack + 0) / (float)stacks;
for (int islice = 0; islice < slices; islice++)
{
float west = -F_PI + F_2_PI * (float)(islice + 0) / (float)slices;
float east = -F_PI + F_2_PI * (float)(islice + 1) / (float)slices;
_DrawSphLatLngVBO(radius, north, west, vbo);
_DrawSphLatLngVBO(radius, south, west, vbo);
_DrawSphLatLngVBO(radius, north, east, vbo);
_DrawSphLatLngVBO(radius, north, east, vbo);
_DrawSphLatLngVBO(radius, south, west, vbo);
_DrawSphLatLngVBO(radius, south, east, vbo);
}
}
vbo->glEnd();
}