2 * Exercise the EXT_422_pixels extension, a less convenient
3 * alternative to MESA_ycbcr_texture. Requires ARB_fragment_program
4 * to perform the final YUV->RGB conversion.
6 * Brian Paul 13 September 2002
7 * Keith Whitwell 30 November 2004
19 #include "../util/readtex.c" /* I know, this is a hack. */
21 #define TEXTURE_FILE "../images/tile.rgb"
23 static GLfloat Xrot
= 0, Yrot
= 0, Zrot
= 0;
24 static GLint ImgWidth
, ImgHeight
;
25 static GLushort
*ImageYUV
= NULL
;
26 static const GLuint yuvObj
= 100;
27 static const GLuint rgbObj
= 101;
29 static void Init( int argc
, char *argv
[] );
31 static void DrawObject(void)
36 glVertex2f(-1.0, -1.0);
39 glVertex2f(1.0, -1.0);
45 glVertex2f(-1.0, 1.0);
50 static void Display( void )
52 static int firsttime
= 1;
56 Init( 0, 0 ); /* don't ask */
59 glClear( GL_COLOR_BUFFER_BIT
);
60 glBindTexture(GL_TEXTURE_2D
, yuvObj
);
63 glEnable(GL_FRAGMENT_PROGRAM_ARB
);
64 glTranslatef( -1.1, 0.0, -15.0 );
65 glRotatef(Xrot
, 1.0, 0.0, 0.0);
66 glRotatef(Yrot
, 0.0, 1.0, 0.0);
67 glRotatef(Zrot
, 0.0, 0.0, 1.0);
68 glBindTexture(GL_TEXTURE_2D
, yuvObj
);
73 glDisable(GL_FRAGMENT_PROGRAM_ARB
);
74 glTranslatef( 1.1, 0.0, -15.0 );
75 glRotatef(Xrot
, 1.0, 0.0, 0.0);
76 glRotatef(Yrot
, 0.0, 1.0, 0.0);
77 glRotatef(Zrot
, 0.0, 0.0, 1.0);
78 glBindTexture(GL_TEXTURE_2D
, rgbObj
);
86 static void Reshape( int width
, int height
)
88 glViewport( 0, 0, width
, height
);
89 glMatrixMode( GL_PROJECTION
);
91 glFrustum( -1.1, 1.1, -1.1, 1.1, 10.0, 100.0 );
92 glMatrixMode( GL_MODELVIEW
);
94 glTranslatef( 0.0, 0.0, -15.0 );
98 static void Key( unsigned char key
, int x
, int y
)
111 static void SpecialKey( int key
, int x
, int y
)
137 /* #define LINEAR_FILTER */
139 static void Init( int argc
, char *argv
[] )
142 const GLfloat yuvtorgb
[16] = {
143 1.164, 1.164, 1.164, 0,
145 1.596, -.813, 0.0, 0,
146 (-.0625*1.164 + -.5*1.596), (-.0625*1.164 + -.5*-.813 + -.5*-.391), (-.0625*1.164 + -.5*2.018), 1
149 if (!glutExtensionSupported("GL_ARB_fragment_program")) {
150 printf("Error: GL_ARB_fragment_program not supported!\n");
154 if (!glutExtensionSupported("GL_EXT_422_pixels")) {
155 printf("Error: GL_EXT_422_pixels not supported!\n");
159 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
163 /* Load the texture as YCbCr.
165 glBindTexture(GL_TEXTURE_2D
, yuvObj
);
166 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
167 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
169 ImageYUV
= LoadYUVImage(file
, &ImgWidth
, &ImgHeight
);
171 printf("Couldn't read %s\n", TEXTURE_FILE
);
175 glTexImage2D(GL_TEXTURE_2D
, 0,
177 ImgWidth
, ImgHeight
, 0,
179 GL_UNSIGNED_BYTE
, ImageYUV
);
181 glEnable(GL_TEXTURE_2D
);
183 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
186 static const char *modulateYUV
=
189 "TEX R0, fragment.texcoord[0], texture[0], 2D; \n"
191 "ADD R0, R0, {-0.0625, -0.5, -0.5, 0.0}; \n"
192 "DP3 result.color.x, R0, {1.164, 1.596, 0.0}; \n"
193 "DP3 result.color.y, R0, {1.164, -0.813, -0.391}; \n"
194 "DP3 result.color.z, R0, {1.164, 0.0, 2.018}; \n"
195 "MOV result.color.w, R0.w; \n"
203 /* Setup the fragment program */
204 glGenProgramsARB(1, &modulateProg
);
205 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, modulateProg
);
206 glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
207 strlen(modulateYUV
), (const GLubyte
*)modulateYUV
);
209 printf("glGetError = 0x%x\n", (int) glGetError());
210 printf("glError(GL_PROGRAM_ERROR_STRING_ARB) = %s\n",
211 (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB
));
212 assert(glIsProgramARB(modulateProg
));
216 /* Now the same, but use a color matrix to do the conversion at
219 glBindTexture(GL_TEXTURE_2D
, rgbObj
);
220 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
221 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
223 glMatrixMode( GL_COLOR_MATRIX
);
224 glLoadMatrixf( yuvtorgb
);
226 glTexImage2D(GL_TEXTURE_2D
, 0,
228 ImgWidth
, ImgHeight
, 0,
230 GL_UNSIGNED_BYTE
, ImageYUV
);
233 glMatrixMode( GL_MODELVIEW
);
235 glEnable(GL_TEXTURE_2D
);
237 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
240 glShadeModel(GL_FLAT
);
241 glClearColor(0.3, 0.3, 0.4, 1.0);
245 int main( int argc
, char *argv
[] )
247 glutInit( &argc
, argv
);
248 glutInitWindowSize( 300, 300 );
249 glutInitWindowPosition( 0, 0 );
250 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
251 glutCreateWindow(argv
[0] );
253 glutReshapeFunc( Reshape
);
254 glutKeyboardFunc( Key
);
255 glutSpecialFunc( SpecialKey
);
256 glutDisplayFunc( Display
);