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.

material.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* Copyright (c) Mark J. Kilgard, 1994. */
  2. /*
  3. * (c) Copyright 1993, Silicon Graphics, Inc.
  4. * ALL RIGHTS RESERVED
  5. * Permission to use, copy, modify, and distribute this software for
  6. * any purpose and without fee is hereby granted, provided that the above
  7. * copyright notice appear in all copies and that both the copyright notice
  8. * and this permission notice appear in supporting documentation, and that
  9. * the name of Silicon Graphics, Inc. not be used in advertising
  10. * or publicity pertaining to distribution of the software without specific,
  11. * written prior permission.
  12. *
  13. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  17. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  22. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25. *
  26. * US Government Users Restricted Rights
  27. * Use, duplication, or disclosure by the Government is subject to
  28. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30. * clause at DFARS 252.227-7013 and/or in similar or successor
  31. * clauses in the FAR or the DOD or NASA FAR Supplement.
  32. * Unpublished-- rights reserved under the copyright laws of the
  33. * United States. Contractor/manufacturer is Silicon Graphics,
  34. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  35. *
  36. * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37. */
  38. /*
  39. * material.c
  40. * This program demonstrates the use of the GL lighting model.
  41. * Several objects are drawn using different material characteristics.
  42. * A single light source illuminates the objects.
  43. */
  44. #include <stdlib.h>
  45. #include <GL/glut.h>
  46. /* Initialize z-buffer, projection matrix, light source,
  47. * and lighting model. Do not specify a material property here.
  48. */
  49. void myinit(void)
  50. {
  51. GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  52. GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  53. GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
  54. GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
  55. GLfloat local_view[] = { 0.0 };
  56. glEnable(GL_DEPTH_TEST);
  57. glDepthFunc(GL_LESS);
  58. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  59. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  60. glLightfv(GL_LIGHT0, GL_POSITION, position);
  61. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  62. glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
  63. glEnable(GL_LIGHTING);
  64. glEnable(GL_LIGHT0);
  65. glClearColor(0.0, 0.1, 0.1, 0.0);
  66. }
  67. /* Draw twelve spheres in 3 rows with 4 columns.
  68. * The spheres in the first row have materials with no ambient reflection.
  69. * The second row has materials with significant ambient reflection.
  70. * The third row has materials with colored ambient reflection.
  71. *
  72. * The first column has materials with blue, diffuse reflection only.
  73. * The second column has blue diffuse reflection, as well as specular
  74. * reflection with a low shininess exponent.
  75. * The third column has blue diffuse reflection, as well as specular
  76. * reflection with a high shininess exponent (a more concentrated highlight).
  77. * The fourth column has materials which also include an emissive component.
  78. *
  79. * glTranslatef() is used to move spheres to their appropriate locations.
  80. */
  81. void display(void)
  82. {
  83. GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
  84. GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
  85. GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };
  86. GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
  87. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  88. GLfloat no_shininess[] = { 0.0 };
  89. GLfloat low_shininess[] = { 5.0 };
  90. GLfloat high_shininess[] = { 100.0 };
  91. GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0};
  92. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  93. /* draw sphere in first row, first column
  94. * diffuse reflection only; no ambient or specular
  95. */
  96. glPushMatrix();
  97. glTranslatef (-3.75, 3.0, 0.0);
  98. glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  99. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  100. glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  101. glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  102. glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  103. glutSolidSphere(1.0, 16, 16);
  104. glPopMatrix();
  105. /* draw sphere in first row, second column
  106. * diffuse and specular reflection; low shininess; no ambient
  107. */
  108. glPushMatrix();
  109. glTranslatef (-1.25, 3.0, 0.0);
  110. glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  111. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  112. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  113. glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
  114. glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  115. glutSolidSphere(1.0, 16, 16);
  116. glPopMatrix();
  117. /* draw sphere in first row, third column
  118. * diffuse and specular reflection; high shininess; no ambient
  119. */
  120. glPushMatrix();
  121. glTranslatef (1.25, 3.0, 0.0);
  122. glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  123. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  124. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  125. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  126. glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  127. glutSolidSphere(1.0, 16, 16);
  128. glPopMatrix();
  129. /* draw sphere in first row, fourth column
  130. * diffuse reflection; emission; no ambient or specular reflection
  131. */
  132. glPushMatrix();
  133. glTranslatef (3.75, 3.0, 0.0);
  134. glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  135. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  136. glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  137. glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  138. glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  139. glutSolidSphere(1.0, 16, 16);
  140. glPopMatrix();
  141. /* draw sphere in second row, first column
  142. * ambient and diffuse reflection; no specular
  143. */
  144. glPushMatrix();
  145. glTranslatef (-3.75, 0.0, 0.0);
  146. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  147. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  148. glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  149. glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  150. glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  151. glutSolidSphere(1.0, 16, 16);
  152. glPopMatrix();
  153. /* draw sphere in second row, second column
  154. * ambient, diffuse and specular reflection; low shininess
  155. */
  156. glPushMatrix();
  157. glTranslatef (-1.25, 0.0, 0.0);
  158. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  159. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  160. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  161. glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
  162. glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  163. glutSolidSphere(1.0, 16, 16);
  164. glPopMatrix();
  165. /* draw sphere in second row, third column
  166. * ambient, diffuse and specular reflection; high shininess
  167. */
  168. glPushMatrix();
  169. glTranslatef (1.25, 0.0, 0.0);
  170. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  171. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  172. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  173. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  174. glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  175. glutSolidSphere(1.0, 16, 16);
  176. glPopMatrix();
  177. /* draw sphere in second row, fourth column
  178. * ambient and diffuse reflection; emission; no specular
  179. */
  180. glPushMatrix();
  181. glTranslatef (3.75, 0.0, 0.0);
  182. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  183. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  184. glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  185. glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  186. glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  187. glutSolidSphere(1.0, 16, 16);
  188. glPopMatrix();
  189. /* draw sphere in third row, first column
  190. * colored ambient and diffuse reflection; no specular
  191. */
  192. glPushMatrix();
  193. glTranslatef (-3.75, -3.0, 0.0);
  194. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  195. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  196. glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  197. glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  198. glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  199. glutSolidSphere(1.0, 16, 16);
  200. glPopMatrix();
  201. /* draw sphere in third row, second column
  202. * colored ambient, diffuse and specular reflection; low shininess
  203. */
  204. glPushMatrix();
  205. glTranslatef (-1.25, -3.0, 0.0);
  206. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  207. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  208. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  209. glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
  210. glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  211. glutSolidSphere(1.0, 16, 16);
  212. glPopMatrix();
  213. /* draw sphere in third row, third column
  214. * colored ambient, diffuse and specular reflection; high shininess
  215. */
  216. glPushMatrix();
  217. glTranslatef (1.25, -3.0, 0.0);
  218. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  219. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  220. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  221. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  222. glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  223. glutSolidSphere(1.0, 16, 16);
  224. glPopMatrix();
  225. /* draw sphere in third row, fourth column
  226. * colored ambient and diffuse reflection; emission; no specular
  227. */
  228. glPushMatrix();
  229. glTranslatef (3.75, -3.0, 0.0);
  230. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  231. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  232. glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  233. glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  234. glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  235. glutSolidSphere(1.0, 16, 16);
  236. glPopMatrix();
  237. glFlush();
  238. }
  239. void myReshape(int w, int h)
  240. {
  241. glViewport(0, 0, w, h);
  242. glMatrixMode(GL_PROJECTION);
  243. glLoadIdentity();
  244. if (w <= (h * 2))
  245. glOrtho (-6.0, 6.0, -3.0*((GLfloat)h*2)/(GLfloat)w,
  246. 3.0*((GLfloat)h*2)/(GLfloat)w, -10.0, 10.0);
  247. else
  248. glOrtho (-6.0*(GLfloat)w/((GLfloat)h*2),
  249. 6.0*(GLfloat)w/((GLfloat)h*2), -3.0, 3.0, -10.0, 10.0);
  250. glMatrixMode(GL_MODELVIEW);
  251. }
  252. static void
  253. key(unsigned char k, int x, int y)
  254. {
  255. switch (k) {
  256. case 27: /* Escape */
  257. exit(0);
  258. break;
  259. default:
  260. return;
  261. }
  262. glutPostRedisplay();
  263. }
  264. /* Main Loop
  265. * Open window with initial window size, title bar,
  266. * RGBA display mode, and handle input events.
  267. */
  268. int main(int argc, char** argv)
  269. {
  270. glutInit(&argc, argv);
  271. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  272. glutInitWindowSize (600, 450);
  273. glutCreateWindow(argv[0]);
  274. myinit();
  275. glutReshapeFunc(myReshape);
  276. glutDisplayFunc(display);
  277. glutKeyboardFunc(key);
  278. glutMainLoop();
  279. return 0; /* ANSI C requires main to return int. */
  280. }