487a9b49
David Mayerich
added the ability...
|
1
2
3
|
#ifndef STIM_GL_SPHARMONICS_H
#define STIM_GL_SPHARMONICS_H
|
01707489
David Mayerich
implemented a new...
|
4
|
#include <stim/math/spharmonics.h>
|
487a9b49
David Mayerich
added the ability...
|
5
|
#include <stim/gl/error.h>
|
01707489
David Mayerich
implemented a new...
|
6
7
|
#include <stim/math/vec3.h>
#include <stim/math/constants.h>
|
487a9b49
David Mayerich
added the ability...
|
8
|
#include <stim/visualization/colormap.h>
|
487a9b49
David Mayerich
added the ability...
|
9
|
|
01707489
David Mayerich
implemented a new...
|
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
|
namespace stim {
template<typename T>
class gl_spharmonics {
GLuint dlist;
GLuint tex;
bool displacement;
bool colormap;
bool magnitude;
void init_tex() {
T* sfunc = (T*)malloc(N * N * sizeof(T)); //create a 2D array to store the spherical function
Sc.get_func(sfunc, N, N); //generate the spherical function based on the Sc coefficients
unsigned char* tex_buffer = (unsigned char*)malloc(3 * N * N); //create a buffer to store the texture map
stim::cpu2cpu<T>(sfunc, tex_buffer, N * N, stim::cmBrewer); //create a Brewer colormap based on the spherical function
stim::buffer2image(tex_buffer, "sfunc.ppm", N, N);
if (tex) glDeleteTextures(1, &tex); //if a texture already exists, delete it
glGenTextures(1, &tex); //create a new texture and store the ID
glBindTexture(GL_TEXTURE_2D, tex); //bind the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, N, N, 0, GL_RGB, GL_UNSIGNED_BYTE, tex_buffer); //copy the color data from the buffer to the GPU
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //initialize all of the texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
|
487a9b49
David Mayerich
added the ability...
|
39
|
|
01707489
David Mayerich
implemented a new...
|
40
41
42
43
44
45
46
47
48
49
50
51
52
|
public:
stim::spharmonics<T> Sc; //spherical harmonic representing the color component
stim::spharmonics<T> Sd; //spherical harmonic representing the displacement component
size_t N;
gl_spharmonics(size_t slices) {
N = slices;
dlist = 0; //initialize the display list index to zero (no list)
tex = 0; //initialize the texture index to zero (no texture)
displacement = true;
colormap = true;
magnitude = true;
}
|
487a9b49
David Mayerich
added the ability...
|
53
|
|
48081b3b
Sebastian Berisha
fixed the culling...
|
54
55
56
57
58
59
60
|
gl_spharmonics(stim::spharmonics<T> disp, stim::spharmonics<T> color, size_t slices) :
gl_spharmonics<T>(slices)
{
Sc = color;
Sd = disp;
}
|
01707489
David Mayerich
implemented a new...
|
61
62
63
|
~gl_spharmonics() {
if (dlist) glDeleteLists(dlist, 1); //delete the display list when the object is destroyed
}
|
487a9b49
David Mayerich
added the ability...
|
64
|
|
7f1ab3c2
Pavel Govyadinov
fixed problems wi...
|
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
|
///saves the coeffients of a spherical harmonics in a text documents.
void save_coeffs(std::string filename)
{
std::ofstream myfile;
myfile.open(filename.c_str());
for(int i = 0; i < Sd.C.size()-1; i++)
{
myfile << Sd.C[i] << ",";
}
myfile << Sd.C[Sd.C.size()-1] << std::endl;
for(int i = 0; i < Sc.C.size()-1; i++)
{
myfile << Sc.C[i] << ",";
}
myfile << Sc.C[Sc.C.size()-1] << std::endl;
myfile.close();
}
void save(std::string filename)
{
if (!tex) {
init_tex();
}
stim::obj<T> object;
size_t theta_i, phi_i;
T d_theta = (T)stim::TAU / (T)N;
T d_phi = (T)stim::PI / (T)(N-1);
object.matKd("sfunc.jpg");
for (phi_i = 1; phi_i < N; phi_i++) {
T phi = phi_i * d_phi;
object.Begin(OBJ_TRIANGLE_STRIP);
for (theta_i = 0; theta_i <= N; theta_i++) {
T theta = (N - theta_i) * d_theta;
float theta_t = 1 - (float)theta_i / (float)N;
T r;
if (!displacement) r = 1; //if no displacement, set the r value to 1 (renders a unit sphere)
else r = Sd(theta, phi); //otherwise calculate the displacement value
glColor3f(1.0f, 1.0f, 1.0f);
if (!colormap) { //if no colormap is being rendered
if (r < 0) glColor3f(1.0, 0.0, 0.0); //if r is negative, render it red
else glColor3f(0.0, 1.0, 0.0); //otherwise render in green
}
if (magnitude) { //if the magnitude is being displaced, calculate the magnitude of r
if (r < 0) r = -r;
}
stim::vec3<T> s(r, theta, phi);
stim::vec3<T> c = s.sph2cart();
stim::vec3<T> n; //allocate a value to store the normal
if (!displacement) n = c; //if there is no displacement, the normal is spherical
else n = Sd.dphi(theta, phi).cross(Sd.dtheta(theta, phi)); //otherwise calculate the normal as the cross product of derivatives
object.TexCoord(theta_t, (float)phi_i / (float)N);
//std::cout << theta_t <<" "<<(float)phi_i / (float)N << "----------------";
object.Normal(n[0], n[1], n[2]);
object.Vertex(c[0], c[1], c[2]);
T r1;
if (!displacement) r1 = 1;
else r1 = Sd(theta, phi - d_phi);
if (!colormap) { //if no colormap is being rendered
if (r1 < 0) glColor3f(1.0, 0.0, 0.0); //if r1 is negative, render it red
else glColor3f(0.0, 1.0, 0.0); //otherwise render in green
}
if (magnitude) { //if the magnitude is being rendered, calculate the magnitude of r
if (r1 < 0) r1 = -r1;
}
stim::vec3<T> s1(r1, theta, phi - d_phi);
stim::vec3<T> c1 = s1.sph2cart();
stim::vec3<T> n1;
if (!displacement) n1 = c1;
else n1 = Sd.dphi(theta, phi - d_phi).cross(Sd.dtheta(theta, phi - d_phi));
//std::cout << theta_t << " " << (float)(phi_i - 1) / (float)N << std::endl;
object.TexCoord(theta_t, 1.0/(2*(N)) + (float)(phi_i-1) / (float)N);
object.Normal(n1[0], n1[1], n1[2]);
object.Vertex(c1[0], c1[1], c1[2]);
}
object.End();
}
object.matKd();
object.save(filename);
}
|
01707489
David Mayerich
implemented a new...
|
154
155
156
157
158
|
/// This function renders the spherical harmonic to the current OpenGL context
void render() {
//glShadeModel(GL_FLAT);
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_CULL_FACE);
|
48081b3b
Sebastian Berisha
fixed the culling...
|
159
160
|
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
|
01707489
David Mayerich
implemented a new...
|
161
162
163
164
|
if (!tex) {
init_tex();
}
|
487a9b49
David Mayerich
added the ability...
|
165
|
|
01707489
David Mayerich
implemented a new...
|
166
167
168
169
|
if (colormap) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
}
|
487a9b49
David Mayerich
added the ability...
|
170
|
|
01707489
David Mayerich
implemented a new...
|
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
|
if (!dlist) {
dlist = glGenLists(1);
glNewList(dlist, GL_COMPILE);
glPushAttrib(GL_ENABLE_BIT);
glEnable(GL_NORMALIZE);
//Draw the Sphere
size_t theta_i, phi_i;
T d_theta = (T)stim::TAU / (T)N;
T d_phi = (T)stim::PI / (T)(N-1);
for (phi_i = 1; phi_i < N; phi_i++) {
T phi = phi_i * d_phi;
glBegin(GL_QUAD_STRIP);
for (theta_i = 0; theta_i <= N; theta_i++) {
T theta = (N - theta_i) * d_theta;
float theta_t = 1 - (float)theta_i / (float)N;
T r;
if (!displacement) r = 1; //if no displacement, set the r value to 1 (renders a unit sphere)
else r = Sd(theta, phi); //otherwise calculate the displacement value
glColor3f(1.0f, 1.0f, 1.0f);
if (!colormap) { //if no colormap is being rendered
if (r < 0) glColor3f(1.0, 0.0, 0.0); //if r is negative, render it red
else glColor3f(0.0, 1.0, 0.0); //otherwise render in green
}
if (magnitude) { //if the magnitude is being displaced, calculate the magnitude of r
if (r < 0) r = -r;
}
stim::vec3<T> s(r, theta, phi);
stim::vec3<T> c = s.sph2cart();
stim::vec3<T> n; //allocate a value to store the normal
if (!displacement) n = c; //if there is no displacement, the normal is spherical
else n = Sd.dphi(theta, phi).cross(Sd.dtheta(theta, phi)); //otherwise calculate the normal as the cross product of derivatives
glTexCoord2f(theta_t, (float)phi_i / (float)N);
//std::cout << theta_t <<" "<<(float)phi_i / (float)N << "----------------";
glNormal3f(n[0], n[1], n[2]);
glVertex3f(c[0], c[1], c[2]);
T r1;
if (!displacement) r1 = 1;
else r1 = Sd(theta, phi - d_phi);
if (!colormap) { //if no colormap is being rendered
if (r1 < 0) glColor3f(1.0, 0.0, 0.0); //if r1 is negative, render it red
else glColor3f(0.0, 1.0, 0.0); //otherwise render in green
}
if (magnitude) { //if the magnitude is being rendered, calculate the magnitude of r
if (r1 < 0) r1 = -r1;
}
stim::vec3<T> s1(r1, theta, phi - d_phi);
stim::vec3<T> c1 = s1.sph2cart();
stim::vec3<T> n1;
if (!displacement) n1 = c1;
else n1 = Sd.dphi(theta, phi - d_phi).cross(Sd.dtheta(theta, phi - d_phi));
//std::cout << theta_t << " " << (float)(phi_i - 1) / (float)N << std::endl;
glTexCoord2f(theta_t, 1.0/(2*(N)) + (float)(phi_i-1) / (float)N);
glNormal3f(n1[0], n1[1], n1[2]);
glVertex3f(c1[0], c1[1], c1[2]);
}
glEnd();
|
487a9b49
David Mayerich
added the ability...
|
234
|
}
|
01707489
David Mayerich
implemented a new...
|
235
236
|
glPopAttrib();
glEndList();
|
487a9b49
David Mayerich
added the ability...
|
237
|
}
|
01707489
David Mayerich
implemented a new...
|
238
|
glCallList(dlist); //call the display list to render
|
487a9b49
David Mayerich
added the ability...
|
239
|
|
01707489
David Mayerich
implemented a new...
|
240
|
glPopAttrib();
|
48081b3b
Sebastian Berisha
fixed the culling...
|
241
242
|
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
|
487a9b49
David Mayerich
added the ability...
|
243
|
}
|
487a9b49
David Mayerich
added the ability...
|
244
|
|
01707489
David Mayerich
implemented a new...
|
245
246
247
248
|
/// Push a coefficient to the spherical harmonic - by default, push applies the component to both the displacement and color SH
void push(T coeff) {
Sd.push(coeff);
Sc.push(coeff);
|
487a9b49
David Mayerich
added the ability...
|
249
|
}
|
487a9b49
David Mayerich
added the ability...
|
250
|
|
01707489
David Mayerich
implemented a new...
|
251
252
253
254
255
|
/// Resize the spherical harmonic coefficient array
void resize(size_t s) {
Sd.resize(s);
Sc.resize(s);
}
|
487a9b49
David Mayerich
added the ability...
|
256
|
|
01707489
David Mayerich
implemented a new...
|
257
258
259
260
|
/// Set a spherical harmonic coefficient to the given value
void setc(unsigned int c, T value) {
Sd.setc(c, value);
Sc.setc(c, value);
|
9bfae4d0
Pavel Govyadinov
extended gl_sphar...
|
261
|
}
|
9bfae4d0
Pavel Govyadinov
extended gl_sphar...
|
262
|
|
01707489
David Mayerich
implemented a new...
|
263
264
265
266
|
void project(T* data, size_t x, size_t y, size_t nc) {
Sd.project(data, x, y, nc);
Sc = Sd;
}
|
9bfae4d0
Pavel Govyadinov
extended gl_sphar...
|
267
|
|
01707489
David Mayerich
implemented a new...
|
268
|
/// Project a set of samples onto the basis
|
48081b3b
Sebastian Berisha
fixed the culling...
|
269
|
void project(std::vector<vec3<float> >& vlist, size_t nc) {
|
01707489
David Mayerich
implemented a new...
|
270
271
272
|
Sd.project(vlist, nc);
Sc = Sd;
}
|
9bfae4d0
Pavel Govyadinov
extended gl_sphar...
|
273
|
|
01707489
David Mayerich
implemented a new...
|
274
|
/// Calculate a density function from a list of points in spherical coordinates
|
48081b3b
Sebastian Berisha
fixed the culling...
|
275
|
void pdf(std::vector<stim::vec3<T> >& vlist, size_t nc) {
|
01707489
David Mayerich
implemented a new...
|
276
277
278
|
Sd.pdf(vlist, nc);
Sc = Sd;
}
|
9bfae4d0
Pavel Govyadinov
extended gl_sphar...
|
279
|
|
01707489
David Mayerich
implemented a new...
|
280
281
282
|
void slices(size_t s) {
N = s;
}
|
487a9b49
David Mayerich
added the ability...
|
283
|
|
01707489
David Mayerich
implemented a new...
|
284
285
286
|
size_t slices() {
return N;
}
|
487a9b49
David Mayerich
added the ability...
|
287
|
|
01707489
David Mayerich
implemented a new...
|
288
289
290
291
292
|
void rendermode(bool displace, bool color, bool mag = true) {
displacement = displace;
colormap = color;
magnitude = mag;
}
|
487a9b49
David Mayerich
added the ability...
|
293
|
|
01707489
David Mayerich
implemented a new...
|
294
295
|
};
}
|
487a9b49
David Mayerich
added the ability...
|
296
297
298
|
|
48081b3b
Sebastian Berisha
fixed the culling...
|
299
|
#endif
|