Commit 2a18be6d1d4001cfe634e60e0377ab6b4cea5911
New comments and bug fixes as well as optimizations
Showing
3 changed files
with
518 additions
and
54 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
... | ... | @@ -9,6 +9,9 @@ |
9 | 9 | #define DIM_Y 10890 |
10 | 10 | #define DIM_X 20 |
11 | 11 | typedef unsigned char uchar; |
12 | +//surface<void, 2> texOut; ///// maybe just do a normal array instead of a surface. | |
13 | + //we may not need a surface at all. | |
14 | +//texture<float, cudaTextureType2D, cudaReadModeElementType> texTemplate | |
12 | 15 | texture<uchar, cudaTextureType2D, cudaReadModeElementType> texIn; |
13 | 16 | float *result; |
14 | 17 | float* v_dif; |
... | ... | @@ -59,8 +62,15 @@ void get_diff (float *result) |
59 | 62 | |
60 | 63 | //float valIn = tex2D(texIn, x, y); |
61 | 64 | float valIn = tex2D(texIn, x, y)/255.0; |
65 | + //int idx = x*DIM_X+y; | |
66 | + | |
67 | + //uchar4 color = tex2D(texIn, x, y); | |
68 | + //float3 tempcolor = make_float3(color.x, color.y, color.z); | |
69 | + //float valIn = tempcolor.x + tempcolor.y + tempcolor.z; | |
70 | + //float valIn = tex2D(texIn, x, y); | |
62 | 71 | float valTemp = Template(x); |
63 | - result[idx] = abs(valIn-valTemp); | |
72 | +// result[idx] = (float) x/(blockDim.x*gridDim.x);//abs(x); | |
73 | + result[idx] = abs(valIn-valTemp); | |
64 | 74 | // #if __CUDA_ARCH__>=200 |
65 | 75 | // printf("Value is : %f\n and the result is : %f\n", valIn, result[idx]); |
66 | 76 | // #endif | ... | ... |
stim/gl/gl_spider.h
... | ... | @@ -45,8 +45,8 @@ class gl_spider : public virtual gl_texture<T> |
45 | 45 | cudaGraphicsResource_t resource; |
46 | 46 | GLuint fboID; |
47 | 47 | GLuint texbufferID; |
48 | - int iter = 0; //temporary for testing | |
49 | - | |
48 | + int iter; //temporary for testing | |
49 | + int numSamples; | |
50 | 50 | void |
51 | 51 | findOptimalPosition() |
52 | 52 | { |
... | ... | @@ -64,7 +64,7 @@ class gl_spider : public virtual gl_texture<T> |
64 | 64 | /* Method for finding the best scale for the spider. |
65 | 65 | changes the x, y, z size of the spider to minimize the cost |
66 | 66 | function. */ |
67 | - genTemplate(magVectors, 1); | |
67 | + genTemplate(magVectors, 2); | |
68 | 68 | int best = getCost(); |
69 | 69 | } |
70 | 70 | |
... | ... | @@ -74,8 +74,9 @@ class gl_spider : public virtual gl_texture<T> |
74 | 74 | /* Method for finding the best scale for the spider. |
75 | 75 | changes the x, y, z size of the spider to minimize the cost |
76 | 76 | function. */ |
77 | - genTemplate(dirVectors, 1); | |
78 | - int best = getCost(); | |
77 | + genTemplate(dirVectors, 0); | |
78 | + //getSample(position, direction, magnitude); | |
79 | + //int best = getCost(); | |
79 | 80 | } |
80 | 81 | |
81 | 82 | |
... | ... | @@ -84,14 +85,10 @@ class gl_spider : public virtual gl_texture<T> |
84 | 85 | { |
85 | 86 | /*find the optimum direction and scale */ |
86 | 87 | } |
87 | - /* | |
88 | - void | |
89 | - Step() | |
90 | - { | |
91 | - // move to the new position | |
92 | - } | |
93 | - */ | |
94 | - | |
88 | + | |
89 | + ///@param width sets the width of the buffer. | |
90 | + ///@param height sets the height of the buffer. | |
91 | + ///Function for setting up the 2D buffer that stores the samples. | |
95 | 92 | void |
96 | 93 | GenerateFBO(unsigned int width, unsigned int height) |
97 | 94 | { |
... | ... | @@ -111,7 +108,11 @@ class gl_spider : public virtual gl_texture<T> |
111 | 108 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
112 | 109 | glBindTexture(GL_TEXTURE_2D, 0); |
113 | 110 | } |
114 | - | |
111 | + | |
112 | + ///@param v_x x-coordinate in buffer-space, | |
113 | + ///@param v_y y-coordinate in buffer-space. | |
114 | + ///Samples the texturespace and places a sample in the provided coordinates | |
115 | + ///of bufferspace. | |
115 | 116 | void |
116 | 117 | UpdateBuffer(float v_x, float v_y) |
117 | 118 | { |
... | ... | @@ -121,11 +122,16 @@ class gl_spider : public virtual gl_texture<T> |
121 | 122 | stim::vec<float>p2; |
122 | 123 | stim::vec<float>p3; |
123 | 124 | stim::vec<float>p4; |
125 | + std::cout << "hor" << std::endl; | |
124 | 126 | p1 = hor.p(1,1); |
127 | + std::cout << p1 << std::endl; | |
125 | 128 | p2 = hor.p(1,0); |
129 | + std::cout << p2 << std::endl; | |
126 | 130 | p3 = hor.p(0,0); |
131 | + std::cout << p3 << std::endl; | |
127 | 132 | p4 = hor.p(0,1); |
128 | - glBegin(GL_QUADS); | |
133 | + std::cout << p4 << std::endl; | |
134 | + /* glBegin(GL_QUADS); | |
129 | 135 | glTexCoord3f( |
130 | 136 | // p1[0]/S[1], |
131 | 137 | // p1[1]/S[2], |
... | ... | @@ -135,6 +141,7 @@ class gl_spider : public virtual gl_texture<T> |
135 | 141 | p1[1], |
136 | 142 | p1[2] |
137 | 143 | ); |
144 | + CHECK_OPENGL_ERROR | |
138 | 145 | //glVertex2f(0.0,0.0); |
139 | 146 | glVertex2f(v_x,v_y); |
140 | 147 | glTexCoord3f( |
... | ... | @@ -168,11 +175,17 @@ class gl_spider : public virtual gl_texture<T> |
168 | 175 | //glVertex2f(0.0, 2.0); |
169 | 176 | glVertex2f(v_x, v_y+len); |
170 | 177 | glEnd(); |
178 | +*/ | |
179 | + std::cout << "ver" << std::endl; | |
171 | 180 | p1 = ver.p(1,1); |
181 | + std::cout << p1 << std::endl; | |
172 | 182 | p2 = ver.p(1,0); |
183 | + std::cout << p2 << std::endl; | |
173 | 184 | p3 = ver.p(0,0); |
185 | + std::cout << p3 << std::endl; | |
174 | 186 | p4 = ver.p(0,1); |
175 | - glBegin(GL_QUADS); | |
187 | + std::cout << p4 << std::endl; | |
188 | +/* glBegin(GL_QUADS); | |
176 | 189 | glTexCoord3f( |
177 | 190 | // p1[0]/S[1], |
178 | 191 | // p1[1]/S[2], |
... | ... | @@ -213,17 +226,24 @@ class gl_spider : public virtual gl_texture<T> |
213 | 226 | ); |
214 | 227 | //glVertex2f(1.0, 2.0); |
215 | 228 | glVertex2f(v_x+len, v_y+len); |
216 | - glEnd(); | |
229 | + glEnd(); */ | |
217 | 230 | } |
218 | - | |
231 | + | |
232 | + ///@param vector of stim::vec in that stores all of the samplable vectors. | |
233 | + ///@param type, one of three operations, 0 for Direction vectors | |
234 | + /// 1 for Position, 2 for Magnitude. | |
235 | + ///Function for filling the buffer up with the data based on the vectors | |
236 | + ///Each vector represents a two rectangular templates. | |
237 | + ///Loops through all of the vectors and transfers rect. associated with it | |
238 | + ///Into buffer-space. | |
219 | 239 | void |
220 | - genTemplate(std::vector<stim::vec<float> > in, int type = 0) | |
240 | + genTemplate(std::vector<stim::vec<float> > in, int type) | |
221 | 241 | { |
222 | 242 | float x = 0.0; |
223 | 243 | vec<float> Y(1.0,0.0,0.0); |
224 | - Bind(); | |
225 | 244 | switch(type) { |
226 | 245 | case 0: //Direction |
246 | + Bind(); | |
227 | 247 | for(int i = 0; i < in.size(); i++) |
228 | 248 | { |
229 | 249 | if(cos(Y.dot(in[i]))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} |
... | ... | @@ -237,10 +257,11 @@ class gl_spider : public virtual gl_texture<T> |
237 | 257 | hor.n()); |
238 | 258 | UpdateBuffer(x, x+i*10.0); |
239 | 259 | } |
260 | + Unbind(); | |
240 | 261 | std::cout<< "Hmmm" << std::endl; |
241 | - Update(); | |
242 | 262 | break; |
243 | 263 | case 1: //Position |
264 | + Bind(); | |
244 | 265 | if(cos(Y.dot(direction))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} |
245 | 266 | else{Y[0] = 1.0; Y[1] = 0.0;} |
246 | 267 | |
... | ... | @@ -251,10 +272,13 @@ class gl_spider : public virtual gl_texture<T> |
251 | 272 | .norm()); |
252 | 273 | ver = stim::rect<float>(magnitude, in[i], direction, |
253 | 274 | hor.n()); |
254 | - UpdateBuffer(x, x+i*10.0); | |
275 | + //UpdateBuffer(x, x+i*10.0); | |
276 | + std::cout<< "Hmmm" << std::endl; | |
255 | 277 | } |
278 | + Unbind(); | |
256 | 279 | break; |
257 | 280 | case 2: //Scale |
281 | + Bind(); | |
258 | 282 | if(cos(Y.dot(direction))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} |
259 | 283 | else{Y[0] = 1.0; Y[1] = 0.0;} |
260 | 284 | |
... | ... | @@ -265,7 +289,10 @@ class gl_spider : public virtual gl_texture<T> |
265 | 289 | .norm()); |
266 | 290 | ver = stim::rect<float>(in[i], position, direction, |
267 | 291 | hor.n()); |
292 | + //UpdateBuffer(x, x+i*10.0); | |
293 | + std::cout<< "Hmmm" << std::endl; | |
268 | 294 | } |
295 | + Unbind(); | |
269 | 296 | break; |
270 | 297 | default: |
271 | 298 | std::cout << "unknown case have been passed" |
... | ... | @@ -273,14 +300,13 @@ class gl_spider : public virtual gl_texture<T> |
273 | 300 | break; |
274 | 301 | } |
275 | 302 | Unbind(); |
303 | + CHECK_OPENGL_ERROR | |
276 | 304 | } |
277 | 305 | |
278 | - /* | |
279 | - Method for controling the buffer and texture binding in order to properly | |
280 | - do the render to texture. | |
281 | - */ | |
306 | + ///Method for controling the buffer and texture binding in order to properly | |
307 | + ///do the render to texture. | |
282 | 308 | void |
283 | - Bind(int numSamples = 1089) | |
309 | + Bind() | |
284 | 310 | { |
285 | 311 | float len = 10.0; |
286 | 312 | glBindFramebuffer(GL_FRAMEBUFFER, fboID);//set up GL buffer |
... | ... | @@ -306,9 +332,7 @@ class gl_spider : public virtual gl_texture<T> |
306 | 332 | glBindTexture(GL_TEXTURE_3D, texID); |
307 | 333 | } |
308 | 334 | |
309 | - /* | |
310 | - Method for Unbinding all of the texture resources | |
311 | - */ | |
335 | + ///Method for Unbinding all of the texture resources | |
312 | 336 | void |
313 | 337 | Unbind() |
314 | 338 | { |
... | ... | @@ -319,21 +343,20 @@ class gl_spider : public virtual gl_texture<T> |
319 | 343 | glBindTexture(GL_TEXTURE_2D, 0); |
320 | 344 | } |
321 | 345 | |
322 | - /* | |
323 | - Method for populating the buffer with the sampled texture. | |
324 | - usually uses the default direction vector and then | |
325 | - */ | |
346 | + ///@param solidAngle, the size of the arc to sample. | |
347 | + ///Method for populating the vector arrays with sampled vectors. | |
348 | + ///uses the default direction vector <0,0,1> | |
326 | 349 | void |
327 | - genDirectionVectors(int numSamples = 1089, float solidAngle = M_PI/1.5) | |
350 | + genDirectionVectors(float solidAngle = M_PI/2) | |
328 | 351 | { |
329 | 352 | |
330 | - vec<float> d_s = direction.cart2sph(); | |
331 | - dirVectors.resize(1089); | |
353 | + vec<float> d_s = direction.cart2sph().norm(); | |
354 | + //dirVectors.resize(numSamples); //THIS LINE ADDS AN ERROR | |
332 | 355 | vec<float> temp; |
333 | 356 | int dim = (sqrt(numSamples)-1)/2; |
334 | 357 | float p0 = M_PI/3; |
335 | - float dt = solidAngle/(1.0 * dim); | |
336 | - float dp = p0/(1.0*dim); | |
358 | + float dt = solidAngle/(2.0 * (dim + 1)); | |
359 | + float dp = p0/(2.0*(dim + 1)); | |
337 | 360 | |
338 | 361 | for(int i = -dim; i <= dim; i++){ |
339 | 362 | for(int j = -dim; j <= dim; j++){ |
... | ... | @@ -342,15 +365,19 @@ class gl_spider : public virtual gl_texture<T> |
342 | 365 | temp[0] = d_s[0]; //rotate vector |
343 | 366 | temp[1] = d_s[1]+dt*i; |
344 | 367 | temp[2] = d_s[2]+dp*j; |
345 | - | |
368 | + | |
346 | 369 | temp = (temp.sph2cart()).norm(); //back to cart |
370 | + std::cout << temp << std::endl; | |
347 | 371 | dirVectors.push_back(temp); |
348 | 372 | } |
349 | 373 | } |
350 | 374 | } |
351 | 375 | |
376 | + ///@param solidAngle, the size of the arc to sample. | |
377 | + ///Method for populating the buffer with the sampled texture. | |
378 | + ///uses the default vector <0,0,0> | |
352 | 379 | void |
353 | - genPositionVectors(int numSamples = 1089, float delta = 0.2) | |
380 | + genPositionVectors(float delta = 0.2) | |
354 | 381 | { |
355 | 382 | |
356 | 383 | vec<float> temp; |
... | ... | @@ -370,13 +397,16 @@ class gl_spider : public virtual gl_texture<T> |
370 | 397 | 0.5+step*i, |
371 | 398 | 0.5+step*j |
372 | 399 | ); |
373 | - dirVectors.push_back(temp); | |
400 | + posVectors.push_back(temp); | |
374 | 401 | } |
375 | 402 | } |
376 | 403 | } |
377 | 404 | |
405 | + ///@param solidAngle, the size of the arc to sample. | |
406 | + ///Method for populating the buffer with the sampled texture. | |
407 | + ///uses the default magnitude <1,1,0> | |
378 | 408 | void |
379 | - genMagnitudeVectors(int numSamples = 1089, float delta = 0.5) | |
409 | + genMagnitudeVectors(float delta = 0.5) | |
380 | 410 | { |
381 | 411 | |
382 | 412 | int dim = (sqrt(numSamples)-1)/2; |
... | ... | @@ -399,8 +429,8 @@ class gl_spider : public virtual gl_texture<T> |
399 | 429 | //--------------------------------CUDA METHODS------------------------------// |
400 | 430 | //--------------------------------------------------------------------------// |
401 | 431 | |
402 | - /* Method for registering the texture with Cuda for shared | |
403 | - access */ | |
432 | + /// Method for registering the texture with Cuda for shared | |
433 | + /// access. | |
404 | 434 | void |
405 | 435 | createResource() |
406 | 436 | { |
... | ... | @@ -414,7 +444,7 @@ class gl_spider : public virtual gl_texture<T> |
414 | 444 | ); |
415 | 445 | } |
416 | 446 | |
417 | - /* Method for freeing the texture from Cuda for gl access */ | |
447 | + ///Method for freeing the texture from Cuda for gl access. | |
418 | 448 | void |
419 | 449 | destroyResource() |
420 | 450 | { |
... | ... | @@ -423,8 +453,8 @@ class gl_spider : public virtual gl_texture<T> |
423 | 453 | ); |
424 | 454 | } |
425 | 455 | |
426 | - /* Entry-point into the cuda code for calculating the cost | |
427 | - of a given samples array (in texture form) */ | |
456 | + ///Entry-point into the cuda code for calculating the cost | |
457 | + /// of a given samples array (in texture form) | |
428 | 458 | int |
429 | 459 | getCost() |
430 | 460 | { |
... | ... | @@ -447,6 +477,17 @@ class gl_spider : public virtual gl_texture<T> |
447 | 477 | setPosition(0.0,0.0,0.0); |
448 | 478 | setDirection(0.0,0.0,1.0); |
449 | 479 | setMagnitude(1.0); |
480 | + //numSamples = 1089; | |
481 | + numSamples = 9; | |
482 | + std::cout << "I did this" <<std::endl; | |
483 | + } | |
484 | + gl_spider | |
485 | + (int samples) | |
486 | + { | |
487 | + setPosition(0.0,0.0,0.0); | |
488 | + setDirection(0.0,0.0,1.0); | |
489 | + setMagnitude(1.0); | |
490 | + numSamples = samples; | |
450 | 491 | } |
451 | 492 | |
452 | 493 | ///temporary constructor for convenience, will be removed in further updates. |
... | ... | @@ -457,22 +498,24 @@ class gl_spider : public virtual gl_texture<T> |
457 | 498 | setPosition(pos_x, pos_y, pos_z); |
458 | 499 | setDirection(dir_x, dir_y, dir_z); |
459 | 500 | setMagnitude(mag_x); |
501 | + | |
460 | 502 | } |
461 | 503 | ///@param GLuint id texture that is going to be sampled. |
462 | - ///@param numSamples number of samples per operation. | |
463 | 504 | ///Attached the spider to the texture with the given GLuint ID. |
464 | 505 | ///Samples in the default direction acting as the init method. |
465 | 506 | void |
466 | - attachSpider(GLuint id, int numSamples = 1089) | |
507 | + attachSpider(GLuint id) | |
467 | 508 | { |
468 | 509 | texID = id; |
510 | + iter = 0; ///for debugging purposes | |
469 | 511 | GenerateFBO(20, numSamples*10); |
470 | 512 | //sampleDirection(); |
471 | 513 | genDirectionVectors(); |
472 | - genTemplate(posVectors, 1); | |
514 | + CHECK_OPENGL_ERROR | |
473 | 515 | genPositionVectors(); |
474 | 516 | genMagnitudeVectors(); |
475 | 517 | gl_texture<T>::setDims(0.6, 0.6, 2.0); |
518 | + genTemplate(dirVectors, 0); | |
476 | 519 | //gl_texture<T>::setDims(1.0, 1.0,1.0); |
477 | 520 | } |
478 | 521 | |
... | ... | @@ -541,7 +584,7 @@ class gl_spider : public virtual gl_texture<T> |
541 | 584 | //glRotatef(roty, 0.0, 1.0, 0.0); |
542 | 585 | //glRotatef(rotx, 1.0, 0.0 ,0.0); |
543 | 586 | glTranslatef(pos[0], pos[1], pos[2]); |
544 | - glScalef(1/512/S[0], 1/512/S[1], 1/512/S[2]); | |
587 | + glScalef(1/512/S[0], 1/512/S[1], 1/426/S[2]); | |
545 | 588 | Unbind(); |
546 | 589 | } |
547 | 590 | |
... | ... | @@ -603,7 +646,7 @@ class gl_spider : public virtual gl_texture<T> |
603 | 646 | void |
604 | 647 | initCuda() |
605 | 648 | { |
606 | - //stim::cudaSetDevice(); | |
649 | + stim::cudaSetDevice(); | |
607 | 650 | //GLint max; |
608 | 651 | //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max); |
609 | 652 | //std::cout << max << std::endl; | ... | ... |