Commit db3c28c9940e4ae17b12a7616f539acf0cf9416e
1 parent
25580fe7
Implemented glDisplay lists. implemented findOptimalPosition, and findOptimalSca…
…le functions. Tested and works fine, but cannot handle some cases. genTemplate is now called only three times; during initialization.
Showing
1 changed file
with
81 additions
and
37 deletions
Show diff stats
stim/gl/gl_spider.h
... | ... | @@ -47,9 +47,10 @@ class gl_spider : public virtual gl_texture<T> |
47 | 47 | using gl_texture<T>::texID; |
48 | 48 | using gl_texture<T>::S; |
49 | 49 | using gl_texture<T>::R; |
50 | - cudaArray* c_Array; | |
51 | 50 | cudaGraphicsResource_t resource; |
52 | 51 | |
52 | + GLuint dList; | |
53 | + GLubyte list[3]; | |
53 | 54 | GLuint fboID; |
54 | 55 | GLuint texbufferID; |
55 | 56 | int iter; //temporary for testing |
... | ... | @@ -61,7 +62,10 @@ class gl_spider : public virtual gl_texture<T> |
61 | 62 | void |
62 | 63 | findOptimalDirection() |
63 | 64 | { |
64 | - genTemplate(dirVectors, 0); | |
65 | + //genTemplate(dirVectors, 0); | |
66 | + Bind(); | |
67 | + positionTemplate(); | |
68 | + glCallList(dList); | |
65 | 69 | int best = getCost(); |
66 | 70 | stim::vec<float, 4> next; |
67 | 71 | next[0] = dirVectors[best][0]*S[1]*R[1]; |
... | ... | @@ -69,10 +73,11 @@ class gl_spider : public virtual gl_texture<T> |
69 | 73 | next[2] = dirVectors[best][2]*S[3]*R[3]; |
70 | 74 | next[3] = 1; |
71 | 75 | next = (currentTransform*next).norm(); |
72 | - setPosition( position[0]+next[0]*magnitude[0]/4, | |
73 | - position[1]+next[1]*magnitude[0]/4, | |
74 | - position[2]+next[2]*magnitude[0]/4); | |
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); | |
75 | 79 | setDirection(next[0], next[1], next[2]); |
80 | + Unbind(); | |
76 | 81 | } |
77 | 82 | |
78 | 83 | /// Method for finding the best direction for the spider. |
... | ... | @@ -81,9 +86,21 @@ class gl_spider : public virtual gl_texture<T> |
81 | 86 | void |
82 | 87 | findOptimalPosition() |
83 | 88 | { |
84 | - | |
85 | - genTemplate(magVectors, 1); | |
89 | + Bind(); | |
90 | + positionTemplate(); | |
91 | + glCallList(dList+1); | |
86 | 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(); | |
87 | 104 | } |
88 | 105 | |
89 | 106 | /// Method for finding the best scale for the spider. |
... | ... | @@ -92,8 +109,19 @@ class gl_spider : public virtual gl_texture<T> |
92 | 109 | void |
93 | 110 | findOptimalScale() |
94 | 111 | { |
95 | - genTemplate(magVectors, 2); | |
112 | + Bind(); | |
113 | + positionTemplate(); | |
114 | + glCallList(dList+2); | |
96 | 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(); | |
97 | 125 | } |
98 | 126 | |
99 | 127 | |
... | ... | @@ -144,7 +172,8 @@ class gl_spider : public virtual gl_texture<T> |
144 | 172 | void |
145 | 173 | genPositionVectors(float delta = 0.2) |
146 | 174 | { |
147 | - | |
175 | + ofstream file; | |
176 | + //file.open("dvectors.txt"); | |
148 | 177 | vec<float> temp; |
149 | 178 | int dim = (sqrt(numSamples)-1)/2; |
150 | 179 | stim::rect<float> samplingPlane = |
... | ... | @@ -161,6 +190,8 @@ class gl_spider : public virtual gl_texture<T> |
161 | 190 | 0.5+step*i, |
162 | 191 | 0.5+step*j |
163 | 192 | ); |
193 | + //file << temp[0] << ";" << temp[1] | |
194 | + // << ";" << temp[2] << endl; | |
164 | 195 | posVectors.push_back(temp); |
165 | 196 | } |
166 | 197 | } |
... | ... | @@ -183,8 +214,7 @@ class gl_spider : public virtual gl_texture<T> |
183 | 214 | for(int i = 0; i < numSamples; i++){ |
184 | 215 | //Create linear index |
185 | 216 | factor = (min+step*i)*magnitude[0]; |
186 | - temp[0] = factor; | |
187 | - temp[1] = factor; | |
217 | + temp = factor; | |
188 | 218 | magVectors.push_back(temp); |
189 | 219 | } |
190 | 220 | } |
... | ... | @@ -202,13 +232,11 @@ class gl_spider : public virtual gl_texture<T> |
202 | 232 | vec<float> Y(1.0,0.0,0.0); |
203 | 233 | vec<float> pos(0.0,0.0,0.0); |
204 | 234 | vec<float> mag(1.0, 1.0, 1.0); |
235 | + vec<float> dir(0.0, 0.0, 1.0); | |
205 | 236 | switch(type) { |
206 | 237 | case 0: //Direction |
207 | 238 | Bind(); |
208 | - positionTemplate(); ///this can be placed anywhere | |
209 | - ///as long as the GL_TEXTURE | |
210 | - ///is not cleared after this call. | |
211 | - | |
239 | + glNewList(dList+type, GL_COMPILE); | |
212 | 240 | for(int i = 0; i < in.size(); i++) |
213 | 241 | { |
214 | 242 | if(cos(Y.dot(in[i]))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} |
... | ... | @@ -222,45 +250,47 @@ class gl_spider : public virtual gl_texture<T> |
222 | 250 | hor.n()); |
223 | 251 | UpdateBuffer(x, x+i*10.0); |
224 | 252 | } |
225 | - //getSample(); | |
253 | + glEndList(); | |
226 | 254 | Unbind(); |
227 | 255 | break; |
228 | 256 | case 1: //Position |
229 | 257 | Bind(); |
230 | - positionTemplate(); | |
258 | + glNewList(dList+type, GL_COMPILE); | |
231 | 259 | if(cos(Y.dot(direction))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} |
232 | 260 | else{Y[0] = 1.0; Y[1] = 0.0;} |
233 | 261 | |
234 | 262 | for(int i = 0; i < in.size(); i++) |
235 | 263 | { |
236 | - hor = stim::rect<float>(magnitude, | |
237 | - in[i], direction, | |
264 | + hor = stim::rect<float>(mag, | |
265 | + in[i], dir, | |
238 | 266 | ((Y.cross(direction)).cross(direction)) |
239 | 267 | .norm()); |
240 | - ver = stim::rect<float>(magnitude, | |
241 | - in[i], direction, | |
268 | + ver = stim::rect<float>(mag, | |
269 | + in[i], dir, | |
242 | 270 | hor.n()); |
243 | 271 | UpdateBuffer(x, x+i*10.0); |
244 | 272 | } |
245 | - Unbind(); | |
273 | + glEndList(); | |
274 | + Unbind(); | |
246 | 275 | break; |
247 | 276 | case 2: //Scale |
248 | 277 | Bind(); |
249 | - positionTemplate(); | |
278 | + glNewList(dList+type, GL_COMPILE); | |
250 | 279 | if(cos(Y.dot(direction))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} |
251 | 280 | else{Y[0] = 1.0; Y[1] = 0.0;} |
252 | 281 | |
253 | 282 | for(int i = 0; i < in.size(); i++) |
254 | 283 | { |
255 | 284 | hor = stim::rect<float>(in[i], |
256 | - position, direction, | |
285 | + pos, dir, | |
257 | 286 | ((Y.cross(direction)).cross(direction)) |
258 | 287 | .norm()); |
259 | 288 | ver = stim::rect<float>(in[i], |
260 | - position, direction, | |
289 | + pos, dir, | |
261 | 290 | hor.n()); |
262 | 291 | UpdateBuffer(x, x+i*10.0); |
263 | 292 | } |
293 | + glEndList(); | |
264 | 294 | Unbind(); |
265 | 295 | break; |
266 | 296 | default: |
... | ... | @@ -426,6 +456,8 @@ class gl_spider : public virtual gl_texture<T> |
426 | 456 | stim::vec<float, 4> rot = getRotation(direction); |
427 | 457 | glMatrixMode(GL_TEXTURE); |
428 | 458 | glLoadIdentity(); |
459 | + | |
460 | + glRotatef(rot[0], rot[1], rot[2], rot[3]); | |
429 | 461 | glScalef(1.0/S[1]/R[1], 1.0/S[2]/R[2], 1.0/S[3]/R[3]); |
430 | 462 | glTranslatef(position[0], |
431 | 463 | position[1], |
... | ... | @@ -433,7 +465,7 @@ class gl_spider : public virtual gl_texture<T> |
433 | 465 | glScalef(magnitude[0], |
434 | 466 | magnitude[1], |
435 | 467 | magnitude[0]); |
436 | - glRotatef(rot[0], rot[1], rot[2], rot[3]); | |
468 | + | |
437 | 469 | float curTrans[16]; |
438 | 470 | glGetFloatv(GL_TEXTURE_MATRIX, curTrans); |
439 | 471 | fillTransform(curTrans); |
... | ... | @@ -540,9 +572,16 @@ class gl_spider : public virtual gl_texture<T> |
540 | 572 | genDirectionVectors(); |
541 | 573 | genPositionVectors(); |
542 | 574 | genMagnitudeVectors(); |
575 | + | |
543 | 576 | gl_texture<T>::setDims(0.6, 0.6, 1.0); |
544 | 577 | setSize(512, 512, 426); |
578 | + | |
579 | + dList = glGenLists(3); | |
580 | + list[0] = 0; list[1] = 1; list[2] = 2; | |
581 | + glListBase(dList); | |
545 | 582 | genTemplate(dirVectors, 0); |
583 | + genTemplate(posVectors, 1); | |
584 | + genTemplate(magVectors, 2); | |
546 | 585 | } |
547 | 586 | |
548 | 587 | //--------------------------------------------------------------------------// |
... | ... | @@ -658,8 +697,21 @@ class gl_spider : public virtual gl_texture<T> |
658 | 697 | } |
659 | 698 | return out; |
660 | 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 | + } | |
661 | 712 | |
662 | - ///temporary method for visualization. | |
713 | + ///Function to get back the framebuffer Object attached to the spider. | |
714 | + ///For external access. | |
663 | 715 | GLuint |
664 | 716 | getFB() |
665 | 717 | { |
... | ... | @@ -688,18 +740,10 @@ class gl_spider : public virtual gl_texture<T> |
688 | 740 | Step() |
689 | 741 | { |
690 | 742 | findOptimalDirection(); |
743 | + findOptimalPosition(); | |
744 | + findOptimalScale(); | |
691 | 745 | } |
692 | 746 | |
693 | - void | |
694 | - fillTransform(float mat[16]) | |
695 | - { | |
696 | - for(int r = 0; r < 4; r++){ | |
697 | - for(int c = 0; c < 4; c++){ | |
698 | - currentTransform(r,c) = mat[c*4+r]; | |
699 | - } | |
700 | - } | |
701 | - | |
702 | - } | |
703 | 747 | |
704 | 748 | void |
705 | 749 | printTransform() | ... | ... |