Clone of mesa.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

alpha3D.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /*
  38. * alpha3D.c
  39. * This program demonstrates how to intermix opaque and
  40. * alpha blended polygons in the same scene, by using
  41. * glDepthMask. Press the 'a' key to animate moving the
  42. * transparent object through the opaque object. Press
  43. * the 'r' key to reset the scene.
  44. */
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <GL/glut.h>
  48. #define MAXZ 8.0
  49. #define MINZ -8.0
  50. #define ZINC 0.4
  51. static float solidZ = MAXZ;
  52. static float transparentZ = MINZ;
  53. static GLuint sphereList, cubeList;
  54. static void init(void)
  55. {
  56. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 0.15 };
  57. GLfloat mat_shininess[] = { 100.0 };
  58. GLfloat position[] = { 0.5, 0.5, 1.0, 0.0 };
  59. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  60. glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  61. glLightfv(GL_LIGHT0, GL_POSITION, position);
  62. glEnable(GL_LIGHTING);
  63. glEnable(GL_LIGHT0);
  64. glEnable(GL_DEPTH_TEST);
  65. sphereList = glGenLists(1);
  66. glNewList(sphereList, GL_COMPILE);
  67. glutSolidSphere (0.4, 16, 16);
  68. glEndList();
  69. cubeList = glGenLists(1);
  70. glNewList(cubeList, GL_COMPILE);
  71. glutSolidCube (0.6);
  72. glEndList();
  73. }
  74. void display(void)
  75. {
  76. GLfloat mat_solid[] = { 0.75, 0.75, 0.0, 1.0 };
  77. GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 };
  78. GLfloat mat_transparent[] = { 0.0, 0.8, 0.8, 0.6 };
  79. GLfloat mat_emission[] = { 0.0, 0.3, 0.3, 0.6 };
  80. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  81. glPushMatrix ();
  82. glTranslatef (-0.15, -0.15, solidZ);
  83. glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero);
  84. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_solid);
  85. glCallList (sphereList);
  86. glPopMatrix ();
  87. glPushMatrix ();
  88. glTranslatef (0.15, 0.15, transparentZ);
  89. glRotatef (15.0, 1.0, 1.0, 0.0);
  90. glRotatef (30.0, 0.0, 1.0, 0.0);
  91. glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  92. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_transparent);
  93. glEnable (GL_BLEND);
  94. glDepthMask (GL_FALSE);
  95. glBlendFunc (GL_SRC_ALPHA, GL_ONE);
  96. glCallList (cubeList);
  97. glDepthMask (GL_TRUE);
  98. glDisable (GL_BLEND);
  99. glPopMatrix ();
  100. glutSwapBuffers();
  101. }
  102. void reshape(int w, int h)
  103. {
  104. glViewport(0, 0, (GLint) w, (GLint) h);
  105. glMatrixMode(GL_PROJECTION);
  106. glLoadIdentity();
  107. if (w <= h)
  108. glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
  109. 1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
  110. else
  111. glOrtho (-1.5*(GLfloat)w/(GLfloat)h,
  112. 1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
  113. glMatrixMode(GL_MODELVIEW);
  114. glLoadIdentity();
  115. }
  116. void animate(void)
  117. {
  118. if (solidZ <= MINZ || transparentZ >= MAXZ)
  119. glutIdleFunc(NULL);
  120. else {
  121. solidZ -= ZINC;
  122. transparentZ += ZINC;
  123. glutPostRedisplay();
  124. }
  125. }
  126. /* ARGSUSED1 */
  127. void keyboard(unsigned char key, int x, int y)
  128. {
  129. switch (key) {
  130. case 'a':
  131. case 'A':
  132. solidZ = MAXZ;
  133. transparentZ = MINZ;
  134. glutIdleFunc(animate);
  135. break;
  136. case 'r':
  137. case 'R':
  138. solidZ = MAXZ;
  139. transparentZ = MINZ;
  140. glutPostRedisplay();
  141. break;
  142. case 27:
  143. exit(0);
  144. }
  145. }
  146. int main(int argc, char** argv)
  147. {
  148. glutInit(&argc, argv);
  149. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  150. glutInitWindowSize(500, 500);
  151. glutCreateWindow(argv[0]);
  152. init();
  153. glutReshapeFunc(reshape);
  154. glutKeyboardFunc(keyboard);
  155. glutDisplayFunc(display);
  156. glutMainLoop();
  157. return 0;
  158. }