/opengles/redbook/alpha.c

http://ftk.googlecode.com/ · C · 141 lines · 80 code · 13 blank · 48 comment · 5 complexity · ff7ee83d5c75e38f0d1f4c3452a574f0 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. * alpha.c
  37. * This program draws several overlapping filled polygons
  38. * to demonstrate the effect order has on alpha blending results.
  39. * Use the 't' key to toggle the order of drawing polygons.
  40. */
  41. #include <stdlib.h>
  42. #include "ug.h"
  43. static int leftFirst = GL_TRUE;
  44. /* Initialize alpha blending function.
  45. */
  46. static void init(void)
  47. {
  48. static const GLfloat v[] = {
  49. 0.1, 0.9, 0.0,
  50. 0.1, 0.1, 0.0,
  51. 0.7, 0.5, 0.0,
  52. 0.9, 0.9, 0.0,
  53. 0.3, 0.5, 0.0,
  54. 0.9, 0.1, 0.0,
  55. };
  56. glEnable (GL_BLEND);
  57. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  58. glShadeModel (GL_FLAT);
  59. glClearColor (0.0, 0.0, 0.0, 0.0);
  60. glVertexPointer(3, GL_FLOAT, 0, v);
  61. glEnableClientState(GL_VERTEX_ARRAY);
  62. }
  63. static void drawLeftTriangle(void)
  64. {
  65. /* draw yellow triangle on LHS of screen */
  66. glColor4f(1.0, 1.0, 0.0, 0.75);
  67. glDrawArrays(GL_TRIANGLES, 0, 3);
  68. }
  69. static void drawRightTriangle(void)
  70. {
  71. /* draw cyan triangle on RHS of screen */
  72. glColor4f(0.0, 1.0, 1.0, 0.75);
  73. glDrawArrays(GL_TRIANGLES, 3, 3);
  74. }
  75. void display(UGWindow uwin)
  76. {
  77. glClear(GL_COLOR_BUFFER_BIT);
  78. if (leftFirst) {
  79. drawLeftTriangle();
  80. drawRightTriangle();
  81. }
  82. else {
  83. drawRightTriangle();
  84. drawLeftTriangle();
  85. }
  86. glFlush();
  87. ugSwapBuffers(uwin);
  88. }
  89. void reshape(UGWindow uwin, int w, int h)
  90. {
  91. glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  92. glMatrixMode(GL_PROJECTION);
  93. glLoadIdentity();
  94. if (w <= h)
  95. glOrthof(0.0, 1.0, 0.0, 1.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0);
  96. else
  97. glOrthof(0.0, 1.0*(GLfloat)w/(GLfloat)h, 0.0, 1.0, -1.0, 1.0);
  98. }
  99. void keyboard(UGWindow uwin, int key, int x, int y)
  100. {
  101. switch (key) {
  102. case 't':
  103. case 'T':
  104. leftFirst = !leftFirst;
  105. ugPostRedisplay(uwin);
  106. break;
  107. case 27: /* Escape key */
  108. exit(0);
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. /* Main Loop
  115. * Open window with initial window size, title bar,
  116. * RGBA display mode, and handle input events.
  117. */
  118. int main(int argc, char** argv)
  119. {
  120. UGCtx ug = ugInit();
  121. UGWindow uwin = ugCreateWindow (ug, "", argv[0], 200, 200, 100, 100);
  122. init ();
  123. ugDisplayFunc(uwin, display);
  124. ugReshapeFunc(uwin, reshape);
  125. ugKeyboardFunc(uwin, keyboard);
  126. ugMainLoop(ug);
  127. return 0;
  128. }