/opengles/redbook/alpha3D.c

http://ftk.googlecode.com/ · C · 175 lines · 115 code · 18 blank · 42 comment · 5 complexity · e59d52815763bc9f41efe4d824db0faa 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. /*
  36. * alpha3D.c
  37. * This program demonstrates how to intermix opaque and
  38. * alpha blended polygons in the same scene, by using
  39. * glDepthMask. Press the 'a' key to animate moving the
  40. * transparent object through the opaque object. Press
  41. * the 'r' key to reset the scene.
  42. */
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include "ug.h"
  46. #define MAXZ 8.0
  47. #define MINZ -8.0
  48. #define ZINC 0.4
  49. static float solidZ = MAXZ;
  50. static float transparentZ = MINZ;
  51. static void init(void)
  52. {
  53. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 0.15 };
  54. GLfloat mat_shininess[] = { 100.0 };
  55. GLfloat position[] = { 0.5, 0.5, 1.0, 0.0 };
  56. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  57. glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  58. glLightfv(GL_LIGHT0, GL_POSITION, position);
  59. glEnable(GL_LIGHTING);
  60. glEnable(GL_LIGHT0);
  61. glEnable(GL_DEPTH_TEST);
  62. }
  63. static void display(UGWindow uwin)
  64. {
  65. GLfloat mat_solid[] = { 0.75, 0.75, 0.0, 1.0 };
  66. GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 };
  67. GLfloat mat_transparent[] = { 0.0, 0.8, 0.8, 0.6 };
  68. GLfloat mat_emission[] = { 0.0, 0.3, 0.3, 0.6 };
  69. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  70. glPushMatrix ();
  71. glTranslatef (-0.15, -0.15, solidZ);
  72. glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_zero);
  73. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_solid);
  74. ugSolidSpheref(0.4, 32, 32);
  75. glPopMatrix ();
  76. glPushMatrix ();
  77. glTranslatef (0.15, 0.15, transparentZ);
  78. glRotatef (15.0, 1.0, 1.0, 0.0);
  79. glRotatef (30.0, 0.0, 1.0, 0.0);
  80. glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission);
  81. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_transparent);
  82. glEnable (GL_BLEND);
  83. glDepthMask (GL_FALSE);
  84. glBlendFunc (GL_SRC_ALPHA, GL_ONE);
  85. ugSolidCubef(0.6);
  86. glDepthMask (GL_TRUE);
  87. glDisable (GL_BLEND);
  88. glPopMatrix ();
  89. ugSwapBuffers(uwin);
  90. }
  91. static void reshape(UGWindow uwin, int w, int h)
  92. {
  93. glViewport(0, 0, (GLint) w, (GLint) h);
  94. glMatrixMode(GL_PROJECTION);
  95. glLoadIdentity();
  96. if (w <= h)
  97. glOrthof(-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
  98. 1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
  99. else
  100. glOrthof(-1.5*(GLfloat)w/(GLfloat)h,
  101. 1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
  102. glMatrixMode(GL_MODELVIEW);
  103. glLoadIdentity();
  104. }
  105. static void animate(UGWindow uwin)
  106. {
  107. if (solidZ <= MINZ || transparentZ >= MAXZ)
  108. ugIdleFunc(ugCtxFromWin(uwin), NULL);
  109. else {
  110. solidZ -= ZINC;
  111. transparentZ += ZINC;
  112. ugPostRedisplay(uwin);
  113. }
  114. }
  115. static void keyboard(UGWindow uwin, int key, int x, int y)
  116. {
  117. switch (key) {
  118. case 'a':
  119. case 'A':
  120. solidZ = MAXZ;
  121. transparentZ = MINZ;
  122. ugIdleFunc(ugCtxFromWin(uwin), animate);
  123. break;
  124. case 'r':
  125. case 'R':
  126. solidZ = MAXZ;
  127. transparentZ = MINZ;
  128. ugPostRedisplay(uwin);
  129. break;
  130. case 27:
  131. exit(0);
  132. }
  133. }
  134. #ifdef FTK_AS_PLUGIN
  135. #include "ftk_app_demo.h"
  136. FTK_HIDE int FTK_MAIN(int argc, char* argv[]);
  137. FtkApp* ftk_app_demo_alpha3D_create()
  138. {
  139. return ftk_app_demo_create(_("alpha3D"), ftk_main);
  140. }
  141. #else
  142. #define FTK_HIDE extern
  143. #endif /*FTK_AS_PLUGIN*/
  144. FTK_HIDE int FTK_MAIN(int argc, char* argv[])
  145. {
  146. UGCtx ug = ugInit();
  147. UGWindow uwin = ugCreateWindow (ug, "", "alpha3D", 500, 500, 100, 100);
  148. init();
  149. ugDisplayFunc(uwin, display);
  150. ugReshapeFunc(uwin, reshape);
  151. ugKeyboardFunc(uwin, keyboard);
  152. ugIdleFunc(ugCtxFromWin(uwin), animate);
  153. #ifndef FTK_AS_PLUGIN
  154. ugMainLoop(ug);
  155. #endif
  156. return 0;
  157. }