9 #define GL_GLEXT_PROTOTYPES
18 #define IMAGE_FILE "../images/arch.rgb"
20 static int ImgWidth
, ImgHeight
;
21 static GLenum ImgFormat
;
22 static GLubyte
*Image
= NULL
;
24 static int WinWidth
= 800, WinHeight
= 800;
25 static int Xpos
, Ypos
;
26 static int Scissor
= 0;
27 static float Xzoom
, Yzoom
;
28 static GLboolean DrawFront
= GL_FALSE
;
29 static GLboolean Dither
= GL_TRUE
;
32 static void Reset( void )
40 static void Display( void )
42 const int dx
= (WinWidth
- ImgWidth
) / 2;
43 const int dy
= (WinHeight
- ImgHeight
) / 2;
46 glDrawBuffer(GL_FRONT
);
47 glReadBuffer(GL_FRONT
);
50 glDrawBuffer(GL_BACK
);
51 glReadBuffer(GL_BACK
);
54 glClear( GL_COLOR_BUFFER_BIT
);
56 /* draw original image */
57 glWindowPos2iARB(dx
, dy
);
58 glDrawPixels(ImgWidth
, ImgHeight
, ImgFormat
, GL_UNSIGNED_BYTE
, Image
);
61 glEnable(GL_SCISSOR_TEST
);
64 glPixelZoom(Xzoom
, Yzoom
);
65 glWindowPos2iARB(Xpos
, Ypos
);
66 glCopyPixels(dx
, dy
, ImgWidth
, ImgHeight
, GL_COLOR
);
69 glDisable(GL_SCISSOR_TEST
);
78 static void Reshape( int width
, int height
)
83 glViewport( 0, 0, width
, height
);
84 glMatrixMode( GL_PROJECTION
);
86 glOrtho( 0.0, width
, 0.0, height
, 0.0, 2.0 );
87 glMatrixMode( GL_MODELVIEW
);
90 glScissor(width
/4, height
/4, width
/2, height
/2);
94 static void Key( unsigned char key
, int x
, int y
)
107 glDisable(GL_DITHER
);
125 DrawFront
= !DrawFront
;
126 printf("glDrawBuffer(%s)\n", DrawFront
? "GL_FRONT" : "GL_BACK");
136 static void SpecialKey( int key
, int x
, int y
)
138 const int step
= (glutGetModifiers() & GLUT_ACTIVE_SHIFT
) ? 10 : 1;
159 static void Init( GLboolean ciMode
, const char *filename
)
161 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
162 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
164 Image
= LoadRGBImage( filename
, &ImgWidth
, &ImgHeight
, &ImgFormat
);
166 printf("Couldn't read %s\n", filename
);
171 /* Convert RGB image to grayscale */
172 GLubyte
*indexImage
= (GLubyte
*) malloc( ImgWidth
* ImgHeight
);
174 for (i
=0; i
<ImgWidth
*ImgHeight
; i
++) {
175 int gray
= Image
[i
*3] + Image
[i
*3+1] + Image
[i
*3+2];
176 indexImage
[i
] = gray
/ 3;
180 ImgFormat
= GL_COLOR_INDEX
;
182 for (i
=0;i
<255;i
++) {
184 glutSetColor(i
, g
, g
, g
);
188 printf("Loaded %d by %d image\n", ImgWidth
, ImgHeight
);
190 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
191 glPixelStorei(GL_UNPACK_ROW_LENGTH
, ImgWidth
);
197 static void Usage(void)
200 printf(" SPACE Reset Parameters\n");
201 printf(" Up/Down Move image up/down (SHIFT for large step)\n");
202 printf(" Left/Right Move image left/right (SHIFT for large step)\n");
203 printf(" x Decrease X-axis PixelZoom\n");
204 printf(" X Increase X-axis PixelZoom\n");
205 printf(" y Decrease Y-axis PixelZoom\n");
206 printf(" Y Increase Y-axis PixelZoom\n");
207 printf(" s Toggle GL_SCISSOR_TEST\n");
208 printf(" f Toggle front/back buffer drawing\n");
209 printf(" ESC Exit\n");
213 int main( int argc
, char *argv
[] )
215 GLboolean ciMode
= GL_FALSE
;
216 const char *filename
= IMAGE_FILE
;
219 if (argc
> i
&& strcmp(argv
[i
], "-ci")==0) {
227 glutInit( &argc
, argv
);
228 glutInitWindowPosition( 0, 0 );
229 glutInitWindowSize( WinWidth
, WinHeight
);
232 glutInitDisplayMode( GLUT_INDEX
| GLUT_DOUBLE
);
234 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
236 glutCreateWindow(argv
[0]);
238 Init(ciMode
, filename
);
241 glutReshapeFunc( Reshape
);
242 glutKeyboardFunc( Key
);
243 glutSpecialFunc( SpecialKey
);
244 glutDisplayFunc( Display
);