33 #ifndef __INCLUDE_MIST_DRAW__
34 #define __INCLUDE_MIST_DRAW__
37 #ifndef __INCLUDE_MIST_H__
42 #ifndef __INCLUDE_MIST_COLOR_H__
46 #ifndef __INCLUDE_MIST_VECTOR__
50 #ifndef __INCLUDE_BITMAP_H__
55 #if defined( __MIST_WINDOWS__ ) && __MIST_WINDOWS__ > 0
74 static float get_value(
double v ){
return( static_cast< float >( v ) ); }
80 static float get_value(
double v ){
return( static_cast< float >( v ) / 255.0f ); }
83 template <
class T >
struct gl_type_trait { _MIST_CONST( GLenum, gl_type, 0 ); };
84 template <>
struct gl_type_trait< unsigned char > { _MIST_CONST( GLenum, gl_type, GL_UNSIGNED_BYTE ); };
85 template <>
struct gl_type_trait< unsigned short >{ _MIST_CONST( GLenum, gl_type, GL_UNSIGNED_SHORT ); };
86 template <>
struct gl_type_trait< unsigned int > { _MIST_CONST( GLenum, gl_type, GL_UNSIGNED_INT ); };
87 template <>
struct gl_type_trait< unsigned long > { _MIST_CONST( GLenum, gl_type, GL_UNSIGNED_INT ); };
88 template <>
struct gl_type_trait< signed char > { _MIST_CONST( GLenum, gl_type, GL_BYTE ); };
89 template <>
struct gl_type_trait< signed short > { _MIST_CONST( GLenum, gl_type, GL_SHORT ); };
90 template <>
struct gl_type_trait< signed int > { _MIST_CONST( GLenum, gl_type, GL_INT ); };
91 template <>
struct gl_type_trait< signed long > { _MIST_CONST( GLenum, gl_type, GL_INT ); };
92 template <>
struct gl_type_trait< bool > { _MIST_CONST( GLenum, gl_type, GL_UNSIGNED_BYTE ); };
93 template <>
struct gl_type_trait< char > { _MIST_CONST( GLenum, gl_type, GL_BYTE ); };
94 template <>
struct gl_type_trait< float > { _MIST_CONST( GLenum, gl_type, GL_FLOAT ); };
95 template <>
struct gl_type_trait< double > { _MIST_CONST( GLenum, gl_type, GL_DOUBLE ); };
98 template <
bool color >
99 struct __glTexImage2D__
101 template <
class Array >
102 static void glTexImage2D(
const Array &image )
105 GL_TEXTURE_2D, 0, GL_RGBA,
106 static_cast< GLsizei >( image.width( ) ),
107 static_cast< GLsizei >( image.height( ) ),
110 gl_type_trait< typename Array::value_type >::gl_type,
111 static_cast< const GLvoid* >( &( image[0] ) )
117 struct __glTexImage2D__< true >
119 template <
class T,
class Allocator >
120 static void glTexImage2D(
const array2< rgb< T >, Allocator > &image )
123 GL_TEXTURE_2D, 0, GL_RGBA,
124 static_cast< GLsizei >( image.width( ) ),
125 static_cast< GLsizei >( image.height( ) ),
128 gl_type_trait< T >::gl_type,
129 static_cast< const GLvoid* >( &( image[0] ) )
133 template <
size_t BITS,
class Allocator >
134 static void glTexImage2D(
const bitmap< BITS, Allocator > &image )
137 GL_TEXTURE_2D, 0, GL_RGBA,
138 static_cast< GLsizei >( image.width( ) ),
139 static_cast< GLsizei >( image.height( ) ),
142 gl_type_trait< unsigned char >::gl_type,
143 static_cast< const GLvoid* >( &( image[0] ) )
147 template <
class T,
class Allocator >
148 static void glTexImage2D(
const array2< rgba< T >, Allocator > &image )
151 GL_TEXTURE_2D, 0, GL_RGBA,
152 static_cast< GLsizei >( image.width( ) ),
153 static_cast< GLsizei >( image.height( ) ),
156 gl_type_trait< T >::gl_type,
157 static_cast< const GLvoid* >( &( image[0] ) )
162 template <
bool color >
163 struct __glDrawPixels__
165 template <
class Array >
166 static void glDrawPixels(
const Array &image,
typename Array::size_type w,
typename Array::size_type h )
169 static_cast< GLsizei >( w ),
170 static_cast< GLsizei >( h ),
172 gl_type_trait< typename Array::value_type >::gl_type,
173 static_cast< const GLvoid* >( &( image[0] ) )
179 struct __glDrawPixels__< true >
181 template <
class T,
class Allocator >
182 static void glDrawPixels(
const array2< rgb< T >, Allocator > &image,
typename array2< T, Allocator >::size_type w,
typename array2< T, Allocator >::size_type h )
185 static_cast< GLsizei >( w ),
186 static_cast< GLsizei >( h ),
188 gl_type_trait< T >::gl_type,
189 static_cast< const GLvoid* >( &( image[0] ) )
193 template <
size_t BITS,
class Allocator >
194 static void glDrawPixels(
const bitmap< BITS, Allocator > &image,
typename bitmap< BITS, Allocator >::size_type w,
typename bitmap< BITS, Allocator >::size_type h )
197 static_cast< GLsizei >( w ),
198 static_cast< GLsizei >( h ),
200 gl_type_trait< unsigned char >::gl_type,
201 static_cast< const GLvoid* >( &( image[0] ) )
205 template <
class T,
class Allocator >
206 static void glDrawPixels(
const array2< rgba< T >, Allocator > &image,
typename array2< T, Allocator >::size_type w,
typename array2< T, Allocator >::size_type h )
209 static_cast< GLsizei >( w ),
210 static_cast< GLsizei >( h ),
212 gl_type_trait< T >::gl_type,
213 static_cast< const GLvoid* >( &( image[0] ) )
218 template<
class Array >
219 void glTexImage2D(
const Array &image )
221 __glTexImage2D__< is_color< typename Array::value_type >::value >::glTexImage2D( image );
224 template<
class Array >
225 void glDrawPixels(
const Array &image,
typename Array::size_type w,
typename Array::size_type h )
227 __glDrawPixels__< is_color< typename Array::value_type >::value >::glDrawPixels( image, w, h );
231 inline size_t floor_square_index(
size_t v )
238 for(
size_t i = 1, _2 = 2 ; i < 64 ; i++ )
250 inline size_t floor_square(
size_t v )
257 for(
size_t i = 1, _2 = 2 ; i < 64 ; i++ )
270 inline bool is_floor_square(
size_t v )
272 return( ( v & ( v - 1 ) ) == 0 );
306 template<
class Array >
308 typename Array::size_type image_width,
typename Array::size_type image_height,
309 typename Array::size_type window_width,
typename Array::size_type window_height,
310 double zoom = 1.0,
double xpos = 0.0,
double ypos = 0.0,
bool interpolate =
true )
313 typedef typename Array::value_type value_type;
314 typedef pixel_data::pixel< is_float< value_type >::value > pixel;
316 if( image_width > image.width( ) || image_height > image.height( ) )
return(
false );
317 if( image.width( ) != image.height( ) )
return(
false );
319 typedef typename Array::size_type size_type;
322 if( !pixel_data::is_floor_square( image.width( ) ) )
return(
false );
325 if( image.empty( ) )
return(
false );
327 double ttt =
static_cast< double >( image.width( ) );
328 int interpolate_ = interpolate ? GL_LINEAR: GL_NEAREST;
330 glMatrixMode( GL_MODELVIEW );
332 glMatrixMode( GL_PROJECTION );
335 GLboolean isLighting = glIsEnabled( GL_LIGHTING );
336 glDisable( GL_LIGHTING );
337 glEnable( GL_BLEND );
339 glViewport( 0, 0, static_cast< GLint >( window_width ), static_cast< GLint >( window_height ) );
341 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
343 glPixelStorei( GL_UNPACK_ROW_LENGTH, static_cast< GLint >( image.width( ) ) );
344 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
345 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
346 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, interpolate_ );
347 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, interpolate_ );
349 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
351 pixel_data::glTexImage2D( image );
353 glEnable( GL_TEXTURE_2D );
355 double v0[2], v1[2], v2[2], v3[2], t0[2], t1[2], t2[2], t3[2];
356 double imgW =
static_cast< double >( image_width );
357 double imgH =
static_cast< double >( image_height );
358 double winW =
static_cast< double >( window_width );
359 double winH =
static_cast< double >( window_height );
370 v0[0] = ( -1.0 - xpos / imgW ) * zoom;
371 v0[1] = ( -1.0 + ypos / imgH ) * zoom;
372 v1[0] = ( 1.0 - xpos / imgW ) * zoom;
373 v1[1] = ( -1.0 + ypos / imgH ) * zoom;
374 v2[0] = ( 1.0 - xpos / imgW ) * zoom;
375 v2[1] = ( 1.0 + ypos / imgH ) * zoom;
376 v3[0] = ( -1.0 - xpos / imgW ) * zoom;
377 v3[1] = ( 1.0 + ypos / imgH ) * zoom;
379 double win_aspect = winH / winW;
380 double aspect = imgH * image.reso2( ) / image.reso1( ) / imgW;
381 if( win_aspect < aspect )
383 v0[0] *= win_aspect / aspect;
384 v1[0] *= win_aspect / aspect;
385 v2[0] *= win_aspect / aspect;
386 v3[0] *= win_aspect / aspect;
391 v0[1] /= win_aspect / aspect;
392 v1[1] /= win_aspect / aspect;
393 v2[1] /= win_aspect / aspect;
394 v3[1] /= win_aspect / aspect;
399 glTexCoord2d( t0[0], t0[1] );
400 glVertex2d ( v0[0], v0[1] );
401 glTexCoord2d( t1[0], t1[1] );
402 glVertex2d ( v1[0], v1[1] );
403 glTexCoord2d( t2[0], t2[1] );
404 glVertex2d ( v2[0], v2[1] );
405 glTexCoord2d( t3[0], t3[1] );
406 glVertex2d ( v3[0], v3[1] );
409 glDisable( GL_TEXTURE_2D );
411 if( isLighting == GL_TRUE ) glEnable( GL_LIGHTING );
458 template<
class Array >
459 bool draw_image(
const Array &image,
typename Array::size_type window_width,
typename Array::size_type window_height,
460 double zoom = 1.0,
double xpos = 0.0,
double ypos = 0.0,
double back_r = 0.0,
double back_g = 0.0,
double back_b = 0.0,
bool interpolate =
true,
bool blend =
false )
462 typedef typename Array::size_type size_type;
463 typedef typename Array::value_type value_type;
464 typedef pixel_data::pixel< is_float< value_type >::value > pixel;
465 size_type size = image.width( ) > image.height( ) ? image.width( ) : image.height( );
466 size_type ttt = pixel_data::floor_square( size );
470 float r = pixel::get_value( back_r );
471 float g = pixel::get_value( back_g );
472 float b = pixel::get_value( back_b );
474 glClearColor( r, g, b, 1.0f );
475 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
478 if( ttt == image.width( ) && ttt == image.height( ) )
480 return(
draw_buffer( image, ttt, ttt, window_width, window_height, zoom, xpos, ypos, interpolate ) );
486 if( ttt > img.width( ) )
488 img.resize( ttt, ttt );
490 img.reso1( image.reso1( ) );
491 img.reso2( image.reso2( ) );
494 for( j = 0 ; j < image.height( ) ; j++ )
496 for( i = 0 ; i < image.width( ) ; i++ )
498 img( i, j ) = image( i, j );
502 return(
draw_buffer( img, image.width( ), image.height( ), window_width, window_height, zoom, xpos, ypos, interpolate ) );
539 template<
class Array >
540 bool draw_pixels(
const Array &image,
typename Array::size_type window_width,
typename Array::size_type window_height,
541 typename Array::difference_type xpos = 0,
typename Array::difference_type ypos = 0,
bool blend =
false )
543 typedef typename Array::size_type size_type;
544 typedef typename Array::value_type value_type;
545 typedef pixel_data::pixel< is_float< value_type >::value > pixel;
547 if( image.empty( ) )
return(
false );
551 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
552 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
555 GLsizei w =
static_cast< GLsizei
>( window_width / 2 );
556 GLsizei h =
static_cast< GLsizei
>( window_height / 2 );
558 glMatrixMode( GL_PROJECTION );
560 gluOrtho2D( -w, w, -h, h );
562 glMatrixMode( GL_MODELVIEW );
566 gluLookAt( w, h, -1, w, h, 0, 0, -1, 0 );
568 GLboolean isLighting = glIsEnabled( GL_LIGHTING );
569 glDisable( GL_LIGHTING );
570 glEnable( GL_BLEND );
572 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
574 size_type ww = image.width( );
575 size_type hh = image.height( );
577 glPixelStorei( GL_UNPACK_ROW_LENGTH, static_cast< GLint >( image.width( ) ) );
580 glPixelStorei( GL_UNPACK_SKIP_PIXELS, static_cast< GLint >( -xpos ) );
586 glPixelStorei( GL_UNPACK_SKIP_ROWS, static_cast< GLint >( -ypos ) );
591 glRasterPos2i( static_cast< GLsizei >( xpos ), static_cast< GLsizei >( ypos ) );
592 glPixelZoom( 1.0f, -1.0f );
593 pixel_data::glDrawPixels( image, ww, hh );
595 if( isLighting == GL_TRUE ) glEnable( GL_LIGHTING );
618 double winW,
double winH,
double zoom,
double posx,
double posy )
620 if( imgX == 0.0 || imgY == 0.0 || winW == 0.0 || winH == 0.0 )
625 double xx = ( pt.
x - winW / 2.0 ) / winW * 2.0;
626 double yy = ( winH / 2.0 - pt.
y ) / winH * 2.0;
627 double win_aspect = winH / winW;
628 aspect = imgY * aspect / imgX;
629 if( win_aspect < aspect )
631 xx = xx / win_aspect * aspect / zoom + posx / imgX;
632 yy = -( yy / zoom - posy / imgY );
636 xx = xx / zoom + posx / imgX;
637 yy = -( yy * win_aspect / aspect / zoom - posy / imgY );
640 return(
vector2< double >( ( xx + 1.0 ) * imgX * 0.5, ( yy + 1.0 ) * imgY * 0.5 ) );
660 double winW,
double winH,
double zoom,
double posx,
double posy )
662 if( imgX == 0.0 || imgY == 0.0 || winW == 0.0 || winH == 0.0 )
667 double xx = ( pt.
x - imgX / 2.0 ) / imgX * 2.0;
668 double yy = ( imgY / 2.0 - pt.
y ) / imgY * 2.0;
669 double win_aspect = winH / winW;
670 aspect = imgY * aspect / imgX;
671 if( win_aspect < aspect )
673 xx = ( xx - posx / imgX ) * zoom * win_aspect / aspect;
674 yy = ( yy + posy / imgY ) * zoom;
678 xx = ( xx - posx / imgX ) * zoom;
679 yy = ( yy + posy / imgY ) * zoom / win_aspect * aspect;
681 return(
vector2< double >( ( xx + 1.0 ) * winW * 0.5, ( 1.0 - yy ) * winH * 0.5 ) );
693 #endif // __INCLUDE_MIST_DRAW__