Blame view

stim/gl/gl_spider.h 16.3 KB
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
1
2
3
  #ifndef STIM_GL_SPIDER_H
  #define STIM_GL_SPIDER_H
  
a9b45efe   Pavel Govyadinov   changes to spider
4
  #include <GL/glew.h>
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
5
  #include <GL/glut.h>
a9b45efe   Pavel Govyadinov   changes to spider
6
7
8
  #include <cuda.h>
  #include <cuda_gl_interop.h>
  #include <cudaGL.h>
1a456186   Pavel Govyadinov   Added directional...
9
  #include <math.h>
42145f38   Pavel Govyadinov   Fixed the issues ...
10
  #include "../gl/gl_texture.h"
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
11
12
13
  #include "../visualization/camera.h"
  #include "./error.h"
  #include "../math/vector.h"
4cefeb6d   Pavel Govyadinov   Changes to the re...
14
  #include "../math/rect.h"
a9b45efe   Pavel Govyadinov   changes to spider
15
16
  #include "../cuda/cost.h"
  #include "../cuda/glbind.h"
385d2447   Pavel Govyadinov   Checkpoint: Conve...
17
  #include <vector>
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
18
  
42145f38   Pavel Govyadinov   Fixed the issues ...
19
20
21
  #include <iostream>
  #include <fstream>
  
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
22
23
24
25
26
27
28
  namespace stim
  {
  
  template<typename T>
  class gl_spider : public virtual gl_texture<T>
  {
  	//doen't use gl_texture really, just needs the GLuint id.
4cefeb6d   Pavel Govyadinov   Changes to the re...
29
  	//doesn't even need the texture iD really.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
30
  	private:
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
31
32
33
34
  		stim::vec<float> position;  	//vector designating the position of the spider.
  		stim::vec<float> direction;	//vector designating the orientation of the spider
  						//always a unit vector.
  		stim::vec<float> magnitude;	//magnitude of the direction vector.
4cefeb6d   Pavel Govyadinov   Changes to the re...
35
36
  						//mag[0] = length.
  						//mag[1] = width.
385d2447   Pavel Govyadinov   Checkpoint: Conve...
37
38
39
  		std::vector<stim::vec<float> > dirVectors;
  		std::vector<stim::vec<float> > posVectors;
  		std::vector<stim::vec<float> > magVectors;
f304d6de   Pavel Govyadinov   added rotation to...
40
  		double currentTransform[16];
4cefeb6d   Pavel Govyadinov   Changes to the re...
41
  		using gl_texture<T>::texID;
385d2447   Pavel Govyadinov   Checkpoint: Conve...
42
  		using gl_texture<T>::S;
a9b45efe   Pavel Govyadinov   changes to spider
43
  		cudaArray* c_Array;
a9b45efe   Pavel Govyadinov   changes to spider
44
45
46
  		cudaGraphicsResource_t resource;
  		GLuint fboID;
  		GLuint texbufferID;
2a18be6d   Pavel Govyadinov   New comments and ...
47
48
  		int iter;		//temporary for testing
  		int numSamples;
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
49
50
51
52
53
54
  		void
  		findOptimalPosition()
  		{
  			/* Method for finding the best direction for the spider.
  			   Not sure if necessary since the next position for the spider
  			   will be at direction * magnitude. */
887a3e49   Pavel Govyadinov   fixed some order ...
55
  			
957a4248   Pavel Govyadinov   renamed getSample...
56
  			positionTemplate(position, direction, magnitude);
385d2447   Pavel Govyadinov   Checkpoint: Conve...
57
  			int best = getCost();
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
58
59
60
61
62
63
64
65
  		}
  	
  		void
  		findOptimalScale()
  		{
  			/* Method for finding the best scale for the spider.
  			   changes the x, y, z size of the spider to minimize the cost
  			   function. */
2a18be6d   Pavel Govyadinov   New comments and ...
66
  			genTemplate(magVectors, 2);
385d2447   Pavel Govyadinov   Checkpoint: Conve...
67
  			int best = getCost();
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
68
69
70
  		}
  
  		void
385d2447   Pavel Govyadinov   Checkpoint: Conve...
71
72
73
74
75
  		findOptimalDirection()
  		{
  			/* Method for finding the best scale for the spider.
  			   changes the x, y, z size of the spider to minimize the cost
  			   function. */
957a4248   Pavel Govyadinov   renamed getSample...
76
  			//getSample(position, direction, magnitude);
2a18be6d   Pavel Govyadinov   New comments and ...
77
  			genTemplate(dirVectors, 0);
f304d6de   Pavel Govyadinov   added rotation to...
78
  			int best = getCost();
2a18be6d   Pavel Govyadinov   New comments and ...
79
80
  			//getSample(position, direction, magnitude);
  			//int best = getCost();
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
81
  		}
385d2447   Pavel Govyadinov   Checkpoint: Conve...
82
  
a9b45efe   Pavel Govyadinov   changes to spider
83
  		
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
84
85
86
87
88
  		void
  		Optimize()
  		{
  			/*find the optimum direction and scale */ 
  		}
2a18be6d   Pavel Govyadinov   New comments and ...
89
90
91
92
  
  		///@param width sets the width of the buffer.
  		///@param height sets the height of the buffer.
  		///Function for setting up the 2D buffer that stores the samples.
a9f956be   Pavel Govyadinov   Fixed the cost fu...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  		void
  		GenerateFBO(unsigned int width, unsigned int height)
  		{
  			glGenFramebuffers(1, &fboID);
  			glBindFramebuffer(GL_FRAMEBUFFER, fboID);
  			int numChannels = 1;
  			unsigned char* texels = new unsigned char[width * height * numChannels];
  			glGenTextures(1, &texbufferID);
  			glBindTexture(GL_TEXTURE_2D, texbufferID);
  			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  			glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE,
  				 width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texels);   
  			delete[] texels;
  			glBindFramebuffer(GL_FRAMEBUFFER, 0); 
  			glBindTexture(GL_TEXTURE_2D, 0);
  		}
2a18be6d   Pavel Govyadinov   New comments and ...
112
113
114
115
116
  		
  		///@param v_x x-coordinate in buffer-space,
  		///@param v_y y-coordinate in buffer-space.
  		///Samples the texturespace and places a sample in the provided coordinates
  		///of bufferspace.
a9f956be   Pavel Govyadinov   Fixed the cost fu...
117
118
119
  		void
  		UpdateBuffer(float v_x, float v_y)
  		{	
a9f956be   Pavel Govyadinov   Fixed the cost fu...
120
121
122
123
124
  			float len = 10.0;
  			stim::vec<float>p1; 
          	        stim::vec<float>p2; 
  	                stim::vec<float>p3; 
                  	stim::vec<float>p4;	
a9f956be   Pavel Govyadinov   Fixed the cost fu...
125
  			p1 = hor.p(1,1);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
126
  			p2 = hor.p(1,0);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
127
  			p3 = hor.p(0,0);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
128
  			p4 = hor.p(0,1);
5e7c7581   Pavel Govyadinov   Debugging build f...
129
  			glBegin(GL_QUADS);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
130
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
131
132
133
134
  					p1[0],
  					p1[1],
  					p1[2]
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
135
136
  				glVertex2f(v_x,v_y);
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
137
138
139
140
  					p2[0],
  					p2[1],
  					p2[2]
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
141
142
  				glVertex2f(v_x+len, v_y);
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
143
144
145
146
  					p3[0],
  					p3[1],
  					p3[2]
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
147
148
  				glVertex2f(v_x+len, v_y+len);
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
149
150
151
  					p4[0],
  					p4[1],
  					p4[2]
a9f956be   Pavel Govyadinov   Fixed the cost fu...
152
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
153
154
  				glVertex2f(v_x, v_y+len);
  			 glEnd();
5e7c7581   Pavel Govyadinov   Debugging build f...
155
  
a9f956be   Pavel Govyadinov   Fixed the cost fu...
156
  			 p1 = ver.p(1,1);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
157
  			 p2 = ver.p(1,0);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
158
  			 p3 = ver.p(0,0);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
159
  			 p4 = ver.p(0,1);
5e7c7581   Pavel Govyadinov   Debugging build f...
160
  		 	 glBegin(GL_QUADS);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
161
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
162
163
164
165
  					p1[0],
  					p1[1],
  					p1[2]
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
166
167
  				glVertex2f(v_x+len, v_y);
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
168
169
170
171
  					p2[0],
  					p2[1],
  					p2[2]
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
172
173
  				glVertex2f(v_x+2*len, v_y);
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
174
175
176
177
  					p3[0],
  					p3[1],
  					p3[2]
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
178
179
  				glVertex2f(v_x+2*len, v_y+len);
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
180
181
182
183
  					p4[0],
  					p4[1],
  					p4[2]
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
184
  				glVertex2f(v_x+len, v_y+len);
5e7c7581   Pavel Govyadinov   Debugging build f...
185
186
  			glEnd(); 
  			CHECK_OPENGL_ERROR
a9f956be   Pavel Govyadinov   Fixed the cost fu...
187
  		}
2a18be6d   Pavel Govyadinov   New comments and ...
188
189
190
191
192
193
194
195
  		
  		///@param vector of stim::vec in that stores all of the samplable vectors.
  		///@param type, one of three operations, 0 for Direction vectors
  		///	1 for Position, 2 for Magnitude.
  		///Function for filling the buffer up with the data based on the vectors
  		///Each vector represents a two rectangular templates.
  		///Loops through all of the vectors and transfers rect. associated with it
  		///Into buffer-space.
a9f956be   Pavel Govyadinov   Fixed the cost fu...
196
  		void
2a18be6d   Pavel Govyadinov   New comments and ...
197
  		genTemplate(std::vector<stim::vec<float> > in, int type)
a9f956be   Pavel Govyadinov   Fixed the cost fu...
198
  		{
385d2447   Pavel Govyadinov   Checkpoint: Conve...
199
  			float x = 0.0;
a9f956be   Pavel Govyadinov   Fixed the cost fu...
200
  			vec<float> Y(1.0,0.0,0.0);
5e7c7581   Pavel Govyadinov   Debugging build f...
201
202
  			vec<float> pos(0.0,0.0,0.0);
  			vec<float> mag(1.0, 1.0, 1.0);
5f81932b   David Mayerich   restored Pavel's ...
203
                          switch(type) {
385d2447   Pavel Govyadinov   Checkpoint: Conve...
204
  				case 0:          //Direction
957a4248   Pavel Govyadinov   renamed getSample...
205
206
  					Bind();
  				positionTemplate();
385d2447   Pavel Govyadinov   Checkpoint: Conve...
207
208
209
210
211
  					for(int i = 0; i < in.size(); i++)
  					{
  					 	if(cos(Y.dot(in[i]))< 0.087){                                                                             Y[0] = 0.0; Y[1] = 1.0;}
  						else{Y[0] = 1.0; Y[1] = 0.0;}
  
5e7c7581   Pavel Govyadinov   Debugging build f...
212
213
                                           	hor = stim::rect<float>(mag,
  							 pos, in[i],
385d2447   Pavel Govyadinov   Checkpoint: Conve...
214
                          				((Y.cross(in[i])).cross(in[i])).norm());
5e7c7581   Pavel Govyadinov   Debugging build f...
215
216
                				 	ver = stim::rect<float>(mag,
  							 pos, in[i],
385d2447   Pavel Govyadinov   Checkpoint: Conve...
217
                          				hor.n());
5e7c7581   Pavel Govyadinov   Debugging build f...
218
  			//			std::cout<< (x+i*10.0)  << std::endl;
385d2447   Pavel Govyadinov   Checkpoint: Conve...
219
220
  						UpdateBuffer(x, x+i*10.0);
  					 }
957a4248   Pavel Govyadinov   renamed getSample...
221
  					 //getSample();
2a18be6d   Pavel Govyadinov   New comments and ...
222
  					 Unbind();
5f81932b   David Mayerich   restored Pavel's ...
223
                 				 break;
385d2447   Pavel Govyadinov   Checkpoint: Conve...
224
          			case 1:		 //Position
2a18be6d   Pavel Govyadinov   New comments and ...
225
  					Bind();	
385d2447   Pavel Govyadinov   Checkpoint: Conve...
226
227
228
229
230
231
232
233
234
235
  					if(cos(Y.dot(direction))< 0.087){                                                                             Y[0] = 0.0; Y[1] = 1.0;}
  					else{Y[0] = 1.0; Y[1] = 0.0;}
  
  					for(int i = 0; i < in.size(); i++)
  					{
                  				hor = stim::rect<float>(magnitude, in[i], direction,
                        					((Y.cross(direction)).cross(direction))
  							.norm());
                  				ver = stim::rect<float>(magnitude, in[i], direction,
                          				hor.n());
2a18be6d   Pavel Govyadinov   New comments and ...
236
237
  						//UpdateBuffer(x, x+i*10.0);
  						std::cout<< "Hmmm" << std::endl;
385d2447   Pavel Govyadinov   Checkpoint: Conve...
238
  					 }
2a18be6d   Pavel Govyadinov   New comments and ...
239
  					Unbind();
5f81932b   David Mayerich   restored Pavel's ...
240
                  			 break;
385d2447   Pavel Govyadinov   Checkpoint: Conve...
241
          			case 2:		  //Scale
2a18be6d   Pavel Govyadinov   New comments and ...
242
  					Bind();	
385d2447   Pavel Govyadinov   Checkpoint: Conve...
243
244
245
246
247
248
249
250
251
252
  					if(cos(Y.dot(direction))< 0.087){                                                                             Y[0] = 0.0; Y[1] = 1.0;}
  					else{Y[0] = 1.0; Y[1] = 0.0;}
  
                  			for(int i = 0; i < in.size(); i++)
  					{
  						hor = stim::rect<float>(in[i], position, direction,
                        					((Y.cross(direction)).cross(direction))
  							.norm());
                  				ver = stim::rect<float>(in[i], position, direction,
                          				hor.n());
2a18be6d   Pavel Govyadinov   New comments and ...
253
254
  						//UpdateBuffer(x, x+i*10.0);
  						std::cout<< "Hmmm" << std::endl;
385d2447   Pavel Govyadinov   Checkpoint: Conve...
255
  					}
2a18be6d   Pavel Govyadinov   New comments and ...
256
  					Unbind();
385d2447   Pavel Govyadinov   Checkpoint: Conve...
257
  					break;
5f81932b   David Mayerich   restored Pavel's ...
258
259
260
261
262
          			default:
                  			 std::cout << "unknown case have been passed"
                           			<< std::endl;
  					 break;
  			}               
385d2447   Pavel Govyadinov   Checkpoint: Conve...
263
  		Unbind();
2a18be6d   Pavel Govyadinov   New comments and ...
264
  		CHECK_OPENGL_ERROR
a9f956be   Pavel Govyadinov   Fixed the cost fu...
265
  		}
5f81932b   David Mayerich   restored Pavel's ...
266
  		
2a18be6d   Pavel Govyadinov   New comments and ...
267
268
  		///Method for controling the buffer and texture binding in order to properly
  		///do the render to texture.
a9f956be   Pavel Govyadinov   Fixed the cost fu...
269
  		void
2a18be6d   Pavel Govyadinov   New comments and ...
270
  		Bind()
a9f956be   Pavel Govyadinov   Fixed the cost fu...
271
  		{
5f81932b   David Mayerich   restored Pavel's ...
272
  			float len = 10.0;
a9f956be   Pavel Govyadinov   Fixed the cost fu...
273
274
275
276
277
278
279
280
281
282
283
  			glBindFramebuffer(GL_FRAMEBUFFER, fboID);//set up GL buffer		
  			glFramebufferTexture2D(
  				GL_FRAMEBUFFER,
  				GL_COLOR_ATTACHMENT0,
  				GL_TEXTURE_2D,
  				texbufferID,
  				0);
  			glBindFramebuffer(GL_FRAMEBUFFER, fboID);
  			GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
  			glDrawBuffers(1, DrawBuffers);
  			glBindTexture(GL_TEXTURE_2D, texbufferID);
5e7c7581   Pavel Govyadinov   Debugging build f...
284
  			glClearColor(1,1,1,1);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
285
  			glClear(GL_COLOR_BUFFER_BIT);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
286
287
288
289
290
291
292
293
  			glMatrixMode(GL_PROJECTION);
  			glLoadIdentity();
  			glMatrixMode(GL_MODELVIEW);
  			glLoadIdentity();
  			glViewport(0,0,2.0*len, numSamples*len);
  			gluOrtho2D(0.0,2.0*len,0.0,numSamples*len);
  			glEnable(GL_TEXTURE_3D);
  			glBindTexture(GL_TEXTURE_3D, texID);
957a4248   Pavel Govyadinov   renamed getSample...
294
  
5e7c7581   Pavel Govyadinov   Debugging build f...
295
  			CHECK_OPENGL_ERROR
5f81932b   David Mayerich   restored Pavel's ...
296
297
  		}
  		
2a18be6d   Pavel Govyadinov   New comments and ...
298
  		///Method for Unbinding all of the texture resources
5f81932b   David Mayerich   restored Pavel's ...
299
300
301
302
303
304
305
306
307
  		void
  		Unbind()
  		{
  			//Finalize GL_buffer
  			glBindTexture(GL_TEXTURE_3D, 0);                      
  			glDisable(GL_TEXTURE_3D);
  			glBindFramebuffer(GL_FRAMEBUFFER,0);
  			glBindTexture(GL_TEXTURE_2D, 0);
  		}
385d2447   Pavel Govyadinov   Checkpoint: Conve...
308
  
2a18be6d   Pavel Govyadinov   New comments and ...
309
310
311
  		///@param solidAngle, the size of the arc to sample.
  		///Method for populating the vector arrays with sampled vectors.
  		///uses the default direction vector <0,0,1>
5f81932b   David Mayerich   restored Pavel's ...
312
  		void
2a18be6d   Pavel Govyadinov   New comments and ...
313
  		genDirectionVectors(float solidAngle = M_PI/2)
5f81932b   David Mayerich   restored Pavel's ...
314
  		{
a9f956be   Pavel Govyadinov   Fixed the cost fu...
315
  			
2a18be6d   Pavel Govyadinov   New comments and ...
316
317
  			vec<float> d_s = direction.cart2sph().norm();
  			//dirVectors.resize(numSamples);		//THIS LINE ADDS AN ERROR
5f81932b   David Mayerich   restored Pavel's ...
318
319
  			vec<float> temp;
  			int dim = (sqrt(numSamples)-1)/2;
5f81932b   David Mayerich   restored Pavel's ...
320
  			float p0  	= M_PI/3;
2a18be6d   Pavel Govyadinov   New comments and ...
321
322
  			float dt  	= solidAngle/(2.0 * (dim + 1));
  			float dp  	= p0/(2.0*(dim + 1));
5f81932b   David Mayerich   restored Pavel's ...
323
  	
a9f956be   Pavel Govyadinov   Fixed the cost fu...
324
325
326
  			for(int i = -dim; i <= dim; i++){
  				for(int j = -dim; j <= dim; j++){
  					//Create linear index
385d2447   Pavel Govyadinov   Checkpoint: Conve...
327
  					
42145f38   Pavel Govyadinov   Fixed the issues ...
328
  					temp[0] = d_s[0]; 			//rotate vector
a9f956be   Pavel Govyadinov   Fixed the cost fu...
329
330
  					temp[1] = d_s[1]+dt*i;
  					temp[2] = d_s[2]+dp*j;
2a18be6d   Pavel Govyadinov   New comments and ...
331
  					
42145f38   Pavel Govyadinov   Fixed the issues ...
332
  					temp = (temp.sph2cart()).norm();		//back to cart
385d2447   Pavel Govyadinov   Checkpoint: Conve...
333
  					dirVectors.push_back(temp);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
334
335
  				}
  			}
5f81932b   David Mayerich   restored Pavel's ...
336
337
  		}
  
2a18be6d   Pavel Govyadinov   New comments and ...
338
339
340
  		///@param solidAngle, the size of the arc to sample.
  		///Method for populating the buffer with the sampled texture.
  		///uses the default vector <0,0,0>
5f81932b   David Mayerich   restored Pavel's ...
341
  		void
2a18be6d   Pavel Govyadinov   New comments and ...
342
  		genPositionVectors(float delta = 0.2)
5f81932b   David Mayerich   restored Pavel's ...
343
344
  		{
  			
5f81932b   David Mayerich   restored Pavel's ...
345
346
  			vec<float> temp;
  			int dim = (sqrt(numSamples)-1)/2;
5f81932b   David Mayerich   restored Pavel's ...
347
348
349
  			stim::rect<float> samplingPlane =
  				 stim::rect<float>(magnitude[0]*delta, position, direction);
  			float step = 1.0/(dim);
5f81932b   David Mayerich   restored Pavel's ...
350
351
352
353
354
355
356
  			//Loop over the samples, keeping the original position sample
  			//in the center of the resulting texture.
  
  			int idx;
  			for(int i = -dim; i <= dim; i++){
  				for(int j = -dim; j <= dim; j++){
  					//Create linear index
5f81932b   David Mayerich   restored Pavel's ...
357
358
359
360
361
  
  					temp = samplingPlane.p(
  							0.5+step*i,
  								 0.5+step*j
  										);
2a18be6d   Pavel Govyadinov   New comments and ...
362
  					posVectors.push_back(temp);
5f81932b   David Mayerich   restored Pavel's ...
363
364
  				}
  			}
5f81932b   David Mayerich   restored Pavel's ...
365
366
  		}
  
2a18be6d   Pavel Govyadinov   New comments and ...
367
368
369
  		///@param solidAngle, the size of the arc to sample.
  		///Method for populating the buffer with the sampled texture.
  		///uses the default magnitude <1,1,0>
5f81932b   David Mayerich   restored Pavel's ...
370
  		void
2a18be6d   Pavel Govyadinov   New comments and ...
371
  		genMagnitudeVectors(float delta = 0.5)
5f81932b   David Mayerich   restored Pavel's ...
372
373
  		{
  			
5f81932b   David Mayerich   restored Pavel's ...
374
  			int dim = (sqrt(numSamples)-1)/2;
5f81932b   David Mayerich   restored Pavel's ...
375
376
377
378
379
  			float min 	= 1.0-delta;
  			float max 	= 1.0+delta;
  			float step	= (max-min)/(numSamples-1);
  			float factor;
  			vec<float> temp;
5f81932b   David Mayerich   restored Pavel's ...
380
381
  
  			for(int i = 0; i < numSamples; i++){
385d2447   Pavel Govyadinov   Checkpoint: Conve...
382
383
384
385
386
  				//Create linear index
  				factor = (min+step*i)*magnitude[0];
  				temp[0] = factor;
  				temp[1] = factor;
  				magVectors.push_back(temp);	
5f81932b   David Mayerich   restored Pavel's ...
387
  			}
42145f38   Pavel Govyadinov   Fixed the issues ...
388
389
390
391
392
393
  		}
  
  //--------------------------------------------------------------------------//
  //--------------------------------CUDA METHODS------------------------------//
  //--------------------------------------------------------------------------//
  		
2a18be6d   Pavel Govyadinov   New comments and ...
394
395
  		/// Method for registering the texture with Cuda for shared
  		///	access.
42145f38   Pavel Govyadinov   Fixed the issues ...
396
397
398
399
400
401
402
403
404
405
406
407
408
  		void
  		createResource()
  		{
  			HANDLE_ERROR(
  				cudaGraphicsGLRegisterImage(
  					 &resource,
  				 	texbufferID,
  				 	GL_TEXTURE_2D,
  				 	//CU_GRAPHICS_REGISTER_FLAGS_NONE)
  					cudaGraphicsMapFlagsReadOnly)
  			);
  		} 
  		
2a18be6d   Pavel Govyadinov   New comments and ...
409
  		///Method for freeing the texture from Cuda for gl access.
42145f38   Pavel Govyadinov   Fixed the issues ...
410
411
412
413
414
415
416
417
  		void
  		destroyResource()
  		{
  			HANDLE_ERROR(
  				cudaGraphicsUnregisterResource(resource)
  			);		
  		}
  
2a18be6d   Pavel Govyadinov   New comments and ...
418
419
  		///Entry-point into the cuda code for calculating the cost
  		///	of a given samples array (in texture form) 
42145f38   Pavel Govyadinov   Fixed the issues ...
420
421
422
423
  		int
  		getCost()
  		{
  			createResource();
5e7c7581   Pavel Govyadinov   Debugging build f...
424
  			int cost = 	get_cost(resource, iter, numSamples);
42145f38   Pavel Govyadinov   Fixed the issues ...
425
426
427
  			destroyResource();
  			iter++;
  			return cost;
a9f956be   Pavel Govyadinov   Fixed the cost fu...
428
429
  		}
  
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
430
  	public:
4cefeb6d   Pavel Govyadinov   Changes to the re...
431
  
13c2a7d4   Pavel Govyadinov   some changes to t...
432
433
434
  		stim::rect<float> hor;
  		stim::rect<float> ver;	
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
435
  		///Default Constructor
13c2a7d4   Pavel Govyadinov   some changes to t...
436
437
  		gl_spider
  		()
4cefeb6d   Pavel Govyadinov   Changes to the re...
438
439
  		{
  			setPosition(0.0,0.0,0.0);
5f81932b   David Mayerich   restored Pavel's ...
440
  			setDirection(0.0,0.0,1.0);
385d2447   Pavel Govyadinov   Checkpoint: Conve...
441
  			setMagnitude(1.0);
957a4248   Pavel Govyadinov   renamed getSample...
442
443
  			//numSamples = 1089;
  			numSamples = 9;
2a18be6d   Pavel Govyadinov   New comments and ...
444
445
446
447
448
449
450
451
452
  			std::cout << "I did this" <<std::endl;
  		}
  		gl_spider
  		(int samples)
  		{
  			setPosition(0.0,0.0,0.0);
  			setDirection(0.0,0.0,1.0);
  			setMagnitude(1.0);
  			numSamples = samples;
4cefeb6d   Pavel Govyadinov   Changes to the re...
453
454
  		}
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
455
  		///temporary constructor for convenience, will be removed in further updates.	
13c2a7d4   Pavel Govyadinov   some changes to t...
456
457
  		gl_spider
  		(float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z,
5f81932b   David Mayerich   restored Pavel's ...
458
  			float mag_x)
13c2a7d4   Pavel Govyadinov   some changes to t...
459
  		{
887a3e49   Pavel Govyadinov   fixed some order ...
460
461
462
  			setPosition(pos_x, pos_y, pos_z);
  			setDirection(dir_x, dir_y, dir_z);
  			setMagnitude(mag_x);
2a18be6d   Pavel Govyadinov   New comments and ...
463
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
464
465
  		}
  		///@param GLuint id texture that is going to be sampled.
385d2447   Pavel Govyadinov   Checkpoint: Conve...
466
467
  		///Attached the spider to the texture with the given GLuint ID.
  		///Samples in the default direction acting as the init method.	
a9b45efe   Pavel Govyadinov   changes to spider
468
  		void
2a18be6d   Pavel Govyadinov   New comments and ...
469
  		attachSpider(GLuint id)
a9b45efe   Pavel Govyadinov   changes to spider
470
471
  		{
  			texID = id;
2a18be6d   Pavel Govyadinov   New comments and ...
472
  			iter = 0; ///for debugging purposes
5f81932b   David Mayerich   restored Pavel's ...
473
  			GenerateFBO(20, numSamples*10);
385d2447   Pavel Govyadinov   Checkpoint: Conve...
474
  			genDirectionVectors();
887a3e49   Pavel Govyadinov   fixed some order ...
475
476
477
  			genPositionVectors();
  			genMagnitudeVectors();
  			gl_texture<T>::setDims(0.6, 0.6, 2.0);
2a18be6d   Pavel Govyadinov   New comments and ...
478
  			genTemplate(dirVectors, 0);
a39577bf   Pavel Govyadinov   Changes to the sp...
479
  		}
5f81932b   David Mayerich   restored Pavel's ...
480
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
481
  		///temporary Method necessary for visualization and testing.
a39577bf   Pavel Govyadinov   Changes to the sp...
482
483
484
  		void
  		Update()
  		{
13c2a7d4   Pavel Govyadinov   some changes to t...
485
486
487
  			vec<float> Y(1.0,0.0,0.0);
  			if(cos(Y.dot(direction))< 0.087){
  				Y[0] = 0.0; Y[1] = 1.0;}
879321c4   Pavel Govyadinov   minor changes
488
489
  			hor = stim::rect<float>(magnitude, position, direction.norm(),
  				((Y.cross(direction)).cross(direction)).norm());
13c2a7d4   Pavel Govyadinov   some changes to t...
490
  			ver = stim::rect<float>(magnitude, position, direction.norm(),
879321c4   Pavel Govyadinov   minor changes
491
  				 hor.n());
4cefeb6d   Pavel Govyadinov   Changes to the re...
492
  		}
1a456186   Pavel Govyadinov   Added directional...
493
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
494
  		///Returns the position vector.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
495
  		vec<float>
a39577bf   Pavel Govyadinov   Changes to the sp...
496
  		getPosition()
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
497
498
499
500
  		{
  			return position;
  		}
  	
385d2447   Pavel Govyadinov   Checkpoint: Conve...
501
  		///Returns the direction vector.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
502
  		vec<float>
a39577bf   Pavel Govyadinov   Changes to the sp...
503
  		getDirection()
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
504
505
506
507
  		{
  			return direction;
  		}
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
508
  		///Returns the magnitude vector.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
509
  		vec<float>
a39577bf   Pavel Govyadinov   Changes to the sp...
510
  		getMagnitude()
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
511
512
513
514
  		{
  			return magnitude;
  		}
  	
385d2447   Pavel Govyadinov   Checkpoint: Conve...
515
516
  		///@param vector pos, the new position.
  		///Sets the position vector to input vector pos.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
517
518
519
520
521
  		void
  		setPosition(vec<float> pos)
  		{
  			position = pos;
  		}
385d2447   Pavel Govyadinov   Checkpoint: Conve...
522
523
524
525
526
  
  		///@param x x-coordinate.
  		///@param y y-coordinate.
  		///@param z z-coordinate.
  		///Sets the position vector to the input float coordinates x,y,z.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
527
528
529
530
531
532
533
  		void
  		setPosition(float x, float y, float z)
  		{
  			position[0] = x;
  			position[1] = y;
  			position[2] = z;
  		}
887a3e49   Pavel Govyadinov   fixed some order ...
534
  		
957a4248   Pavel Govyadinov   renamed getSample...
535
  		void positionTemplate(vec<float> pos, vec<float> dir, vec<float> mag)
887a3e49   Pavel Govyadinov   fixed some order ...
536
  		{
f304d6de   Pavel Govyadinov   added rotation to...
537
  			stim::vec<float, 4> rot = getRotation(dir);
887a3e49   Pavel Govyadinov   fixed some order ...
538
539
  			glMatrixMode(GL_TEXTURE);
  			glLoadIdentity();
f304d6de   Pavel Govyadinov   added rotation to...
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
  			glTranslatef(position[0]/512/S[1],
  				 position[1]/512/S[2],
  				 position[2]/426/S[3]);
  			glScalef(magnitude[0]/512/S[1],
  				 magnitude[1]/512/S[2],
  				 magnitude[0]/426/S[3]);
  			glRotatef(rot[0], rot[1], rot[2], rot[3]);
  			glGetDoublev(GL_TEXTURE_MATRIX, currentTransform);
  			int c = 0;
  			for(int i = 0; i < 16; i++){
  				if(c<3){
  					std::cerr << "[" << currentTransform[i] << "]" << " ";
  					c++;
  				} else {
  					std::cerr << "[" << currentTransform[i] << "]" << "\n";
  					c = 0;
  				}
  			}
  			CHECK_OPENGL_ERROR
957a4248   Pavel Govyadinov   renamed getSample...
559
  			glMatrixMode(GL_MODELVIEW);
887a3e49   Pavel Govyadinov   fixed some order ...
560
  		}
957a4248   Pavel Govyadinov   renamed getSample...
561
562
563
564
565
  
  		void positionTemplate()
  		{
  			positionTemplate(position, direction, magnitude);
  		}	
385d2447   Pavel Govyadinov   Checkpoint: Conve...
566
567
  		///@param vector dir, the new direction.
  		///Sets the direction vector to input vector dir.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
568
569
570
571
572
573
  		void
  		setDirection(vec<float> dir)
  		{
  			direction = dir;
  		}
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
574
575
576
577
  		///@param x x-coordinate.
  		///@param y y-coordinate.
  		///@param z z-coordinate.
  		///Sets the direction vector to the input float coordinates x,y,z.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
578
579
580
581
582
583
584
  		void
  		setDirection(float x, float y, float z)
  		{
  			direction[0] = x;
  			direction[1] = y;
  			direction[2] = z;
  		}
385d2447   Pavel Govyadinov   Checkpoint: Conve...
585
586
587
  			
  		///@param vector dir, the new direction.
  		///Sets the magnitude vector to the input vector mag.	
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
588
589
590
  		void
  		setMagnitude(vec<float> mag)
  		{
5f81932b   David Mayerich   restored Pavel's ...
591
592
  			magnitude[0] = mag[0];
  			magnitude[1] = mag[0];
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
593
594
  		}
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
595
596
  		///@param mag size of the sampled region.
  		///Sets the magnitude vector to the input mag for both templates.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
597
  		void
5f81932b   David Mayerich   restored Pavel's ...
598
  		setMagnitude(float mag)
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
599
  		{
5f81932b   David Mayerich   restored Pavel's ...
600
601
  			magnitude[0] = mag;
  			magnitude[1] = mag;
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
602
  		}
5f81932b   David Mayerich   restored Pavel's ...
603
  		
f304d6de   Pavel Govyadinov   added rotation to...
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
  		///@param dir, the vector to which we are rotating
  		///given a vector to align to, finds the required
  		///axis and angle for glRotatef
  		stim::vec<float, 4>
  		getRotation(stim::vec<float> dir)
  		{
  			stim::vec<float, 4> out;
  			stim::vec<float> from(0,0,1);
  			out[0] = acos(from.dot(dir))*M_PI/180;
  			if(out[0] < 0.0001){
  				out[0] = 0.0;
  				out[1] = 0.0;
  				out[2] = 0.0;
  				out[3] = 1.0;
  			} else {
  				stim::vec<float> temp;
  				temp = (from.cross(dir)).norm();
  				out[1] = temp[0];
  				out[2] = temp[1];
  				out[3] = temp[2];
  			}
  			return out;
  		}
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
628
  		///temporary method for visualization.
a9b45efe   Pavel Govyadinov   changes to spider
629
630
631
632
633
  		GLuint
  		getFB()
  		{
  			return fboID;
  		}
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
634
  
13c2a7d4   Pavel Govyadinov   some changes to t...
635
636
637
  		void
  		Step()
  		{
42145f38   Pavel Govyadinov   Fixed the issues ...
638
  			findOptimalDirection();
13c2a7d4   Pavel Govyadinov   some changes to t...
639
  		}
a9b45efe   Pavel Govyadinov   changes to spider
640
  
42145f38   Pavel Govyadinov   Fixed the issues ...
641
  
a9b45efe   Pavel Govyadinov   changes to spider
642
  
42145f38   Pavel Govyadinov   Fixed the issues ...
643
644
645
646
647
  		/* Method for initializing the cuda devices, necessary only
  			there are multiple cuda devices */
  		void
  		initCuda()
  		{	
2a18be6d   Pavel Govyadinov   New comments and ...
648
  			stim::cudaSetDevice();
42145f38   Pavel Govyadinov   Fixed the issues ...
649
650
651
  			//GLint max;
  			//glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
  			//std::cout << max << std::endl;
a9b45efe   Pavel Govyadinov   changes to spider
652
  		}
4cefeb6d   Pavel Govyadinov   Changes to the re...
653
  };
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
654
655
  }
  #endif