1/*
2 * Test the GL_NV_texture_rectangle and GL_MESA_ycrcb_texture extensions.
3 *
4 * Brian Paul   13 September 2002
5 */
6
7
8#include <math.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <GL/glew.h>
13#include "glut_wrap.h"
14
15#include "../util/readtex.c"   /* I know, this is a hack. */
16
17#define TEXTURE_FILE DEMOS_DATA_DIR "tile.rgb"
18
19static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;
20static GLint ImgWidth, ImgHeight;
21static GLushort *ImageYUV = NULL;
22static GLubyte *ImageRGB = NULL;
23static const GLuint yuvObj = 100;
24static const GLuint rgbObj = 101;
25
26
27static void DrawObject(void)
28{
29   glBegin(GL_QUADS);
30
31   glTexCoord2f(0, 0);
32   glVertex2f(-1.0, -1.0);
33
34   glTexCoord2f(1, 0);
35   glVertex2f(1.0, -1.0);
36
37   glTexCoord2f(1, 1);
38   glVertex2f(1.0, 1.0);
39
40   glTexCoord2f(0, 1);
41   glVertex2f(-1.0, 1.0);
42
43   glEnd();
44}
45
46
47static void Display( void )
48{
49   glClear( GL_COLOR_BUFFER_BIT );
50
51   glPushMatrix();
52      glTranslatef( -1.1, 0.0, -15.0 );
53      glRotatef(Xrot, 1.0, 0.0, 0.0);
54      glRotatef(Yrot, 0.0, 1.0, 0.0);
55      glRotatef(Zrot, 0.0, 0.0, 1.0);
56      glBindTexture(GL_TEXTURE_2D, yuvObj);
57      DrawObject();
58   glPopMatrix();
59
60   glPushMatrix();
61      glTranslatef(  1.1, 0.0, -15.0 );
62      glRotatef(Xrot, 1.0, 0.0, 0.0);
63      glRotatef(Yrot, 0.0, 1.0, 0.0);
64      glRotatef(Zrot, 0.0, 0.0, 1.0);
65      glBindTexture(GL_TEXTURE_2D, rgbObj);
66      DrawObject();
67   glPopMatrix();
68
69   glutSwapBuffers();
70}
71
72
73static void Reshape( int width, int height )
74{
75   glViewport( 0, 0, width, height );
76   glMatrixMode( GL_PROJECTION );
77   glLoadIdentity();
78   glFrustum( -1.1, 1.1, -1.1, 1.1, 10.0, 100.0 );
79   glMatrixMode( GL_MODELVIEW );
80   glLoadIdentity();
81   glTranslatef( 0.0, 0.0, -15.0 );
82}
83
84
85static void Key( unsigned char key, int x, int y )
86{
87   (void) x;
88   (void) y;
89   switch (key) {
90      case 27:
91         exit(0);
92         break;
93   }
94   glutPostRedisplay();
95}
96
97
98static void SpecialKey( int key, int x, int y )
99{
100   float step = 3.0;
101   (void) x;
102   (void) y;
103
104   switch (key) {
105      case GLUT_KEY_UP:
106         Xrot += step;
107         break;
108      case GLUT_KEY_DOWN:
109         Xrot -= step;
110         break;
111      case GLUT_KEY_LEFT:
112         Yrot += step;
113         break;
114      case GLUT_KEY_RIGHT:
115         Yrot -= step;
116         break;
117   }
118   glutPostRedisplay();
119}
120
121
122#define CLAMP( X, MIN, MAX )  ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
123
124
125
126/* #define LINEAR_FILTER */
127
128static void Init( int argc, char *argv[] )
129{
130   const char *file;
131   GLenum  format;
132
133   if (!glutExtensionSupported("GL_MESA_ycbcr_texture")) {
134      printf("Sorry, GL_MESA_ycbcr_texture is required\n");
135      exit(0);
136   }
137
138   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
139
140   if (argc > 1)
141      file = argv[1];
142   else
143      file = TEXTURE_FILE;
144
145   /* First load the texture as YCbCr.
146    */
147
148   glBindTexture(GL_TEXTURE_2D, yuvObj);
149   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
150   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
151
152   ImageYUV = LoadYUVImage(file, &ImgWidth, &ImgHeight );
153   if (!ImageYUV) {
154      printf("Couldn't read %s\n", TEXTURE_FILE);
155      exit(0);
156   }
157
158   printf("Image: %dx%d\n", ImgWidth, ImgHeight);
159
160
161   glTexImage2D(GL_TEXTURE_2D, 0,
162                GL_YCBCR_MESA,
163		ImgWidth, ImgHeight, 0,
164                GL_YCBCR_MESA,
165		GL_UNSIGNED_SHORT_8_8_MESA, ImageYUV);
166
167   glEnable(GL_TEXTURE_2D);
168
169   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
170
171
172
173   /* Now load the texture as RGB.
174    */
175
176   glBindTexture(GL_TEXTURE_2D, rgbObj);
177   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
178   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
179
180   ImageRGB = LoadRGBImage(file, &ImgWidth, &ImgHeight, &format );
181   if (!ImageRGB) {
182      printf("Couldn't read %s\n", TEXTURE_FILE);
183      exit(0);
184   }
185
186   printf("Image: %dx%d\n", ImgWidth, ImgHeight);
187
188
189   glTexImage2D(GL_TEXTURE_2D, 0,
190                format,
191		ImgWidth, ImgHeight, 0,
192                format,
193		GL_UNSIGNED_BYTE, ImageRGB);
194
195   glEnable(GL_TEXTURE_2D);
196
197   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
198
199
200
201   glShadeModel(GL_FLAT);
202   glClearColor(0.3, 0.3, 0.4, 1.0);
203
204   if (argc > 1 && strcmp(argv[1], "-info")==0) {
205      printf("GL_RENDERER   = %s\n", (char *) glGetString(GL_RENDERER));
206      printf("GL_VERSION    = %s\n", (char *) glGetString(GL_VERSION));
207      printf("GL_VENDOR     = %s\n", (char *) glGetString(GL_VENDOR));
208      printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
209   }
210
211   printf( "Both images should appear the same.\n" );
212}
213
214
215int main( int argc, char *argv[] )
216{
217   glutInit( &argc, argv );
218   glutInitWindowSize( 300, 300 );
219   glutInitWindowPosition( 0, 0 );
220   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
221   glutCreateWindow(argv[0] );
222   glewInit();
223
224   Init( argc, argv );
225
226   glutReshapeFunc( Reshape );
227   glutKeyboardFunc( Key );
228   glutSpecialFunc( SpecialKey );
229   glutDisplayFunc( Display );
230
231   glutMainLoop();
232   return 0;
233}
234