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.

texgen.c 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 1993-1997, Silicon Graphics, Inc.
  3. * ALL RIGHTS RESERVED
  4. * Permission to use, copy, modify, and distribute this software for
  5. * any purpose and without fee is hereby granted, provided that the above
  6. * copyright notice appear in all copies and that both the copyright notice
  7. * and this permission notice appear in supporting documentation, and that
  8. * the name of Silicon Graphics, Inc. not be used in advertising
  9. * or publicity pertaining to distribution of the software without specific,
  10. * written prior permission.
  11. *
  12. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  16. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  21. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24. *
  25. * US Government Users Restricted Rights
  26. * Use, duplication, or disclosure by the Government is subject to
  27. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29. * clause at DFARS 252.227-7013 and/or in similar or successor
  30. * clauses in the FAR or the DOD or NASA FAR Supplement.
  31. * Unpublished-- rights reserved under the copyright laws of the
  32. * United States. Contractor/manufacturer is Silicon Graphics,
  33. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  34. *
  35. * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
  36. */
  37. /* texgen.c
  38. * This program draws a texture mapped teapot with
  39. * automatically generated texture coordinates. The
  40. * texture is rendered as stripes on the teapot.
  41. * Initially, the object is drawn with texture coordinates
  42. * based upon the object coordinates of the vertex
  43. * and distance from the plane x = 0. Pressing the 'e'
  44. * key changes the coordinate generation to eye coordinates
  45. * of the vertex. Pressing the 'o' key switches it back
  46. * to the object coordinates. Pressing the 's' key
  47. * changes the plane to a slanted one (x + y + z = 0).
  48. * Pressing the 'x' key switches it back to x = 0.
  49. */
  50. #include <GL/glut.h>
  51. #include <stdlib.h>
  52. #include <stdio.h>
  53. #define stripeImageWidth 32
  54. GLubyte stripeImage[4*stripeImageWidth];
  55. #ifdef GL_VERSION_1_1
  56. static GLuint texName;
  57. #endif
  58. void makeStripeImage(void)
  59. {
  60. int j;
  61. for (j = 0; j < stripeImageWidth; j++) {
  62. stripeImage[4*j] = (GLubyte) ((j<=4) ? 255 : 0);
  63. stripeImage[4*j+1] = (GLubyte) ((j>4) ? 255 : 0);
  64. stripeImage[4*j+2] = (GLubyte) 0;
  65. stripeImage[4*j+3] = (GLubyte) 255;
  66. }
  67. }
  68. /* planes for texture coordinate generation */
  69. static GLfloat xequalzero[] = {1.0, 0.0, 0.0, 0.0};
  70. static GLfloat slanted[] = {1.0, 1.0, 1.0, 0.0};
  71. static GLfloat *currentCoeff;
  72. static GLenum currentPlane;
  73. static GLint currentGenMode;
  74. void init(void)
  75. {
  76. glClearColor (0.0, 0.0, 0.0, 0.0);
  77. glEnable(GL_DEPTH_TEST);
  78. glShadeModel(GL_SMOOTH);
  79. makeStripeImage();
  80. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  81. #ifdef GL_VERSION_1_1
  82. glGenTextures(1, &texName);
  83. glBindTexture(GL_TEXTURE_1D, texName);
  84. #endif
  85. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  86. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  87. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  88. #ifdef GL_VERSION_1_1
  89. glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, stripeImageWidth, 0,
  90. GL_RGBA, GL_UNSIGNED_BYTE, stripeImage);
  91. #else
  92. glTexImage1D(GL_TEXTURE_1D, 0, 4, stripeImageWidth, 0,
  93. GL_RGBA, GL_UNSIGNED_BYTE, stripeImage);
  94. #endif
  95. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  96. currentCoeff = xequalzero;
  97. currentGenMode = GL_OBJECT_LINEAR;
  98. currentPlane = GL_OBJECT_PLANE;
  99. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, currentGenMode);
  100. glTexGenfv(GL_S, currentPlane, currentCoeff);
  101. glEnable(GL_TEXTURE_GEN_S);
  102. glEnable(GL_TEXTURE_1D);
  103. glEnable(GL_CULL_FACE);
  104. glEnable(GL_LIGHTING);
  105. glEnable(GL_LIGHT0);
  106. glEnable(GL_AUTO_NORMAL);
  107. glEnable(GL_NORMALIZE);
  108. glFrontFace(GL_CW);
  109. glCullFace(GL_BACK);
  110. glMaterialf (GL_FRONT, GL_SHININESS, 64.0);
  111. }
  112. void display(void)
  113. {
  114. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  115. glPushMatrix ();
  116. glRotatef(45.0, 0.0, 0.0, 1.0);
  117. #ifdef GL_VERSION_1_1
  118. glBindTexture(GL_TEXTURE_1D, texName);
  119. #endif
  120. glutSolidTeapot(2.0);
  121. glPopMatrix ();
  122. glFlush();
  123. }
  124. void reshape(int w, int h)
  125. {
  126. glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  127. glMatrixMode(GL_PROJECTION);
  128. glLoadIdentity();
  129. if (w <= h)
  130. glOrtho (-3.5, 3.5, -3.5*(GLfloat)h/(GLfloat)w,
  131. 3.5*(GLfloat)h/(GLfloat)w, -3.5, 3.5);
  132. else
  133. glOrtho (-3.5*(GLfloat)w/(GLfloat)h,
  134. 3.5*(GLfloat)w/(GLfloat)h, -3.5, 3.5, -3.5, 3.5);
  135. glMatrixMode(GL_MODELVIEW);
  136. glLoadIdentity();
  137. }
  138. /* ARGSUSED1 */
  139. void keyboard (unsigned char key, int x, int y)
  140. {
  141. switch (key) {
  142. case 'e':
  143. case 'E':
  144. currentGenMode = GL_EYE_LINEAR;
  145. currentPlane = GL_EYE_PLANE;
  146. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, currentGenMode);
  147. glTexGenfv(GL_S, currentPlane, currentCoeff);
  148. glutPostRedisplay();
  149. break;
  150. case 'o':
  151. case 'O':
  152. currentGenMode = GL_OBJECT_LINEAR;
  153. currentPlane = GL_OBJECT_PLANE;
  154. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, currentGenMode);
  155. glTexGenfv(GL_S, currentPlane, currentCoeff);
  156. glutPostRedisplay();
  157. break;
  158. case 's':
  159. case 'S':
  160. currentCoeff = slanted;
  161. glTexGenfv(GL_S, currentPlane, currentCoeff);
  162. glutPostRedisplay();
  163. break;
  164. case 'x':
  165. case 'X':
  166. currentCoeff = xequalzero;
  167. glTexGenfv(GL_S, currentPlane, currentCoeff);
  168. glutPostRedisplay();
  169. break;
  170. case 27:
  171. exit(0);
  172. break;
  173. default:
  174. break;
  175. }
  176. }
  177. int main(int argc, char** argv)
  178. {
  179. glutInit(&argc, argv);
  180. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  181. glutInitWindowSize(256, 256);
  182. glutInitWindowPosition(100, 100);
  183. glutCreateWindow (argv[0]);
  184. init ();
  185. glutDisplayFunc(display);
  186. glutReshapeFunc(reshape);
  187. glutKeyboardFunc(keyboard);
  188. glutMainLoop();
  189. return 0;
  190. }