Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and
  5. * its documentation for any purpose is hereby granted without fee, provided
  6. * that (i) the above copyright notices and this permission notice appear in
  7. * all copies of the software and related documentation, and (ii) the name of
  8. * Silicon Graphics may not be used in any advertising or
  9. * publicity relating to the software without the specific, prior written
  10. * permission of Silicon Graphics.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13. * ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <math.h>
  28. #include <GL/glut.h>
  29. #ifndef PI
  30. #define PI 3.14159265358979323846
  31. #endif
  32. #define GETCOORD(frame, x, y) (&(theMesh.coords[frame*theMesh.numCoords+(x)+(y)*(theMesh.widthX+1)]))
  33. #define GETFACET(frame, x, y) (&(theMesh.facets[frame*theMesh.numFacets+(x)+(y)*theMesh.widthX]))
  34. GLenum rgb, doubleBuffer;
  35. #include "tkmap.c"
  36. GLint colorIndexes1[3];
  37. GLint colorIndexes2[3];
  38. GLenum clearMask = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
  39. GLenum smooth = GL_FALSE;
  40. GLenum lighting = GL_TRUE;
  41. GLenum depth = GL_TRUE;
  42. GLenum stepMode = GL_FALSE;
  43. GLenum spinMode = GL_FALSE;
  44. GLint contouring = 0;
  45. GLint widthX, widthY;
  46. GLint checkerSize;
  47. float height;
  48. GLint frames, curFrame = 0, nextFrame = 0;
  49. struct facet {
  50. float color[3];
  51. float normal[3];
  52. };
  53. struct coord {
  54. float vertex[3];
  55. float normal[3];
  56. };
  57. struct mesh {
  58. GLint widthX, widthY;
  59. GLint numFacets;
  60. GLint numCoords;
  61. GLint frames;
  62. struct coord *coords;
  63. struct facet *facets;
  64. } theMesh;
  65. GLubyte contourTexture1[] = {
  66. 255, 255, 255, 255,
  67. 255, 255, 255, 255,
  68. 255, 255, 255, 255,
  69. 127, 127, 127, 127,
  70. };
  71. GLubyte contourTexture2[] = {
  72. 255, 255, 255, 255,
  73. 255, 127, 127, 127,
  74. 255, 127, 127, 127,
  75. 255, 127, 127, 127,
  76. };
  77. void GLUTCALLBACK glut_post_redisplay_p(void)
  78. {
  79. glutPostRedisplay();
  80. }
  81. static void Animate(void)
  82. {
  83. struct coord *coord;
  84. struct facet *facet;
  85. float *lastColor;
  86. float *thisColor;
  87. GLint i, j;
  88. glClear(clearMask);
  89. if (nextFrame || !stepMode) {
  90. curFrame++;
  91. }
  92. if (curFrame >= theMesh.frames) {
  93. curFrame = 0;
  94. }
  95. if ((nextFrame || !stepMode) && spinMode) {
  96. glRotatef(5.0, 0.0, 0.0, 1.0);
  97. }
  98. nextFrame = 0;
  99. for (i = 0; i < theMesh.widthX; i++) {
  100. glBegin(GL_QUAD_STRIP);
  101. lastColor = NULL;
  102. for (j = 0; j < theMesh.widthY; j++) {
  103. facet = GETFACET(curFrame, i, j);
  104. if (!smooth && lighting) {
  105. glNormal3fv(facet->normal);
  106. }
  107. if (lighting) {
  108. if (rgb) {
  109. thisColor = facet->color;
  110. glColor3fv(facet->color);
  111. } else {
  112. thisColor = facet->color;
  113. glMaterialfv(GL_FRONT_AND_BACK, GL_COLOR_INDEXES,
  114. facet->color);
  115. }
  116. } else {
  117. if (rgb) {
  118. thisColor = facet->color;
  119. glColor3fv(facet->color);
  120. } else {
  121. thisColor = facet->color;
  122. glIndexf(facet->color[1]);
  123. }
  124. }
  125. if (!lastColor || (thisColor[0] != lastColor[0] && smooth)) {
  126. if (lastColor) {
  127. glEnd();
  128. glBegin(GL_QUAD_STRIP);
  129. }
  130. coord = GETCOORD(curFrame, i, j);
  131. if (smooth && lighting) {
  132. glNormal3fv(coord->normal);
  133. }
  134. glVertex3fv(coord->vertex);
  135. coord = GETCOORD(curFrame, i+1, j);
  136. if (smooth && lighting) {
  137. glNormal3fv(coord->normal);
  138. }
  139. glVertex3fv(coord->vertex);
  140. }
  141. coord = GETCOORD(curFrame, i, j+1);
  142. if (smooth && lighting) {
  143. glNormal3fv(coord->normal);
  144. }
  145. glVertex3fv(coord->vertex);
  146. coord = GETCOORD(curFrame, i+1, j+1);
  147. if (smooth && lighting) {
  148. glNormal3fv(coord->normal);
  149. }
  150. glVertex3fv(coord->vertex);
  151. lastColor = thisColor;
  152. }
  153. glEnd();
  154. }
  155. glFlush();
  156. if (doubleBuffer) {
  157. glutSwapBuffers();
  158. }
  159. }
  160. static void SetColorMap(void)
  161. {
  162. static float green[3] = {0.2, 1.0, 0.2};
  163. static float red[3] = {1.0, 0.2, 0.2};
  164. float *color = 0, percent;
  165. GLint *indexes = 0, entries, i, j;
  166. entries = glutGet(GLUT_WINDOW_COLORMAP_SIZE);
  167. colorIndexes1[0] = 1;
  168. colorIndexes1[1] = 1 + (GLint)((entries - 1) * 0.3);
  169. colorIndexes1[2] = (GLint)((entries - 1) * 0.5);
  170. colorIndexes2[0] = 1 + (GLint)((entries - 1) * 0.5);
  171. colorIndexes2[1] = 1 + (GLint)((entries - 1) * 0.8);
  172. colorIndexes2[2] = entries - 1;
  173. for (i = 0; i < 2; i++) {
  174. switch (i) {
  175. case 0:
  176. color = green;
  177. indexes = colorIndexes1;
  178. break;
  179. case 1:
  180. color = red;
  181. indexes = colorIndexes2;
  182. break;
  183. }
  184. for (j = indexes[0]; j < indexes[1]; j++) {
  185. percent = 0.2 + 0.8 * (j - indexes[0]) /
  186. (float)(indexes[1] - indexes[0]);
  187. glutSetColor(j, percent*color[0], percent*color[1],
  188. percent*color[2]);
  189. }
  190. for (j=indexes[1]; j<=indexes[2]; j++) {
  191. percent = (j - indexes[1]) / (float)(indexes[2] - indexes[1]);
  192. glutSetColor(j, percent*(1-color[0])+color[0],
  193. percent*(1-color[1])+color[1],
  194. percent*(1-color[2])+color[2]);
  195. }
  196. }
  197. }
  198. static void InitMesh(void)
  199. {
  200. struct coord *coord;
  201. struct facet *facet;
  202. float dp1[3], dp2[3];
  203. float *pt1, *pt2, *pt3;
  204. float angle, d, x, y;
  205. GLint numFacets, numCoords, frameNum, i, j;
  206. theMesh.widthX = widthX;
  207. theMesh.widthY = widthY;
  208. theMesh.frames = frames;
  209. numFacets = widthX * widthY;
  210. numCoords = (widthX + 1) * (widthY + 1);
  211. theMesh.numCoords = numCoords;
  212. theMesh.numFacets = numFacets;
  213. theMesh.coords = (struct coord *)malloc(frames*numCoords*
  214. sizeof(struct coord));
  215. theMesh.facets = (struct facet *)malloc(frames*numFacets*
  216. sizeof(struct facet));
  217. if (theMesh.coords == NULL || theMesh.facets == NULL) {
  218. printf("Out of memory.\n");
  219. exit(1);
  220. }
  221. for (frameNum = 0; frameNum < frames; frameNum++) {
  222. for (i = 0; i <= widthX; i++) {
  223. x = i / (float)widthX;
  224. for (j = 0; j <= widthY; j++) {
  225. y = j / (float)widthY;
  226. d = sqrt(x*x+y*y);
  227. if (d == 0.0) {
  228. d = 0.0001;
  229. }
  230. angle = 2 * PI * d + (2 * PI / frames * frameNum);
  231. coord = GETCOORD(frameNum, i, j);
  232. coord->vertex[0] = x - 0.5;
  233. coord->vertex[1] = y - 0.5;
  234. coord->vertex[2] = (height - height * d) * cos(angle);
  235. coord->normal[0] = -(height / d) * x * ((1 - d) * 2 * PI *
  236. sin(angle) + cos(angle));
  237. coord->normal[1] = -(height / d) * y * ((1 - d) * 2 * PI *
  238. sin(angle) + cos(angle));
  239. coord->normal[2] = -1;
  240. d = 1.0 / sqrt(coord->normal[0]*coord->normal[0]+
  241. coord->normal[1]*coord->normal[1]+1);
  242. coord->normal[0] *= d;
  243. coord->normal[1] *= d;
  244. coord->normal[2] *= d;
  245. }
  246. }
  247. for (i = 0; i < widthX; i++) {
  248. for (j = 0; j < widthY; j++) {
  249. facet = GETFACET(frameNum, i, j);
  250. if (((i/checkerSize)%2)^(j/checkerSize)%2) {
  251. if (rgb) {
  252. facet->color[0] = 1.0;
  253. facet->color[1] = 0.2;
  254. facet->color[2] = 0.2;
  255. } else {
  256. facet->color[0] = colorIndexes1[0];
  257. facet->color[1] = colorIndexes1[1];
  258. facet->color[2] = colorIndexes1[2];
  259. }
  260. } else {
  261. if (rgb) {
  262. facet->color[0] = 0.2;
  263. facet->color[1] = 1.0;
  264. facet->color[2] = 0.2;
  265. } else {
  266. facet->color[0] = colorIndexes2[0];
  267. facet->color[1] = colorIndexes2[1];
  268. facet->color[2] = colorIndexes2[2];
  269. }
  270. }
  271. pt1 = GETCOORD(frameNum, i, j)->vertex;
  272. pt2 = GETCOORD(frameNum, i, j+1)->vertex;
  273. pt3 = GETCOORD(frameNum, i+1, j+1)->vertex;
  274. dp1[0] = pt2[0] - pt1[0];
  275. dp1[1] = pt2[1] - pt1[1];
  276. dp1[2] = pt2[2] - pt1[2];
  277. dp2[0] = pt3[0] - pt2[0];
  278. dp2[1] = pt3[1] - pt2[1];
  279. dp2[2] = pt3[2] - pt2[2];
  280. facet->normal[0] = dp1[1] * dp2[2] - dp1[2] * dp2[1];
  281. facet->normal[1] = dp1[2] * dp2[0] - dp1[0] * dp2[2];
  282. facet->normal[2] = dp1[0] * dp2[1] - dp1[1] * dp2[0];
  283. d = 1.0 / sqrt(facet->normal[0]*facet->normal[0]+
  284. facet->normal[1]*facet->normal[1]+
  285. facet->normal[2]*facet->normal[2]);
  286. facet->normal[0] *= d;
  287. facet->normal[1] *= d;
  288. facet->normal[2] *= d;
  289. }
  290. }
  291. }
  292. }
  293. static void InitMaterials(void)
  294. {
  295. static float ambient[] = {0.1, 0.1, 0.1, 1.0};
  296. static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
  297. static float position[] = {90.0, 90.0, 150.0, 0.0};
  298. static float front_mat_shininess[] = {60.0};
  299. static float front_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
  300. static float front_mat_diffuse[] = {0.5, 0.28, 0.38, 1.0};
  301. static float back_mat_shininess[] = {60.0};
  302. static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
  303. static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
  304. static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
  305. static float lmodel_twoside[] = {GL_TRUE};
  306. glMatrixMode(GL_PROJECTION);
  307. gluPerspective(90.0, 1.0, 0.5, 10.0);
  308. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  309. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  310. glLightfv(GL_LIGHT0, GL_POSITION, position);
  311. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  312. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  313. glEnable(GL_LIGHTING);
  314. glEnable(GL_LIGHT0);
  315. glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
  316. glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
  317. glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  318. glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);
  319. glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
  320. glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  321. if (rgb) {
  322. glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  323. }
  324. if (rgb) {
  325. glEnable(GL_COLOR_MATERIAL);
  326. } else {
  327. SetColorMap();
  328. }
  329. }
  330. static void InitTexture(void)
  331. {
  332. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  333. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  334. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  335. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  336. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  337. }
  338. static void Init(void)
  339. {
  340. glClearColor(0.0, 0.0, 0.0, 0.0);
  341. glShadeModel(GL_FLAT);
  342. glFrontFace(GL_CW);
  343. glEnable(GL_DEPTH_TEST);
  344. InitMaterials();
  345. InitTexture();
  346. InitMesh();
  347. glMatrixMode(GL_MODELVIEW);
  348. glTranslatef(0.0, 0.4, -1.8);
  349. glScalef(2.0, 2.0, 2.0);
  350. glRotatef(-35.0, 1.0, 0.0, 0.0);
  351. glRotatef(35.0, 0.0, 0.0, 1.0);
  352. }
  353. static void Reshape(int width, int height)
  354. {
  355. glViewport(0, 0, (GLint)width, (GLint)height);
  356. }
  357. static void Key(unsigned char key, int x, int y)
  358. {
  359. switch (key) {
  360. case 27:
  361. exit(1);
  362. case 'c':
  363. contouring++;
  364. if (contouring == 1) {
  365. static GLfloat map[4] = {0, 0, 20, 0};
  366. glTexImage2D(GL_TEXTURE_2D, 0, 3, 4, 4, 0, GL_LUMINANCE,
  367. GL_UNSIGNED_BYTE, (GLvoid *)contourTexture1);
  368. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  369. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  370. glTexGenfv(GL_S, GL_OBJECT_PLANE, map);
  371. glTexGenfv(GL_T, GL_OBJECT_PLANE, map);
  372. glEnable(GL_TEXTURE_2D);
  373. glEnable(GL_TEXTURE_GEN_S);
  374. glEnable(GL_TEXTURE_GEN_T);
  375. } else if (contouring == 2) {
  376. static GLfloat map[4] = {0, 0, 20, 0};
  377. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  378. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  379. glPushMatrix();
  380. glMatrixMode(GL_MODELVIEW);
  381. glLoadIdentity();
  382. glTexGenfv(GL_S, GL_EYE_PLANE, map);
  383. glTexGenfv(GL_T, GL_EYE_PLANE, map);
  384. glPopMatrix();
  385. } else {
  386. contouring = 0;
  387. glDisable(GL_TEXTURE_GEN_S);
  388. glDisable(GL_TEXTURE_GEN_T);
  389. glDisable(GL_TEXTURE_2D);
  390. }
  391. break;
  392. case 's':
  393. smooth = !smooth;
  394. if (smooth) {
  395. glShadeModel(GL_SMOOTH);
  396. } else {
  397. glShadeModel(GL_FLAT);
  398. }
  399. break;
  400. case 'l':
  401. lighting = !lighting;
  402. if (lighting) {
  403. glEnable(GL_LIGHTING);
  404. glEnable(GL_LIGHT0);
  405. if (rgb) {
  406. glEnable(GL_COLOR_MATERIAL);
  407. }
  408. } else {
  409. glDisable(GL_LIGHTING);
  410. glDisable(GL_LIGHT0);
  411. if (rgb) {
  412. glDisable(GL_COLOR_MATERIAL);
  413. }
  414. }
  415. break;
  416. case 'd':
  417. depth = !depth;
  418. if (depth) {
  419. glEnable(GL_DEPTH_TEST);
  420. clearMask |= GL_DEPTH_BUFFER_BIT;
  421. } else {
  422. glDisable(GL_DEPTH_TEST);
  423. clearMask &= ~GL_DEPTH_BUFFER_BIT;
  424. }
  425. break;
  426. case 32:
  427. stepMode = !stepMode;
  428. if (stepMode) {
  429. glutIdleFunc(0);
  430. } else {
  431. glutIdleFunc(glut_post_redisplay_p);
  432. }
  433. break;
  434. case 'n':
  435. if (stepMode) {
  436. nextFrame = 1;
  437. }
  438. break;
  439. case 'a':
  440. spinMode = !spinMode;
  441. break;
  442. default:
  443. return;
  444. }
  445. glutPostRedisplay();
  446. }
  447. static GLenum Args(int argc, char **argv)
  448. {
  449. GLint i;
  450. rgb = GL_TRUE;
  451. doubleBuffer = GL_TRUE;
  452. frames = 10;
  453. widthX = 10;
  454. widthY = 10;
  455. checkerSize = 2;
  456. height = 0.2;
  457. for (i = 1; i < argc; i++) {
  458. if (strcmp(argv[i], "-ci") == 0) {
  459. rgb = GL_FALSE;
  460. } else if (strcmp(argv[i], "-rgb") == 0) {
  461. rgb = GL_TRUE;
  462. } else if (strcmp(argv[i], "-sb") == 0) {
  463. doubleBuffer = GL_FALSE;
  464. } else if (strcmp(argv[i], "-db") == 0) {
  465. doubleBuffer = GL_TRUE;
  466. } else if (strcmp(argv[i], "-grid") == 0) {
  467. if (i+2 >= argc || argv[i+1][0] == '-' || argv[i+2][0] == '-') {
  468. printf("-grid (No numbers).\n");
  469. return GL_FALSE;
  470. } else {
  471. widthX = atoi(argv[++i]);
  472. widthY = atoi(argv[++i]);
  473. }
  474. } else if (strcmp(argv[i], "-size") == 0) {
  475. if (i+1 >= argc || argv[i+1][0] == '-') {
  476. printf("-checker (No number).\n");
  477. return GL_FALSE;
  478. } else {
  479. checkerSize = atoi(argv[++i]);
  480. }
  481. } else if (strcmp(argv[i], "-wave") == 0) {
  482. if (i+1 >= argc || argv[i+1][0] == '-') {
  483. printf("-wave (No number).\n");
  484. return GL_FALSE;
  485. } else {
  486. height = atof(argv[++i]);
  487. }
  488. } else if (strcmp(argv[i], "-frames") == 0) {
  489. if (i+1 >= argc || argv[i+1][0] == '-') {
  490. printf("-frames (No number).\n");
  491. return GL_FALSE;
  492. } else {
  493. frames = atoi(argv[++i]);
  494. }
  495. } else {
  496. printf("%s (Bad option).\n", argv[i]);
  497. return GL_FALSE;
  498. }
  499. }
  500. return GL_TRUE;
  501. }
  502. int main(int argc, char **argv)
  503. {
  504. GLenum type;
  505. glutInit(&argc, argv);
  506. if (Args(argc, argv) == GL_FALSE) {
  507. exit(1);
  508. }
  509. glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300);
  510. type = GLUT_DEPTH;
  511. type |= (rgb) ? GLUT_RGB : GLUT_INDEX;
  512. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  513. glutInitDisplayMode(type);
  514. if (glutCreateWindow("Wave Demo") == GL_FALSE) {
  515. exit(1);
  516. }
  517. InitMap();
  518. Init();
  519. glutReshapeFunc(Reshape);
  520. glutKeyboardFunc(Key);
  521. glutDisplayFunc(Animate);
  522. glutIdleFunc(glut_post_redisplay_p);
  523. glutMainLoop();
  524. return 0;
  525. }