/opengles/redbook/texsub.c

http://ftk.googlecode.com/ · C · 199 lines · 141 code · 15 blank · 43 comment · 6 complexity · aa0c782f3229ad9d59b62789d5b9b9eb MD5 · raw file

  1. /*
  2. * License Applicability. Except to the extent portions of this file are
  3. * made subject to an alternative license as permitted in the SGI Free
  4. * Software License B, Version 1.1 (the "License"), the contents of this
  5. * file are subject only to the provisions of the License. You may not use
  6. * this file except in compliance with the License. You may obtain a copy
  7. * of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
  8. * Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
  9. *
  10. * http://oss.sgi.com/projects/FreeB
  11. *
  12. * Note that, as provided in the License, the Software is distributed on an
  13. * "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
  14. * DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
  15. * CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
  16. * PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  17. *
  18. * Original Code. The Original Code is: OpenGL Sample Implementation,
  19. * Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
  20. * Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
  21. * Copyright in any portions created by third parties is as indicated
  22. * elsewhere herein. All Rights Reserved.
  23. *
  24. * Additional Notice Provisions: The application programming interfaces
  25. * established by SGI in conjunction with the Original Code are The
  26. * OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
  27. * April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
  28. * 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
  29. * Window System(R) (Version 1.3), released October 19, 1998. This software
  30. * was created using the OpenGL(R) version 1.2.1 Sample Implementation
  31. * published by SGI, but has not been independently verified as being
  32. * compliant with the OpenGL(R) version 1.2.1 Specification.
  33. *
  34. */
  35. /* texsub.c
  36. * This program texture maps a checkerboard image onto
  37. * two rectangles. This program clamps the texture, if
  38. * the texture coordinates fall outside 0.0 and 1.0.
  39. * If the s key is pressed, a texture subimage is used to
  40. * alter the original texture. If the r key is pressed,
  41. * the original texture is restored.
  42. */
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include "ug.h"
  46. #if defined(GL_VERSION_1_1) || defined(GL_OES_VERSION_1_0)
  47. /* Create checkerboard textures */
  48. #define checkImageWidth 64
  49. #define checkImageHeight 64
  50. #define subImageWidth 16
  51. #define subImageHeight 16
  52. static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
  53. static GLubyte subImage[subImageHeight][subImageWidth][4];
  54. static GLuint texName;
  55. void makeCheckImages(void)
  56. {
  57. int i, j, c;
  58. for (i = 0; i < checkImageHeight; i++) {
  59. for (j = 0; j < checkImageWidth; j++) {
  60. c = ((((i&0x8)==0)^((j&0x8))==0))*255;
  61. checkImage[i][j][0] = (GLubyte) c;
  62. checkImage[i][j][1] = (GLubyte) c;
  63. checkImage[i][j][2] = (GLubyte) c;
  64. checkImage[i][j][3] = (GLubyte) 255;
  65. }
  66. }
  67. for (i = 0; i < subImageHeight; i++) {
  68. for (j = 0; j < subImageWidth; j++) {
  69. c = ((((i&0x4)==0)^((j&0x4))==0))*255;
  70. subImage[i][j][0] = (GLubyte) c;
  71. subImage[i][j][1] = (GLubyte) 0;
  72. subImage[i][j][2] = (GLubyte) 0;
  73. subImage[i][j][3] = (GLubyte) 255;
  74. }
  75. }
  76. }
  77. void init(void)
  78. {
  79. static const GLfloat v[] = {
  80. -2.0, -1.0, 0.0,
  81. -2.0, 1.0, 0.0,
  82. 0.0, -1.0, 0.0,
  83. 0.0, 1.0, 0.0,
  84. 1.0, -1.0, 0.0,
  85. 1.0, 1.0, 0.0,
  86. 2.41421, -1.0, -1.41421,
  87. 2.41421, 1.0, -1.41421,
  88. };
  89. static const GLfloat t[] = {
  90. 0.0, 0.0,
  91. 0.0, 1.0,
  92. 1.0, 0.0,
  93. 1.0, 1.0,
  94. 0.0, 0.0,
  95. 0.0, 1.0,
  96. 1.0, 0.0,
  97. 1.0, 1.0,
  98. };
  99. glClearColor (0.0, 0.0, 0.0, 0.0);
  100. glShadeModel(GL_FLAT);
  101. glEnable(GL_DEPTH_TEST);
  102. makeCheckImages();
  103. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  104. glGenTextures(1, &texName);
  105. glBindTexture(GL_TEXTURE_2D, texName);
  106. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  107. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  108. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  109. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  110. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight,
  111. 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
  112. glVertexPointer(3, GL_FLOAT, 0, v);
  113. glTexCoordPointer(2, GL_FLOAT, 0, t);
  114. glEnableClientState(GL_VERTEX_ARRAY);
  115. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  116. }
  117. void display(UGWindow uwin)
  118. {
  119. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  120. glEnable(GL_TEXTURE_2D);
  121. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  122. glBindTexture(GL_TEXTURE_2D, texName);
  123. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  124. glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
  125. glFlush();
  126. glDisable(GL_TEXTURE_2D);
  127. ugSwapBuffers(uwin);
  128. }
  129. void reshape(UGWindow uwin, int w, int h)
  130. {
  131. glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  132. glMatrixMode(GL_PROJECTION);
  133. glLoadIdentity();
  134. ugluPerspectivef(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
  135. glMatrixMode(GL_MODELVIEW);
  136. glLoadIdentity();
  137. glTranslatef(0.0, 0.0, -3.6);
  138. }
  139. void keyboard (UGWindow uwin, int key, int x, int y)
  140. {
  141. switch (key) {
  142. case 's':
  143. case 'S':
  144. glBindTexture(GL_TEXTURE_2D, texName);
  145. glTexSubImage2D(GL_TEXTURE_2D, 0, 12, 44, subImageWidth,
  146. subImageHeight, GL_RGBA,
  147. GL_UNSIGNED_BYTE, subImage);
  148. ugPostRedisplay(uwin);
  149. break;
  150. case 'r':
  151. case 'R':
  152. glBindTexture(GL_TEXTURE_2D, texName);
  153. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,
  154. checkImageHeight, 0, GL_RGBA,
  155. GL_UNSIGNED_BYTE, checkImage);
  156. ugPostRedisplay(uwin);
  157. break;
  158. case 27:
  159. exit(0);
  160. break;
  161. default:
  162. break;
  163. }
  164. }
  165. int main(int argc, char** argv)
  166. {
  167. UGCtx ug = ugInit();
  168. UGWindow uwin = ugCreateWindow (ug, "UG_DEPTH", argv[0], 250, 250, 100, 100);
  169. init();
  170. ugDisplayFunc(uwin, display);
  171. ugReshapeFunc(uwin, reshape);
  172. ugKeyboardFunc(uwin, keyboard);
  173. ugMainLoop(ug);
  174. return 0;
  175. }
  176. #else
  177. int main(int argc, char** argv)
  178. {
  179. fprintf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0.\n");
  180. fprintf (stderr, "If your implementation of OpenGL Version 1.0 has the right extensions,\n");
  181. fprintf (stderr, "you may be able to modify this program to make it run.\n");
  182. return 0;
  183. }
  184. #endif