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.

pixeltex.c 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * GL_SGIS_pixel_texture demo
  3. *
  4. * Brian Paul
  5. * 6 Apr 2000
  6. *
  7. * Copyright (C) 2000 Brian Paul All Rights Reserved.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included
  17. * in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  23. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. /*
  27. * How this works:
  28. * 1. We load the image into a 2D texture.
  29. * 2. We generate a sequence of RGB images in which the R component
  30. * is really the S texture coordinate and the G component is really
  31. * the T texture coordinate.
  32. * By warping the mapping from R to S and G to T we can get non-linear
  33. * distortions.
  34. * 3. Draw the warped image (a 2-D warping function) with pixel texgen
  35. * enabled.
  36. * 4. Loop over the warped images to animate.
  37. *
  38. * The pixel texgen extension can also be used to do color-space
  39. * conversions. For example, we could convert YCR to RGB with a
  40. * 3D texture map which takes YCR as the S,T,R texture coordinate and
  41. * returns RGB texel values.
  42. *
  43. * You can use this extension in (at least) two ways:
  44. * 1. glDrawPixels w/ color space conversion/warping
  45. * 2. glDrawPixels to spatially warp another image in texture memory
  46. *
  47. * We're basically using glDrawPixels to draw a texture coordinate image.
  48. */
  49. #include <math.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <GL/glut.h>
  54. #include <GL/glext.h>
  55. #include "readtex.c" /* I know, this is a hack. */
  56. #define TEXTURE_FILE "../images/girl.rgb"
  57. static int ImgWidth = 300, ImgHeight = 300;
  58. #define FRAMES 20
  59. static GLubyte *ImgData[FRAMES];
  60. static GLint Frame = 0;
  61. static GLboolean TextureFlag = GL_TRUE;
  62. static void Display( void )
  63. {
  64. glClear( GL_COLOR_BUFFER_BIT );
  65. if (TextureFlag) {
  66. glEnable(GL_PIXEL_TEXTURE_SGIS);
  67. glEnable(GL_TEXTURE_2D);
  68. }
  69. else {
  70. glDisable(GL_PIXEL_TEXTURE_SGIS);
  71. glDisable(GL_TEXTURE_2D);
  72. }
  73. glColor3f(1, 1, 1);
  74. glRasterPos2f(10, 10);
  75. glDrawPixels(ImgWidth, ImgHeight, GL_RGB, GL_UNSIGNED_BYTE, ImgData[Frame]);
  76. glutSwapBuffers();
  77. }
  78. static void Reshape( int width, int height )
  79. {
  80. glViewport( 0, 0, width, height );
  81. glMatrixMode( GL_PROJECTION );
  82. glLoadIdentity();
  83. glOrtho(0, width, 0, height, -1, 1);
  84. glMatrixMode( GL_MODELVIEW );
  85. glLoadIdentity();
  86. }
  87. static void Key( unsigned char key, int x, int y )
  88. {
  89. (void) x;
  90. (void) y;
  91. switch (key) {
  92. case ' ':
  93. TextureFlag = !TextureFlag;
  94. break;
  95. case 27:
  96. exit(0);
  97. break;
  98. }
  99. glutPostRedisplay();
  100. }
  101. static void Idle(void)
  102. {
  103. Frame++;
  104. if (Frame >= FRAMES)
  105. Frame = 0;
  106. glutPostRedisplay();
  107. }
  108. static GLubyte warp(GLfloat s, int frame)
  109. {
  110. static const GLfloat pi = 3.14159265;
  111. static int halfFrame = FRAMES / 2;
  112. GLfloat y, weight, v;
  113. if (frame >= halfFrame)
  114. frame = halfFrame - (frame - halfFrame);
  115. y = sin(s * pi);
  116. weight = (float) frame / (FRAMES-1);
  117. v = y * (0.8 * weight + 0.2);
  118. return (GLint) (v * 255.0F);
  119. }
  120. static void InitImage(void)
  121. {
  122. int i, j, frame;
  123. for (frame = 0; frame < FRAMES; frame++) {
  124. ImgData[frame] = (GLubyte *) malloc(ImgWidth * ImgHeight * 3);
  125. for (i = 0; i < ImgHeight; i++) {
  126. for (j = 0; j < ImgWidth; j++) {
  127. GLubyte *pixel = ImgData[frame] + (i * ImgWidth + j) * 3;
  128. pixel[0] = warp((float) j / (ImgWidth - 0), frame);
  129. pixel[1] = warp((float) i / (ImgHeight - 0), frame);
  130. pixel[2] = 0.0;
  131. }
  132. }
  133. }
  134. }
  135. static void Init( int argc, char *argv[] )
  136. {
  137. const char *exten = (const char *) glGetString(GL_EXTENSIONS);
  138. if (!strstr(exten, "GL_SGIS_pixel_texture")) {
  139. printf("Sorry, GL_SGIS_pixel_texture not supported by this renderer.\n");
  140. exit(1);
  141. }
  142. /* linear filtering looks nicer, but it's slower, since it's in software */
  143. #if 1
  144. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  145. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  146. #else
  147. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  148. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  149. #endif
  150. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  151. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  152. if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
  153. printf("Error: couldn't load texture image\n");
  154. exit(1);
  155. }
  156. glClearColor(0.3, 0.3, 0.4, 1.0);
  157. InitImage();
  158. printf("Hit SPACE to toggle pixel texgen\n");
  159. }
  160. int main( int argc, char *argv[] )
  161. {
  162. glutInit( &argc, argv );
  163. glutInitWindowSize( 330, 330 );
  164. glutInitWindowPosition( 0, 0 );
  165. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  166. glutCreateWindow(argv[0] );
  167. Init( argc, argv );
  168. glutKeyboardFunc( Key );
  169. glutReshapeFunc( Reshape );
  170. glutDisplayFunc( Display );
  171. glutIdleFunc( Idle );
  172. glutMainLoop();
  173. return 0;
  174. }