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 | +include(FindPackageHandleStandardArgs) | ||
2 | + | ||
3 | +set(STIM_INCLUDE_DIR $ENV{STIMLIB_PATH}) | ||
4 | + | ||
5 | +find_package_handle_standard_args(STIM DEFAULT_MSG STIM_INCLUDE_DIR) | ||
6 | + | ||
7 | +if(STIM_FOUND) | ||
8 | + set(STIM_INCLUDE_DIRS ${STIM_INCLUDE_DIR}) | ||
9 | +endif() | ||
0 | \ No newline at end of file | 10 | \ No newline at end of file |
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,8 +6,8 @@ | ||
6 | #include "../visualization/colormap.h" | 6 | #include "../visualization/colormap.h" |
7 | #include <sstream> | 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 | typedef unsigned char uchar; | 11 | typedef unsigned char uchar; |
12 | texture<uchar, cudaTextureType2D, cudaReadModeElementType> texIn; | 12 | texture<uchar, cudaTextureType2D, cudaReadModeElementType> texIn; |
13 | float *result; | 13 | float *result; |
@@ -24,7 +24,9 @@ inline void checkCUDAerrors(const char *msg) | @@ -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 | float get_sum(float *diff) | 30 | float get_sum(float *diff) |
29 | { | 31 | { |
30 | 32 | ||
@@ -39,6 +41,9 @@ float get_sum(float *diff) | @@ -39,6 +41,9 @@ float get_sum(float *diff) | ||
39 | return out; | 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 | __device__ float Template(int x) | 47 | __device__ float Template(int x) |
43 | { | 48 | { |
44 | if(x < 20/6 || x > 20*5/6 || (x > 20*2/6 && x < 20*4/6)){ | 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,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 | __global__ | 60 | __global__ |
53 | void get_diff (float *result) | 61 | void get_diff (float *result) |
54 | { | 62 | { |
55 | //cuPrintf("Hello"); | 63 | //cuPrintf("Hello"); |
56 | int x = threadIdx.x + blockIdx.x * blockDim.x; | 64 | int x = threadIdx.x + blockIdx.x * blockDim.x; |
57 | int y = threadIdx.y + blockIdx.y * blockDim.y; | 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 | float valIn = tex2D(texIn, x, y)/255.0; | 68 | float valIn = tex2D(texIn, x, y)/255.0; |
62 | float valTemp = Template(x); | 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 | //cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<uchar> (); | 81 | //cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<uchar> (); |
75 | //cudaMallocArray(&result, &channelDesc, DIM_X, DIM_Y, 0); | 82 | //cudaMallocArray(&result, &channelDesc, DIM_X, DIM_Y, 0); |
@@ -86,7 +93,7 @@ void initArray(cudaGraphicsResource_t src) | @@ -86,7 +93,7 @@ void initArray(cudaGraphicsResource_t src) | ||
86 | HANDLE_ERROR( | 93 | HANDLE_ERROR( |
87 | cudaBindTextureToArray(texIn, srcArray) | 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 | checkCUDAerrors("Memory Allocation Issue 1"); | 97 | checkCUDAerrors("Memory Allocation Issue 1"); |
91 | cudaMalloc((void **) &v_dif, 20*10*sizeof(float)); | 98 | cudaMalloc((void **) &v_dif, 20*10*sizeof(float)); |
92 | checkCUDAerrors("Memory Allocation Issue 2"); | 99 | checkCUDAerrors("Memory Allocation Issue 2"); |
@@ -94,7 +101,9 @@ void initArray(cudaGraphicsResource_t src) | @@ -94,7 +101,9 @@ void initArray(cudaGraphicsResource_t src) | ||
94 | // cudaBindTextureToArray(texIn, ptr, &channelDesc) | 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 | void cleanUP(cudaGraphicsResource_t src) | 107 | void cleanUP(cudaGraphicsResource_t src) |
99 | { | 108 | { |
100 | HANDLE_ERROR( | 109 | HANDLE_ERROR( |
@@ -110,20 +119,23 @@ void cleanUP(cudaGraphicsResource_t src) | @@ -110,20 +119,23 @@ void cleanUP(cudaGraphicsResource_t src) | ||
110 | cudaFree(v_dif) | 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 | extern "C" | 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 | float mini = 10000000000000000.0; | 130 | float mini = 10000000000000000.0; |
119 | int idx; | 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 | dim3 block(1, 1); | 135 | dim3 block(1, 1); |
124 | get_diff <<< grid, block >>> (result); | 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 | output[i] = get_sum(result+(20*10*i)); | 139 | output[i] = get_sum(result+(20*10*i)); |
128 | if(output[i] <= mini){ | 140 | if(output[i] <= mini){ |
129 | mini = output[i]; | 141 | mini = output[i]; |
stim/gl/gl_spider.h
@@ -7,18 +7,24 @@ | @@ -7,18 +7,24 @@ | ||
7 | #include <cuda_gl_interop.h> | 7 | #include <cuda_gl_interop.h> |
8 | #include <cudaGL.h> | 8 | #include <cudaGL.h> |
9 | #include <math.h> | 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 | #include <vector> | 18 | #include <vector> |
18 | 19 | ||
19 | #include <iostream> | 20 | #include <iostream> |
20 | #include <fstream> | 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 | namespace stim | 28 | namespace stim |
23 | { | 29 | { |
24 | 30 | ||
@@ -37,84 +43,271 @@ class gl_spider : public virtual gl_texture<T> | @@ -37,84 +43,271 @@ class gl_spider : public virtual gl_texture<T> | ||
37 | std::vector<stim::vec<float> > dirVectors; | 43 | std::vector<stim::vec<float> > dirVectors; |
38 | std::vector<stim::vec<float> > posVectors; | 44 | std::vector<stim::vec<float> > posVectors; |
39 | std::vector<stim::vec<float> > magVectors; | 45 | std::vector<stim::vec<float> > magVectors; |
46 | + stim::matrix<float, 4> currentTransform; | ||
40 | using gl_texture<T>::texID; | 47 | using gl_texture<T>::texID; |
41 | using gl_texture<T>::S; | 48 | using gl_texture<T>::S; |
42 | - cudaArray* c_Array; | ||
43 | - //void** devPtr; | ||
44 | - //size_t size; | 49 | + using gl_texture<T>::R; |
45 | cudaGraphicsResource_t resource; | 50 | cudaGraphicsResource_t resource; |
51 | + | ||
52 | + GLuint dList; | ||
53 | + GLubyte list[3]; | ||
46 | GLuint fboID; | 54 | GLuint fboID; |
47 | GLuint texbufferID; | 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 | void | 86 | void |
51 | findOptimalPosition() | 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 | int best = getCost(); | 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 | void | 109 | void |
61 | findOptimalScale() | 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 | int best = getCost(); | 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 | void | 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 | void | 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 | void | 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 | void | 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 | void | 308 | void |
115 | UpdateBuffer(float v_x, float v_y) | 309 | UpdateBuffer(float v_x, float v_y) |
116 | { | 310 | { |
117 | - //std::cout << v_y << std::endl; | ||
118 | float len = 10.0; | 311 | float len = 10.0; |
119 | stim::vec<float>p1; | 312 | stim::vec<float>p1; |
120 | stim::vec<float>p2; | 313 | stim::vec<float>p2; |
@@ -126,158 +319,97 @@ class gl_spider : public virtual gl_texture<T> | @@ -126,158 +319,97 @@ class gl_spider : public virtual gl_texture<T> | ||
126 | p4 = hor.p(0,1); | 319 | p4 = hor.p(0,1); |
127 | glBegin(GL_QUADS); | 320 | glBegin(GL_QUADS); |
128 | glTexCoord3f( | 321 | glTexCoord3f( |
129 | - // p1[0]/S[1], | ||
130 | - // p1[1]/S[2], | ||
131 | - // p1[2]/S[3] | ||
132 | - // ); | ||
133 | p1[0], | 322 | p1[0], |
134 | p1[1], | 323 | p1[1], |
135 | p1[2] | 324 | p1[2] |
136 | ); | 325 | ); |
137 | - //glVertex2f(0.0,0.0); | ||
138 | glVertex2f(v_x,v_y); | 326 | glVertex2f(v_x,v_y); |
139 | glTexCoord3f( | 327 | glTexCoord3f( |
140 | - // p2[0]/S[1], | ||
141 | - // p2[1]/S[2], | ||
142 | - // p2[2]/S[3] | ||
143 | p2[0], | 328 | p2[0], |
144 | p2[1], | 329 | p2[1], |
145 | p2[2] | 330 | p2[2] |
146 | ); | 331 | ); |
147 | - //glVertex2f(1.0, 0.0); | ||
148 | glVertex2f(v_x+len, v_y); | 332 | glVertex2f(v_x+len, v_y); |
149 | glTexCoord3f( | 333 | glTexCoord3f( |
150 | - // p2[0]/S[1], | ||
151 | - // p2[1]/S[2], | ||
152 | - // p2[2]/S[3] | ||
153 | p3[0], | 334 | p3[0], |
154 | p3[1], | 335 | p3[1], |
155 | p3[2] | 336 | p3[2] |
156 | ); | 337 | ); |
157 | - //glVertex2f(1.0, 2.0); | ||
158 | glVertex2f(v_x+len, v_y+len); | 338 | glVertex2f(v_x+len, v_y+len); |
159 | glTexCoord3f( | 339 | glTexCoord3f( |
160 | - // p4[0]/S[1], | ||
161 | - // p4[1]/S[2], | ||
162 | - // p4[2]/S[3] | ||
163 | p4[0], | 340 | p4[0], |
164 | p4[1], | 341 | p4[1], |
165 | p4[2] | 342 | p4[2] |
166 | ); | 343 | ); |
167 | - //glVertex2f(0.0, 2.0); | ||
168 | glVertex2f(v_x, v_y+len); | 344 | glVertex2f(v_x, v_y+len); |
169 | glEnd(); | 345 | glEnd(); |
346 | + | ||
170 | p1 = ver.p(1,1); | 347 | p1 = ver.p(1,1); |
171 | p2 = ver.p(1,0); | 348 | p2 = ver.p(1,0); |
172 | p3 = ver.p(0,0); | 349 | p3 = ver.p(0,0); |
173 | p4 = ver.p(0,1); | 350 | p4 = ver.p(0,1); |
174 | glBegin(GL_QUADS); | 351 | glBegin(GL_QUADS); |
175 | glTexCoord3f( | 352 | glTexCoord3f( |
176 | - // p1[0]/S[1], | ||
177 | - // p1[1]/S[2], | ||
178 | - // p1[2]/S[3] | ||
179 | p1[0], | 353 | p1[0], |
180 | p1[1], | 354 | p1[1], |
181 | p1[2] | 355 | p1[2] |
182 | ); | 356 | ); |
183 | - //glVertex2f(1.0, 0.0); | ||
184 | glVertex2f(v_x+len, v_y); | 357 | glVertex2f(v_x+len, v_y); |
185 | glTexCoord3f( | 358 | glTexCoord3f( |
186 | - // p2[0]/S[1], | ||
187 | - // p2[1]/S[2], | ||
188 | - // p2[2]/S[3] | ||
189 | p2[0], | 359 | p2[0], |
190 | p2[1], | 360 | p2[1], |
191 | p2[2] | 361 | p2[2] |
192 | ); | 362 | ); |
193 | - //glVertex2f(2.0, 0.0); | ||
194 | glVertex2f(v_x+2*len, v_y); | 363 | glVertex2f(v_x+2*len, v_y); |
195 | glTexCoord3f( | 364 | glTexCoord3f( |
196 | - // p3[0]/S[1], | ||
197 | - // p3[1]/S[2], | ||
198 | - // p3[2]/S[3] | ||
199 | p3[0], | 365 | p3[0], |
200 | p3[1], | 366 | p3[1], |
201 | p3[2] | 367 | p3[2] |
202 | ); | 368 | ); |
203 | - //glVertex2f(2.0, 2.0); | ||
204 | glVertex2f(v_x+2*len, v_y+len); | 369 | glVertex2f(v_x+2*len, v_y+len); |
205 | glTexCoord3f( | 370 | glTexCoord3f( |
206 | - // p4[0]/S[1], | ||
207 | - // p4[1]/S[2], | ||
208 | - // p4[2]/S[3] | ||
209 | p4[0], | 371 | p4[0], |
210 | p4[1], | 372 | p4[1], |
211 | p4[2] | 373 | p4[2] |
212 | ); | 374 | ); |
213 | - //glVertex2f(1.0, 2.0); | ||
214 | glVertex2f(v_x+len, v_y+len); | 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 | void | 411 | void |
280 | - Bind(int numSamples = 1089) | 412 | + Bind() |
281 | { | 413 | { |
282 | float len = 10.0; | 414 | float len = 10.0; |
283 | glBindFramebuffer(GL_FRAMEBUFFER, fboID);//set up GL buffer | 415 | glBindFramebuffer(GL_FRAMEBUFFER, fboID);//set up GL buffer |
@@ -291,7 +423,7 @@ class gl_spider : public virtual gl_texture<T> | @@ -291,7 +423,7 @@ class gl_spider : public virtual gl_texture<T> | ||
291 | GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; | 423 | GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; |
292 | glDrawBuffers(1, DrawBuffers); | 424 | glDrawBuffers(1, DrawBuffers); |
293 | glBindTexture(GL_TEXTURE_2D, texbufferID); | 425 | glBindTexture(GL_TEXTURE_2D, texbufferID); |
294 | - glClearColor(0,0,0,0); | 426 | + glClearColor(1,1,1,1); |
295 | glClear(GL_COLOR_BUFFER_BIT); | 427 | glClear(GL_COLOR_BUFFER_BIT); |
296 | glMatrixMode(GL_PROJECTION); | 428 | glMatrixMode(GL_PROJECTION); |
297 | glLoadIdentity(); | 429 | glLoadIdentity(); |
@@ -301,11 +433,11 @@ class gl_spider : public virtual gl_texture<T> | @@ -301,11 +433,11 @@ class gl_spider : public virtual gl_texture<T> | ||
301 | gluOrtho2D(0.0,2.0*len,0.0,numSamples*len); | 433 | gluOrtho2D(0.0,2.0*len,0.0,numSamples*len); |
302 | glEnable(GL_TEXTURE_3D); | 434 | glEnable(GL_TEXTURE_3D); |
303 | glBindTexture(GL_TEXTURE_3D, texID); | 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 | void | 441 | void |
310 | Unbind() | 442 | Unbind() |
311 | { | 443 | { |
@@ -316,88 +448,42 @@ class gl_spider : public virtual gl_texture<T> | @@ -316,88 +448,42 @@ class gl_spider : public virtual gl_texture<T> | ||
316 | glBindTexture(GL_TEXTURE_2D, 0); | 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 | //--------------------------------CUDA METHODS------------------------------// | 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 | void | 487 | void |
402 | createResource() | 488 | createResource() |
403 | { | 489 | { |
@@ -411,7 +497,7 @@ class gl_spider : public virtual gl_texture<T> | @@ -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 | void | 501 | void |
416 | destroyResource() | 502 | destroyResource() |
417 | { | 503 | { |
@@ -420,23 +506,27 @@ class gl_spider : public virtual gl_texture<T> | @@ -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 | int | 511 | int |
426 | getCost() | 512 | getCost() |
427 | { | 513 | { |
428 | createResource(); | 514 | createResource(); |
429 | - int cost = get_cost(resource, iter); | 515 | + int cost = get_cost(resource, iter, numSamples); |
430 | destroyResource(); | 516 | destroyResource(); |
431 | iter++; | 517 | iter++; |
432 | return cost; | 518 | return cost; |
433 | } | 519 | } |
434 | 520 | ||
435 | public: | 521 | public: |
436 | - | ||
437 | stim::rect<float> hor; | 522 | stim::rect<float> hor; |
438 | stim::rect<float> ver; | 523 | stim::rect<float> ver; |
439 | 524 | ||
525 | +//--------------------------------------------------------------------------// | ||
526 | +//-----------------------------CONSTRUCTORS---------------------------------// | ||
527 | +//--------------------------------------------------------------------------// | ||
528 | + | ||
529 | + | ||
440 | ///Default Constructor | 530 | ///Default Constructor |
441 | gl_spider | 531 | gl_spider |
442 | () | 532 | () |
@@ -444,6 +534,19 @@ class gl_spider : public virtual gl_texture<T> | @@ -444,6 +534,19 @@ class gl_spider : public virtual gl_texture<T> | ||
444 | setPosition(0.0,0.0,0.0); | 534 | setPosition(0.0,0.0,0.0); |
445 | setDirection(0.0,0.0,1.0); | 535 | setDirection(0.0,0.0,1.0); |
446 | setMagnitude(1.0); | 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 | ///temporary constructor for convenience, will be removed in further updates. | 552 | ///temporary constructor for convenience, will be removed in further updates. |
@@ -451,38 +554,39 @@ class gl_spider : public virtual gl_texture<T> | @@ -451,38 +554,39 @@ class gl_spider : public virtual gl_texture<T> | ||
451 | (float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z, | 554 | (float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z, |
452 | float mag_x) | 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 | ///@param GLuint id texture that is going to be sampled. | 562 | ///@param GLuint id texture that is going to be sampled. |
459 | - ///@param numSamples number of samples per operation. | ||
460 | ///Attached the spider to the texture with the given GLuint ID. | 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 | void | 566 | void |
463 | - attachSpider(GLuint id, int numSamples = 1089) | 567 | + attachSpider(GLuint id) |
464 | { | 568 | { |
465 | texID = id; | 569 | texID = id; |
570 | + iter = 0; ///for debugging purposes | ||
466 | GenerateFBO(20, numSamples*10); | 571 | GenerateFBO(20, numSamples*10); |
467 | - //sampleDirection(); | ||
468 | genDirectionVectors(); | 572 | genDirectionVectors(); |
573 | + genPositionVectors(); | ||
574 | + genMagnitudeVectors(); | ||
575 | + | ||
469 | gl_texture<T>::setDims(0.6, 0.6, 1.0); | 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); | ||