Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ext422square.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  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.
  5. *
  6. * Brian Paul 13 September 2002
  7. * Keith Whitwell 30 November 2004
  8. */
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #define GL_GLEXT_PROTOTYPES
  14. #include <GL/glut.h>
  15. #include <assert.h>
  16. #include "../util/readtex.c" /* I know, this is a hack. */
  17. #define TEXTURE_FILE "../images/tile.rgb"
  18. static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;
  19. static GLint ImgWidth, ImgHeight;
  20. static GLushort *ImageYUV = NULL;
  21. static const GLuint yuvObj = 100;
  22. static const GLuint rgbObj = 101;
  23. static void Init( int argc, char *argv[] );
  24. static void DrawObject(void)
  25. {
  26. glBegin(GL_QUADS);
  27. glTexCoord2f(0, 0);
  28. glVertex2f(-1.0, -1.0);
  29. glTexCoord2f(1, 0);
  30. glVertex2f(1.0, -1.0);
  31. glTexCoord2f(1, 1);
  32. glVertex2f(1.0, 1.0);
  33. glTexCoord2f(0, 1);
  34. glVertex2f(-1.0, 1.0);
  35. glEnd();
  36. }
  37. static void Display( void )
  38. {
  39. static int firsttime = 1;
  40. if (firsttime) {
  41. firsttime = 0;
  42. Init( 0, 0 ); /* don't ask */
  43. }
  44. glClear( GL_COLOR_BUFFER_BIT );
  45. glBindTexture(GL_TEXTURE_2D, yuvObj);
  46. glPushMatrix();
  47. glEnable(GL_FRAGMENT_PROGRAM_ARB);
  48. glTranslatef( -1.1, 0.0, -15.0 );
  49. glRotatef(Xrot, 1.0, 0.0, 0.0);
  50. glRotatef(Yrot, 0.0, 1.0, 0.0);
  51. glRotatef(Zrot, 0.0, 0.0, 1.0);
  52. glBindTexture(GL_TEXTURE_2D, yuvObj);
  53. DrawObject();
  54. glPopMatrix();
  55. glPushMatrix();
  56. glDisable(GL_FRAGMENT_PROGRAM_ARB);
  57. glTranslatef( 1.1, 0.0, -15.0 );
  58. glRotatef(Xrot, 1.0, 0.0, 0.0);
  59. glRotatef(Yrot, 0.0, 1.0, 0.0);
  60. glRotatef(Zrot, 0.0, 0.0, 1.0);
  61. glBindTexture(GL_TEXTURE_2D, rgbObj);
  62. DrawObject();
  63. glPopMatrix();
  64. glutSwapBuffers();
  65. }
  66. static void Reshape( int width, int height )
  67. {
  68. glViewport( 0, 0, width, height );
  69. glMatrixMode( GL_PROJECTION );
  70. glLoadIdentity();
  71. glFrustum( -1.1, 1.1, -1.1, 1.1, 10.0, 100.0 );
  72. glMatrixMode( GL_MODELVIEW );
  73. glLoadIdentity();
  74. glTranslatef( 0.0, 0.0, -15.0 );
  75. }
  76. static void Key( unsigned char key, int x, int y )
  77. {
  78. (void) x;
  79. (void) y;
  80. switch (key) {
  81. case 27:
  82. exit(0);
  83. break;
  84. }
  85. glutPostRedisplay();
  86. }
  87. static void SpecialKey( int key, int x, int y )
  88. {
  89. float step = 3.0;
  90. (void) x;
  91. (void) y;
  92. switch (key) {
  93. case GLUT_KEY_UP:
  94. Xrot += step;
  95. break;
  96. case GLUT_KEY_DOWN:
  97. Xrot -= step;
  98. break;
  99. case GLUT_KEY_LEFT:
  100. Yrot += step;
  101. break;
  102. case GLUT_KEY_RIGHT:
  103. Yrot -= step;
  104. break;
  105. }
  106. glutPostRedisplay();
  107. }
  108. /* #define LINEAR_FILTER */
  109. static void Init( int argc, char *argv[] )
  110. {
  111. const char *file;
  112. const GLfloat yuvtorgb[16] = {
  113. 1.164, 1.164, 1.164, 0,
  114. 0, -.391, 2.018, 0,
  115. 1.596, -.813, 0.0, 0,
  116. (-.0625*1.164 + -.5*1.596), (-.0625*1.164 + -.5*-.813 + -.5*-.391), (-.0625*1.164 + -.5*2.018), 1
  117. };
  118. if (!glutExtensionSupported("GL_ARB_fragment_program")) {
  119. printf("Error: GL_ARB_fragment_program not supported!\n");
  120. exit(1);
  121. }
  122. if (!glutExtensionSupported("GL_EXT_422_pixels")) {
  123. printf("Error: GL_EXT_422_pixels not supported!\n");
  124. exit(1);
  125. }
  126. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  127. file = TEXTURE_FILE;
  128. /* Load the texture as YCbCr.
  129. */
  130. glBindTexture(GL_TEXTURE_2D, yuvObj);
  131. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  132. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  133. ImageYUV = LoadYUVImage(file, &ImgWidth, &ImgHeight );
  134. if (!ImageYUV) {
  135. printf("Couldn't read %s\n", TEXTURE_FILE);
  136. exit(0);
  137. }
  138. glTexImage2D(GL_TEXTURE_2D, 0,
  139. GL_RGB,
  140. ImgWidth, ImgHeight, 0,
  141. GL_422_EXT,
  142. GL_UNSIGNED_BYTE, ImageYUV);
  143. glEnable(GL_TEXTURE_2D);
  144. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  145. {
  146. static const char *modulateYUV =
  147. "!!ARBfp1.0\n"
  148. "TEMP R0;\n"
  149. "TEX R0, fragment.texcoord[0], texture[0], 2D; \n"
  150. "ADD R0, R0, {-0.0625, -0.5, -0.5, 0.0}; \n"
  151. "DP3 result.color.x, R0, {1.164, 1.596, 0.0}; \n"
  152. "DP3 result.color.y, R0, {1.164, -0.813, -0.391}; \n"
  153. "DP3 result.color.z, R0, {1.164, 0.0, 2.018}; \n"
  154. "MOV result.color.w, R0.w; \n"
  155. "END"
  156. ;
  157. GLuint modulateProg;
  158. /* Setup the fragment program */
  159. glGenProgramsARB(1, &modulateProg);
  160. glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, modulateProg);
  161. glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
  162. strlen(modulateYUV), (const GLubyte *)modulateYUV);
  163. printf("glGetError = 0x%x\n", (int) glGetError());
  164. printf("glError(GL_PROGRAM_ERROR_STRING_ARB) = %s\n",
  165. (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB));
  166. assert(glIsProgramARB(modulateProg));
  167. }
  168. /* Now the same, but use a color matrix to do the conversion at
  169. * upload time:
  170. */
  171. glBindTexture(GL_TEXTURE_2D, rgbObj);
  172. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  173. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  174. glMatrixMode( GL_COLOR_MATRIX );
  175. glLoadMatrixf( yuvtorgb );
  176. glTexImage2D(GL_TEXTURE_2D, 0,
  177. GL_RGB,
  178. ImgWidth, ImgHeight, 0,
  179. GL_422_EXT,
  180. GL_UNSIGNED_BYTE, ImageYUV);
  181. glLoadIdentity();
  182. glMatrixMode( GL_MODELVIEW );
  183. glEnable(GL_TEXTURE_2D);
  184. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  185. glShadeModel(GL_FLAT);
  186. glClearColor(0.3, 0.3, 0.4, 1.0);
  187. }
  188. int main( int argc, char *argv[] )
  189. {
  190. glutInit( &argc, argv );
  191. glutInitWindowSize( 300, 300 );
  192. glutInitWindowPosition( 0, 0 );
  193. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  194. glutCreateWindow(argv[0] );
  195. glutReshapeFunc( Reshape );
  196. glutKeyboardFunc( Key );
  197. glutSpecialFunc( SpecialKey );
  198. glutDisplayFunc( Display );
  199. glutMainLoop();
  200. return 0;
  201. }