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.

tri-cull.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and
  5. * its documentation for any purpose is hereby granted without fee, provided
  6. * that (i) the above copyright notices and this permission notice appear in
  7. * all copies of the software and related documentation, and (ii) the name of
  8. * Silicon Graphics may not be used in any advertising or
  9. * publicity relating to the software without the specific, prior written
  10. * permission of Silicon Graphics.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13. * ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <GL/glut.h>
  28. static GLenum doubleBuffer;
  29. static GLint cullmode = 0;
  30. static GLenum front = GL_CCW; /* GL default */
  31. static void cull(void)
  32. {
  33. cullmode = (cullmode + 1) % 4;
  34. if (cullmode == 0) {
  35. glCullFace(GL_FRONT);
  36. glEnable(GL_CULL_FACE);
  37. printf("cull GL_FRONT\n");
  38. }
  39. else if (cullmode == 1) {
  40. glCullFace(GL_BACK);
  41. glEnable(GL_CULL_FACE);
  42. printf("cull GL_BACK\n");
  43. }
  44. else if (cullmode == 2) {
  45. glCullFace(GL_FRONT_AND_BACK);
  46. glEnable(GL_CULL_FACE);
  47. printf("cull GL_FRONT_AND_BACK\n");
  48. }
  49. else {
  50. glDisable(GL_CULL_FACE);
  51. printf("cull none\n");
  52. }
  53. }
  54. static void Init(void)
  55. {
  56. fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  57. fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  58. fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  59. fflush(stderr);
  60. glClearColor(0.0, 0.0, 1.0, 0.0);
  61. cull();
  62. }
  63. static void Reshape(int width, int height)
  64. {
  65. glViewport(0, 0, (GLint)width, (GLint)height);
  66. glMatrixMode(GL_PROJECTION);
  67. glLoadIdentity();
  68. glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  69. glMatrixMode(GL_MODELVIEW);
  70. }
  71. static void Key(unsigned char key, int x, int y)
  72. {
  73. switch (key) {
  74. case 27:
  75. exit(1);
  76. case 'c':
  77. cull();
  78. break;
  79. case 'f':
  80. front = ((front == GL_CCW) ? GL_CW : GL_CCW);
  81. glFrontFace(front);
  82. printf("front face = %s\n", front == GL_CCW ? "GL_CCW" : "GL_CW");
  83. break;
  84. default:
  85. return;
  86. }
  87. glutPostRedisplay();
  88. }
  89. static void Draw(void)
  90. {
  91. glClear(GL_COLOR_BUFFER_BIT);
  92. glBegin(GL_TRIANGLES);
  93. /* CCW / front-facing */
  94. glColor3f(0,0,.7);
  95. glVertex3f(-0.1, -0.9, -30.0);
  96. glColor3f(.8,0,0);
  97. glVertex3f(-0.1, 0.9, -30.0);
  98. glColor3f(0,.9,0);
  99. glVertex3f(-0.9, 0.0, -30.0);
  100. /* CW / back-facing */
  101. glColor3f(0,0,.7);
  102. glVertex3f( 0.1, -0.9, -30.0);
  103. glColor3f(.8,0,0);
  104. glVertex3f( 0.1, 0.9, -30.0);
  105. glColor3f(0,.9,0);
  106. glVertex3f( 0.9, 0.0, -30.0);
  107. glEnd();
  108. glFlush();
  109. if (doubleBuffer) {
  110. glutSwapBuffers();
  111. }
  112. }
  113. static GLenum Args(int argc, char **argv)
  114. {
  115. GLint i;
  116. doubleBuffer = GL_FALSE;
  117. for (i = 1; i < argc; i++) {
  118. if (strcmp(argv[i], "-sb") == 0) {
  119. doubleBuffer = GL_FALSE;
  120. } else if (strcmp(argv[i], "-db") == 0) {
  121. doubleBuffer = GL_TRUE;
  122. } else {
  123. fprintf(stderr, "%s (Bad option).\n", argv[i]);
  124. return GL_FALSE;
  125. }
  126. }
  127. return GL_TRUE;
  128. }
  129. int main(int argc, char **argv)
  130. {
  131. GLenum type;
  132. glutInit(&argc, argv);
  133. if (Args(argc, argv) == GL_FALSE) {
  134. exit(1);
  135. }
  136. glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
  137. type = GLUT_RGB | GLUT_ALPHA;
  138. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  139. glutInitDisplayMode(type);
  140. if (glutCreateWindow(*argv) == GL_FALSE) {
  141. exit(1);
  142. }
  143. Init();
  144. glutReshapeFunc(Reshape);
  145. glutKeyboardFunc(Key);
  146. glutDisplayFunc(Draw);
  147. glutMainLoop();
  148. return 0;
  149. }