750592ed79f2efd71c3e8e2d1761b15b716642d3
[mesa.git] / progs / tests / invert.c
1 /*
2 * (C) Copyright IBM Corporation 2005
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file invert.c
27 *
28 * Simple test of GL_MESA_pack_invert functionality. Three squares are
29 * drawn. The first two should look the same, and the third one should
30 * look inverted.
31 *
32 * \author Ian Romanick <idr@us.ibm.com>
33 */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <GL/glut.h>
39
40 #include "readtex.h"
41
42 #define IMAGE_FILE "../images/tree3.rgb"
43
44 static int Width = 420;
45 static int Height = 150;
46 static const GLfloat Near = 5.0, Far = 25.0;
47
48 static GLubyte * image = NULL;
49 static GLubyte * temp_image = NULL;
50 static GLuint img_width = 0;
51 static GLuint img_height = 0;
52 static GLuint img_format = 0;
53
54 PFNGLWINDOWPOS2IPROC win_pos_2i = NULL;
55
56
57 static void Display( void )
58 {
59 GLint err;
60
61
62 glClearColor(0.2, 0.2, 0.8, 0);
63 glClear( GL_COLOR_BUFFER_BIT );
64
65
66 /* This is the "reference" square.
67 */
68
69 (*win_pos_2i)( 5, 5 );
70 glDrawPixels( img_width, img_height, img_format, GL_UNSIGNED_BYTE, image );
71
72 glPixelStorei( GL_PACK_INVERT_MESA, GL_FALSE );
73 err = glGetError();
74 if ( err != GL_NO_ERROR ) {
75 printf( "Setting PACK_INVERT_MESA to false generated an error (0x%04x).\n",
76 err );
77 }
78
79 glReadPixels( 5, 5, img_width, img_height, img_format, GL_UNSIGNED_BYTE, temp_image );
80 (*win_pos_2i)( 5 + 1 * (10 + img_width), 5 );
81 glDrawPixels( img_width, img_height, img_format, GL_UNSIGNED_BYTE, temp_image );
82
83 glPixelStorei( GL_PACK_INVERT_MESA, GL_TRUE );
84 err = glGetError();
85 if ( err != GL_NO_ERROR ) {
86 printf( "Setting PACK_INVERT_MESA to true generated an error (0x%04x).\n",
87 err );
88 }
89
90 glReadPixels( 5, 5, img_width, img_height, img_format, GL_UNSIGNED_BYTE, temp_image );
91 (*win_pos_2i)( 5 + 2 * (10 + img_width), 5 );
92 glDrawPixels( img_width, img_height, img_format, GL_UNSIGNED_BYTE, temp_image );
93
94 glutSwapBuffers();
95 }
96
97
98 static void Reshape( int width, int height )
99 {
100 GLfloat ar = (float) width / (float) height;
101 Width = width;
102 Height = height;
103 glViewport( 0, 0, width, height );
104 glMatrixMode( GL_PROJECTION );
105 glLoadIdentity();
106 glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
107 glMatrixMode( GL_MODELVIEW );
108 glLoadIdentity();
109 glTranslatef( 0.0, 0.0, -15.0 );
110 }
111
112
113 static void Key( unsigned char key, int x, int y )
114 {
115 (void) x;
116 (void) y;
117 switch (key) {
118 case 27:
119 exit(0);
120 break;
121 }
122 glutPostRedisplay();
123 }
124
125
126 static void Init( void )
127 {
128 const char * const ver_string = (const char * const)
129 glGetString( GL_VERSION );
130 const float ver = strtof( ver_string, NULL );
131
132
133 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
134 printf("GL_VERSION = %s\n", ver_string);
135
136 if ( !glutExtensionSupported("GL_MESA_pack_invert") ) {
137 printf("\nSorry, this program requires GL_MESA_pack_invert.\n");
138 exit(1);
139 }
140
141 if ( ver >= 1.4 ) {
142 win_pos_2i = (PFNGLWINDOWPOS2IPROC) glutGetProcAddress( "glWindowPos2i" );
143 }
144 else if ( glutExtensionSupported("GL_ARB_window_pos") ) {
145 win_pos_2i = (PFNGLWINDOWPOS2IPROC) glutGetProcAddress( "glWindowPos2iARB" );
146 }
147 else if ( glutExtensionSupported("GL_MESA_window_pos") ) {
148 win_pos_2i = (PFNGLWINDOWPOS2IPROC) glutGetProcAddress( "glWindowPos2iMESA" );
149 }
150
151
152 /* Do this check as a separate if-statement instead of as an else in case
153 * one of the required extensions is supported but glutGetProcAddress
154 * returns NULL.
155 */
156
157 if ( win_pos_2i == NULL ) {
158 printf("\nSorry, this program requires either GL 1.4 (or higher),\n"
159 "GL_ARB_window_pos, or GL_MESA_window_pos.\n");
160 exit(1);
161 }
162
163 printf("\nThe left 2 squares should be the same color, and the right\n"
164 "square should look upside-down.\n");
165
166
167 image = LoadRGBImage( IMAGE_FILE, & img_width, & img_height,
168 & img_format );
169 if ( image == NULL ) {
170 printf( "Could not open image file \"%s\".\n", IMAGE_FILE );
171 exit(1);
172 }
173
174 temp_image = malloc( 3 * img_height * img_width );
175 if ( temp_image == NULL ) {
176 printf( "Could not allocate memory for temporary image.\n" );
177 exit(1);
178 }
179 }
180
181
182 int main( int argc, char *argv[] )
183 {
184 glutInit( &argc, argv );
185 glutInitWindowPosition( 0, 0 );
186 glutInitWindowSize( Width, Height );
187 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
188 glutCreateWindow( "GL_MESA_pack_invert test" );
189 glutReshapeFunc( Reshape );
190 glutKeyboardFunc( Key );
191 glutDisplayFunc( Display );
192 Init();
193 glutMainLoop();
194 return 0;
195 }