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
|
|
01707489
David Mayerich
implemented a new...
|
65
66
67
68
69
|
/// 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...
|
70
71
|
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
|
01707489
David Mayerich
implemented a new...
|
72
73
74
75
|
if (!tex) {
init_tex();
}
|
487a9b49
David Mayerich
added the ability...
|
76
|
|
01707489
David Mayerich
implemented a new...
|
77
78
79
80
|
if (colormap) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
}
|
487a9b49
David Mayerich
added the ability...
|
81
|
|
01707489
David Mayerich
implemented a new...
|
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
|
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...
|
145
|
}
|
01707489
David Mayerich
implemented a new...
|
146
147
|
glPopAttrib();
glEndList();
|
487a9b49
David Mayerich
added the ability...
|
148
|
}
|
01707489
David Mayerich
implemented a new...
|
149
|
glCallList(dlist); //call the display list to render
|
487a9b49
David Mayerich
added the ability...
|
150
|
|
01707489
David Mayerich
implemented a new...
|
151
|
glPopAttrib();
|
48081b3b
Sebastian Berisha
fixed the culling...
|
152
153
|
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
|
487a9b49
David Mayerich
added the ability...
|
154
|
}
|
487a9b49
David Mayerich
added the ability...
|
155
|
|
01707489
David Mayerich
implemented a new...
|
156
157
158
159
|
/// 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...
|
160
|
}
|
487a9b49
David Mayerich
added the ability...
|
161
|
|
01707489
David Mayerich
implemented a new...
|
162
163
164
165
166
|
/// Resize the spherical harmonic coefficient array
void resize(size_t s) {
Sd.resize(s);
Sc.resize(s);
}
|
487a9b49
David Mayerich
added the ability...
|
167
|
|
01707489
David Mayerich
implemented a new...
|
168
169
170
171
|
/// 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...
|
172
|
}
|
9bfae4d0
Pavel Govyadinov
extended gl_sphar...
|
173
|
|
01707489
David Mayerich
implemented a new...
|
174
175
176
177
|
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...
|
178
|
|
01707489
David Mayerich
implemented a new...
|
179
|
/// Project a set of samples onto the basis
|
48081b3b
Sebastian Berisha
fixed the culling...
|
180
|
void project(std::vector<vec3<float> >& vlist, size_t nc) {
|
01707489
David Mayerich
implemented a new...
|
181
182
183
|
Sd.project(vlist, nc);
Sc = Sd;
}
|
9bfae4d0
Pavel Govyadinov
extended gl_sphar...
|
184
|
|
01707489
David Mayerich
implemented a new...
|
185
|
/// Calculate a density function from a list of points in spherical coordinates
|
48081b3b
Sebastian Berisha
fixed the culling...
|
186
|
void pdf(std::vector<stim::vec3<T> >& vlist, size_t nc) {
|
01707489
David Mayerich
implemented a new...
|
187
188
189
|
Sd.pdf(vlist, nc);
Sc = Sd;
}
|
9bfae4d0
Pavel Govyadinov
extended gl_sphar...
|
190
|
|
01707489
David Mayerich
implemented a new...
|
191
192
193
|
void slices(size_t s) {
N = s;
}
|
487a9b49
David Mayerich
added the ability...
|
194
|
|
01707489
David Mayerich
implemented a new...
|
195
196
197
|
size_t slices() {
return N;
}
|
487a9b49
David Mayerich
added the ability...
|
198
|
|
01707489
David Mayerich
implemented a new...
|
199
200
201
202
203
|
void rendermode(bool displace, bool color, bool mag = true) {
displacement = displace;
colormap = color;
magnitude = mag;
}
|
487a9b49
David Mayerich
added the ability...
|
204
|
|
01707489
David Mayerich
implemented a new...
|
205
206
|
};
}
|
487a9b49
David Mayerich
added the ability...
|
207
208
209
|
|
48081b3b
Sebastian Berisha
fixed the culling...
|
210
|
#endif
|