st/mesa: fix incorrect RowStride computation
[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/glew.h>
12 #include <GL/glut.h>
13 #include "../util/readtex.c"
14
15 #define IMAGE_FILE "../images/girl.rgb"
16
17 static GLfloat Zrot = 0;
18
19 static void Display( void )
20 {
21 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
22
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();
36
37 glutSwapBuffers();
38 }
39
40
41 static void Reshape( int width, int height )
42 {
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 );
50 }
51
52
53 static void Key( unsigned char key, int x, int y )
54 {
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;
67 }
68 glutPostRedisplay();
69 }
70
71
72 static void Init( void )
73 {
74 GLubyte *image;
75 int imgWidth, imgHeight, minDim, w;
76 GLenum imgFormat;
77
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);
81 }
82
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);
88 }
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;
102 }
103 else {
104 image[k+0] = 0;
105 image[k+1] = 255;
106 image[k+2] = 0;
107 }
108 }
109 }
110 #endif
111
112 printf("Read %d x %d\n", imgWidth, imgHeight);
113
114 minDim = imgWidth < imgHeight ? imgWidth : imgHeight;
115
116 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
117
118 /*
119 * 1D Texture. Test proxy first, if that works, test non-proxy target.
120 */
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);
125
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);
130 }
131
132
133 /*
134 * 2D Texture
135 */
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);
140
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);
145 }
146
147
148 /*
149 * 3D Texture
150 */
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);
155
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);
160 }
161
162
163 /*
164 * Cube Texture
165 */
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);
171
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);
177 }
178
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);
182 }
183
184
185 int main( int argc, char *argv[] )
186 {
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;
199 }