prog/tests: Fix MSVC build.
[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/glew.h>
39 #include <GL/glut.h>
40
41 #include "readtex.h"
42
43 #define IMAGE_FILE "../images/tree3.rgb"
44
45 static int Width = 420;
46 static int Height = 150;
47 static const GLfloat Near = 5.0, Far = 25.0;
48
49 static GLubyte * image = NULL;
50 static GLubyte * temp_image = NULL;
51 static GLuint img_width = 0;
52 static GLuint img_height = 0;
53 static GLuint img_format = 0;
54
55 PFNGLWINDOWPOS2IPROC win_pos_2i = NULL;
56
57
58 static void Display( void )
59 {
60 GLint err;
61
62
63 glClearColor(0.2, 0.2, 0.8, 0);
64 glClear( GL_COLOR_BUFFER_BIT );
65
66
67 /* This is the "reference" square.
68 */
69
70 (*win_pos_2i)( 5, 5 );
71 glDrawPixels( img_width, img_height, img_format, GL_UNSIGNED_BYTE, image );
72
73 glPixelStorei( GL_PACK_INVERT_MESA, GL_FALSE );
74 err = glGetError();
75 if ( err != GL_NO_ERROR ) {
76 printf( "Setting PACK_INVERT_MESA to false generated an error (0x%04x).\n",
77 err );
78 }
79
80 glReadPixels( 5, 5, img_width, img_height, img_format, GL_UNSIGNED_BYTE, temp_image );
81 (*win_pos_2i)( 5 + 1 * (10 + img_width), 5 );
82 glDrawPixels( img_width, img_height, img_format, GL_UNSIGNED_BYTE, temp_image );
83
84 glPixelStorei( GL_PACK_INVERT_MESA, GL_TRUE );
85 err = glGetError();
86 if ( err != GL_NO_ERROR ) {
87 printf( "Setting PACK_INVERT_MESA to true generated an error (0x%04x).\n",
88 err );
89 }
90
91 glReadPixels( 5, 5, img_width, img_height, img_format, GL_UNSIGNED_BYTE, temp_image );
92 (*win_pos_2i)( 5 + 2 * (10 + img_width), 5 );
93 glDrawPixels( img_width, img_height, img_format, GL_UNSIGNED_BYTE, temp_image );
94
95 glutSwapBuffers();
96 }
97
98
99 static void Reshape( int width, int height )
100 {
101 GLfloat ar = (float) width / (float) height;
102 Width = width;
103 Height = height;
104 glViewport( 0, 0, width, height );
105 glMatrixMode( GL_PROJECTION );
106 glLoadIdentity();
107 glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
108 glMatrixMode( GL_MODELVIEW );
109 glLoadIdentity();
110 glTranslatef( 0.0, 0.0, -15.0 );
111 }
112
113
114 static void Key( unsigned char key, int x, int y )
115 {
116 (void) x;
117 (void) y;
118 switch (key) {
119 case 27:
120 exit(0);
121 break;
122 }
123 glutPostRedisplay();
124 }
125
126
127 static void Init( void )
128 {
129 const char * const ver_string = (const char * const)
130 glGetString( GL_VERSION );
131 const float ver = strtod( ver_string, NULL );
132
133
134 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
135 printf("GL_VERSION = %s\n", ver_string);
136
137 if ( !glutExtensionSupported("GL_MESA_pack_invert") ) {
138 printf("\nSorry, this program requires GL_MESA_pack_invert.\n");
139 exit(1);
140 }
141
142 if ( ver >= 1.4 ) {
143 win_pos_2i = (PFNGLWINDOWPOS2IPROC) glutGetProcAddress( "glWindowPos2i" );
144 }
145 else if ( glutExtensionSupported("GL_ARB_window_pos") ) {
146 win_pos_2i = (PFNGLWINDOWPOS2IPROC) glutGetProcAddress( "glWindowPos2iARB" );
147 }
148 else if ( glutExtensionSupported("GL_MESA_window_pos") ) {
149 win_pos_2i = (PFNGLWINDOWPOS2IPROC) glutGetProcAddress( "glWindowPos2iMESA" );
150 }
151
152
153 /* Do this check as a separate if-statement instead of as an else in case
154 * one of the required extensions is supported but glutGetProcAddress
155 * returns NULL.
156 */
157
158 if ( win_pos_2i == NULL ) {
159 printf("\nSorry, this program requires either GL 1.4 (or higher),\n"
160 "GL_ARB_window_pos, or GL_MESA_window_pos.\n");
161 exit(1);
162 }
163
164 printf("\nThe left 2 squares should be the same color, and the right\n"
165 "square should look upside-down.\n");
166
167
168 image = LoadRGBImage( IMAGE_FILE, & img_width, & img_height,
169 & img_format );
170 if ( image == NULL ) {
171 printf( "Could not open image file \"%s\".\n", IMAGE_FILE );
172 exit(1);
173 }
174
175 temp_image = malloc( 3 * img_height * img_width );
176 if ( temp_image == NULL ) {
177 printf( "Could not allocate memory for temporary image.\n" );
178 exit(1);
179 }
180 }
181
182
183 int main( int argc, char *argv[] )
184 {
185 glutInit( &argc, argv );
186 glutInitWindowPosition( 0, 0 );
187 glutInitWindowSize( Width, Height );
188 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
189 glutCreateWindow( "GL_MESA_pack_invert test" );
190 glewInit();
191 glutReshapeFunc( Reshape );
192 glutKeyboardFunc( Key );
193 glutDisplayFunc( Display );
194 Init();
195 glutMainLoop();
196 return 0;
197 }