Commit f1e9fe98ca570f1b64ea27b8ee1efe4f9428d355
Merge branch 'Dev'
Showing
1 changed file
with
217 additions
and
7 deletions
Show diff stats
gl/gl_spider.h
1 | #ifndef STIM_GL_SPIDER_H | 1 | #ifndef STIM_GL_SPIDER_H |
2 | #define STIM_GL_SPIDER_H | 2 | #define STIM_GL_SPIDER_H |
3 | 3 | ||
4 | +#include <GL/glew.h> | ||
4 | #include <GL/glut.h> | 5 | #include <GL/glut.h> |
5 | -//#include <GL/glew.h> | 6 | +#include <cuda.h> |
7 | +#include <cuda_gl_interop.h> | ||
8 | +#include <cudaGL.h> | ||
6 | #include "gl_texture.h" | 9 | #include "gl_texture.h" |
7 | #include "../visualization/camera.h" | 10 | #include "../visualization/camera.h" |
8 | #include "./error.h" | 11 | #include "./error.h" |
9 | #include "../math/vector.h" | 12 | #include "../math/vector.h" |
10 | #include "../math/rect.h" | 13 | #include "../math/rect.h" |
14 | +#include "../cuda/cost.h" | ||
15 | +#include "../cuda/glbind.h" | ||
11 | 16 | ||
12 | namespace stim | 17 | namespace stim |
13 | { | 18 | { |
@@ -25,10 +30,14 @@ class gl_spider : public virtual gl_texture<T> | @@ -25,10 +30,14 @@ class gl_spider : public virtual gl_texture<T> | ||
25 | stim::vec<float> magnitude; //magnitude of the direction vector. | 30 | stim::vec<float> magnitude; //magnitude of the direction vector. |
26 | //mag[0] = length. | 31 | //mag[0] = length. |
27 | //mag[1] = width. | 32 | //mag[1] = width. |
28 | - // Also maybe we some texture representation of | ||
29 | - // of the spider i.e [WH_pxl, BL_pxl, WH_pxl]. | ||
30 | using gl_texture<T>::texID; | 33 | using gl_texture<T>::texID; |
31 | - | 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; | ||
32 | 41 | ||
33 | void | 42 | void |
34 | findOptimalDirection() | 43 | findOptimalDirection() |
@@ -66,7 +75,7 @@ class gl_spider : public virtual gl_texture<T> | @@ -66,7 +75,7 @@ class gl_spider : public virtual gl_texture<T> | ||
66 | position can be set with gl_texture coordinates */ | 75 | position can be set with gl_texture coordinates */ |
67 | 76 | ||
68 | } | 77 | } |
69 | - | 78 | + |
70 | void | 79 | void |
71 | Optimize() | 80 | Optimize() |
72 | { | 81 | { |
@@ -92,7 +101,8 @@ class gl_spider : public virtual gl_texture<T> | @@ -92,7 +101,8 @@ class gl_spider : public virtual gl_texture<T> | ||
92 | setPosition(0.0,0.0,0.0); | 101 | setPosition(0.0,0.0,0.0); |
93 | setDirection(1.0,1.0,1.0); | 102 | setDirection(1.0,1.0,1.0); |
94 | setMagnitude(0.1,0.1); | 103 | setMagnitude(0.1,0.1); |
95 | - Update(); | 104 | + //GenerateFBO(400,200); |
105 | + //Update(); | ||
96 | } | 106 | } |
97 | 107 | ||
98 | gl_spider | 108 | gl_spider |
@@ -101,7 +111,8 @@ class gl_spider : public virtual gl_texture<T> | @@ -101,7 +111,8 @@ class gl_spider : public virtual gl_texture<T> | ||
101 | position = pos; | 111 | position = pos; |
102 | direction = dir; | 112 | direction = dir; |
103 | magnitude = mag; | 113 | magnitude = mag; |
104 | - Update(); | 114 | + //GenerateFBO(400,200); |
115 | + //Update(); | ||
105 | } | 116 | } |
106 | //temporary cost for convenience. | 117 | //temporary cost for convenience. |
107 | gl_spider | 118 | gl_spider |
@@ -111,6 +122,15 @@ class gl_spider : public virtual gl_texture<T> | @@ -111,6 +122,15 @@ class gl_spider : public virtual gl_texture<T> | ||
111 | setPosition(pos_x, pos_y, pos_z); | 122 | setPosition(pos_x, pos_y, pos_z); |
112 | setDirection(dir_x, dir_y, dir_z); | 123 | setDirection(dir_x, dir_y, dir_z); |
113 | setMagnitude(mag_x, mag_y); | 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); | ||
114 | Update(); | 134 | Update(); |
115 | } | 135 | } |
116 | 136 | ||
@@ -124,6 +144,7 @@ class gl_spider : public virtual gl_texture<T> | @@ -124,6 +144,7 @@ class gl_spider : public virtual gl_texture<T> | ||
124 | ((Y.cross(direction)).cross(direction)).norm()); | 144 | ((Y.cross(direction)).cross(direction)).norm()); |
125 | ver = stim::rect<float>(magnitude, position, direction.norm(), | 145 | ver = stim::rect<float>(magnitude, position, direction.norm(), |
126 | hor.n()); | 146 | hor.n()); |
147 | + UpdateBuffer(); | ||
127 | } | 148 | } |
128 | 149 | ||
129 | vec<float> | 150 | vec<float> |
@@ -184,6 +205,12 @@ class gl_spider : public virtual gl_texture<T> | @@ -184,6 +205,12 @@ class gl_spider : public virtual gl_texture<T> | ||
184 | magnitude[0] = x; | 205 | magnitude[0] = x; |
185 | magnitude[1] = y; | 206 | magnitude[1] = y; |
186 | } | 207 | } |
208 | + | ||
209 | + GLuint | ||
210 | + getFB() | ||
211 | + { | ||
212 | + return fboID; | ||
213 | + } | ||
187 | 214 | ||
188 | void | 215 | void |
189 | Step() | 216 | Step() |
@@ -196,6 +223,189 @@ class gl_spider : public virtual gl_texture<T> | @@ -196,6 +223,189 @@ class gl_spider : public virtual gl_texture<T> | ||
196 | << std::endl; | 223 | << std::endl; |
197 | 224 | ||
198 | } | 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 | + } | ||
199 | }; | 409 | }; |
200 | } | 410 | } |
201 | #endif | 411 | #endif |