demos: add missing binaries to .gitignore
[mesa-demos.git] / src / tests / arbnpot.c
blob21875af5b20d4b8571793cf1e3af62a3d60b3189
1 /*
2 * Test NPOT textures with the GL_ARB_texture_non_power_of_two extension.
3 * Brian Paul
4 * 2 July 2003
5 */
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11 #include <GL/glew.h>
12 #include "glut_wrap.h"
13 #include "../util/readtex.c"
15 #define IMAGE_FILE DEMOS_DATA_DIR "girl.rgb"
17 static GLfloat Zrot = 0;
19 static void Display( void )
21 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
23 glPushMatrix();
24 glRotatef(Zrot, 0, 0, 1);
25 glBegin(GL_POLYGON);
26 glTexCoord2f(0, 0);
27 glVertex2f(-1, -1);
28 glTexCoord2f(1, 0);
29 glVertex2f(1, -1);
30 glTexCoord2f(1, 1);
31 glVertex2f(1, 1);
32 glTexCoord2f(0, 1);
33 glVertex2f(-1, 1);
34 glEnd();
35 glPopMatrix();
37 glutSwapBuffers();
41 static void Reshape( int width, int height )
43 glViewport( 0, 0, width, height );
44 glMatrixMode( GL_PROJECTION );
45 glLoadIdentity();
46 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
47 glMatrixMode( GL_MODELVIEW );
48 glLoadIdentity();
49 glTranslatef( 0.0, 0.0, -7.0 );
53 static void Key( unsigned char key, int x, int y )
55 (void) x;
56 (void) y;
57 switch (key) {
58 case 'z':
59 Zrot -= 1.0;
60 break;
61 case 'Z':
62 Zrot += 1.0;
63 break;
64 case 27:
65 exit(0);
66 break;
68 glutPostRedisplay();
72 static void Init( void )
74 GLubyte *image;
75 int imgWidth, imgHeight, minDim, w;
76 GLenum imgFormat;
78 if (!glutExtensionSupported("GL_ARB_texture_non_power_of_two")) {
79 printf("Sorry, this program requires GL_ARB_texture_non_power_of_two\n");
80 exit(1);
83 #if 1
84 image = LoadRGBImage( IMAGE_FILE, &imgWidth, &imgHeight, &imgFormat );
85 if (!image) {
86 printf("Couldn't read %s\n", IMAGE_FILE);
87 exit(0);
89 #else
90 int i, j;
91 imgFormat = GL_RGB;
92 imgWidth = 3;
93 imgHeight = 3;
94 image = malloc(imgWidth * imgHeight * 3);
95 for (i = 0; i < imgHeight; i++) {
96 for (j = 0; j < imgWidth; j++) {
97 int k = (i * imgWidth + j) * 3;
98 if ((i + j) & 1) {
99 image[k+0] = 255;
100 image[k+1] = 0;
101 image[k+2] = 0;
103 else {
104 image[k+0] = 0;
105 image[k+1] = 255;
106 image[k+2] = 0;
110 #endif
112 printf("Read %d x %d\n", imgWidth, imgHeight);
114 minDim = imgWidth < imgHeight ? imgWidth : imgHeight;
116 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
119 * 1D Texture. Test proxy first, if that works, test non-proxy target.
121 glTexImage1D(GL_PROXY_TEXTURE_1D, 0, GL_RGB, imgWidth, 0,
122 imgFormat, GL_UNSIGNED_BYTE, image);
123 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_1D, 0, GL_TEXTURE_WIDTH, &w);
124 assert(w == imgWidth || w == 0);
126 if (w) {
127 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, imgWidth, 0,
128 imgFormat, GL_UNSIGNED_BYTE, image);
129 assert(glGetError() == GL_NO_ERROR);
134 * 2D Texture
136 glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_RGB, imgWidth, imgHeight, 0,
137 imgFormat, GL_UNSIGNED_BYTE, image);
138 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
139 assert(w == imgWidth || w == 0);
141 if (w) {
142 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imgWidth, imgHeight, 0,
143 imgFormat, GL_UNSIGNED_BYTE, image);
144 assert(glGetError() == GL_NO_ERROR);
149 * 3D Texture
151 glTexImage3D(GL_PROXY_TEXTURE_3D, 0, GL_RGB, imgWidth, imgHeight, 1, 0,
152 imgFormat, GL_UNSIGNED_BYTE, image);
153 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_WIDTH, &w);
154 assert(w == imgWidth || w == 0);
156 if (w) {
157 glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, imgWidth, imgHeight, 1, 0,
158 imgFormat, GL_UNSIGNED_BYTE, image);
159 assert(glGetError() == GL_NO_ERROR);
164 * Cube Texture
166 glTexImage2D(GL_PROXY_TEXTURE_CUBE_MAP, 0, GL_RGB,
167 minDim, minDim, 0,
168 imgFormat, GL_UNSIGNED_BYTE, image);
169 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_CUBE_MAP, 0, GL_TEXTURE_WIDTH, &w);
170 assert(w == minDim || w == 0);
172 if (w) {
173 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB,
174 minDim, minDim, 0,
175 imgFormat, GL_UNSIGNED_BYTE, image);
176 assert(glGetError() == GL_NO_ERROR);
179 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
180 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
181 glEnable(GL_TEXTURE_2D);
185 int main( int argc, char *argv[] )
187 glutInit( &argc, argv );
188 glutInitWindowPosition( 0, 0 );
189 glutInitWindowSize( 400, 400 );
190 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
191 glutCreateWindow(argv[0]);
192 glewInit();
193 glutReshapeFunc( Reshape );
194 glutKeyboardFunc( Key );
195 glutDisplayFunc( Display );
196 Init();
197 glutMainLoop();
198 return 0;