/opengles/redbook/wrap.c

http://ftk.googlecode.com/ · C · 192 lines · 130 code · 16 blank · 46 comment · 3 complexity · 378729bc8406d2727b3589505569e44e 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. /* wrap.c
  36. * This program texture maps a checkerboard image onto
  37. * two rectangles. This program demonstrates the wrapping
  38. * modes, if the texture coordinates fall outside 0.0 and 1.0.
  39. * Interaction: Pressing the 's' and 'S' keys switch the
  40. * wrapping between clamping and repeating for the s parameter.
  41. * The 't' and 'T' keys control the wrapping for the t parameter.
  42. *
  43. * If running this program on OpenGL 1.0, texture objects are
  44. * not used.
  45. */
  46. #include <stdlib.h>
  47. #include <stdio.h>
  48. #include "ug.h"
  49. /* Create checkerboard texture */
  50. #define checkImageWidth 64
  51. #define checkImageHeight 64
  52. static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
  53. #ifdef GL_VERSION_1_1
  54. static GLuint texName;
  55. #endif
  56. void makeCheckImage(void)
  57. {
  58. int i, j, c;
  59. for (i = 0; i < checkImageHeight; i++) {
  60. for (j = 0; j < checkImageWidth; j++) {
  61. c = ((((i&0x8)==0)^((j&0x8))==0))*255;
  62. checkImage[i][j][0] = (GLubyte) c;
  63. checkImage[i][j][1] = (GLubyte) c;
  64. checkImage[i][j][2] = (GLubyte) c;
  65. checkImage[i][j][3] = (GLubyte) 255;
  66. }
  67. }
  68. }
  69. void init(void)
  70. {
  71. static const GLfloat v[] = {
  72. -2.0, -1.0, 0.0,
  73. -2.0, 1.0, 0.0,
  74. 0.0, -1.0, 0.0,
  75. 0.0, 1.0, 0.0,
  76. 1.0, -1.0, 0.0,
  77. 1.0, 1.0, 0.0,
  78. 2.41421, -1.0, -1.41421,
  79. 2.41421, 1.0, -1.41421,
  80. };
  81. static const GLfloat t[] = {
  82. 0.0, 0.0,
  83. 0.0, 3.0,
  84. 3.0, 0.0,
  85. 3.0, 3.0,
  86. 0.0, 0.0,
  87. 0.0, 3.0,
  88. 3.0, 0.0,
  89. 3.0, 3.0,
  90. };
  91. glClearColor (0.0, 0.0, 0.0, 0.0);
  92. glShadeModel(GL_FLAT);
  93. glEnable(GL_DEPTH_TEST);
  94. makeCheckImage();
  95. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  96. #ifdef GL_VERSION_1_1
  97. glGenTextures(1, &texName);
  98. glBindTexture(GL_TEXTURE_2D, texName);
  99. #endif
  100. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  101. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  102. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  103. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  104. #ifdef GL_VERSION_1_1
  105. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight,
  106. 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
  107. #else
  108. glTexImage2D(GL_TEXTURE_2D, 0, 4, checkImageWidth, checkImageHeight,
  109. 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
  110. #endif
  111. glVertexPointer(3, GL_FLOAT, 0, v);
  112. glTexCoordPointer(2, GL_FLOAT, 0, t);
  113. glEnableClientState(GL_VERTEX_ARRAY);
  114. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  115. }
  116. void display(UGWindow uwin)
  117. {
  118. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  119. glEnable(GL_TEXTURE_2D);
  120. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  121. #ifdef GL_VERSION_1_1
  122. glBindTexture(GL_TEXTURE_2D, texName);
  123. #endif
  124. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  125. glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
  126. glFlush();
  127. ugSwapBuffers(uwin);
  128. glDisable(GL_TEXTURE_2D);
  129. }
  130. void reshape(UGWindow uwin, int w, int h)
  131. {
  132. glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  133. glMatrixMode(GL_PROJECTION);
  134. glLoadIdentity();
  135. ugluPerspectivef(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
  136. glMatrixMode(GL_MODELVIEW);
  137. glLoadIdentity();
  138. glTranslatef(0.0, 0.0, -3.6);
  139. }
  140. void keyboard (UGWindow uwin, int key, int x, int y)
  141. {
  142. switch (key) {
  143. case 's':
  144. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  145. ugPostRedisplay(uwin);
  146. break;
  147. case 'S':
  148. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  149. ugPostRedisplay(uwin);
  150. break;
  151. case 't':
  152. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  153. ugPostRedisplay(uwin);
  154. break;
  155. case 'T':
  156. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  157. ugPostRedisplay(uwin);
  158. break;
  159. case 27:
  160. exit(0);
  161. break;
  162. default:
  163. break;
  164. }
  165. }
  166. int main(int argc, char** argv)
  167. {
  168. UGCtx ug = ugInit();
  169. UGWindow uwin = ugCreateWindow (ug, "UG_DEPTH", argv[0], 250, 250, 100, 100);
  170. init ();
  171. ugDisplayFunc(uwin, display);
  172. ugReshapeFunc(uwin, reshape);
  173. ugKeyboardFunc(uwin, keyboard);
  174. ugMainLoop(ug);
  175. return 0;
  176. }