Blame view

stim/visualization/gl_spharmonics.h 6.56 KB
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
  

01707489   David Mayerich   implemented a new...
54
55
56
  		~gl_spharmonics() {

  			if (dlist) glDeleteLists(dlist, 1);		//delete the display list when the object is destroyed

  		}

487a9b49   David Mayerich   added the ability...
57
  

01707489   David Mayerich   implemented a new...
58
59
60
61
62
63
64
65
66
  		/// This function renders the spherical harmonic to the current OpenGL context

  		void render() {

  			//glShadeModel(GL_FLAT);

  			glPushAttrib(GL_ENABLE_BIT);

  			glDisable(GL_CULL_FACE);

  			

  			if (!tex) {

  				init_tex();

  			}

487a9b49   David Mayerich   added the ability...
67
  

01707489   David Mayerich   implemented a new...
68
69
70
71
  			if (colormap) {

  				glEnable(GL_TEXTURE_2D);

  				glBindTexture(GL_TEXTURE_2D, tex);

  			}

487a9b49   David Mayerich   added the ability...
72
  

01707489   David Mayerich   implemented a new...
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
  			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...
136
  				}

01707489   David Mayerich   implemented a new...
137
138
  				glPopAttrib();

  				glEndList();

487a9b49   David Mayerich   added the ability...
139
  			}

01707489   David Mayerich   implemented a new...
140
  			glCallList(dlist);											//call the display list to render

487a9b49   David Mayerich   added the ability...
141
  

01707489   David Mayerich   implemented a new...
142
  			glPopAttrib();

487a9b49   David Mayerich   added the ability...
143
  		}

487a9b49   David Mayerich   added the ability...
144
  

01707489   David Mayerich   implemented a new...
145
146
147
148
  		/// 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...
149
  		}

487a9b49   David Mayerich   added the ability...
150
  

01707489   David Mayerich   implemented a new...
151
152
153
154
155
  		/// Resize the spherical harmonic coefficient array

  		void resize(size_t s) {

  			Sd.resize(s);

  			Sc.resize(s);

  		}

487a9b49   David Mayerich   added the ability...
156
  

01707489   David Mayerich   implemented a new...
157
158
159
160
  		/// 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...
161
  		}

9bfae4d0   Pavel Govyadinov   extended gl_sphar...
162
  

01707489   David Mayerich   implemented a new...
163
164
165
166
  		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...
167
  

01707489   David Mayerich   implemented a new...
168
169
170
171
172
  		/// Project a set of samples onto the basis

  		void project(std::vector<vec3<float>>& vlist, size_t nc) {

  			Sd.project(vlist, nc);

  			Sc = Sd;

  		}

9bfae4d0   Pavel Govyadinov   extended gl_sphar...
173
  

01707489   David Mayerich   implemented a new...
174
175
176
177
178
  		/// Calculate a density function from a list of points in spherical coordinates

  		void pdf(std::vector<stim::vec3<T>>& vlist, size_t nc) {

  			Sd.pdf(vlist, nc);

  			Sc = Sd;

  		}

9bfae4d0   Pavel Govyadinov   extended gl_sphar...
179
  

01707489   David Mayerich   implemented a new...
180
181
182
  		void slices(size_t s) {

  			N = s;

  		}

487a9b49   David Mayerich   added the ability...
183
  

01707489   David Mayerich   implemented a new...
184
185
186
  		size_t slices() {

  			return N;

  		}

487a9b49   David Mayerich   added the ability...
187
  

01707489   David Mayerich   implemented a new...
188
189
190
191
192
  		void rendermode(bool displace, bool color, bool mag = true) {

  			displacement = displace;

  			colormap = color;

  			magnitude = mag;

  		}

487a9b49   David Mayerich   added the ability...
193
  

01707489   David Mayerich   implemented a new...
194
195
  	};

  }

487a9b49   David Mayerich   added the ability...
196
197
198
  

  

  

01707489   David Mayerich   implemented a new...
199
  #endif