/opengles/redbook/texbind.c

http://ftk.googlecode.com/ · C · 183 lines · 128 code · 16 blank · 39 comment · 4 complexity · 2b54b9cd77f9e6a51ff33bf9c73de361 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. /* texbind.c
  36. * This program demonstrates using glBindTexture() by
  37. * creating and managing two textures.
  38. */
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41. #include "ug.h"
  42. #if defined(GL_VERSION_1_1) || defined(GL_OES_VERSION_1_0)
  43. /* Create checkerboard texture */
  44. #define checkImageWidth 64
  45. #define checkImageHeight 64
  46. static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
  47. static GLubyte otherImage[checkImageHeight][checkImageWidth][4];
  48. static GLuint texName[2];
  49. void makeCheckImages(void)
  50. {
  51. int i, j, c;
  52. for (i = 0; i < checkImageHeight; i++) {
  53. for (j = 0; j < checkImageWidth; j++) {
  54. c = ((((i&0x8)==0)^((j&0x8))==0))*255;
  55. checkImage[i][j][0] = (GLubyte) c;
  56. checkImage[i][j][1] = (GLubyte) c;
  57. checkImage[i][j][2] = (GLubyte) c;
  58. checkImage[i][j][3] = (GLubyte) 255;
  59. c = ((((i&0x10)==0)^((j&0x10))==0))*255;
  60. otherImage[i][j][0] = (GLubyte) c;
  61. otherImage[i][j][1] = (GLubyte) 0;
  62. otherImage[i][j][2] = (GLubyte) 0;
  63. otherImage[i][j][3] = (GLubyte) 255;
  64. }
  65. }
  66. }
  67. void init(void)
  68. {
  69. static const GLfloat v[] = {
  70. -2.0, -1.0, 0.0,
  71. -2.0, 1.0, 0.0,
  72. 0.0, -1.0, 0.0,
  73. 0.0, 1.0, 0.0,
  74. 1.0, -1.0, 0.0,
  75. 1.0, 1.0, 0.0,
  76. 2.41421, -1.0, -1.41421,
  77. 2.41421, 1.0, -1.41421,
  78. };
  79. static const GLfloat t[] = {
  80. 0.0, 0.0,
  81. 0.0, 1.0,
  82. 1.0, 0.0,
  83. 1.0, 1.0,
  84. 0.0, 0.0,
  85. 0.0, 1.0,
  86. 1.0, 0.0,
  87. 1.0, 1.0,
  88. };
  89. glClearColor (0.0, 0.0, 0.0, 0.0);
  90. glShadeModel(GL_FLAT);
  91. glEnable(GL_DEPTH_TEST);
  92. makeCheckImages();
  93. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  94. glGenTextures(2, texName);
  95. glBindTexture(GL_TEXTURE_2D, texName[0]);
  96. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  97. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  98. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  99. GL_NEAREST);
  100. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  101. GL_NEAREST);
  102. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,
  103. checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  104. checkImage);
  105. glBindTexture(GL_TEXTURE_2D, texName[1]);
  106. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  107. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  108. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  109. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  110. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  111. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,
  112. checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  113. otherImage);
  114. glEnable(GL_TEXTURE_2D);
  115. glVertexPointer(3, GL_FLOAT, 0, v);
  116. glTexCoordPointer(2, GL_FLOAT, 0, t);
  117. glEnableClientState(GL_VERTEX_ARRAY);
  118. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  119. }
  120. void display(UGWindow uwin)
  121. {
  122. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  123. glBindTexture(GL_TEXTURE_2D, texName[0]);
  124. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  125. glBindTexture(GL_TEXTURE_2D, texName[1]);
  126. glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
  127. glFlush();
  128. ugSwapBuffers(uwin);
  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 27:
  144. exit(0);
  145. break;
  146. }
  147. }
  148. int main(int argc, char** argv)
  149. {
  150. UGCtx ug = ugInit();
  151. UGWindow uwin = ugCreateWindow (ug, "UG_DEPTH", argv[0], 250, 250, 100, 100);
  152. init();
  153. ugDisplayFunc(uwin, display);
  154. ugReshapeFunc(uwin, reshape);
  155. ugKeyboardFunc(uwin, keyboard);
  156. ugMainLoop(ug);
  157. return 0;
  158. }
  159. #else
  160. int main(int argc, char** argv)
  161. {
  162. fprintf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0.\n");
  163. fprintf (stderr, "If your implementation of OpenGL Version 1.0 has the right extensions,\n");
  164. fprintf (stderr, "you may be able to modify this program to make it run.\n");
  165. return 0;
  166. }
  167. #endif