OSDN Git Service

first
[psychlops/cpp.git] / psychlops / extension / devices / psychlops_devices_crs_bits.cpp
1 /*\r
2  *  psychlops_devices_nidaqmxbase.cpp\r
3  *  Psychlops Standard Library (Universal)\r
4  *\r
5  *  Last Modified 2009/04/14 by Kenchi HOSOKAWA\r
6  *  (C) 2009 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO\r
7  */\r
8 \r
9 #include "../../core/ApplicationInterfaces/psychlops_code_exception.h"\r
10 #include "../../core/devices/psychlops_io_hid.h"\r
11 #include "../../core/graphic/psychlops_g_canvas.h"\r
12 #include "../../core/math/psychlops_math.h"\r
13 #include "../../platform/gl/extension/psychlops_g_shader_gl.h"\r
14 \r
15 #include "psychlops_devices_crs_bits.h"\r
16 \r
17 #if defined(__APPLE__)\r
18 #else\r
19 //#include "C:\Program Files\Cambridge Research Systems\VSGV7\Windows\WIN32\MSC\include\bits++.h"\r
20 #endif\r
21 \r
22 \r
23 \r
24 namespace Psychlops {\r
25 \r
26 namespace Devices {\r
27 \r
28         const unsigned char BitsTlock::COMMAND_R[8] = { 69 ,40 ,19 ,119,52 ,233,41 ,183 };\r
29         const unsigned char BitsTlock::COMMAND_G[8] = { 33 ,230,190,84 ,12 ,108,201,124 };\r
30         const unsigned char BitsTlock::COMMAND_B[8] = { 56 ,208,102,207,192,172,80 ,221 };\r
31 \r
32         const unsigned char BitsTlock::PALLETE_R[8] = { 36 ,63 , 8 ,211,3  ,112,56 ,34 };\r
33         const unsigned char BitsTlock::PALLETE_G[8] = {106 ,136,19 ,25 ,115,68, 41, 156};\r
34         const unsigned char BitsTlock::PALLETE_B[8] = {133 ,163,138,46 ,164,9  ,49, 208};\r
35 \r
36         inline double CTB(int param)\r
37         {\r
38                 return param / 255.0;\r
39         }\r
40 \r
41         void BitsPallete::initialize()\r
42         {\r
43                 if(!is_initialized)\r
44                 {\r
45                 Color::setGammaValue(1.0,1.0,1.0);\r
46                         tlock.set(20,1,Image::RGB);\r
47                         for(int i=0; i<8; i++) {\r
48                                 tlock.pix(i, 0, Color(CTB(BitsTlock::COMMAND_R[i]), CTB(BitsTlock::COMMAND_G[i]), CTB(BitsTlock::COMMAND_B[i])) );\r
49                         }\r
50                 Color::setGammaValue(1.0,1.0,1.0, Color::BITS_MONO);\r
51                 }\r
52         }\r
53 \r
54         BitsPallete::BitsPallete()\r
55         {\r
56                 is_initialized = false;\r
57                 initialize();\r
58         }\r
59 \r
60         void BitsPallete::set(double *val)\r
61         {\r
62                 Color::setGammaValue(1.0,1.0,1.0);\r
63                 int packet_length = 1;\r
64                 tlock.pix(8, 0, Color(CTB(0), CTB(0), CTB(packet_length-1)));\r
65                 tlock.pix(9, 0, Color(CTB(6), CTB(0), CTB(2)));\r
66                 tlock.pix(10, 0, Color(CTB(0), CTB(0), CTB(0)));\r
67                 tlock.pix(11, 0, Color(CTB(7), CTB(255), CTB(255)));\r
68 \r
69                 tlock.pix(12, 0, Color(CTB(0), CTB(0), CTB(0)));\r
70                 //tlock.pix(14, 0, Color(CTB(0), CTB(0), CTB(0));\r
71                 //tlock.pix(16, 0, Color(CTB(0), CTB(0), CTB(0));\r
72                 //tlock.pix(18, 0, Color(CTB(0), CTB(0), CTB(0));\r
73 \r
74                 short w = val[0];\r
75                 tlock.pix(13, 0, Color(CTB(7), w >> 8 / 255, w >> 255 ));\r
76                 Color::setGammaValue(1.0,1.0,1.0, Color::BITS_MONO);\r
77         }\r
78 \r
79 \r
80         void BitsTrigger::initialize()\r
81         {\r
82                 if(!is_initialized)\r
83                 {\r
84                 Color::setGammaValue(1.0,1.0,1.0);\r
85                         tlock.set(20,1,Image::RGB);\r
86                         for(int i=0; i<8; i++) {\r
87                                 tlock.pix(i, 0, Color(CTB(BitsTlock::COMMAND_R[i]), CTB(BitsTlock::COMMAND_G[i]), CTB(BitsTlock::COMMAND_B[i])) );\r
88                         }\r
89                 Color::setGammaValue(1.0,1.0,1.0, Color::BITS_MONO);\r
90                 }\r
91         }\r
92 \r
93         BitsTrigger::BitsTrigger()\r
94         {\r
95                 is_initialized = false;\r
96                 initialize();\r
97         }\r
98 \r
99         void BitsTrigger::set(double val)\r
100         {\r
101                 Color::setGammaValue(1.0,1.0,1.0);\r
102                 int packet_length = 1;\r
103                 tlock.pix(8, 0, Color(CTB(0), CTB(0), CTB(packet_length-1)));\r
104                 tlock.pix(9, 0, Color(CTB(6), CTB(0), CTB(2)));\r
105                 tlock.pix(10, 0, Color(CTB(0), CTB(0), CTB(0)));\r
106                 tlock.pix(11, 0, Color(CTB(7), CTB(255), CTB(255)));\r
107 \r
108                 tlock.pix(12, 0, Color(CTB(0), CTB(0), CTB(0)));\r
109                 //tlock.pix(14, 0, Color(CTB(0), CTB(0), CTB(0));\r
110                 //tlock.pix(16, 0, Color(CTB(0), CTB(0), CTB(0));\r
111                 //tlock.pix(18, 0, Color(CTB(0), CTB(0), CTB(0));\r
112 \r
113                 short w = val;\r
114                 tlock.pix(13, 0, Color(CTB(7), w >> 8 / 255, w >> 255 ));\r
115                 tlock.setDatum(Point(0, Display::getHeight()-1));\r
116                 Color::setGammaValue(1.0,1.0,1.0, Color::BITS_MONO);\r
117         }\r
118 \r
119         void BitsTrigger::out()\r
120         {\r
121                 tlock.draw();\r
122         }\r
123 \r
124 \r
125 \r
126 /*\r
127 \r
128         void BitsResponseBox::initialize()\r
129         {\r
130                 if(!is_initialized)\r
131                 {\r
132                         tlock.set(12,1,Image::RGB);\r
133                         for(int i=0; i<8; i++) {\r
134                                 tlock.pix(i, 0, Color(CTB(BitsTlock::COMMAND_R[i]), CTB(BitsTlock::COMMAND_G[i]), CTB(BitsTlock::COMMAND_B[i])) );\r
135                         }\r
136                         tlock.pix(8, 0, Color(CTB(0), CTB(0), CTB(1)));\r
137                         tlock.pix(9, 0, Color(CTB(6), CTB(12), CTB(0)));\r
138                         tlock.pix(10, 0, Color(CTB(0), CTB(0), CTB(0)));\r
139                         tlock.pix(11, 0, Color(CTB(0), CTB(0), CTB(0)));\r
140                 }\r
141         }\r
142 \r
143         BitsResponseBox::BitsResponseBox()\r
144         {\r
145                 is_initialized = false;\r
146                 initialize();\r
147         }\r
148         BitsResponseBox::BitsResponseBox(unsigned long BoxType)\r
149         {\r
150                 is_initialized = false;\r
151                 initialize();\r
152                 open(BoxType);\r
153         }\r
154         BitsResponseBox::~BitsResponseBox()\r
155         {\r
156                 close();\r
157         }\r
158         void BitsResponseBox::buzzer(unsigned long Period, unsigned long Frequency)\r
159         {\r
160                 bitsCbboxBuzzer(Period, Frequency);  /* Buzz CB1 buzzer\r
161         }\r
162         void BitsResponseBox::sendback()\r
163         {\r
164                 bitsCbboxSendback();                 /* Ask explicitly for switch settings\r
165         }\r
166 \r
167         int  changeA;\r
168         int  switchA;\r
169         int  changeB;\r
170         int  switchB;\r
171         int  changeC;\r
172         int  switchC;\r
173         long counter;\r
174 \r
175         void BitsResponseBox::reset()\r
176         {\r
177                 bitsResetTimer();\r
178         }\r
179         void BitsResponseBox::check()\r
180         {\r
181                 bitsCbboxCheck(&C);                  /* Check if any switches have changed\r
182         }\r
183         void BitsResponseBox::flush()\r
184         {\r
185                 bitsCbboxFlush();                            /* Flush the input buffer\r
186         }\r
187 \r
188         void BitsResponseBox::open(unsigned long BoxType)\r
189         {\r
190                 DWORD res = bitsCbboxOpen(BoxType);\r
191                 std::cout << res << std::endl;\r
192         }\r
193         void BitsResponseBox::close()\r
194         {\r
195                 bitsCbboxClose();                            /* Reset interrupt vectors\r
196         }\r
197         */\r
198 \r
199 \r
200 \r
201 \r
202         ////////        Construct and Destruct  ////////\r
203         class DirectGetColor {\r
204                 public:\r
205                 inline static unsigned char round8bit(double value) {\r
206                         double val = value*255.0+0.5, integer_part, frac;\r
207                         frac = modf(val, &integer_part);\r
208                         if(frac!=0.0) return (unsigned char)integer_part;\r
209                         if((int)(integer_part)%2==0) return (unsigned char)integer_part; else return (unsigned char)integer_part-1;\r
210                 }\r
211                 static void setColor4ub(const Color &color) {\r
212                         glColor4ub(round8bit(color.getR()), round8bit(color.getG()), round8bit(color.getB()), round8bit(color.getA()));\r
213                 }\r
214                 static void setColorClear4ub(const Color &color) {\r
215                         glClearColor((GLclampf)round8bit(color.getR())/255.0, (GLclampf)round8bit(color.getG())/255.0, (GLclampf)round8bit(color.getB())/255.0, (GLclampf)round8bit(color.getA())/255.0);\r
216                 }\r
217                 static void setColor4Through(const Color &color) {\r
218                         glColor4d(color.getR(), color.getG(), color.getB(), color.getA());\r
219                 }\r
220                 static void setColorClaer4Through(const Color &color) {\r
221                         glClearColor(color.getR(), color.getG(), color.getB(), color.getA());\r
222                 }\r
223         };\r
224 \r
225 \r
226         CanvasBits::CanvasBits(void) : Canvas() {\r
227         }\r
228         CanvasBits::CanvasBits(CanvasMode mode) : Canvas(mode) {\r
229         }\r
230         CanvasBits::CanvasBits(int d_width, int d_height, int d_colordepth, double d_refreshrate, CanvasMode mode)\r
231          : Canvas(d_width, d_height, d_colordepth, d_refreshrate) {\r
232         }\r
233         CanvasBits::~CanvasBits(void) {\r
234                 release();\r
235         }\r
236 \r
237 /*\r
238         void CanvasBits::popMatrixes() {\r
239                 glMatrixMode(GL_MODELVIEW);\r
240                 glPopMatrix();\r
241                 glMatrixMode(GL_PROJECTION);\r
242                 glPopMatrix();\r
243         }\r
244         void CanvasBits::loadOrthoProjection() {\r
245                 glMatrixMode(GL_PROJECTION);\r
246                 glPushMatrix();\r
247                 glOrtho(-0.375, Width-0.375, Height-0.375, -0.375, -1000, 1000);\r
248                 glMatrixMode(GL_MODELVIEW);\r
249                 glPushMatrix();\r
250 //              glDisable(GL_LIGHTING);\r
251 //              glDisable(GL_DEPTH_TEST);\r
252         }\r
253 */\r
254         void CanvasBits::initAPIprop() {\r
255                 switch(colordepth) {\r
256                 case 24:\r
257                 case 32:\r
258                         APIsetColor = &(DirectGetColor::setColor4Through);\r
259                         APIsetColorClear = &(DirectGetColor::setColorClaer4Through);\r
260                         break;\r
261                 default:\r
262                         APIsetColor = &(DirectGetColor::setColor4Through);\r
263                         APIsetColorClear = &(DirectGetColor::setColorClaer4Through);\r
264                         break;\r
265                 }\r
266 \r
267                 glDrawBuffer(GL_BACK);\r
268                 glReadBuffer(GL_BACK);\r
269                 //glMatrixMode(GL_PROJECTION);\r
270                 //glLoadIdentity();\r
271 //              glOrtho(0, Width, Height, 0, -1, 1);\r
272                 //glOrtho(-0.375, Width-0.375, Height-0.375, 0-0.375, -1, 1);\r
273                 //glMatrixMode(GL_MODELVIEW);\r
274                 //glLoadIdentity();\r
275 //              glTranslated(0.375, 0.375, 0.0);\r
276 //              glPushMatrix();\r
277 \r
278         pushPixToPixProjection();\r
279                 initAPIattributes();\r
280 \r
281                 glEnableClientState(GL_VERTEX_ARRAY);\r
282                 //std::string source = ShaderAPI::shader_core_pix;\r
283                 std::string source = ShaderAPI::shader_core_pix_bitscolor;\r
284                 source += "void main(void){ pix( gl_Color.r, gl_Color.g, gl_Color.b, gl_Color.a ); }";\r
285                 std::string sourcev = "";\r
286                 sourcev +=\r
287                 "#version 140\r\n"\r
288                 "uniform mat4 projectionMatrix;\r\n"\r
289                 "uniform mat4 modelviewMatrix;\r\n"\r
290                 "in vec3 position;\r\n"\r
291                 "void main(void){\r\n"\r
292                 "  vec4 base_pos = projectionMatrix * modelviewMatrix * vec4(position, 1.0);\r\n"\r
293                 "  base_pos.x = base_pos.x * 2.0;\r\n;"\r
294                 "  gl_Position = base_pos;\r\n"\r
295                 "}\r\n";\r
296                 std::vector<std::string> vars;\r
297                 try{\r
298                         //shader.cacheVertex(sourcev, *this);\r
299                         shader.cache(source, vars, *this);\r
300                 } catch(Exception *e)\r
301                 {\r
302                         std::cout << sourcev << std::endl << std::endl;\r
303                         std::cout << e->toString();\r
304                         exit(0);\r
305                 }\r
306                 glUseProgram(shader.gl2Program);\r
307 \r
308 \r
309                 GLclampf red=0.0, green=0.0, blue=0.0, alpha=1.0;\r
310 //              default_drawing_mode_ = GL_POINTS;\r
311                 glClearColor(red,green,blue,alpha);\r
312                 clear(Color::black);\r
313                 flip(2);\r
314                 clear(Color::black);\r
315                 flip(2);\r
316         }\r
317 \r
318         void CanvasBits::initAPIattributes() {\r
319                 glPushAttrib(GL_ALL_ATTRIB_BITS);\r
320 \r
321                 ////    default settings        ////\r
322                 glShadeModel(GL_FLAT);\r
323                 glEnable(GL_ALPHA_TEST);\r
324                 glEnable(GL_BLEND);\r
325                 glEnable(GL_LINE_STIPPLE);\r
326                 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);\r
327                 glDisable(GL_POINT_SMOOTH);\r
328                 glDisable(GL_LINE_SMOOTH);\r
329                 glDisable(GL_POLYGON_SMOOTH);\r
330                 glDisable(GL_DEPTH_TEST);\r
331                 glDisable(GL_DITHER);\r
332                 glDisable(GL_FOG);\r
333                 glDisable(GL_LIGHTING);\r
334                 glDisable(GL_LOGIC_OP);\r
335                 glDisable(GL_STENCIL_TEST);\r
336                 glDisable(GL_TEXTURE_1D);\r
337                 glDisable(GL_TEXTURE_2D);\r
338                 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);\r
339                 glPixelTransferi(GL_MAP_STENCIL, GL_FALSE);\r
340                 glPixelTransferi(GL_RED_SCALE, 1);\r
341                 glPixelTransferi(GL_RED_BIAS, 0);\r
342                 glPixelTransferi(GL_GREEN_SCALE, 1);\r
343                 glPixelTransferi(GL_GREEN_BIAS, 0);\r
344                 glPixelTransferi(GL_BLUE_SCALE, 1);\r
345                 glPixelTransferi(GL_BLUE_BIAS, 0);\r
346                 glPixelTransferi(GL_ALPHA_SCALE, 1);\r
347                 glPixelTransferi(GL_ALPHA_BIAS, 0);\r
348                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);\r
349                 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);\r
350                 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);\r
351                 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);\r
352                 glPixelStoref(GL_UNPACK_ALIGNMENT, 4);\r
353                 glPixelStorei(GL_PACK_ALIGNMENT, 4);\r
354                 glPixelStoref(GL_PACK_ALIGNMENT, 4);\r
355                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);\r
356         }\r
357 \r
358         CanvasBits& CanvasBits::clear(const Color &col)\r
359         {\r
360                 Rectangle r(getWidth()+1, getHeight()+1);\r
361                 r.draw(col);\r
362                 return *this;\r
363         }\r
364 \r
365 \r
366         inline float setVertex(double x)\r
367         {\r
368                 return Math::round(x)*2;\r
369         }\r
370 \r
371 \r
372         CanvasBits& CanvasBits::rect(const Rectangle &drawee, const Color &color)\r
373         {\r
374                 float verts[12] = {\r
375                                                         setVertex(drawee.getLeft()), drawee.getTop(), 0,\r
376                                                         setVertex(drawee.getLeft()), drawee.getBottom()+1, 0,\r
377                                                         setVertex(drawee.getRight()+1), drawee.getBottom()+1, 0,\r
378                                                         setVertex(drawee.getRight()+1), drawee.getTop(), 0\r
379                                  };\r
380                 glVertexPointer(3, GL_FLOAT, 0, verts);\r
381 \r
382                 APIsetColor(color);\r
383 //              glColor4f(color.getR(), color.getG(), color.getB(), color.getA());\r
384                 glDrawArrays(GL_QUADS,0,4);\r
385                 return *this;\r
386         }\r
387 \r
388         CanvasBits& CanvasBits::rect(const Rectangle &drawee, const Stroke &strk)\r
389         {\r
390                 float verts[12] = {\r
391                                                         setVertex(drawee.getLeft()), drawee.getTop(), 0,\r
392                                                         setVertex(drawee.getLeft()), drawee.getBottom()+1, 0,\r
393                                                         setVertex(drawee.getRight()+1), drawee.getBottom()+1, 0,\r
394                                                         setVertex(drawee.getRight()+1), drawee.getTop(), 0\r
395                                  };\r
396                 glVertexPointer(3, GL_FLOAT, 0, verts);\r
397 \r
398                 APIsetColor(strk.color);\r
399                 glDrawArrays(GL_LINE_LOOP,0,4);\r
400                 return *this;\r
401         }\r
402 \r
403 \r
404         CanvasBits& CanvasBits::image(const Image &img) {\r
405                 drawImage(img, img.targetarea_.getLeft(), img.targetarea_.getTop());\r
406                 return *this;\r
407         }\r
408         CanvasBits& CanvasBits::image(const Image &img, const double left, const double top) {\r
409                 drawImage(img, left, top);\r
410                 return *this;\r
411         }\r
412 \r
413         void CanvasBits::drawImage(const Image &img, const double left, const double top) {\r
414                 //int left = Math::round(left__);\r
415                 //int top = Math::round(top__);\r
416                 if(img.caches.count(this)==0) {\r
417                         //img.cache(*this);\r
418                 }\r
419 \r
420                 if(img.caches.count(this)!=0) {\r
421                         APIImageCache *api_ = img.caches[this].id;\r
422                         double tex_top = 1.0;\r
423                         double tex_bottom = ((double)api_->tex_height-img.height_)/api_->tex_height;\r
424 \r
425                         glUseProgram(0);\r
426                         glColor4f(1.0,1.0,1.0,1.0);\r
427                         glEnable(GL_TEXTURE_2D);\r
428                         glBindTexture(GL_TEXTURE_2D, api_->getTexIndex());\r
429                         glBegin(GL_QUADS);\r
430 \r
431                                 glTexCoord2d(0, tex_top);\r
432                                         glVertex2f(left, top);\r
433                                 glTexCoord2d((double)img.width_/api_->tex_width , tex_top);\r
434                                         glVertex2f(left+img.targetarea_.getWidth(), top);\r
435                                 glTexCoord2d((double)img.width_/api_->tex_width , tex_bottom);\r
436                                         glVertex2f(left+img.targetarea_.getWidth(), top+img.targetarea_.getHeight());\r
437                                 glTexCoord2d(0 , tex_bottom);\r
438                                         glVertex2f(left, top+img.targetarea_.getHeight());\r
439 \r
440                         glEnd();\r
441                         glDisable(GL_TEXTURE_2D);\r
442                         glUseProgram(shader.gl2Program);\r
443                 } else {\r
444                         glRasterPos2d(left,img.height_+top);\r
445                         if( img.pixcomp_==Image::RGBA ) {\r
446                                 glPushAttrib(GL_ALL_ATTRIB_BITS);\r
447                                 glEnable(GL_BLEND);\r
448                                 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);\r
449                                 glDrawPixels((GLsizei)img.getWidth(), (GLsizei)img.getHeight(), APIImageProperties::PixCompGL_[img.pixcomp_], APIImageProperties::PixPrecGL_[img.pixprec_], img.getBitmapPtr());\r
450                                 glDisable(GL_BLEND);\r
451                                 glPopAttrib();\r
452                         } else {\r
453                                 glDrawPixels((GLsizei)img.getWidth(), (GLsizei)img.getHeight(), APIImageProperties::PixCompGL_[img.pixcomp_], APIImageProperties::PixPrecGL_[img.pixprec_], img.getBitmapPtr());\r
454                         }\r
455                 }\r
456         }\r
457 /*\r
458         void CanvasBits::drawImage(Image &img, const Rectangle &target_area)\r
459         {\r
460                 if(img.caches.count(this)==0) {\r
461                         img.cache(*this);\r
462                 }\r
463                 APIImageCache *api_ = img.caches[this].id;\r
464                 double tex_top = 1.0;\r
465                 double tex_bottom = ((double)api_->tex_height-img.height_)/api_->tex_height;\r
466 \r
467                 glColor4f(1.0,1.0,1.0,1.0);\r
468                 glEnable(GL_TEXTURE_2D);\r
469                 glBindTexture(GL_TEXTURE_2D, api_->getTexIndex());\r
470                 glBegin(GL_QUADS);\r
471                         glTexCoord2d(0, tex_top);\r
472                                 glVertex2f(target_area.left, target_area.top);\r
473                         glTexCoord2d((double)img.width_/api_->tex_width , tex_top);\r
474                                 glVertex2f(target_area.right, target_area.top);\r
475                         glTexCoord2d((double)img.width_/api_->tex_width , tex_bottom);\r
476                                 glVertex2f(target_area.right, target_area.bottom);\r
477                         glTexCoord2d(0 , tex_bottom);\r
478                                 glVertex2f(target_area.left, target_area.bottom);\r
479 \r
480                 glEnd();\r
481                 glDisable(GL_TEXTURE_2D);\r
482         }\r
483 \r
484         CanvasBits& CanvasBits::image(Image &img, const Rectangle &target_area, const Rectangle &source_rect)\r
485         {\r
486                 if(img.caches.count(this)==0) {\r
487                         img.cache(*this);\r
488                 }\r
489                 APIImageCache *api_ = img.caches[this].id;\r
490 //              double right = left + source_rect.getWidth();\r
491 //              double bottom = top + source_rect.getHeight();\r
492                 double tex_top = 1.0;\r
493                 double tex_bottom = ((double)api_->tex_height-img.height_)/api_->tex_height;\r
494                 double tex_left = 0.0;\r
495                 double tex_right = (double)img.width_/api_->tex_width;\r
496                 double tex_height = tex_top - tex_bottom;\r
497                 double tex_width  = tex_right - tex_left;\r
498                 tex_left = tex_width * source_rect.getLeft() / (img.getWidth()-1.0);\r
499                 tex_top = 1.0 - tex_height * source_rect.getTop() / (img.getHeight()-1.0);\r
500                 tex_right = tex_width * source_rect.getRight() / (img.getWidth()-1.0);\r
501                 tex_bottom = 1.0 - tex_height * source_rect.getBottom() / (img.getHeight()-1.0);\r
502 //std::cout << "D   " << tex_left << " " << tex_top << " " << tex_right << " " << tex_bottom << std::endl;\r
503 \r
504                 glColor4f(1.0,1.0,1.0,1.0);\r
505                 glEnable(GL_TEXTURE_2D);\r
506                 glBindTexture(GL_TEXTURE_2D, api_->getTexIndex());\r
507                 glBegin(GL_QUADS);\r
508 \r
509                 glTexCoord2d(tex_left, tex_top);\r
510 //              glVertex2f(left, top);\r
511                 glVertex2f(target_area.left, target_area.top);\r
512 \r
513                 glTexCoord2d(tex_right, tex_top);\r
514 //              glVertex2f(right, top);\r
515                 glVertex2f(target_area.right+1, target_area.top);\r
516 \r
517                 glTexCoord2d(tex_right , tex_bottom);\r
518 //              glVertex2f(right, bottom);\r
519                 glVertex2f(target_area.right+1, target_area.bottom+1);\r
520 \r
521                 glTexCoord2d(tex_left , tex_bottom);\r
522 //              glVertex2f(left, bottom);\r
523                 glVertex2f(target_area.left, target_area.bottom+1);\r
524 \r
525 \r
526                 glEnd();\r
527                 glDisable(GL_TEXTURE_2D);\r
528                 return *this;\r
529         }\r
530 */\r
531 \r
532         void CanvasBits::cacheImageBody(Image &img) {\r
533                 APIImageCache *api_ = 0;\r
534                 int tex_width = 0, tex_height = 0;\r
535 \r
536                 if(img.caches.count(this)==0) {\r
537                         int i;\r
538                         i=0;\r
539                         while(pow(2.0,++i)<img.width_*2) {}\r
540                         tex_width = pow(2.0,i);\r
541                         i=0;\r
542                         while(pow(2.0,++i)<img.height_) {}\r
543                         tex_height = pow(2.0,i);\r
544                 } else {\r
545                         api_ = img.caches[this].id;\r
546                         tex_width = api_->tex_width;\r
547                         tex_height = api_->tex_height;\r
548                 }\r
549 \r
550                 Image *tmp = new Image(tex_width, tex_height, img.pixcomp_, Image::BYTE);\r
551                 int ra, hir, lor, hig, log, hib, lob;\r
552                 double r, g, b, alpha;\r
553                 for(int y=0; y<img.height_; y++) {\r
554                         for(int x=0; x<img.width_; x++) {\r
555                                 img.getPix(x,y).get(r, g, b, alpha);\r
556                                 ra = (int)(r * 65535);\r
557                                 hir = (ra & 0xff00) >> 8;\r
558                                 lor = (ra & 0xff);\r
559                                 ra = (int)(g * 65535);\r
560                                 hig = (ra & 0xff00) >> 8;\r
561                                 log = (ra & 0xff);\r
562                                 ra = (int)(b * 65535);\r
563                                 hib = (ra & 0xff00) >> 8;\r
564                                 lob = (ra & 0xff);\r
565                                 tmp->pix(x*2  ,y, Color(hir/255.0, hig/255.0, hib/255.0, alpha));\r
566                                 tmp->pix(x*2+1,y, Color(lor/255.0, log/255.0, lob/255.0, alpha));\r
567                         }\r
568                 }\r
569 \r
570                 if(img.caches.count(this)!=0) {\r
571                         glEnable(GL_TEXTURE_2D);\r
572                         glBindTexture(GL_TEXTURE_2D, api_->VRAMoffset);\r
573                         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, api_->tex_width, api_->tex_height, APIImageProperties::PixCompGL_[img.pixcomp_], APIImageProperties::PixPrecGL_[Image::BYTE], tmp->getBitmapPtr());\r
574                         glDisable(GL_TEXTURE_2D);\r
575                 } else {\r
576                         api_ = new APIImageCache;\r
577                         img.caches.insert(std::pair<DrawableWithCache*, ImageCache_>(this, ImageCache_(api_, false)));\r
578                         api_->tex_width = tex_width;\r
579                         api_->tex_height = tex_height;\r
580                         glEnable(GL_TEXTURE_2D);\r
581                         glGenTextures(1, &api_->VRAMoffset);\r
582                         glBindTexture(GL_TEXTURE_2D, api_->VRAMoffset);\r
583                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);\r
584                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);\r
585 //                              glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);\r
586 //                              glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);\r
587                         glTexImage2D(GL_TEXTURE_2D, 0, Image::PixCompSize_[img.pixcomp_], api_->tex_width, api_->tex_height, 0, APIImageProperties::PixCompGL_[img.pixcomp_], APIImageProperties::PixPrecGL_[Image::BYTE], tmp->getBitmapPtr());\r
588                         glDisable(GL_TEXTURE_2D);\r
589 \r
590 //                              APIImageProperties::regist((int)Display::getWidth(), (int)Display::getHeight(), width_, height_, VRAMleft_, VRAMtop_);\r
591 //                              glDisable(GL_BLEND);\r
592 //                              glDrawBuffer(GL_AUX1);\r
593 //                              for(int y=0; y<height_; y++) {\r
594 //                                      for(int x=0; x<width_; x++) {\r
595 //                                              Display::pix(x+VRAMleft_, y+VRAMtop_, getPix(x, y));\r
596 //                                      }\r
597 //                              }\r
598 //                              glDrawBuffer(GL_BACK);\r
599 //                              glEnable(GL_BLEND);\r
600 \r
601                 }\r
602                 glDrawBuffer(GL_AUX1);\r
603                 glColor4f(1.0,1.0,1.0,1.0);\r
604                 glEnable(GL_TEXTURE_2D);\r
605                 glBindTexture(GL_TEXTURE_2D, api_->getTexIndex());\r
606                 glBegin(GL_QUADS);\r
607                         glTexCoord2d(0 , img.height_);\r
608                                 glVertex2f(img.targetarea_.getLeft() , img.targetarea_.getTop());\r
609                         glTexCoord2d(img.width_ , img.height_);\r
610                                 glVertex2f(img.targetarea_.getRight()+1, img.targetarea_.getTop());\r
611                         glTexCoord2d(img.width_ , 0);\r
612                                 glVertex2f(img.targetarea_.getRight()+1, img.targetarea_.getBottom()+1);\r
613                         glTexCoord2d(0, 0);\r
614                                 glVertex2f(img.targetarea_.getLeft() , img.targetarea_.getBottom()+1);\r
615                 glEnd();\r
616                 glDisable(GL_TEXTURE_2D);\r
617                 glDrawBuffer(GL_BACK);\r
618                 delete tmp;\r
619         }\r
620 \r
621 \r
622 \r
623 \r
624 }\r
625 \r
626 \r
627 }       /*      <- namespace Psycholops         */\r
628 \r
629 \r