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.

flat-clip.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Test flat shading and clipping.
  3. *
  4. * Brian Paul
  5. * 30 August 2007
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include <GL/glut.h>
  11. static int Win;
  12. static GLfloat Scale = 2.0, Zrot = 50;
  13. static GLenum Mode = GL_LINE_LOOP;
  14. static GLboolean Smooth = 0;
  15. static GLenum PolygonMode = GL_FILL;
  16. static void
  17. Draw(void)
  18. {
  19. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  20. if (Smooth)
  21. glShadeModel(GL_SMOOTH);
  22. else
  23. glShadeModel(GL_FLAT);
  24. glPushMatrix();
  25. glScalef(Scale, Scale, 1);
  26. glRotatef(Zrot, 0, 0, 1);
  27. glPolygonMode(GL_FRONT_AND_BACK, PolygonMode);
  28. glBegin(Mode);
  29. glColor3f(1, 0, 0);
  30. glVertex2f(-1, -1);
  31. glColor3f(0, 1, 0);
  32. glVertex2f( 2, -1);
  33. glColor3f(0, 0, 1);
  34. glVertex2f( 0, 1);
  35. glEnd();
  36. glPushMatrix();
  37. glScalef(0.9, 0.9, 1);
  38. glBegin(Mode);
  39. glColor3f(1, 0, 0);
  40. glVertex2f( 0, 1);
  41. glColor3f(0, 0, 1);
  42. glVertex2f( 2, -1);
  43. glColor3f(0, 1, 0);
  44. glVertex2f(-1, -1);
  45. glEnd();
  46. glPopMatrix();
  47. glPopMatrix();
  48. glutSwapBuffers();
  49. }
  50. static void
  51. Reshape(int width, int height)
  52. {
  53. glViewport(0, 0, width, height);
  54. glMatrixMode(GL_PROJECTION);
  55. glLoadIdentity();
  56. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  57. glMatrixMode(GL_MODELVIEW);
  58. glLoadIdentity();
  59. glTranslatef(0.0, 0.0, -15.0);
  60. }
  61. static void
  62. Key(unsigned char key, int x, int y)
  63. {
  64. (void) x;
  65. (void) y;
  66. switch (key) {
  67. case 'p':
  68. if (Mode == GL_TRIANGLES)
  69. Mode = GL_LINE_LOOP;
  70. else
  71. Mode = GL_TRIANGLES;
  72. break;
  73. case 'f':
  74. if (PolygonMode == GL_POINT)
  75. PolygonMode = GL_LINE;
  76. else if (PolygonMode == GL_LINE)
  77. PolygonMode = GL_FILL;
  78. else
  79. PolygonMode = GL_POINT;
  80. printf("PolygonMode = 0x%x\n", PolygonMode);
  81. break;
  82. case 'r':
  83. Zrot -= 5.0;
  84. break;
  85. case 'R':
  86. Zrot += 5.0;
  87. break;
  88. case 'z':
  89. Scale *= 1.1;
  90. break;
  91. case 'Z':
  92. Scale /= 1.1;
  93. break;
  94. case 's':
  95. Smooth = !Smooth;
  96. break;
  97. case 27:
  98. glutDestroyWindow(Win);
  99. exit(0);
  100. break;
  101. }
  102. glutPostRedisplay();
  103. }
  104. static void
  105. Init(void)
  106. {
  107. printf("Usage:\n");
  108. printf(" z/Z: change triangle size\n");
  109. printf(" r/R: rotate\n");
  110. printf(" p: toggle line/fill mode\n");
  111. printf(" s: toggle smooth/flat shading\n");
  112. printf(" f: switch polygon fill mode\n");
  113. }
  114. int
  115. main(int argc, char *argv[])
  116. {
  117. glutInit(&argc, argv);
  118. glutInitWindowPosition(0, 0);
  119. glutInitWindowSize(400, 400);
  120. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  121. Win = glutCreateWindow(argv[0]);
  122. glutReshapeFunc(Reshape);
  123. glutKeyboardFunc(Key);
  124. glutDisplayFunc(Draw);
  125. Init();
  126. glutMainLoop();
  127. return 0;
  128. }