Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 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 | #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/tile.rgb" |
| 18 | |
| 19 | static GLfloat Xrot = 0, Yrot = 0, Zrot = 0; |
| 20 | static GLint ImgWidth, ImgHeight; |
Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 21 | static GLushort *ImageYUV = NULL; |
Brian Paul | f98921d | 2003-05-30 15:36:04 +0000 | [diff] [blame] | 22 | static GLubyte *ImageRGB = NULL; |
| 23 | static const GLuint yuvObj = 100; |
| 24 | static const GLuint rgbObj = 101; |
Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | static 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 | |
| 47 | static void Display( void ) |
| 48 | { |
| 49 | glClear( GL_COLOR_BUFFER_BIT ); |
| 50 | |
| 51 | glPushMatrix(); |
Brian Paul | f98921d | 2003-05-30 15:36:04 +0000 | [diff] [blame] | 52 | glTranslatef( -1.1, 0.0, -15.0 ); |
Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 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); |
Brian Paul | f98921d | 2003-05-30 15:36:04 +0000 | [diff] [blame] | 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); |
Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 66 | DrawObject(); |
| 67 | glPopMatrix(); |
| 68 | |
| 69 | glutSwapBuffers(); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static void Reshape( int width, int height ) |
| 74 | { |
| 75 | glViewport( 0, 0, width, height ); |
| 76 | glMatrixMode( GL_PROJECTION ); |
| 77 | glLoadIdentity(); |
Brian Paul | f98921d | 2003-05-30 15:36:04 +0000 | [diff] [blame] | 78 | glFrustum( -1.1, 1.1, -1.1, 1.1, 10.0, 100.0 ); |
Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 79 | glMatrixMode( GL_MODELVIEW ); |
| 80 | glLoadIdentity(); |
| 81 | glTranslatef( 0.0, 0.0, -15.0 ); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | static 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 | |
| 98 | static 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 | |
Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 124 | |
| 125 | |
| 126 | /* #define LINEAR_FILTER */ |
| 127 | |
| 128 | static void Init( int argc, char *argv[] ) |
| 129 | { |
Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 130 | const char *file; |
Brian Paul | f98921d | 2003-05-30 15:36:04 +0000 | [diff] [blame] | 131 | GLenum format; |
Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 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 | |
Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 140 | if (argc > 1) |
| 141 | file = argv[1]; |
| 142 | else |
| 143 | file = TEXTURE_FILE; |
| 144 | |
Brian Paul | f98921d | 2003-05-30 15:36:04 +0000 | [diff] [blame] | 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 | |
Keith Whitwell | 27358a2 | 2003-05-20 09:54:58 +0000 | [diff] [blame] | 152 | ImageYUV = LoadYUVImage(file, &ImgWidth, &ImgHeight ); |
Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 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 | |
Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 160 | |
Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 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); |
Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 166 | |
| 167 | glEnable(GL_TEXTURE_2D); |
| 168 | |
| 169 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 170 | |
Brian Paul | f98921d | 2003-05-30 15:36:04 +0000 | [diff] [blame] | 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 | |
Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 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 | } |
Brian Paul | f98921d | 2003-05-30 15:36:04 +0000 | [diff] [blame] | 210 | |
| 211 | printf( "Both images should appear the same.\n" ); |
Keith Whitwell | eea72ff | 2003-05-20 08:50:02 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
| 215 | int 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 | |
| 223 | Init( argc, argv ); |
| 224 | |
| 225 | glutReshapeFunc( Reshape ); |
| 226 | glutKeyboardFunc( Key ); |
| 227 | glutSpecialFunc( SpecialKey ); |
| 228 | glutDisplayFunc( Display ); |
| 229 | |
| 230 | glutMainLoop(); |
| 231 | return 0; |
| 232 | } |