Commit 57fbfbf92c2f81c9df649e2b635ef8e2dc10425c
Merge branch 'Dev' of git.stim.ee.uh.edu:codebase/stimlib into Dev
Showing
6 changed files
with
917 additions
and
291 deletions
Show diff stats
1 | +#ifndef STIM_GL_SPIDER_H | |
2 | +#define STIM_GL_SPIDER_H | |
3 | + | |
4 | +#include <GL/glew.h> | |
5 | +#include <GL/glut.h> | |
6 | +#include <cuda.h> | |
7 | +#include <cuda_gl_interop.h> | |
8 | +#include <cudaGL.h> | |
9 | +#include "gl_texture.h" | |
10 | +#include "../visualization/camera.h" | |
11 | +#include "./error.h" | |
12 | +#include "../math/vector.h" | |
13 | +#include "../math/rect.h" | |
14 | +#include "../cuda/cost.h" | |
15 | +#include "../cuda/glbind.h" | |
16 | + | |
17 | +namespace stim | |
18 | +{ | |
19 | + | |
20 | +template<typename T> | |
21 | +class gl_spider : public virtual gl_texture<T> | |
22 | +{ | |
23 | + //doen't use gl_texture really, just needs the GLuint id. | |
24 | + //doesn't even need the texture iD really. | |
25 | + private: | |
26 | + stim::camera rotator; | |
27 | + stim::vec<float> position; //vector designating the position of the spider. | |
28 | + stim::vec<float> direction; //vector designating the orientation of the spider | |
29 | + //always a unit vector. | |
30 | + stim::vec<float> magnitude; //magnitude of the direction vector. | |
31 | + //mag[0] = length. | |
32 | + //mag[1] = width. | |
33 | + using gl_texture<T>::texID; | |
34 | + //using image_stack<T>::S; | |
35 | + cudaArray* c_Array; | |
36 | + //void** devPtr; | |
37 | + //size_t size; | |
38 | + cudaGraphicsResource_t resource; | |
39 | + GLuint fboID; | |
40 | + GLuint texbufferID; | |
41 | + | |
42 | + void | |
43 | + findOptimalDirection() | |
44 | + { | |
45 | + /* Method for finding the best direction for the spider. | |
46 | + Uses the camera to rotate. Then Calls Evaluate to find new cost. | |
47 | + */ | |
48 | + } | |
49 | + | |
50 | + void | |
51 | + findOptimalPosition() | |
52 | + { | |
53 | + /* Method for finding the best direction for the spider. | |
54 | + Not sure if necessary since the next position for the spider | |
55 | + will be at direction * magnitude. */ | |
56 | + } | |
57 | + | |
58 | + void | |
59 | + findOptimalScale() | |
60 | + { | |
61 | + /* Method for finding the best scale for the spider. | |
62 | + changes the x, y, z size of the spider to minimize the cost | |
63 | + function. */ | |
64 | + } | |
65 | + | |
66 | + void | |
67 | + Evaluate() | |
68 | + { | |
69 | + /* Uses uniform sampler2D in order to take a difference between | |
70 | + the colors of two textures. 1st texture is the spider template, | |
71 | + the 2nd is the location of the spider's overlap with the | |
72 | + gl_template | |
73 | + | |
74 | + does the spider need to track it's location? Prob not since | |
75 | + position can be set with gl_texture coordinates */ | |
76 | + | |
77 | + } | |
78 | + | |
79 | + void | |
80 | + Optimize() | |
81 | + { | |
82 | + /*find the optimum direction and scale */ | |
83 | + } | |
84 | + /* | |
85 | + void | |
86 | + Step() | |
87 | + { | |
88 | + // move to the new position | |
89 | + } | |
90 | + */ | |
91 | + public: | |
92 | + | |
93 | + stim::rect<float> hor; | |
94 | + stim::rect<float> ver; | |
95 | + | |
96 | + | |
97 | + | |
98 | + gl_spider | |
99 | + () | |
100 | + { | |
101 | + setPosition(0.0,0.0,0.0); | |
102 | + setDirection(1.0,1.0,1.0); | |
103 | + setMagnitude(0.1,0.1); | |
104 | + //GenerateFBO(400,200); | |
105 | + //Update(); | |
106 | + } | |
107 | + | |
108 | + gl_spider | |
109 | + (vec<float> pos, vec<float> dir, vec<float> mag) | |
110 | + { | |
111 | + position = pos; | |
112 | + direction = dir; | |
113 | + magnitude = mag; | |
114 | + //GenerateFBO(400,200); | |
115 | + //Update(); | |
116 | + } | |
117 | + //temporary cost for convenience. | |
118 | + gl_spider | |
119 | + (float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z, | |
120 | + float mag_x, float mag_y) | |
121 | + { | |
122 | + setPosition(pos_x, pos_y, pos_z); | |
123 | + setDirection(dir_x, dir_y, dir_z); | |
124 | + setMagnitude(mag_x, mag_y); | |
125 | + //GenerateFBO(400,200); | |
126 | + //Update(); | |
127 | + } | |
128 | + | |
129 | + void | |
130 | + attachSpider(GLuint id) | |
131 | + { | |
132 | + texID = id; | |
133 | + GenerateFBO(400,200); | |
134 | + Update(); | |
135 | + } | |
136 | + | |
137 | + void | |
138 | + Update() | |
139 | + { | |
140 | + vec<float> Y(1.0,0.0,0.0); | |
141 | + if(cos(Y.dot(direction))< 0.087){ | |
142 | + Y[0] = 0.0; Y[1] = 1.0;} | |
143 | + hor = stim::rect<float>(magnitude, position, direction.norm(), | |
144 | + ((Y.cross(direction)).cross(direction)).norm()); | |
145 | + ver = stim::rect<float>(magnitude, position, direction.norm(), | |
146 | + hor.n()); | |
147 | + UpdateBuffer(); | |
148 | + } | |
149 | + | |
150 | + vec<float> | |
151 | + getPosition() | |
152 | + { | |
153 | + return position; | |
154 | + } | |
155 | + | |
156 | + vec<float> | |
157 | + getDirection() | |
158 | + { | |
159 | + return direction; | |
160 | + } | |
161 | + | |
162 | + vec<float> | |
163 | + getMagnitude() | |
164 | + { | |
165 | + return magnitude; | |
166 | + } | |
167 | + | |
168 | + void | |
169 | + setPosition(vec<float> pos) | |
170 | + { | |
171 | + position = pos; | |
172 | + } | |
173 | + | |
174 | + void | |
175 | + setPosition(float x, float y, float z) | |
176 | + { | |
177 | + position[0] = x; | |
178 | + position[1] = y; | |
179 | + position[2] = z; | |
180 | + } | |
181 | + | |
182 | + void | |
183 | + setDirection(vec<float> dir) | |
184 | + { | |
185 | + direction = dir; | |
186 | + } | |
187 | + | |
188 | + void | |
189 | + setDirection(float x, float y, float z) | |
190 | + { | |
191 | + direction[0] = x; | |
192 | + direction[1] = y; | |
193 | + direction[2] = z; | |
194 | + } | |
195 | + | |
196 | + void | |
197 | + setMagnitude(vec<float> mag) | |
198 | + { | |
199 | + magnitude = mag; | |
200 | + } | |
201 | + | |
202 | + void | |
203 | + setMagnitude(float x, float y) | |
204 | + { | |
205 | + magnitude[0] = x; | |
206 | + magnitude[1] = y; | |
207 | + } | |
208 | + | |
209 | + GLuint | |
210 | + getFB() | |
211 | + { | |
212 | + return fboID; | |
213 | + } | |
214 | + | |
215 | + void | |
216 | + Step() | |
217 | + { | |
218 | + std::cout << position[0] << "," << position[1] << "," << position[1] | |
219 | + << std::endl; | |
220 | + setPosition(direction*magnitude[1]/2+position); | |
221 | + Update(); | |
222 | + std::cout << position[0] << "," << position[1] << "," << position[1] | |
223 | + << std::endl; | |
224 | + | |
225 | + } | |
226 | + | |
227 | + void | |
228 | + UpdateBuffer() | |
229 | + { | |
230 | + stim::vec<float>p1; | |
231 | + stim::vec<float>p2; | |
232 | + stim::vec<float>p3; | |
233 | + stim::vec<float>p4; | |
234 | + glBindFramebuffer(GL_FRAMEBUFFER, fboID); | |
235 | + glFramebufferTexture2D( | |
236 | + GL_FRAMEBUFFER, | |
237 | + GL_COLOR_ATTACHMENT0, | |
238 | + GL_TEXTURE_2D, | |
239 | + texbufferID, | |
240 | + 0); | |
241 | + glBindFramebuffer(GL_FRAMEBUFFER, fboID); | |
242 | + GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; | |
243 | + glDrawBuffers(1, DrawBuffers); | |
244 | + glBindTexture(GL_TEXTURE_2D, texbufferID); | |
245 | + glClearColor(0,0,0,0); | |
246 | + glClear(GL_COLOR_BUFFER_BIT); | |
247 | + glMatrixMode(GL_PROJECTION); | |
248 | + glLoadIdentity(); | |
249 | + glMatrixMode(GL_MODELVIEW); | |
250 | + glLoadIdentity(); | |
251 | + glViewport(0,0,400,200); | |
252 | + gluOrtho2D(0.0,2.0,0.0,2.0); | |
253 | + glEnable(GL_TEXTURE_3D); | |
254 | + glBindTexture(GL_TEXTURE_3D, texID); | |
255 | + p1 = hor.p(1,1); | |
256 | + p2 = hor.p(1,0); | |
257 | + p3 = hor.p(0,0); | |
258 | + p4 = hor.p(0,1); | |
259 | + glBegin(GL_QUADS); | |
260 | + glTexCoord3f( | |
261 | + p1[0], | |
262 | + p1[1], | |
263 | + p1[2] | |
264 | + ); | |
265 | + glVertex2f(0.0,0.0); | |
266 | + glTexCoord3f( | |
267 | + p2[0], | |
268 | + p2[1], | |
269 | + p2[2] | |
270 | + ); | |
271 | + glVertex2f(1.0, 0.0); | |
272 | + glTexCoord3f( | |
273 | + p3[0], | |
274 | + p3[1], | |
275 | + p3[2] | |
276 | + ); | |
277 | + glVertex2f(1.0, 2.0); | |
278 | + glTexCoord3f( | |
279 | + p4[0], | |
280 | + p4[1], | |
281 | + p4[2] | |
282 | + ); | |
283 | + glVertex2f(0.0, 2.0); | |
284 | + glEnd(); | |
285 | + p1 = ver.p(1,1); | |
286 | + p2 = ver.p(1,0); | |
287 | + p3 = ver.p(0,0); | |
288 | + p4 = ver.p(0,1); | |
289 | + glBegin(GL_QUADS); | |
290 | + glTexCoord3f( | |
291 | + p1[0], | |
292 | + p1[1], | |
293 | + p1[2] | |
294 | + ); | |
295 | + glVertex2f(1.0, 0.0); | |
296 | + glTexCoord3f( | |
297 | + p2[0], | |
298 | + p2[1], | |
299 | + p2[2] | |
300 | + ); | |
301 | + glVertex2f(2.0, 0.0); | |
302 | + glTexCoord3f( | |
303 | + p3[0], | |
304 | + p3[1], | |
305 | + p3[2] | |
306 | + ); | |
307 | + glVertex2f(2.0, 2.0); | |
308 | + glTexCoord3f( | |
309 | + p4[0], | |
310 | + p4[1], | |
311 | + p4[2] | |
312 | + ); | |
313 | + glVertex2f(1.0, 2.0); | |
314 | + glEnd(); | |
315 | + glBindTexture(GL_TEXTURE_3D, 0); | |
316 | + glDisable(GL_TEXTURE_3D); | |
317 | + glBindFramebuffer(GL_FRAMEBUFFER,0); | |
318 | + glBindTexture(GL_TEXTURE_2D, 0); | |
319 | + } | |
320 | + | |
321 | + | |
322 | + void | |
323 | + GenerateFBO(unsigned int width, unsigned int height) | |
324 | + { | |
325 | + glGenFramebuffers(1, &fboID); | |
326 | + glBindFramebuffer(GL_FRAMEBUFFER, fboID); | |
327 | + int numChannels = 1; | |
328 | + unsigned char* texels = new unsigned char[width * height * numChannels]; | |
329 | + glGenTextures(1, &texbufferID); | |
330 | + glBindTexture(GL_TEXTURE_2D, texbufferID); | |
331 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | |
332 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | |
333 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
334 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
335 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, | |
336 | + width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texels); | |
337 | + delete[] texels; | |
338 | + glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
339 | + } | |
340 | + | |
341 | + | |
342 | + void | |
343 | + initCuda() | |
344 | + { | |
345 | + /* cudaDeviceProp prop; | |
346 | + int device; | |
347 | + memset( &prop, 0, sizeof(cudaDeviceProp) ); | |
348 | + prop.major = 1; | |
349 | + prop.minor = 0; | |
350 | + HANDLE_ERROR( cudaChooseDevice (&device, &prop ) ); | |
351 | + HANDLE_ERROR( cudaGetDeviceProperties(&prop, device)); | |
352 | + printf(" device number: %d\n", device); | |
353 | + printf(" device name: %s\n", prop.name); | |
354 | + printf(" device maxTexture3D: %d %d %d\n", prop.maxTexture3D[0] ,prop.maxTexture3D[0], prop.maxTexture3D[2]) ; | |
355 | + HANDLE_ERROR( cudaGLSetGLDevice (device)); */ | |
356 | + stim::cudaSetDevice(); | |
357 | +/* HANDLE_ERROR( | |
358 | + cudaGraphicsMapResources(1, &resource, 0) | |
359 | + ); | |
360 | + HANDLE_ERROR( | |
361 | + cudaGraphicsResourceGetMappedPointer( | |
362 | + &devPtr, | |
363 | + size, | |
364 | + resource)); | |
365 | + HANDLE_ERROR( | |
366 | + cudaGraphicsSubResourceGetMappedArray( | |
367 | + &c_Array, | |
368 | + resource, | |
369 | + 0,0) | |
370 | + ); | |
371 | + HANDLE_ERROR( | |
372 | + cudaBindTextureToArray(fboID, c_Array) | |
373 | + ); | |
374 | + HANDLE_ERROR( | |
375 | + cudaGraphicsUnmapResources(1, &resource, 0) | |
376 | + ); | |
377 | + //need to move the constants to video memory. | |
378 | +*/ | |
379 | + } | |
380 | + | |
381 | + void | |
382 | + createResource() | |
383 | + { | |
384 | + HANDLE_ERROR( | |
385 | + cudaGraphicsGLRegisterImage( | |
386 | + &resource, | |
387 | + fboID, | |
388 | + GL_TEXTURE_2D, | |
389 | + CU_GRAPHICS_REGISTER_FLAGS_NONE) | |
390 | + ); | |
391 | + } | |
392 | + | |
393 | + void | |
394 | + destroyResource() | |
395 | + { | |
396 | + HANDLE_ERROR( | |
397 | + cudaGraphicsUnregisterResource(resource) | |
398 | + ); | |
399 | + } | |
400 | + | |
401 | + float | |
402 | + getCost() | |
403 | + { | |
404 | + createResource(); | |
405 | + float cost = get_cost(resource); | |
406 | + destroyResource(); | |
407 | + return cost; | |
408 | + } | |
409 | +}; | |
410 | +} | |
411 | +#endif | ... | ... |
stim/cuda/cost.h
... | ... | @@ -6,8 +6,8 @@ |
6 | 6 | #include "../visualization/colormap.h" |
7 | 7 | #include <sstream> |
8 | 8 | |
9 | -#define DIM_Y 10890 | |
10 | -#define DIM_X 20 | |
9 | + | |
10 | +///Cost function that works with the gl-spider class to find index of the item with min-cost. | |
11 | 11 | typedef unsigned char uchar; |
12 | 12 | texture<uchar, cudaTextureType2D, cudaReadModeElementType> texIn; |
13 | 13 | float *result; |
... | ... | @@ -24,7 +24,9 @@ inline void checkCUDAerrors(const char *msg) |
24 | 24 | } |
25 | 25 | } |
26 | 26 | |
27 | - | |
27 | +///Finds the sum of all the pixes in a gives template element. | |
28 | +///Returns the abosolute value. | |
29 | +///@param *diff, a pointer to the memory block that holds the pixel-differences. | |
28 | 30 | float get_sum(float *diff) |
29 | 31 | { |
30 | 32 | |
... | ... | @@ -39,6 +41,9 @@ float get_sum(float *diff) |
39 | 41 | return out; |
40 | 42 | } |
41 | 43 | |
44 | +///A virtual representation of a uniform template. | |
45 | +///Returns the value of the template pixel. | |
46 | +///@param x, location of a pixel. | |
42 | 47 | __device__ float Template(int x) |
43 | 48 | { |
44 | 49 | if(x < 20/6 || x > 20*5/6 || (x > 20*2/6 && x < 20*4/6)){ |
... | ... | @@ -49,27 +54,29 @@ __device__ float Template(int x) |
49 | 54 | |
50 | 55 | } |
51 | 56 | |
57 | +///Find the difference of the given set of samples and the template | |
58 | +///using cuda acceleration. | |
59 | +///@param *result, a pointer to the memory that stores the result. | |
52 | 60 | __global__ |
53 | 61 | void get_diff (float *result) |
54 | 62 | { |
55 | 63 | //cuPrintf("Hello"); |
56 | 64 | int x = threadIdx.x + blockIdx.x * blockDim.x; |
57 | 65 | int y = threadIdx.y + blockIdx.y * blockDim.y; |
58 | - int idx = y*DIM_X+x; | |
66 | + int idx = y*20+x; | |
59 | 67 | |
60 | - //float valIn = tex2D(texIn, x, y); | |
61 | 68 | float valIn = tex2D(texIn, x, y)/255.0; |
62 | 69 | float valTemp = Template(x); |
63 | - result[idx] = abs(valIn-valTemp); | |
64 | -// #if __CUDA_ARCH__>=200 | |
65 | -// printf("Value is : %f\n and the result is : %f\n", valIn, result[idx]); | |
66 | -// #endif | |
70 | + result[idx] = abs(valIn-valTemp); | |
67 | 71 | } |
68 | 72 | |
69 | 73 | |
70 | 74 | |
71 | - | |
72 | -void initArray(cudaGraphicsResource_t src) | |
75 | +///Initialization function, allocates the memory and passes the necessary | |
76 | +///handles from OpenGL and Cuda. | |
77 | +///@param src, cudaGraphicsResource that handles the shared OpenGL/Cuda Texture | |
78 | +///@param DIM_Y, integer controlling how much memory to allocate. | |
79 | +void initArray(cudaGraphicsResource_t src, int DIM_Y) | |
73 | 80 | { |
74 | 81 | //cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<uchar> (); |
75 | 82 | //cudaMallocArray(&result, &channelDesc, DIM_X, DIM_Y, 0); |
... | ... | @@ -86,7 +93,7 @@ void initArray(cudaGraphicsResource_t src) |
86 | 93 | HANDLE_ERROR( |
87 | 94 | cudaBindTextureToArray(texIn, srcArray) |
88 | 95 | ); |
89 | - cudaMalloc( (void**) &result, DIM_X*DIM_Y*sizeof(float)); | |
96 | + cudaMalloc( (void**) &result, 20*DIM_Y*sizeof(float)); | |
90 | 97 | checkCUDAerrors("Memory Allocation Issue 1"); |
91 | 98 | cudaMalloc((void **) &v_dif, 20*10*sizeof(float)); |
92 | 99 | checkCUDAerrors("Memory Allocation Issue 2"); |
... | ... | @@ -94,7 +101,9 @@ void initArray(cudaGraphicsResource_t src) |
94 | 101 | // cudaBindTextureToArray(texIn, ptr, &channelDesc) |
95 | 102 | // ); |
96 | 103 | } |
97 | - | |
104 | +///Deinit function that frees the memery used and releases the texture resource | |
105 | +///back to OpenGL. | |
106 | +///@param src, cudaGraphicsResource that handles the shared OpenGL/Cuda Texture | |
98 | 107 | void cleanUP(cudaGraphicsResource_t src) |
99 | 108 | { |
100 | 109 | HANDLE_ERROR( |
... | ... | @@ -110,20 +119,23 @@ void cleanUP(cudaGraphicsResource_t src) |
110 | 119 | cudaFree(v_dif) |
111 | 120 | ); |
112 | 121 | } |
113 | - | |
122 | +///External access-point to the cuda function | |
123 | +///@param src, cudaGraphicsResource that handles the shared OpenGL/Cuda Texture | |
124 | +///@param DIM_Y, the number of samples in the template. | |
125 | +///@inter temporary paramenter that tracks the number of times cost.h was called. | |
114 | 126 | extern "C" |
115 | -int get_cost(cudaGraphicsResource_t src, int inter) | |
127 | +int get_cost(cudaGraphicsResource_t src, int inter, int DIM_Y) | |
116 | 128 | { |
117 | - float output[1089]; | |
129 | + float output[DIM_Y]; | |
118 | 130 | float mini = 10000000000000000.0; |
119 | 131 | int idx; |
120 | - stringstream name; | |
121 | - initArray(src); | |
122 | - dim3 grid(20, 10890); | |
132 | + stringstream name; //for debugging | |
133 | + initArray(src, DIM_Y*10); | |
134 | + dim3 grid(20, DIM_Y*10); | |
123 | 135 | dim3 block(1, 1); |
124 | 136 | get_diff <<< grid, block >>> (result); |
125 | - stim::gpu2image<float>(result, "test.bmp", 20,10890,0,1); | |
126 | - for (int i = 0; i < 1089; i++){ | |
137 | + stim::gpu2image<float>(result, "test.bmp", 20,DIM_Y*10,0,1); | |
138 | + for (int i = 0; i < DIM_Y; i++){ | |
127 | 139 | output[i] = get_sum(result+(20*10*i)); |
128 | 140 | if(output[i] <= mini){ |
129 | 141 | mini = output[i]; | ... | ... |
stim/gl/gl_spider.h
... | ... | @@ -7,18 +7,24 @@ |
7 | 7 | #include <cuda_gl_interop.h> |
8 | 8 | #include <cudaGL.h> |
9 | 9 | #include <math.h> |
10 | -#include "../gl/gl_texture.h" | |
11 | -#include "../visualization/camera.h" | |
12 | -#include "./error.h" | |
13 | -#include "../math/vector.h" | |
14 | -#include "../math/rect.h" | |
15 | -#include "../cuda/cost.h" | |
16 | -#include "../cuda/glbind.h" | |
10 | +#include "stim/gl/gl_texture.h" | |
11 | +#include "stim/visualization/camera.h" | |
12 | +#include "stim/gl/error.h" | |
13 | +#include "stim/math/vector.h" | |
14 | +#include "stim/math/rect.h" | |
15 | +#include "stim/math/matrix.h" | |
16 | +#include "stim/cuda/cost.h" | |
17 | +#include "stim/cuda/glbind.h" | |
17 | 18 | #include <vector> |
18 | 19 | |
19 | 20 | #include <iostream> |
20 | 21 | #include <fstream> |
21 | 22 | |
23 | + | |
24 | + | |
25 | +/* Technically since gl_spider inherits from gl_texture, we could | |
26 | + call the init with a path to an image stack, and upload | |
27 | + the images while creating the spider (calling init) */ | |
22 | 28 | namespace stim |
23 | 29 | { |
24 | 30 | |
... | ... | @@ -37,84 +43,271 @@ class gl_spider : public virtual gl_texture<T> |
37 | 43 | std::vector<stim::vec<float> > dirVectors; |
38 | 44 | std::vector<stim::vec<float> > posVectors; |
39 | 45 | std::vector<stim::vec<float> > magVectors; |
46 | + stim::matrix<float, 4> currentTransform; | |
40 | 47 | using gl_texture<T>::texID; |
41 | 48 | using gl_texture<T>::S; |
42 | - cudaArray* c_Array; | |
43 | - //void** devPtr; | |
44 | - //size_t size; | |
49 | + using gl_texture<T>::R; | |
45 | 50 | cudaGraphicsResource_t resource; |
51 | + | |
52 | + GLuint dList; | |
53 | + GLubyte list[3]; | |
46 | 54 | GLuint fboID; |
47 | 55 | GLuint texbufferID; |
48 | - int iter = 0; //temporary for testing | |
56 | + int iter; //temporary for testing | |
57 | + int numSamples; | |
49 | 58 | |
59 | + /// Method for finding the best scale for the spider. | |
60 | + /// changes the x, y, z size of the spider to minimize the cost | |
61 | + /// function. | |
62 | + void | |
63 | + findOptimalDirection() | |
64 | + { | |
65 | + //genTemplate(dirVectors, 0); | |
66 | + Bind(); | |
67 | + positionTemplate(); | |
68 | + glCallList(dList); | |
69 | + int best = getCost(); | |
70 | + stim::vec<float, 4> next; | |
71 | + next[0] = dirVectors[best][0]*S[1]*R[1]; | |
72 | + next[1] = dirVectors[best][1]*S[2]*R[2]; | |
73 | + next[2] = dirVectors[best][2]*S[3]*R[3]; | |
74 | + next[3] = 1; | |
75 | + next = (currentTransform*next).norm(); | |
76 | + setPosition( position[0]+next[0]*magnitude[0]/3, | |
77 | + position[1]+next[1]*magnitude[0]/3, | |
78 | + position[2]+next[2]*magnitude[0]/3); | |
79 | + setDirection(next[0], next[1], next[2]); | |
80 | + Unbind(); | |
81 | + } | |
82 | + | |
83 | + /// Method for finding the best direction for the spider. | |
84 | + /// Not sure if necessary since the next position for the spider | |
85 | + /// will be at direction * magnitude. | |
50 | 86 | void |
51 | 87 | findOptimalPosition() |
52 | 88 | { |
53 | - /* Method for finding the best direction for the spider. | |
54 | - Not sure if necessary since the next position for the spider | |
55 | - will be at direction * magnitude. */ | |
56 | - genTemplate(posVectors, 1); | |
89 | + Bind(); | |
90 | + positionTemplate(); | |
91 | + glCallList(dList+1); | |
57 | 92 | int best = getCost(); |
93 | + stim::vec<float, 4> next; | |
94 | + next[0] = posVectors[best][0]; | |
95 | + next[1] = posVectors[best][1]; | |
96 | + next[2] = posVectors[best][2]; | |
97 | + next[3] = 1; | |
98 | + next = currentTransform*next; | |
99 | + std::cout << "Optimal Position:"<< next << std::endl; | |
100 | + setPosition( next[0]*S[1]*R[1], | |
101 | + next[1]*S[2]*R[2], | |
102 | + next[2]*S[3]*R[3]); | |
103 | + Unbind(); | |
58 | 104 | } |
59 | 105 | |
106 | + /// Method for finding the best scale for the spider. | |
107 | + /// changes the x, y, z size of the spider to minimize the cost | |
108 | + /// function. */ | |
60 | 109 | void |
61 | 110 | findOptimalScale() |
62 | 111 | { |
63 | - /* Method for finding the best scale for the spider. | |
64 | - changes the x, y, z size of the spider to minimize the cost | |
65 | - function. */ | |
66 | - genTemplate(magVectors, 1); | |
112 | + Bind(); | |
113 | + positionTemplate(); | |
114 | + glCallList(dList+2); | |
67 | 115 | int best = getCost(); |
116 | + stim::vec<float, 4> next; | |
117 | + next[0] = magVectors[best][0]*S[1]*R[1]; | |
118 | + next[1] = magVectors[best][1]*S[2]*R[2]; | |
119 | + next[2] = magVectors[best][2]*S[3]*R[3]; | |
120 | + next[3] = 1; | |
121 | + next = currentTransform*next; | |
122 | + std::cout << "Optimal Scale:"<< next << std::endl; | |
123 | + setMagnitude(next[0]); | |
124 | + Unbind(); | |
68 | 125 | } |
69 | 126 | |
127 | + | |
128 | + | |
70 | 129 | void |
71 | - findOptimalDirection() | |
130 | + Optimize() | |
72 | 131 | { |
73 | - /* Method for finding the best scale for the spider. | |
74 | - changes the x, y, z size of the spider to minimize the cost | |
75 | - function. */ | |
76 | - genTemplate(dirVectors, 1); | |
77 | - int best = getCost(); | |
132 | + /*find the optimum direction and scale */ | |
78 | 133 | } |
79 | 134 | |
80 | 135 | |
136 | + | |
137 | + | |
138 | +//--------------------------------------------------------------------------// | |
139 | +//---------------------TEMPLATE CREATION METHODS----------------------------// | |
140 | +//--------------------------------------------------------------------------// | |
141 | + | |
142 | + ///@param solidAngle, the size of the arc to sample. | |
143 | + ///Method for populating the vector arrays with sampled vectors. | |
144 | + ///uses the default direction vector <0,0,1> | |
81 | 145 | void |
82 | - Optimize() | |
146 | + genDirectionVectors(float solidAngle = M_PI) | |
83 | 147 | { |
84 | - /*find the optimum direction and scale */ | |
148 | + vec<float> d_s = direction.cart2sph().norm(); | |
149 | + vec<float> temp; | |
150 | + int dim = (sqrt(numSamples)-1)/2; | |
151 | + float p0 = -M_PI; | |
152 | + float dt = solidAngle/(2.0 * ((float)dim + 1.0)); | |
153 | + float dp = p0/(2.0*((float)dim + 1.0)); | |
154 | + | |
155 | + for(int i = -dim; i <= dim; i++){ | |
156 | + for(int j = -dim; j <= dim; j++){ | |
157 | + //Create linear index | |
158 | + | |
159 | + temp[0] = d_s[0]; //rotate vector | |
160 | + temp[1] = d_s[1]+dp*(float) i; | |
161 | + temp[2] = d_s[2]+dt*(float) j; | |
162 | + | |
163 | + temp = (temp.sph2cart()).norm(); //back to cart | |
164 | + dirVectors.push_back(temp); | |
165 | + } | |
166 | + } | |
85 | 167 | } |
86 | - /* | |
168 | + | |
169 | + ///@param solidAngle, the size of the arc to sample. | |
170 | + ///Method for populating the buffer with the sampled texture. | |
171 | + ///uses the default vector <0,0,0> | |
87 | 172 | void |
88 | - Step() | |
173 | + genPositionVectors(float delta = 0.2) | |
89 | 174 | { |
90 | - // move to the new position | |
175 | + ofstream file; | |
176 | + //file.open("dvectors.txt"); | |
177 | + vec<float> temp; | |
178 | + int dim = (sqrt(numSamples)-1)/2; | |
179 | + stim::rect<float> samplingPlane = | |
180 | + stim::rect<float>(magnitude[0]*delta, position, direction); | |
181 | + float step = 1.0/(dim); | |
182 | + //Loop over the samples, keeping the original position sample | |
183 | + //in the center of the resulting texture. | |
184 | + | |
185 | + for(int i = -dim; i <= dim; i++){ | |
186 | + for(int j = -dim; j <= dim; j++){ | |
187 | + //Create linear index | |
188 | + | |
189 | + temp = samplingPlane.p( | |
190 | + 0.5+step*i, | |
191 | + 0.5+step*j | |
192 | + ); | |
193 | + //file << temp[0] << ";" << temp[1] | |
194 | + // << ";" << temp[2] << endl; | |
195 | + posVectors.push_back(temp); | |
196 | + } | |
197 | + } | |
91 | 198 | } |
92 | - */ | |
93 | - | |
199 | + | |
200 | + ///@param solidAngle, the size of the arc to sample. | |
201 | + ///Method for populating the buffer with the sampled texture. | |
202 | + ///uses the default magnitude <1,1,0> | |
94 | 203 | void |
95 | - GenerateFBO(unsigned int width, unsigned int height) | |
204 | + genMagnitudeVectors(float delta = 0.5) | |
96 | 205 | { |
97 | - glGenFramebuffers(1, &fboID); | |
98 | - glBindFramebuffer(GL_FRAMEBUFFER, fboID); | |
99 | - int numChannels = 1; | |
100 | - unsigned char* texels = new unsigned char[width * height * numChannels]; | |
101 | - glGenTextures(1, &texbufferID); | |
102 | - glBindTexture(GL_TEXTURE_2D, texbufferID); | |
103 | - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | |
104 | - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | |
105 | - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
106 | - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
107 | - glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, | |
108 | - width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texels); | |
109 | - delete[] texels; | |
110 | - glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
111 | - glBindTexture(GL_TEXTURE_2D, 0); | |
206 | + | |
207 | + int dim = (sqrt(numSamples)-1)/2; | |
208 | + float min = 1.0-delta; | |
209 | + float max = 1.0+delta; | |
210 | + float step = (max-min)/(numSamples-1); | |
211 | + float factor; | |
212 | + vec<float> temp; | |
213 | + | |
214 | + for(int i = 0; i < numSamples; i++){ | |
215 | + //Create linear index | |
216 | + factor = (min+step*i)*magnitude[0]; | |
217 | + temp = factor; | |
218 | + magVectors.push_back(temp); | |
219 | + } | |
112 | 220 | } |
221 | + ///@param vector of stim::vec in that stores all of the samplable vectors. | |
222 | + ///@param type, one of three operations, 0 for Direction vectors | |
223 | + /// 1 for Position, 2 for Magnitude. | |
224 | + ///Function for filling the buffer up with the data based on the vectors | |
225 | + ///Each vector represents a two rectangular templates. | |
226 | + ///Loops through all of the vectors and transfers rect. associated with it | |
227 | + ///Into buffer-space. | |
228 | + void | |
229 | + genTemplate(std::vector<stim::vec<float> > in, int type) | |
230 | + { | |
231 | + float x = 0.0; | |
232 | + vec<float> Y(1.0,0.0,0.0); | |
233 | + vec<float> pos(0.0,0.0,0.0); | |
234 | + vec<float> mag(1.0, 1.0, 1.0); | |
235 | + vec<float> dir(0.0, 0.0, 1.0); | |
236 | + switch(type) { | |
237 | + case 0: //Direction | |
238 | + Bind(); | |
239 | + glNewList(dList+type, GL_COMPILE); | |
240 | + for(int i = 0; i < in.size(); i++) | |
241 | + { | |
242 | + if(cos(Y.dot(in[i]))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} | |
243 | + else{Y[0] = 1.0; Y[1] = 0.0;} | |
244 | + | |
245 | + hor = stim::rect<float>(mag, | |
246 | + pos, in[i], | |
247 | + ((Y.cross(in[i])).cross(in[i])).norm()); | |
248 | + ver = stim::rect<float>(mag, | |
249 | + pos, in[i], | |
250 | + hor.n()); | |
251 | + UpdateBuffer(x, x+i*10.0); | |
252 | + } | |
253 | + glEndList(); | |
254 | + Unbind(); | |
255 | + break; | |
256 | + case 1: //Position | |
257 | + Bind(); | |
258 | + glNewList(dList+type, GL_COMPILE); | |
259 | + if(cos(Y.dot(direction))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} | |
260 | + else{Y[0] = 1.0; Y[1] = 0.0;} | |
261 | + | |
262 | + for(int i = 0; i < in.size(); i++) | |
263 | + { | |
264 | + hor = stim::rect<float>(mag, | |
265 | + in[i], dir, | |
266 | + ((Y.cross(direction)).cross(direction)) | |
267 | + .norm()); | |
268 | + ver = stim::rect<float>(mag, | |
269 | + in[i], dir, | |
270 | + hor.n()); | |
271 | + UpdateBuffer(x, x+i*10.0); | |
272 | + } | |
273 | + glEndList(); | |
274 | + Unbind(); | |
275 | + break; | |
276 | + case 2: //Scale | |
277 | + Bind(); | |
278 | + glNewList(dList+type, GL_COMPILE); | |
279 | + if(cos(Y.dot(direction))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} | |
280 | + else{Y[0] = 1.0; Y[1] = 0.0;} | |
113 | 281 | |
282 | + for(int i = 0; i < in.size(); i++) | |
283 | + { | |
284 | + hor = stim::rect<float>(in[i], | |
285 | + pos, dir, | |
286 | + ((Y.cross(direction)).cross(direction)) | |
287 | + .norm()); | |
288 | + ver = stim::rect<float>(in[i], | |
289 | + pos, dir, | |
290 | + hor.n()); | |
291 | + UpdateBuffer(x, x+i*10.0); | |
292 | + } | |
293 | + glEndList(); | |
294 | + Unbind(); | |
295 | + break; | |
296 | + default: | |
297 | + std::cout << "unknown case have been passed" | |
298 | + << std::endl; | |
299 | + break; | |
300 | + } | |
301 | + Unbind(); | |
302 | + CHECK_OPENGL_ERROR | |
303 | + } | |
304 | + ///@param v_x x-coordinate in buffer-space, | |
305 | + ///@param v_y y-coordinate in buffer-space. | |
306 | + ///Samples the texturespace and places a sample in the provided coordinates | |
307 | + ///of bufferspace. | |
114 | 308 | void |
115 | 309 | UpdateBuffer(float v_x, float v_y) |
116 | 310 | { |
117 | - //std::cout << v_y << std::endl; | |
118 | 311 | float len = 10.0; |
119 | 312 | stim::vec<float>p1; |
120 | 313 | stim::vec<float>p2; |
... | ... | @@ -126,158 +319,97 @@ class gl_spider : public virtual gl_texture<T> |
126 | 319 | p4 = hor.p(0,1); |
127 | 320 | glBegin(GL_QUADS); |
128 | 321 | glTexCoord3f( |
129 | - // p1[0]/S[1], | |
130 | - // p1[1]/S[2], | |
131 | - // p1[2]/S[3] | |
132 | - // ); | |
133 | 322 | p1[0], |
134 | 323 | p1[1], |
135 | 324 | p1[2] |
136 | 325 | ); |
137 | - //glVertex2f(0.0,0.0); | |
138 | 326 | glVertex2f(v_x,v_y); |
139 | 327 | glTexCoord3f( |
140 | - // p2[0]/S[1], | |
141 | - // p2[1]/S[2], | |
142 | - // p2[2]/S[3] | |
143 | 328 | p2[0], |
144 | 329 | p2[1], |
145 | 330 | p2[2] |
146 | 331 | ); |
147 | - //glVertex2f(1.0, 0.0); | |
148 | 332 | glVertex2f(v_x+len, v_y); |
149 | 333 | glTexCoord3f( |
150 | - // p2[0]/S[1], | |
151 | - // p2[1]/S[2], | |
152 | - // p2[2]/S[3] | |
153 | 334 | p3[0], |
154 | 335 | p3[1], |
155 | 336 | p3[2] |
156 | 337 | ); |
157 | - //glVertex2f(1.0, 2.0); | |
158 | 338 | glVertex2f(v_x+len, v_y+len); |
159 | 339 | glTexCoord3f( |
160 | - // p4[0]/S[1], | |
161 | - // p4[1]/S[2], | |
162 | - // p4[2]/S[3] | |
163 | 340 | p4[0], |
164 | 341 | p4[1], |
165 | 342 | p4[2] |
166 | 343 | ); |
167 | - //glVertex2f(0.0, 2.0); | |
168 | 344 | glVertex2f(v_x, v_y+len); |
169 | 345 | glEnd(); |
346 | + | |
170 | 347 | p1 = ver.p(1,1); |
171 | 348 | p2 = ver.p(1,0); |
172 | 349 | p3 = ver.p(0,0); |
173 | 350 | p4 = ver.p(0,1); |
174 | 351 | glBegin(GL_QUADS); |
175 | 352 | glTexCoord3f( |
176 | - // p1[0]/S[1], | |
177 | - // p1[1]/S[2], | |
178 | - // p1[2]/S[3] | |
179 | 353 | p1[0], |
180 | 354 | p1[1], |
181 | 355 | p1[2] |
182 | 356 | ); |
183 | - //glVertex2f(1.0, 0.0); | |
184 | 357 | glVertex2f(v_x+len, v_y); |
185 | 358 | glTexCoord3f( |
186 | - // p2[0]/S[1], | |
187 | - // p2[1]/S[2], | |
188 | - // p2[2]/S[3] | |
189 | 359 | p2[0], |
190 | 360 | p2[1], |
191 | 361 | p2[2] |
192 | 362 | ); |
193 | - //glVertex2f(2.0, 0.0); | |
194 | 363 | glVertex2f(v_x+2*len, v_y); |
195 | 364 | glTexCoord3f( |
196 | - // p3[0]/S[1], | |
197 | - // p3[1]/S[2], | |
198 | - // p3[2]/S[3] | |
199 | 365 | p3[0], |
200 | 366 | p3[1], |
201 | 367 | p3[2] |
202 | 368 | ); |
203 | - //glVertex2f(2.0, 2.0); | |
204 | 369 | glVertex2f(v_x+2*len, v_y+len); |
205 | 370 | glTexCoord3f( |
206 | - // p4[0]/S[1], | |
207 | - // p4[1]/S[2], | |
208 | - // p4[2]/S[3] | |
209 | 371 | p4[0], |
210 | 372 | p4[1], |
211 | 373 | p4[2] |
212 | 374 | ); |
213 | - //glVertex2f(1.0, 2.0); | |
214 | 375 | glVertex2f(v_x+len, v_y+len); |
215 | - glEnd(); | |
376 | + glEnd(); | |
377 | + CHECK_OPENGL_ERROR | |
216 | 378 | } |
379 | + | |
217 | 380 | |
218 | - void | |
219 | - genTemplate(std::vector<stim::vec<float> > in, int type = 0) | |
220 | - { | |
221 | - float x = 0.0; | |
222 | - vec<float> Y(1.0,0.0,0.0); | |
223 | - Bind(); | |
224 | - switch(type) { | |
225 | - case 0: //Direction | |
226 | - for(int i = 0; i < in.size(); i++) | |
227 | - { | |
228 | - if(cos(Y.dot(in[i]))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} | |
229 | - else{Y[0] = 1.0; Y[1] = 0.0;} | |
230 | - | |
231 | - hor = stim::rect<float>(magnitude, | |
232 | - position, in[i], | |
233 | - ((Y.cross(in[i])).cross(in[i])).norm()); | |
234 | - ver = stim::rect<float>(magnitude, | |
235 | - position, in[i], | |
236 | - hor.n()); | |
237 | - UpdateBuffer(x, x+i*10.0); | |
238 | - } | |
239 | - break; | |
240 | - case 1: //Position | |
241 | - if(cos(Y.dot(direction))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} | |
242 | - else{Y[0] = 1.0; Y[1] = 0.0;} | |
243 | 381 | |
244 | - for(int i = 0; i < in.size(); i++) | |
245 | - { | |
246 | - hor = stim::rect<float>(magnitude, in[i], direction, | |
247 | - ((Y.cross(direction)).cross(direction)) | |
248 | - .norm()); | |
249 | - ver = stim::rect<float>(magnitude, in[i], direction, | |
250 | - hor.n()); | |
251 | - UpdateBuffer(x, x+i*10.0); | |
252 | - } | |
253 | - break; | |
254 | - case 2: //Scale | |
255 | - if(cos(Y.dot(direction))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} | |
256 | - else{Y[0] = 1.0; Y[1] = 0.0;} | |
382 | +//--------------------------------------------------------------------------// | |
383 | +//--------------------------------GL METHODS--------------------------------// | |
384 | +//--------------------------------------------------------------------------// | |
257 | 385 | |
258 | - for(int i = 0; i < in.size(); i++) | |
259 | - { | |
260 | - hor = stim::rect<float>(in[i], position, direction, | |
261 | - ((Y.cross(direction)).cross(direction)) | |
262 | - .norm()); | |
263 | - ver = stim::rect<float>(in[i], position, direction, | |
264 | - hor.n()); | |
265 | - } | |
266 | - break; | |
267 | - default: | |
268 | - std::cout << "unknown case have been passed" | |
269 | - << std::endl; | |
270 | - break; | |
271 | - } | |
272 | - Unbind(); | |
386 | + ///@param width sets the width of the buffer. | |
387 | + ///@param height sets the height of the buffer. | |
388 | + ///Function for setting up the 2D buffer that stores the samples. | |
389 | + void | |
390 | + GenerateFBO(unsigned int width, unsigned int height) | |
391 | + { | |
392 | + glGenFramebuffers(1, &fboID); | |
393 | + glBindFramebuffer(GL_FRAMEBUFFER, fboID); | |
394 | + int numChannels = 1; | |
395 | + unsigned char* texels = new unsigned char[width * height * numChannels]; | |
396 | + glGenTextures(1, &texbufferID); | |
397 | + glBindTexture(GL_TEXTURE_2D, texbufferID); | |
398 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | |
399 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | |
400 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
401 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
402 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, | |
403 | + width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texels); | |
404 | + delete[] texels; | |
405 | + glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
406 | + glBindTexture(GL_TEXTURE_2D, 0); | |
273 | 407 | } |
274 | - | |
275 | - /* | |
276 | - Method for controling the buffer and texture binding in order to properly | |
277 | - do the render to texture. | |
278 | - */ | |
408 | + | |
409 | + ///Method for controling the buffer and texture binding in order to properly | |
410 | + ///do the render to texture. | |
279 | 411 | void |
280 | - Bind(int numSamples = 1089) | |
412 | + Bind() | |
281 | 413 | { |
282 | 414 | float len = 10.0; |
283 | 415 | glBindFramebuffer(GL_FRAMEBUFFER, fboID);//set up GL buffer |
... | ... | @@ -291,7 +423,7 @@ class gl_spider : public virtual gl_texture<T> |
291 | 423 | GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; |
292 | 424 | glDrawBuffers(1, DrawBuffers); |
293 | 425 | glBindTexture(GL_TEXTURE_2D, texbufferID); |
294 | - glClearColor(0,0,0,0); | |
426 | + glClearColor(1,1,1,1); | |
295 | 427 | glClear(GL_COLOR_BUFFER_BIT); |
296 | 428 | glMatrixMode(GL_PROJECTION); |
297 | 429 | glLoadIdentity(); |
... | ... | @@ -301,11 +433,11 @@ class gl_spider : public virtual gl_texture<T> |
301 | 433 | gluOrtho2D(0.0,2.0*len,0.0,numSamples*len); |
302 | 434 | glEnable(GL_TEXTURE_3D); |
303 | 435 | glBindTexture(GL_TEXTURE_3D, texID); |
436 | + | |
437 | + CHECK_OPENGL_ERROR | |
304 | 438 | } |
305 | 439 | |
306 | - /* | |
307 | - Method for Unbinding all of the texture resources | |
308 | - */ | |
440 | + ///Method for Unbinding all of the texture resources | |
309 | 441 | void |
310 | 442 | Unbind() |
311 | 443 | { |
... | ... | @@ -316,88 +448,42 @@ class gl_spider : public virtual gl_texture<T> |
316 | 448 | glBindTexture(GL_TEXTURE_2D, 0); |
317 | 449 | } |
318 | 450 | |
319 | - /* | |
320 | - Method for populating the buffer with the sampled texture. | |
321 | - usually uses the default direction vector and then | |
322 | - */ | |
323 | - void | |
324 | - genDirectionVectors(int numSamples = 1089, float solidAngle = M_PI/1.5) | |
451 | + ///Method for using the gl manipulation to alighn templates from | |
452 | + ///Template space (-0.5 0.5) to Texture space (0.0, 1.0), | |
453 | + ///Based on the position of the spider in real space (arbitrary). | |
454 | + void positionTemplate() | |
325 | 455 | { |
326 | - | |
327 | - vec<float> d_s = direction.cart2sph(); | |
328 | - dirVectors.resize(1089); | |
329 | - vec<float> temp; | |
330 | - int dim = (sqrt(numSamples)-1)/2; | |
331 | - float p0 = M_PI/3; | |
332 | - float dt = solidAngle/(1.0 * dim); | |
333 | - float dp = p0/(1.0*dim); | |
334 | - | |
335 | - for(int i = -dim; i <= dim; i++){ | |
336 | - for(int j = -dim; j <= dim; j++){ | |
337 | - //Create linear index | |
338 | - | |
339 | - temp[0] = d_s[0]; //rotate vector | |
340 | - temp[1] = d_s[1]+dt*i; | |
341 | - temp[2] = d_s[2]+dp*j; | |
342 | - | |
343 | - temp = (temp.sph2cart()).norm(); //back to cart | |
344 | - dirVectors.push_back(temp); | |
345 | - } | |
346 | - } | |
347 | - } | |
456 | + stim::vec<float, 4> rot = getRotation(direction); | |
457 | + glMatrixMode(GL_TEXTURE); | |
458 | + glLoadIdentity(); | |
348 | 459 | |
349 | - void | |
350 | - genPositionVectors(int numSamples = 1089, float delta = 0.2) | |
351 | - { | |
460 | + glRotatef(rot[0], rot[1], rot[2], rot[3]); | |
461 | + glScalef(1.0/S[1]/R[1], 1.0/S[2]/R[2], 1.0/S[3]/R[3]); | |
462 | + glTranslatef(position[0], | |
463 | + position[1], | |
464 | + position[2]); | |
465 | + glScalef(magnitude[0], | |
466 | + magnitude[1], | |
467 | + magnitude[0]); | |
468 | + | |
469 | + float curTrans[16]; | |
470 | + glGetFloatv(GL_TEXTURE_MATRIX, curTrans); | |
471 | + fillTransform(curTrans); | |
472 | + printTransform(); | |
352 | 473 | |
353 | - vec<float> temp; | |
354 | - int dim = (sqrt(numSamples)-1)/2; | |
355 | - stim::rect<float> samplingPlane = | |
356 | - stim::rect<float>(magnitude[0]*delta, position, direction); | |
357 | - float step = 1.0/(dim); | |
358 | - //Loop over the samples, keeping the original position sample | |
359 | - //in the center of the resulting texture. | |
360 | - | |
361 | - int idx; | |
362 | - for(int i = -dim; i <= dim; i++){ | |
363 | - for(int j = -dim; j <= dim; j++){ | |
364 | - //Create linear index | |
365 | - | |
366 | - temp = samplingPlane.p( | |
367 | - 0.5+step*i, | |
368 | - 0.5+step*j | |
369 | - ); | |
370 | - dirVectors.push_back(temp); | |
371 | - } | |
372 | - } | |
474 | + CHECK_OPENGL_ERROR | |
475 | + glMatrixMode(GL_MODELVIEW); | |
373 | 476 | } |
477 | + | |
478 | + | |
374 | 479 | |
375 | - void | |
376 | - genMagnitudeVectors(int numSamples = 1089, float delta = 0.5) | |
377 | - { | |
378 | - | |
379 | - int dim = (sqrt(numSamples)-1)/2; | |
380 | - float min = 1.0-delta; | |
381 | - float max = 1.0+delta; | |
382 | - float step = (max-min)/(numSamples-1); | |
383 | - float factor; | |
384 | - vec<float> temp; | |
385 | - | |
386 | - for(int i = 0; i < numSamples; i++){ | |
387 | - //Create linear index | |
388 | - factor = (min+step*i)*magnitude[0]; | |
389 | - temp[0] = factor; | |
390 | - temp[1] = factor; | |
391 | - magVectors.push_back(temp); | |
392 | - } | |
393 | - } | |
394 | 480 | |
395 | 481 | //--------------------------------------------------------------------------// |
396 | 482 | //--------------------------------CUDA METHODS------------------------------// |
397 | 483 | //--------------------------------------------------------------------------// |
398 | 484 | |
399 | - /* Method for registering the texture with Cuda for shared | |
400 | - access */ | |
485 | + /// Method for registering the texture with Cuda for shared | |
486 | + /// access. | |
401 | 487 | void |
402 | 488 | createResource() |
403 | 489 | { |
... | ... | @@ -411,7 +497,7 @@ class gl_spider : public virtual gl_texture<T> |
411 | 497 | ); |
412 | 498 | } |
413 | 499 | |
414 | - /* Method for freeing the texture from Cuda for gl access */ | |
500 | + ///Method for freeing the texture from Cuda for gl access. | |
415 | 501 | void |
416 | 502 | destroyResource() |
417 | 503 | { |
... | ... | @@ -420,23 +506,27 @@ class gl_spider : public virtual gl_texture<T> |
420 | 506 | ); |
421 | 507 | } |
422 | 508 | |
423 | - /* Entry-point into the cuda code for calculating the cost | |
424 | - of a given samples array (in texture form) */ | |
509 | + ///Entry-point into the cuda code for calculating the cost | |
510 | + /// of a given samples array (in texture form) | |
425 | 511 | int |
426 | 512 | getCost() |
427 | 513 | { |
428 | 514 | createResource(); |
429 | - int cost = get_cost(resource, iter); | |
515 | + int cost = get_cost(resource, iter, numSamples); | |
430 | 516 | destroyResource(); |
431 | 517 | iter++; |
432 | 518 | return cost; |
433 | 519 | } |
434 | 520 | |
435 | 521 | public: |
436 | - | |
437 | 522 | stim::rect<float> hor; |
438 | 523 | stim::rect<float> ver; |
439 | 524 | |
525 | +//--------------------------------------------------------------------------// | |
526 | +//-----------------------------CONSTRUCTORS---------------------------------// | |
527 | +//--------------------------------------------------------------------------// | |
528 | + | |
529 | + | |
440 | 530 | ///Default Constructor |
441 | 531 | gl_spider |
442 | 532 | () |
... | ... | @@ -444,6 +534,19 @@ class gl_spider : public virtual gl_texture<T> |
444 | 534 | setPosition(0.0,0.0,0.0); |
445 | 535 | setDirection(0.0,0.0,1.0); |
446 | 536 | setMagnitude(1.0); |
537 | + numSamples = 1089; | |
538 | + //numSamples = 9; | |
539 | + } | |
540 | + ///@param samples, the number of samples this spider is going to use. | |
541 | + ///best results if samples is can create a perfect root. | |
542 | + ///Default Constructor | |
543 | + gl_spider | |
544 | + (int samples) | |
545 | + { | |
546 | + setPosition(0.0,0.0,0.0); | |
547 | + setDirection(0.0,0.0,1.0); | |
548 | + setMagnitude(1.0); | |
549 | + numSamples = samples; | |
447 | 550 | } |
448 | 551 | |
449 | 552 | ///temporary constructor for convenience, will be removed in further updates. |
... | ... | @@ -451,38 +554,39 @@ class gl_spider : public virtual gl_texture<T> |
451 | 554 | (float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z, |
452 | 555 | float mag_x) |
453 | 556 | { |
454 | - //setPosition(pos_x, pos_y, pos_z); | |
455 | - //setDirection(dir_x, dir_y, dir_z); | |
456 | - //setMagnitude(mag_x); | |
557 | + setPosition(pos_x, pos_y, pos_z); | |
558 | + setDirection(dir_x, dir_y, dir_z); | |
559 | + setMagnitude(mag_x); | |
560 | + | |
457 | 561 | } |
458 | 562 | ///@param GLuint id texture that is going to be sampled. |
459 | - ///@param numSamples number of samples per operation. | |
460 | 563 | ///Attached the spider to the texture with the given GLuint ID. |
461 | - ///Samples in the default direction acting as the init method. | |
564 | + ///Samples in the default direction acting as the init method. | |
565 | + ///Also acts an init. | |
462 | 566 | void |
463 | - attachSpider(GLuint id, int numSamples = 1089) | |
567 | + attachSpider(GLuint id) | |
464 | 568 | { |
465 | 569 | texID = id; |
570 | + iter = 0; ///for debugging purposes | |
466 | 571 | GenerateFBO(20, numSamples*10); |
467 | - //sampleDirection(); | |
468 | 572 | genDirectionVectors(); |
573 | + genPositionVectors(); | |
574 | + genMagnitudeVectors(); | |
575 | + | |
469 | 576 | gl_texture<T>::setDims(0.6, 0.6, 1.0); |
470 | - //gl_texture<T>::setDims(1.0, 1.0,1.0); | |
577 | + setSize(512, 512, 426); | |
578 | + | |
579 | + dList = glGenLists(3); | |
580 | + list[0] = 0; list[1] = 1; list[2] = 2; | |
581 | + glListBase(dList); | |
582 | + genTemplate(dirVectors, 0); | |
583 | + genTemplate(posVectors, 1); | |
584 | + genTemplate(magVectors, 2); | |
471 | 585 | } |
472 | 586 | |
473 | - ///temporary Method necessary for visualization and testing. | |
474 | - void | |
475 | - Update() | |
476 | - { | |
477 | - vec<float> Y(1.0,0.0,0.0); | |
478 | - if(cos(Y.dot(direction))< 0.087){ | |
479 | - Y[0] = 0.0; Y[1] = 1.0;} | |
480 | - hor = stim::rect<float>(magnitude, position, direction.norm(), | |
481 | - ((Y.cross(direction)).cross(direction)).norm()); | |
482 | - ver = stim::rect<float>(magnitude, position, direction.norm(), | |
483 | - hor.n()); | |
484 | - } | |
485 | - | |
587 | +//--------------------------------------------------------------------------// | |
588 | +//-----------------------------ACCESS METHODS-------------------------------// | |
589 | +//--------------------------------------------------------------------------// | |
486 | 590 | ///Returns the position vector. |
487 | 591 | vec<float> |
488 | 592 | getPosition() |
... | ... | @@ -523,7 +627,6 @@ class gl_spider : public virtual gl_texture<T> |
523 | 627 | position[1] = y; |
524 | 628 | position[2] = z; |
525 | 629 | } |
526 | - | |
527 | 630 | |
528 | 631 | ///@param vector dir, the new direction. |
529 | 632 | ///Sets the direction vector to input vector dir. |
... | ... | @@ -563,29 +666,89 @@ class gl_spider : public virtual gl_texture<T> |
563 | 666 | magnitude[1] = mag; |
564 | 667 | } |
565 | 668 | |
566 | - ///temporary method for visualization. | |
669 | + void | |
670 | + setSize(int x, int y, int z) | |
671 | + { | |
672 | + R[1] = x; | |
673 | + R[2] = y; | |
674 | + R[3] = z; | |
675 | + } | |
676 | + | |
677 | + ///@param dir, the vector to which we are rotating | |
678 | + ///given a vector to align to, finds the required | |
679 | + ///axis and angle for glRotatef | |
680 | + stim::vec<float, 4> | |
681 | + getRotation(stim::vec<float> dir) | |
682 | + { | |
683 | + stim::vec<float, 4> out; | |
684 | + stim::vec<float> from(0,0,1); | |
685 | + out[0] = acos(from.dot(dir))*M_PI/180; | |
686 | + if(out[0] < 0.0001){ | |
687 | + out[0] = 0.0; | |
688 | + out[1] = 0.0; | |
689 | + out[2] = 0.0; | |
690 | + out[3] = 1.0; | |
691 | + } else { | |
692 | + stim::vec<float> temp; | |
693 | + temp = (from.cross(dir)).norm(); | |
694 | + out[1] = temp[0]; | |
695 | + out[2] = temp[1]; | |
696 | + out[3] = temp[2]; | |
697 | + } | |
698 | + return out; | |
699 | + } | |
700 | + ///Function that fills the transform data from GL output to a matrix format. | |
701 | + ///@param mat, a 16 value matrix representing the transformation in row order. | |
702 | + void | |
703 | + fillTransform(float mat[16]) | |
704 | + { | |
705 | + for(int r = 0; r < 4; r++){ | |
706 | + for(int c = 0; c < 4; c++){ | |
707 | + currentTransform(r,c) = mat[c*4+r]; | |
708 | + } | |
709 | + } | |
710 | + | |
711 | + } | |
712 | + | |
713 | + ///Function to get back the framebuffer Object attached to the spider. | |
714 | + ///For external access. | |
567 | 715 | GLuint |
568 | 716 | getFB() |
569 | 717 | { |
570 | 718 | return fboID; |
571 | 719 | } |
572 | 720 | |
721 | +//--------------------------------------------------------------------------// | |
722 | +//-----------------------------TEMPORARY METHODS----------------------------// | |
723 | +//--------------------------------------------------------------------------// | |
724 | + | |
725 | + ///temporary Method necessary for visualization and testing. | |
726 | + void | |
727 | + Update() | |
728 | + { | |
729 | + vec<float> Y(1.0,0.0,0.0); | |
730 | + if(cos(Y.dot(direction))< 0.087){ | |
731 | + Y[0] = 0.0; Y[1] = 1.0;} | |
732 | + hor = stim::rect<float>(magnitude, position, direction.norm(), | |
733 | + ((Y.cross(direction)).cross(direction)).norm()); | |
734 | + ver = stim::rect<float>(magnitude, position, direction.norm(), | |
735 | + hor.n()); | |
736 | + } | |
737 | + | |
738 | + | |
573 | 739 | void |
574 | 740 | Step() |
575 | 741 | { |
576 | 742 | findOptimalDirection(); |
743 | + findOptimalPosition(); | |
744 | + findOptimalScale(); | |
577 | 745 | } |
578 | 746 | |
579 | 747 | |
580 | 748 | void |
581 | - findOptimalDirection() | |
749 | + printTransform() | |
582 | 750 | { |
583 | - /* Method for finding the best direction for the spider. | |
584 | - Uses the camera to rotate. Then Calls Evaluate to find new cost. | |
585 | - */ | |
586 | - //sampleDirection(); | |
587 | - //samplePosition(); | |
588 | - //sampleMagnitude(); | |
751 | + std::cout << currentTransform << std::endl; | |
589 | 752 | } |
590 | 753 | |
591 | 754 | /* Method for initializing the cuda devices, necessary only |
... | ... | @@ -593,7 +756,7 @@ class gl_spider : public virtual gl_texture<T> |
593 | 756 | void |
594 | 757 | initCuda() |
595 | 758 | { |
596 | - //stim::cudaSetDevice(); | |
759 | + stim::cudaSetDevice(); | |
597 | 760 | //GLint max; |
598 | 761 | //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max); |
599 | 762 | //std::cout << max << std::endl; | ... | ... |
stim/gl/gl_texture.h
... | ... | @@ -31,7 +31,6 @@ template<typename T> |
31 | 31 | class gl_texture : public virtual image_stack<T> |
32 | 32 | { |
33 | 33 | private: |
34 | - | |
35 | 34 | ///Method: setTextureType |
36 | 35 | /// Sets the internal texture_type, based on the data |
37 | 36 | /// size. Either 3D, 2D, 1D textures. |
... | ... | @@ -50,6 +49,10 @@ class gl_texture : public virtual image_stack<T> |
50 | 49 | std::string path; |
51 | 50 | GLuint texID; //OpenGL object |
52 | 51 | GLenum texture_type; //1D, 2D, 3D |
52 | + GLint interpType; | |
53 | + GLint texWrap; | |
54 | + GLenum type; | |
55 | + GLenum format; | |
53 | 56 | using image_stack<T>::R; |
54 | 57 | using image_stack<T>::S; |
55 | 58 | using image_stack<T>::ptr; |
... | ... | @@ -61,7 +64,7 @@ class gl_texture : public virtual image_stack<T> |
61 | 64 | /// Creates an instance of the gl_texture object. |
62 | 65 | gl_texture() |
63 | 66 | { |
64 | - | |
67 | + | |
65 | 68 | } |
66 | 69 | |
67 | 70 | ///Method: Path Constructor |
... | ... | @@ -74,8 +77,32 @@ class gl_texture : public virtual image_stack<T> |
74 | 77 | image_stack<T>::load_images(path.append("/*.jpg")); |
75 | 78 | setTextureType(); |
76 | 79 | } |
80 | + ///Method:getSize | |
81 | + ///returns the dimentions of | |
82 | + vec<int> | |
83 | + getSize() | |
84 | + { | |
85 | + stim::vec<int> size(R[1], R[2], R[3]); | |
86 | + return size; | |
87 | + } | |
77 | 88 | |
78 | - | |
89 | + ///Method:setTexParam | |
90 | + ///@param GLint interp --GL_LINEAR, GL_NEAREST... | |
91 | + ///@param GLint twrap --GL_REPEAR, GL_CLAMP_TO_EDGE... | |
92 | + ///@param GLenum dataType --GL_UNSIGNED_BYTE, GL_FLOAT16... | |
93 | + ///@param GLenum dataFormat--GL_LUMINANCE, GL_RGB... | |
94 | + /// Texture paramenters. | |
95 | + void | |
96 | + setTexParam(GLint interp = GL_LINEAR, | |
97 | + GLint twrap = GL_CLAMP_TO_EDGE, | |
98 | + GLenum dataType = GL_UNSIGNED_BYTE, | |
99 | + GLenum dataFormat = GL_LUMINANCE) | |
100 | + { | |
101 | + interpType = interp; | |
102 | + texWrap = twrap; | |
103 | + type = dataType; | |
104 | + format = dataFormat; | |
105 | + } | |
79 | 106 | ///Method:setDims |
80 | 107 | ///@param x size of the voxel in x direction |
81 | 108 | ///@param y size of the voxel in y direction |
... | ... | @@ -148,19 +175,25 @@ class gl_texture : public virtual image_stack<T> |
148 | 175 | glBindTexture(texture_type, texID); |
149 | 176 | glTexParameteri(texture_type, |
150 | 177 | GL_TEXTURE_MIN_FILTER, |
151 | - GL_LINEAR); | |
178 | + interpType); | |
152 | 179 | glTexParameteri(texture_type, |
153 | 180 | GL_TEXTURE_MAG_FILTER, |
154 | - GL_LINEAR); | |
181 | + interpType); | |
155 | 182 | switch(texture_type) |
156 | 183 | { |
157 | 184 | case GL_TEXTURE_3D: |
158 | 185 | glTexParameteri(texture_type, |
159 | - GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
186 | + GL_TEXTURE_WRAP_S,texWrap); | |
187 | + // GL_REPEAT); | |
188 | + // GL_CLAMP_TO_EDGE); | |
160 | 189 | glTexParameteri(texture_type, |
161 | - GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
190 | + GL_TEXTURE_WRAP_T,texWrap); | |
191 | + // GL_REPEAT); | |
192 | + // GL_CLAMP_TO_EDGE); | |
162 | 193 | glTexParameteri(texture_type, |
163 | - GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); | |
194 | + GL_TEXTURE_WRAP_R,texWrap); | |
195 | + // GL_REPEAT); | |
196 | + // GL_CLAMP_TO_EDGE); | |
164 | 197 | glTexImage3D(texture_type, |
165 | 198 | 0, |
166 | 199 | // GL_RGB16, |
... | ... | @@ -169,25 +202,25 @@ class gl_texture : public virtual image_stack<T> |
169 | 202 | R[2], |
170 | 203 | R[3], |
171 | 204 | 0, |
172 | - GL_LUMINANCE, | |
173 | - GL_UNSIGNED_BYTE, | |
205 | + format, | |
206 | + type, | |
174 | 207 | ptr); |
175 | 208 | //GL_UNSIGNED_BYTE can be TYPES, convert to GL equivalents |
176 | 209 | glPixelStorei(GL_PACK_ALIGNMENT,1); |
177 | 210 | break; |
178 | 211 | case GL_TEXTURE_2D: |
179 | 212 | glTexParameteri(texture_type, |
180 | - GL_TEXTURE_WRAP_S, GL_REPEAT); | |
213 | + GL_TEXTURE_WRAP_S, texWrap); | |
181 | 214 | glTexParameteri(texture_type, |
182 | - GL_TEXTURE_WRAP_T, GL_REPEAT); | |
215 | + GL_TEXTURE_WRAP_T, texWrap); | |
183 | 216 | glTexImage2D(texture_type, |
184 | 217 | 0, |
185 | 218 | 1, |
186 | 219 | R[1], |
187 | 220 | R[2], |
188 | 221 | 0, |
189 | - GL_LUMINANCE, | |
190 | - GL_UNSIGNED_BYTE, | |
222 | + format, | |
223 | + type, | |
191 | 224 | ptr); |
192 | 225 | break; |
193 | 226 | } |
... | ... | @@ -201,7 +234,6 @@ class gl_texture : public virtual image_stack<T> |
201 | 234 | return ptr; |
202 | 235 | } |
203 | 236 | |
204 | - | |
205 | 237 | |
206 | 238 | }; |
207 | 239 | } | ... | ... |