blob: 51435acfa0fb2a3f85b753dbc6a3fdb68720806b [file] [log] [blame]
Brianbb4a9b22007-09-14 09:58:32 -06001/**
2 * glCopyPixels test
3 *
4 * Brian Paul
5 * 14 Sep 2007
6 */
7
8
Brianbb4a9b22007-09-14 09:58:32 -06009#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
José Fonseca2e61d132009-01-24 16:39:49 +000012#include <GL/glew.h>
Brianbb4a9b22007-09-14 09:58:32 -060013#include <GL/glut.h>
14
15#include "readtex.h"
16
17#define IMAGE_FILE "../images/arch.rgb"
18
19static int ImgWidth, ImgHeight;
20static GLenum ImgFormat;
21static GLubyte *Image = NULL;
22
23static int WinWidth = 800, WinHeight = 800;
24static int Xpos, Ypos;
25static int Scissor = 0;
26static float Xzoom, Yzoom;
27static GLboolean DrawFront = GL_FALSE;
28static GLboolean Dither = GL_TRUE;
29
30
31static void Reset( void )
32{
33 Xpos = Ypos = 20;
34 Scissor = 0;
35 Xzoom = Yzoom = 1.0;
36}
37
38
39static void Display( void )
40{
41 const int dx = (WinWidth - ImgWidth) / 2;
42 const int dy = (WinHeight - ImgHeight) / 2;
43
44 if (DrawFront) {
45 glDrawBuffer(GL_FRONT);
46 glReadBuffer(GL_FRONT);
47 }
48 else {
49 glDrawBuffer(GL_BACK);
50 glReadBuffer(GL_BACK);
51 }
52
53 glClear( GL_COLOR_BUFFER_BIT );
54
55 /* draw original image */
56 glWindowPos2iARB(dx, dy);
57 glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
58
59 if (Scissor)
60 glEnable(GL_SCISSOR_TEST);
61
62 /* draw copy */
63 glPixelZoom(Xzoom, Yzoom);
64 glWindowPos2iARB(Xpos, Ypos);
65 glCopyPixels(dx, dy, ImgWidth, ImgHeight, GL_COLOR);
66 glPixelZoom(1, 1);
67
68 glDisable(GL_SCISSOR_TEST);
69
70 if (DrawFront)
71 glFinish();
72 else
73 glutSwapBuffers();
74}
75
76
77static void Reshape( int width, int height )
78{
79 WinWidth = width;
80 WinHeight = height;
81
82 glViewport( 0, 0, width, height );
83 glMatrixMode( GL_PROJECTION );
84 glLoadIdentity();
85 glOrtho( 0.0, width, 0.0, height, 0.0, 2.0 );
86 glMatrixMode( GL_MODELVIEW );
87 glLoadIdentity();
88
89 glScissor(width/4, height/4, width/2, height/2);
90}
91
92
93static void Key( unsigned char key, int x, int y )
94{
95 (void) x;
96 (void) y;
97 switch (key) {
98 case ' ':
99 Reset();
100 break;
101 case 'd':
102 Dither = !Dither;
103 if (Dither)
104 glEnable(GL_DITHER);
105 else
106 glDisable(GL_DITHER);
107 break;
108 case 's':
109 Scissor = !Scissor;
110 break;
111 case 'x':
112 Xzoom -= 0.1;
113 break;
114 case 'X':
115 Xzoom += 0.1;
116 break;
117 case 'y':
118 Yzoom -= 0.1;
119 break;
120 case 'Y':
121 Yzoom += 0.1;
122 break;
123 case 'f':
124 DrawFront = !DrawFront;
125 printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
126 break;
127 case 27:
128 exit(0);
129 break;
130 }
131 glutPostRedisplay();
132}
133
134
135static void SpecialKey( int key, int x, int y )
136{
137 const int step = (glutGetModifiers() & GLUT_ACTIVE_SHIFT) ? 10 : 1;
138 (void) x;
139 (void) y;
140 switch (key) {
141 case GLUT_KEY_UP:
142 Ypos += step;
143 break;
144 case GLUT_KEY_DOWN:
145 Ypos -= step;
146 break;
147 case GLUT_KEY_LEFT:
148 Xpos -= step;
149 break;
150 case GLUT_KEY_RIGHT:
151 Xpos += step;
152 break;
153 }
154 glutPostRedisplay();
155}
156
157
158static void Init( GLboolean ciMode, const char *filename )
159{
160 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
161 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
162
163 Image = LoadRGBImage( filename, &ImgWidth, &ImgHeight, &ImgFormat );
164 if (!Image) {
165 printf("Couldn't read %s\n", filename);
166 exit(0);
167 }
168
169 if (ciMode) {
170 /* Convert RGB image to grayscale */
171 GLubyte *indexImage = (GLubyte *) malloc( ImgWidth * ImgHeight );
172 GLint i;
173 for (i=0; i<ImgWidth*ImgHeight; i++) {
174 int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
175 indexImage[i] = gray / 3;
176 }
177 free(Image);
178 Image = indexImage;
179 ImgFormat = GL_COLOR_INDEX;
180
181 for (i=0;i<255;i++) {
182 float g = i / 255.0;
183 glutSetColor(i, g, g, g);
184 }
185 }
186
187 printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
188
189 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
190 glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
191
192 Reset();
193}
194
195
196static void Usage(void)
197{
198 printf("Keys:\n");
199 printf(" SPACE Reset Parameters\n");
200 printf(" Up/Down Move image up/down (SHIFT for large step)\n");
201 printf(" Left/Right Move image left/right (SHIFT for large step)\n");
202 printf(" x Decrease X-axis PixelZoom\n");
203 printf(" X Increase X-axis PixelZoom\n");
204 printf(" y Decrease Y-axis PixelZoom\n");
205 printf(" Y Increase Y-axis PixelZoom\n");
206 printf(" s Toggle GL_SCISSOR_TEST\n");
207 printf(" f Toggle front/back buffer drawing\n");
208 printf(" ESC Exit\n");
209}
210
211
212int main( int argc, char *argv[] )
213{
214 GLboolean ciMode = GL_FALSE;
215 const char *filename = IMAGE_FILE;
216 int i = 1;
217
218 if (argc > i && strcmp(argv[i], "-ci")==0) {
219 ciMode = GL_TRUE;
220 i++;
221 }
222 if (argc > i) {
223 filename = argv[i];
224 }
225
226 glutInit( &argc, argv );
227 glutInitWindowPosition( 0, 0 );
228 glutInitWindowSize( WinWidth, WinHeight );
229
230 if (ciMode)
231 glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
232 else
233 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE);
234
235 glutCreateWindow(argv[0]);
José Fonseca2e61d132009-01-24 16:39:49 +0000236 glewInit();
Brianbb4a9b22007-09-14 09:58:32 -0600237
238 Init(ciMode, filename);
239 Usage();
240
241 glutReshapeFunc( Reshape );
242 glutKeyboardFunc( Key );
243 glutSpecialFunc( SpecialKey );
244 glutDisplayFunc( Display );
245
246 glutMainLoop();
247 return 0;
248}