Clone of mesa.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

polyoff.c 8.8KB

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