tweak an error message
[mesa.git] / progs / tests / yuvrect.c
1 /*
2 * Test the GL_NV_texture_rectangle and GL_MESA_ycrcb_texture extensions.
3 *
4 * Brian Paul 13 September 2002
5 */
6
7 #include <assert.h>
8 #include <math.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #define GL_GLEXT_PROTOTYPES
13 #include <GL/glut.h>
14
15 #include "../util/readtex.c" /* I know, this is a hack. */
16
17 #define TEXTURE_FILE "../images/girl.rgb"
18
19 static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;
20 static GLint ImgWidth, ImgHeight;
21 static GLenum ImgFormat;
22 static GLushort *ImageYUV = NULL;
23
24
25 static void DrawObject(void)
26 {
27 glBegin(GL_QUADS);
28
29 glTexCoord2f(0, 0);
30 glVertex2f(-1.0, -1.0);
31
32 glTexCoord2f(ImgWidth, 0);
33 glVertex2f(1.0, -1.0);
34
35 glTexCoord2f(ImgWidth, ImgHeight);
36 glVertex2f(1.0, 1.0);
37
38 glTexCoord2f(0, ImgHeight);
39 glVertex2f(-1.0, 1.0);
40
41 glEnd();
42 }
43
44
45 static void Display( void )
46 {
47 glClear( GL_COLOR_BUFFER_BIT );
48
49 glPushMatrix();
50 glRotatef(Xrot, 1.0, 0.0, 0.0);
51 glRotatef(Yrot, 0.0, 1.0, 0.0);
52 glRotatef(Zrot, 0.0, 0.0, 1.0);
53 DrawObject();
54 glPopMatrix();
55
56 glutSwapBuffers();
57 }
58
59
60 static void Reshape( int width, int height )
61 {
62 glViewport( 0, 0, width, height );
63 glMatrixMode( GL_PROJECTION );
64 glLoadIdentity();
65 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
66 glMatrixMode( GL_MODELVIEW );
67 glLoadIdentity();
68 glTranslatef( 0.0, 0.0, -15.0 );
69 }
70
71
72 static void Key( unsigned char key, int x, int y )
73 {
74 (void) x;
75 (void) y;
76 switch (key) {
77 case 27:
78 exit(0);
79 break;
80 }
81 glutPostRedisplay();
82 }
83
84
85 static void SpecialKey( int key, int x, int y )
86 {
87 float step = 3.0;
88 (void) x;
89 (void) y;
90
91 switch (key) {
92 case GLUT_KEY_UP:
93 Xrot += step;
94 break;
95 case GLUT_KEY_DOWN:
96 Xrot -= step;
97 break;
98 case GLUT_KEY_LEFT:
99 Yrot += step;
100 break;
101 case GLUT_KEY_RIGHT:
102 Yrot -= step;
103 break;
104 }
105 glutPostRedisplay();
106 }
107
108
109
110 static void Init( int argc, char *argv[] )
111 {
112 GLuint texObj = 100;
113 const char *file;
114
115 if (!glutExtensionSupported("GL_NV_texture_rectangle")) {
116 printf("Sorry, GL_NV_texture_rectangle is required\n");
117 exit(0);
118 }
119
120 if (!glutExtensionSupported("GL_MESA_ycbcr_texture")) {
121 printf("Sorry, GL_MESA_ycbcr_texture is required\n");
122 exit(0);
123 }
124
125 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
126
127 glBindTexture(GL_TEXTURE_RECTANGLE_NV, texObj);
128 #ifdef LINEAR_FILTER
129 /* linear filtering looks much nicer but is much slower for Mesa */
130 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
131 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
132 #else
133 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
134 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
135 #endif
136
137 if (argc > 1)
138 file = argv[1];
139 else
140 file = TEXTURE_FILE;
141
142 ImageYUV = LoadYUVImage(file, &ImgWidth, &ImgHeight);
143 if (!ImageYUV) {
144 printf("Couldn't read %s\n", TEXTURE_FILE);
145 exit(0);
146 }
147
148 printf("Image: %dx%d\n", ImgWidth, ImgHeight);
149
150 glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0,
151 GL_YCBCR_MESA, ImgWidth, ImgHeight, 0,
152 GL_YCBCR_MESA, GL_UNSIGNED_SHORT_8_8_MESA, ImageYUV);
153
154 assert(glGetError() == GL_NO_ERROR);
155 glTexSubImage2D(GL_TEXTURE_RECTANGLE_NV, 0,
156 0, 0, ImgWidth, ImgHeight,
157 GL_YCBCR_MESA, GL_UNSIGNED_SHORT_8_8_MESA, ImageYUV);
158
159 assert(glGetError() == GL_NO_ERROR);
160
161 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
162
163 glEnable(GL_TEXTURE_RECTANGLE_NV);
164
165 glShadeModel(GL_FLAT);
166 glClearColor(0.3, 0.3, 0.4, 1.0);
167
168 if (argc > 1 && strcmp(argv[1], "-info")==0) {
169 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
170 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
171 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
172 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
173 }
174 }
175
176
177 int main( int argc, char *argv[] )
178 {
179 glutInit( &argc, argv );
180 glutInitWindowSize( 300, 300 );
181 glutInitWindowPosition( 0, 0 );
182 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
183 glutCreateWindow(argv[0] );
184
185 Init( argc, argv );
186
187 glutReshapeFunc( Reshape );
188 glutKeyboardFunc( Key );
189 glutSpecialFunc( SpecialKey );
190 glutDisplayFunc( Display );
191
192 glutMainLoop();
193 return 0;
194 }