Clone of mesa.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

polyoff.c 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 1993-1997, Silicon Graphics, Inc.
  3. * ALL RIGHTS RESERVED
  4. * Permission to use, copy, modify, and distribute this software for
  5. * any purpose and without fee is hereby granted, provided that the above
  6. * copyright notice appear in all copies and that both the copyright notice
  7. * and this permission notice appear in supporting documentation, and that
  8. * the name of Silicon Graphics, Inc. not be used in advertising
  9. * or publicity pertaining to distribution of the software without specific,
  10. * written prior permission.
  11. *
  12. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  16. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  21. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24. *
  25. * US Government Users Restricted Rights
  26. * Use, duplication, or disclosure by the Government is subject to
  27. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29. * clause at DFARS 252.227-7013 and/or in similar or successor
  30. * clauses in the FAR or the DOD or NASA FAR Supplement.
  31. * Unpublished-- rights reserved under the copyright laws of the
  32. * United States. Contractor/manufacturer is Silicon Graphics,
  33. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  34. *
  35. * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
  36. */
  37. /*
  38. * polyoff.c
  39. * This program demonstrates polygon offset to draw a shaded
  40. * polygon and its wireframe counterpart without ugly visual
  41. * artifacts ("stitching").
  42. */
  43. #include <GL/glut.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #ifdef GL_VERSION_1_1
  47. GLuint list;
  48. GLint spinx = 0;
  49. GLint spiny = 0;
  50. GLfloat tdist = 0.0;
  51. GLfloat polyfactor = 1.0;
  52. GLfloat polyunits = 1.0;
  53. /* display() draws two spheres, one with a gray, diffuse material,
  54. * the other sphere with a magenta material with a specular highlight.
  55. */
  56. void display (void)
  57. {
  58. GLfloat gray[] = { 0.8, 0.8, 0.8, 1.0 };
  59. GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
  60. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  61. glPushMatrix ();
  62. glTranslatef (0.0, 0.0, tdist);
  63. glRotatef ((GLfloat) spinx, 1.0, 0.0, 0.0);
  64. glRotatef ((GLfloat) spiny, 0.0, 1.0, 0.0);
  65. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
  66. glMaterialfv(GL_FRONT, GL_SPECULAR, black);
  67. glMaterialf(GL_FRONT, GL_SHININESS, 0.0);
  68. glEnable(GL_LIGHTING);
  69. glEnable(GL_LIGHT0);
  70. glEnable(GL_POLYGON_OFFSET_FILL);
  71. glPolygonOffset(polyfactor, polyunits);
  72. glCallList (list);
  73. glDisable(GL_POLYGON_OFFSET_FILL);
  74. glDisable(GL_LIGHTING);
  75. glDisable(GL_LIGHT0);
  76. glColor3f (1.0, 1.0, 1.0);
  77. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  78. glCallList (list);
  79. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  80. glPopMatrix ();
  81. glFlush ();
  82. }
  83. /* specify initial properties
  84. * create display list with sphere
  85. * initialize lighting and depth buffer
  86. */
  87. void gfxinit (void)
  88. {
  89. GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  90. GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  91. GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  92. GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  93. GLfloat global_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
  94. glClearColor (0.0, 0.0, 0.0, 1.0);
  95. list = glGenLists(1);
  96. glNewList (list, GL_COMPILE);
  97. glutSolidSphere(1.0, 20, 12);
  98. glEndList ();
  99. glEnable(GL_DEPTH_TEST);
  100. glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
  101. glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  102. glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
  103. glLightfv (GL_LIGHT0, GL_POSITION, light_position);
  104. glLightModelfv (GL_LIGHT_MODEL_AMBIENT, global_ambient);
  105. }
  106. /* call when window is resized */
  107. void reshape(int width, int height)
  108. {
  109. glViewport (0, 0, width, height);
  110. glMatrixMode (GL_PROJECTION);
  111. glLoadIdentity ();
  112. gluPerspective(45.0, (GLdouble)width/(GLdouble)height,
  113. 1.0, 10.0);
  114. glMatrixMode (GL_MODELVIEW);
  115. glLoadIdentity ();
  116. gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  117. }
  118. /* call when mouse button is pressed */
  119. /* ARGSUSED2 */
  120. void mouse(int button, int state, int x, int y) {
  121. switch (button) {
  122. case GLUT_LEFT_BUTTON:
  123. switch (state) {
  124. case GLUT_DOWN:
  125. spinx = (spinx + 5) % 360;
  126. glutPostRedisplay();
  127. break;
  128. default:
  129. break;
  130. }
  131. break;
  132. case GLUT_MIDDLE_BUTTON:
  133. switch (state) {
  134. case GLUT_DOWN:
  135. spiny = (spiny + 5) % 360;
  136. glutPostRedisplay();
  137. break;
  138. default:
  139. break;
  140. }
  141. break;
  142. case GLUT_RIGHT_BUTTON:
  143. switch (state) {
  144. case GLUT_UP:
  145. exit(0);
  146. break;
  147. default:
  148. break;
  149. }
  150. break;
  151. default:
  152. break;
  153. }
  154. }
  155. /* ARGSUSED1 */
  156. void keyboard (unsigned char key, int x, int y)
  157. {
  158. switch (key) {
  159. case 't':
  160. if (tdist < 4.0) {
  161. tdist = (tdist + 0.5);
  162. glutPostRedisplay();
  163. }
  164. break;
  165. case 'T':
  166. if (tdist > -5.0) {
  167. tdist = (tdist - 0.5);
  168. glutPostRedisplay();
  169. }
  170. break;
  171. case 'F':
  172. polyfactor = polyfactor + 0.1;
  173. printf ("polyfactor is %f\n", polyfactor);
  174. glutPostRedisplay();
  175. break;
  176. case 'f':
  177. polyfactor = polyfactor - 0.1;
  178. printf ("polyfactor is %f\n", polyfactor);
  179. glutPostRedisplay();
  180. break;
  181. case 'U':
  182. polyunits = polyunits + 1.0;
  183. printf ("polyunits is %f\n", polyunits);
  184. glutPostRedisplay();
  185. break;
  186. case 'u':
  187. polyunits = polyunits - 1.0;
  188. printf ("polyunits is %f\n", polyunits);
  189. glutPostRedisplay();
  190. break;
  191. default:
  192. break;
  193. }
  194. }
  195. static void
  196. key(unsigned char k, int x, int y)
  197. {
  198. switch (k) {
  199. case 27: /* Escape */
  200. exit(0);
  201. break;
  202. default:
  203. return;
  204. }
  205. glutPostRedisplay();
  206. }
  207. /* Main Loop
  208. * Open window with initial window size, title bar,
  209. * RGBA display mode, and handle input events.
  210. */
  211. int main(int argc, char** argv)
  212. {
  213. glutInit(&argc, argv);
  214. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  215. glutCreateWindow(argv[0]);
  216. glutReshapeFunc(reshape);
  217. glutDisplayFunc(display);
  218. glutMouseFunc(mouse);
  219. glutKeyboardFunc(keyboard);
  220. gfxinit();
  221. glutKeyboardFunc(key);
  222. glutMainLoop();
  223. return 0;
  224. }
  225. #else
  226. int main(int argc, char** argv)
  227. {
  228. fprintf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0.\n");
  229. fprintf (stderr, "If your implementation of OpenGL Version 1.0 has the right extensions,\n");
  230. fprintf (stderr, "you may be able to modify this program to make it run.\n");
  231. return 0;
  232. }
  233. #endif