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.

polyoff.c 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. static 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. static 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. static 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. fflush(stdout);
  139. draws = 0;
  140. startTime = glutGet(GLUT_ELAPSED_TIME);
  141. spinx = spiny = 0.0;
  142. do {
  143. spinx += xdiff;
  144. spiny += ydiff;
  145. display();
  146. draws++;
  147. endTime = glutGet(GLUT_ELAPSED_TIME);
  148. } while (endTime - startTime < 5000); /* 5 seconds */
  149. /* Results */
  150. seconds = (double) (endTime - startTime) / 1000.0;
  151. fps = draws / seconds;
  152. printf("Result: fps: %g\n", fps);
  153. fflush(stdout);
  154. }
  155. /* call when mouse button is pressed */
  156. /* ARGSUSED2 */
  157. static void mouse(int button, int state, int x, int y) {
  158. switch (button) {
  159. case GLUT_LEFT_BUTTON:
  160. switch (state) {
  161. case GLUT_DOWN:
  162. spinx += 5;
  163. glutPostRedisplay();
  164. break;
  165. default:
  166. break;
  167. }
  168. break;
  169. case GLUT_MIDDLE_BUTTON:
  170. switch (state) {
  171. case GLUT_DOWN:
  172. spiny += 5;
  173. glutPostRedisplay();
  174. break;
  175. default:
  176. break;
  177. }
  178. break;
  179. case GLUT_RIGHT_BUTTON:
  180. switch (state) {
  181. case GLUT_UP:
  182. exit(0);
  183. break;
  184. default:
  185. break;
  186. }
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. /* ARGSUSED1 */
  193. static void keyboard (unsigned char key, int x, int y)
  194. {
  195. switch (key) {
  196. case 't':
  197. if (tdist < 4.0) {
  198. tdist = (tdist + 0.5);
  199. glutPostRedisplay();
  200. }
  201. break;
  202. case 'T':
  203. if (tdist > -5.0) {
  204. tdist = (tdist - 0.5);
  205. glutPostRedisplay();
  206. }
  207. break;
  208. case 'F':
  209. polyfactor = polyfactor + 0.1;
  210. printf ("polyfactor is %f\n", polyfactor);
  211. glutPostRedisplay();
  212. break;
  213. case 'f':
  214. polyfactor = polyfactor - 0.1;
  215. printf ("polyfactor is %f\n", polyfactor);
  216. glutPostRedisplay();
  217. break;
  218. case 'U':
  219. polyunits = polyunits + 1.0;
  220. printf ("polyunits is %f\n", polyunits);
  221. glutPostRedisplay();
  222. break;
  223. case 'u':
  224. polyunits = polyunits - 1.0;
  225. printf ("polyunits is %f\n", polyunits);
  226. glutPostRedisplay();
  227. break;
  228. case 'b':
  229. Benchmark(5.0, 0);
  230. break;
  231. case 'B':
  232. Benchmark(0, 5.0);
  233. break;
  234. case ' ':
  235. fill = !fill;
  236. printf ("fill/line: %d\n", fill);
  237. glutPostRedisplay();
  238. break;
  239. case 27: /* Escape */
  240. exit(0);
  241. break;
  242. default:
  243. break;
  244. }
  245. fflush(stdout);
  246. }
  247. static GLenum Args(int argc, char **argv)
  248. {
  249. GLint i;
  250. doubleBuffer = GL_FALSE;
  251. for (i = 1; i < argc; i++) {
  252. if (strcmp(argv[i], "-sb") == 0) {
  253. doubleBuffer = GL_FALSE;
  254. } else if (strcmp(argv[i], "-db") == 0) {
  255. doubleBuffer = GL_TRUE;
  256. } else {
  257. printf("%s (Bad option).\n", argv[i]);
  258. return GL_FALSE;
  259. }
  260. }
  261. return GL_TRUE;
  262. }
  263. /* Main Loop
  264. * Open window with initial window size, title bar,
  265. * RGBA display mode, and handle input events.
  266. */
  267. int main(int argc, char** argv)
  268. {
  269. GLuint type;
  270. glutInit(&argc, argv);
  271. Args(argc, argv);
  272. type = GLUT_DEPTH | GLUT_RGB;
  273. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  274. glutInitDisplayMode(type);
  275. glutCreateWindow("polyoff");
  276. glutReshapeFunc(reshape);
  277. glutDisplayFunc(display);
  278. glutMouseFunc(mouse);
  279. glutKeyboardFunc(keyboard);
  280. gfxinit();
  281. glutMainLoop();
  282. return 0;
  283. }
  284. #else
  285. int main(int argc, char** argv)
  286. {
  287. fprintf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0.\n");
  288. fprintf (stderr, "If your implementation of OpenGL Version 1.0 has the right extensions,\n");
  289. fprintf (stderr, "you may be able to modify this program to make it run.\n");
  290. return 0;
  291. }
  292. #endif