Clone of mesa.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

polyoff.c 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #include <string.h>
  47. #ifdef GL_VERSION_1_1
  48. GLuint list;
  49. GLint fill = 1;
  50. GLfloat spinx = 0;
  51. GLfloat spiny = 0;
  52. GLfloat tdist = 0.0;
  53. GLfloat polyfactor = 1.0;
  54. GLfloat polyunits = 1.0;
  55. GLboolean doubleBuffer;
  56. /* display() draws two spheres, one with a gray, diffuse material,
  57. * the other sphere with a magenta material with a specular highlight.
  58. */
  59. void display (void)
  60. {
  61. GLfloat gray[] = { 0.8, 0.8, 0.8, 1.0 };
  62. GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
  63. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  64. glPushMatrix ();
  65. glTranslatef (0.0, 0.0, tdist);
  66. glRotatef ((GLfloat) spinx, 1.0, 0.0, 0.0);
  67. glRotatef ((GLfloat) spiny, 0.0, 1.0, 0.0);
  68. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
  69. glMaterialfv(GL_FRONT, GL_SPECULAR, black);
  70. glMaterialf(GL_FRONT, GL_SHININESS, 0.0);
  71. if (fill) {
  72. glEnable(GL_LIGHTING);
  73. glEnable(GL_LIGHT0);
  74. glEnable(GL_POLYGON_OFFSET_FILL);
  75. glPolygonOffset(polyfactor, polyunits);
  76. glCallList (list);
  77. glDisable(GL_POLYGON_OFFSET_FILL);
  78. }
  79. glDisable(GL_LIGHTING);
  80. glDisable(GL_LIGHT0);
  81. glColor3f (1.0, 1.0, 1.0);
  82. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  83. glPolygonOffset(-polyfactor, -polyunits);
  84. if (!fill) glEnable(GL_POLYGON_OFFSET_LINE);
  85. glCallList (list);
  86. glDisable(GL_POLYGON_OFFSET_LINE);
  87. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  88. if (!fill) {
  89. glEnable(GL_LIGHTING);
  90. glEnable(GL_LIGHT0);
  91. glCallList (list);
  92. }
  93. glPopMatrix ();
  94. glFlush ();
  95. if (doubleBuffer) glutSwapBuffers();
  96. }
  97. /* specify initial properties
  98. * create display list with sphere
  99. * initialize lighting and depth buffer
  100. */
  101. void gfxinit (void)
  102. {
  103. GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  104. GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  105. GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  106. GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  107. GLfloat global_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
  108. glClearColor (0.0, 0.0, 0.0, 1.0);
  109. list = glGenLists(1);
  110. glNewList (list, GL_COMPILE);
  111. glutSolidSphere(1.0, 20, 12);
  112. glEndList ();
  113. glEnable(GL_DEPTH_TEST);
  114. glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
  115. glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  116. glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
  117. glLightfv (GL_LIGHT0, GL_POSITION, light_position);
  118. glLightModelfv (GL_LIGHT_MODEL_AMBIENT, global_ambient);
  119. }
  120. /* call when window is resized */
  121. void reshape(int width, int height)
  122. {
  123. glViewport (0, 0, width, height);
  124. glMatrixMode (GL_PROJECTION);
  125. glLoadIdentity ();
  126. gluPerspective(45.0, (GLdouble)width/(GLdouble)height,
  127. 1.0, 10.0);
  128. glMatrixMode (GL_MODELVIEW);
  129. glLoadIdentity ();
  130. gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  131. }
  132. static void Benchmark( float xdiff, float ydiff )
  133. {
  134. int startTime, endTime;
  135. int draws;
  136. double seconds, fps;
  137. printf("Benchmarking...\n");
  138. draws = 0;
  139. startTime = glutGet(GLUT_ELAPSED_TIME);
  140. spinx = spiny = 0.0;
  141. do {
  142. spinx += xdiff;
  143. spiny += ydiff;
  144. display();
  145. draws++;
  146. endTime = glutGet(GLUT_ELAPSED_TIME);
  147. } while (endTime - startTime < 5000); /* 5 seconds */
  148. /* Results */
  149. seconds = (double) (endTime - startTime) / 1000.0;
  150. fps = draws / seconds;
  151. printf("Result: fps: %g\n", fps);
  152. }
  153. /* call when mouse button is pressed */
  154. /* ARGSUSED2 */
  155. void mouse(int button, int state, int x, int y) {
  156. switch (button) {
  157. case GLUT_LEFT_BUTTON:
  158. switch (state) {
  159. case GLUT_DOWN:
  160. spinx += 5;
  161. glutPostRedisplay();
  162. break;
  163. default:
  164. break;
  165. }
  166. break;
  167. case GLUT_MIDDLE_BUTTON:
  168. switch (state) {
  169. case GLUT_DOWN:
  170. spiny += 5;
  171. glutPostRedisplay();
  172. break;
  173. default:
  174. break;
  175. }
  176. break;
  177. case GLUT_RIGHT_BUTTON:
  178. switch (state) {
  179. case GLUT_UP:
  180. exit(0);
  181. break;
  182. default:
  183. break;
  184. }
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. /* ARGSUSED1 */
  191. void keyboard (unsigned char key, int x, int y)
  192. {
  193. switch (key) {
  194. case 't':
  195. if (tdist < 4.0) {
  196. tdist = (tdist + 0.5);
  197. glutPostRedisplay();
  198. }
  199. break;
  200. case 'T':
  201. if (tdist > -5.0) {
  202. tdist = (tdist - 0.5);
  203. glutPostRedisplay();
  204. }
  205. break;
  206. case 'F':
  207. polyfactor = polyfactor + 0.1;
  208. printf ("polyfactor is %f\n", polyfactor);
  209. glutPostRedisplay();
  210. break;
  211. case 'f':
  212. polyfactor = polyfactor - 0.1;
  213. printf ("polyfactor is %f\n", polyfactor);
  214. glutPostRedisplay();
  215. break;
  216. case 'U':
  217. polyunits = polyunits + 1.0;
  218. printf ("polyunits is %f\n", polyunits);
  219. glutPostRedisplay();
  220. break;
  221. case 'u':
  222. polyunits = polyunits - 1.0;
  223. printf ("polyunits is %f\n", polyunits);
  224. glutPostRedisplay();
  225. break;
  226. case 'b':
  227. Benchmark(5.0, 0);
  228. break;
  229. case 'B':
  230. Benchmark(0, 5.0);
  231. break;
  232. case ' ':
  233. fill = !fill;
  234. printf ("fill/line: %d\n", fill);
  235. glutPostRedisplay();
  236. break;
  237. case 27: /* Escape */
  238. exit(0);
  239. break;
  240. default:
  241. break;
  242. }
  243. }
  244. static void
  245. key(unsigned char k, int x, int y)
  246. {
  247. switch (k) {
  248. case 27: /* Escape */
  249. exit(0);
  250. break;
  251. default:
  252. return;
  253. }
  254. glutPostRedisplay();
  255. }
  256. GLenum Args(int argc, char **argv)
  257. {
  258. GLint i;
  259. doubleBuffer = GL_FALSE;
  260. for (i = 1; i < argc; i++) {
  261. if (strcmp(argv[i], "-sb") == 0) {
  262. doubleBuffer = GL_FALSE;
  263. } else if (strcmp(argv[i], "-db") == 0) {
  264. doubleBuffer = GL_TRUE;
  265. } else {
  266. printf("%s (Bad option).\n", argv[i]);
  267. return GL_FALSE;
  268. }
  269. }
  270. return GL_TRUE;
  271. }
  272. /* Main Loop
  273. * Open window with initial window size, title bar,
  274. * RGBA display mode, and handle input events.
  275. */
  276. int main(int argc, char** argv)
  277. {
  278. GLuint type;
  279. glutInit(&argc, argv);
  280. Args(argc, argv);
  281. type = GLUT_DEPTH | GLUT_RGB;
  282. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  283. glutInitDisplayMode(type);
  284. glutCreateWindow("polyoff");
  285. glutReshapeFunc(reshape);
  286. glutDisplayFunc(display);
  287. glutMouseFunc(mouse);
  288. glutKeyboardFunc(keyboard);
  289. gfxinit();
  290. glutMainLoop();
  291. return 0;
  292. }
  293. #else
  294. int main(int argc, char** argv)
  295. {
  296. fprintf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0.\n");
  297. fprintf (stderr, "If your implementation of OpenGL Version 1.0 has the right extensions,\n");
  298. fprintf (stderr, "you may be able to modify this program to make it run.\n");
  299. return 0;
  300. }
  301. #endif