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.

lineclip.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <stdlib.h>
  28. #include <GL/glut.h>
  29. static int win_width, win_height;
  30. static void
  31. line(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
  32. {
  33. glBegin(GL_LINES);
  34. glVertex2f(x1, y1);
  35. glVertex2f(x2, y2);
  36. glEnd();
  37. }
  38. static void
  39. line3(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2)
  40. {
  41. glBegin(GL_LINES);
  42. glVertex3f(x1, y1, z1);
  43. glVertex3f(x2, y2, z2);
  44. glEnd();
  45. }
  46. static void
  47. display(void)
  48. {
  49. glClearColor(0.0, 0.0, 0.0, 0.0);
  50. glClear(GL_COLOR_BUFFER_BIT);
  51. glColor3f(1.0, 0.0, 0.0);
  52. /* 2 lines clipped along xmin */
  53. line(-20, win_height / 2 - 20,
  54. 20, win_height / 2 - 20);
  55. line( 20, win_height / 2 + 20,
  56. -20, win_height / 2 + 20);
  57. glColor3f(0.0, 1.0, 0.0);
  58. /* 2 lines clipped along ymax */
  59. line(win_width / 2 - 20, win_height + 20,
  60. win_width / 2 - 20, win_height - 20);
  61. line(win_width / 2 + 20, win_height - 20,
  62. win_width / 2 + 20, win_height + 20);
  63. glColor3f(0.0, 0.0, 1.0);
  64. /* 2 lines clipped along xmax */
  65. line(win_width - 20, win_height / 2 - 20,
  66. win_width + 20, win_height / 2 - 20);
  67. line(win_width + 20, win_height / 2 + 20,
  68. win_width - 20, win_height / 2 + 20);
  69. glColor3f(1.0, 1.0, 1.0);
  70. /* 2 lines clipped along ymin */
  71. line(win_width / 2 - 20, 20,
  72. win_width / 2 - 20, -20);
  73. line(win_width / 2 + 20, -20,
  74. win_width / 2 + 20, 20);
  75. /* 2 lines clipped along near */
  76. glColor3f(1.0, 0.0, 1.0);
  77. line3(win_width / 2 - 20 - 20, win_height / 2, 0.5,
  78. win_width / 2 - 20 + 20, win_height / 2, -0.5);
  79. line3(win_width / 2 - 20, win_height / 2 - 20, -0.5,
  80. win_width / 2 - 20, win_height / 2 + 20, 0.5);
  81. /* 2 lines clipped along far */
  82. glColor3f(0.0, 1.0, 1.0);
  83. line3(win_width / 2 + 20 - 20, win_height / 2, 1.5,
  84. win_width / 2 + 20 + 20, win_height / 2, 0.5);
  85. line3(win_width / 2 + 20, win_height / 2 - 20, 0.5,
  86. win_width / 2 + 20, win_height / 2 + 20, 1.5);
  87. /* entirely clipped along near/far */
  88. glColor3f(.5, .5, .5);
  89. line3(win_width / 2, win_height / 2 - 20, -0.5,
  90. win_width / 2, win_height / 2 + 20, -0.5);
  91. glColor3f(.5, .5, .5);
  92. line3(win_width / 2, win_height / 2 - 20, 1.5,
  93. win_width / 2, win_height / 2 + 20, 1.5);
  94. glColor3f(1.0, 1.0, 0.0);
  95. /* lines clipped along both x and y limits */
  96. line(-5, 20,
  97. 20, -5); /* xmin, ymin */
  98. line(-5, win_height - 20,
  99. 20, win_height + 5); /* xmin, ymax */
  100. line(win_width - 20, -5,
  101. win_width + 5, 20); /* xmax, ymin */
  102. line(win_width - 20, win_height + 5,
  103. win_width + 5, win_height - 20); /* xmax, ymax */
  104. glutSwapBuffers();
  105. }
  106. static void
  107. reshape(int width, int height)
  108. {
  109. win_width = width;
  110. win_height = height;
  111. glViewport(0, 0, width, height);
  112. glMatrixMode(GL_PROJECTION);
  113. glLoadIdentity();
  114. glOrtho(0, win_width, 0, win_height, 0.0, -1.0);
  115. glMatrixMode(GL_MODELVIEW);
  116. glLoadIdentity();
  117. glTranslatef(.25, .25, 0);
  118. }
  119. static void key( unsigned char key, int x, int y )
  120. {
  121. (void) x;
  122. (void) y;
  123. switch (key) {
  124. case 27: /* esc */
  125. exit(0);
  126. break;
  127. }
  128. glutPostRedisplay();
  129. }
  130. static void
  131. init(void)
  132. {
  133. }
  134. int
  135. main(int argc, char *argv[])
  136. {
  137. win_width = 200;
  138. win_height = 200;
  139. glutInit(&argc, argv);
  140. glutInitWindowPosition(0, 0);
  141. glutInitWindowSize(win_width, win_height);
  142. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  143. glutCreateWindow(argv[0]);
  144. glutReshapeFunc(reshape);
  145. glutKeyboardFunc(key);
  146. glutDisplayFunc(display);
  147. init();
  148. glutMainLoop();
  149. return 0;
  150. }