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