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