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.

eval.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. #define VORDER 10
  30. #define CORDER 10
  31. #define TORDER 3
  32. #define VMAJOR_ORDER 2
  33. #define VMINOR_ORDER 3
  34. #define CMAJOR_ORDER 2
  35. #define CMINOR_ORDER 2
  36. #define TMAJOR_ORDER 2
  37. #define TMINOR_ORDER 2
  38. #define VDIM 4
  39. #define CDIM 4
  40. #define TDIM 2
  41. #define ONE_D 1
  42. #define TWO_D 2
  43. #define EVAL 3
  44. #define MESH 4
  45. GLenum doubleBuffer;
  46. float rotX = 0.0, rotY = 0.0, translateZ = -1.0;
  47. GLenum arrayType = ONE_D;
  48. GLenum colorType = GL_FALSE;
  49. GLenum textureType = GL_FALSE;
  50. GLenum polygonFilled = GL_FALSE;
  51. GLenum lighting = GL_FALSE;
  52. GLenum mapPoint = GL_FALSE;
  53. GLenum mapType = EVAL;
  54. double point1[10*4] = {
  55. -0.5, 0.0, 0.0, 1.0,
  56. -0.4, 0.5, 0.0, 1.0,
  57. -0.3,-0.5, 0.0, 1.0,
  58. -0.2, 0.5, 0.0, 1.0,
  59. -0.1,-0.5, 0.0, 1.0,
  60. 0.0, 0.5, 0.0, 1.0,
  61. 0.1,-0.5, 0.0, 1.0,
  62. 0.2, 0.5, 0.0, 1.0,
  63. 0.3,-0.5, 0.0, 1.0,
  64. 0.4, 0.0, 0.0, 1.0,
  65. };
  66. double cpoint1[10*4] = {
  67. 0.0, 0.0, 1.0, 1.0,
  68. 0.3, 0.0, 0.7, 1.0,
  69. 0.6, 0.0, 0.3, 1.0,
  70. 1.0, 0.0, 0.0, 1.0,
  71. 1.0, 0.3, 0.0, 1.0,
  72. 1.0, 0.6, 0.0, 1.0,
  73. 1.0, 1.0, 0.0, 1.0,
  74. 1.0, 1.0, 0.5, 1.0,
  75. 1.0, 1.0, 1.0, 1.0,
  76. };
  77. double tpoint1[11*4] = {
  78. 0.0, 0.0, 0.0, 1.0,
  79. 0.0, 0.1, 0.0, 1.0,
  80. 0.0, 0.2, 0.0, 1.0,
  81. 0.0, 0.3, 0.0, 1.0,
  82. 0.0, 0.4, 0.0, 1.0,
  83. 0.0, 0.5, 0.0, 1.0,
  84. 0.0, 0.6, 0.0, 1.0,
  85. 0.0, 0.7, 0.0, 1.0,
  86. 0.0, 0.8, 0.0, 1.0,
  87. 0.0, 0.9, 0.0, 1.0,
  88. };
  89. double point2[2*3*4] = {
  90. -0.5, -0.5, 0.5, 1.0,
  91. 0.0, 1.0, 0.5, 1.0,
  92. 0.5, -0.5, 0.5, 1.0,
  93. -0.5, 0.5, -0.5, 1.0,
  94. 0.0, -1.0, -0.5, 1.0,
  95. 0.5, 0.5, -0.5, 1.0,
  96. };
  97. double cpoint2[2*2*4] = {
  98. 0.0, 0.0, 0.0, 1.0,
  99. 0.0, 0.0, 1.0, 1.0,
  100. 0.0, 1.0, 0.0, 1.0,
  101. 1.0, 1.0, 1.0, 1.0,
  102. };
  103. double tpoint2[2*2*2] = {
  104. 0.0, 0.0, 0.0, 1.0,
  105. 1.0, 0.0, 1.0, 1.0,
  106. };
  107. float textureImage[4*2*4] = {
  108. 1.0, 1.0, 1.0, 1.0,
  109. 1.0, 0.0, 0.0, 1.0,
  110. 1.0, 0.0, 0.0, 1.0,
  111. 1.0, 1.0, 1.0, 1.0,
  112. 1.0, 1.0, 1.0, 1.0,
  113. 1.0, 0.0, 0.0, 1.0,
  114. 1.0, 0.0, 0.0, 1.0,
  115. 1.0, 1.0, 1.0, 1.0,
  116. };
  117. static void Init(void)
  118. {
  119. static float ambient[] = {0.1, 0.1, 0.1, 1.0};
  120. static float diffuse[] = {1.0, 1.0, 1.0, 1.0};
  121. static float position[] = {0.0, 0.0, -150.0, 0.0};
  122. static float front_mat_diffuse[] = {1.0, 0.2, 1.0, 1.0};
  123. static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
  124. static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
  125. static float lmodel_twoside[] = {GL_TRUE};
  126. static float decal[] = {GL_DECAL};
  127. static float repeat[] = {GL_REPEAT};
  128. static float nr[] = {GL_NEAREST};
  129. glFrontFace(GL_CCW);
  130. glEnable(GL_DEPTH_TEST);
  131. glMap1d(GL_MAP1_VERTEX_4, 0.0, 1.0, VDIM, VORDER, point1);
  132. glMap1d(GL_MAP1_COLOR_4, 0.0, 1.0, CDIM, CORDER, cpoint1);
  133. glMap2d(GL_MAP2_VERTEX_4, 0.0, 1.0, VMINOR_ORDER*VDIM, VMAJOR_ORDER, 0.0,
  134. 1.0, VDIM, VMINOR_ORDER, point2);
  135. glMap2d(GL_MAP2_COLOR_4, 0.0, 1.0, CMINOR_ORDER*CDIM, CMAJOR_ORDER, 0.0,
  136. 1.0, CDIM, CMINOR_ORDER, cpoint2);
  137. glMap2d(GL_MAP2_TEXTURE_COORD_2, 0.0, 1.0, TMINOR_ORDER*TDIM,
  138. TMAJOR_ORDER, 0.0, 1.0, TDIM, TMINOR_ORDER, tpoint2);
  139. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  140. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  141. glLightfv(GL_LIGHT0, GL_POSITION, position);
  142. glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  143. glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  144. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  145. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  146. glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal);
  147. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeat);
  148. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeat);
  149. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, nr);
  150. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, nr);
  151. glTexImage2D(GL_TEXTURE_2D, 0, 4, 2, 4, 0, GL_RGBA, GL_FLOAT,
  152. (GLvoid *)textureImage);
  153. }
  154. static void DrawPoints1(void)
  155. {
  156. GLint i;
  157. glColor3f(0.0, 1.0, 0.0);
  158. glPointSize(2);
  159. glBegin(GL_POINTS);
  160. for (i = 0; i < VORDER; i++) {
  161. glVertex4dv(&point1[i*4]);
  162. }
  163. glEnd();
  164. }
  165. static void DrawPoints2(void)
  166. {
  167. GLint i, j;
  168. glColor3f(1.0, 0.0, 1.0);
  169. glPointSize(2);
  170. glBegin(GL_POINTS);
  171. for (i = 0; i < VMAJOR_ORDER; i++) {
  172. for (j = 0; j < VMINOR_ORDER; j++) {
  173. glVertex4dv(&point2[i*4*VMINOR_ORDER+j*4]);
  174. }
  175. }
  176. glEnd();
  177. }
  178. static void DrawMapEval1(float du)
  179. {
  180. float u;
  181. glColor3f(1.0, 0.0, 0.0);
  182. glBegin(GL_LINE_STRIP);
  183. for (u = 0.0; u < 1.0; u += du) {
  184. glEvalCoord1d(u);
  185. }
  186. glEvalCoord1d(1.0);
  187. glEnd();
  188. }
  189. static void DrawMapEval2(float du, float dv)
  190. {
  191. float u, v, tmp;
  192. glColor3f(1.0, 0.0, 0.0);
  193. for (v = 0.0; v < 1.0; v += dv) {
  194. glBegin(GL_QUAD_STRIP);
  195. for (u = 0.0; u <= 1.0; u += du) {
  196. glEvalCoord2d(u,v);
  197. tmp = (v + dv < 1.0) ? (v + dv) : 1.0;
  198. glEvalCoord2d(u, tmp);
  199. }
  200. glEvalCoord2d(1.0, v);
  201. glEvalCoord2d(1.0, v+dv);
  202. glEnd();
  203. }
  204. }
  205. static void RenderEval(void)
  206. {
  207. if (colorType) {
  208. glEnable(GL_MAP1_COLOR_4);
  209. glEnable(GL_MAP2_COLOR_4);
  210. } else {
  211. glDisable(GL_MAP1_COLOR_4);
  212. glDisable(GL_MAP2_COLOR_4);
  213. }
  214. if (textureType) {
  215. glEnable(GL_TEXTURE_2D);
  216. glEnable(GL_MAP2_TEXTURE_COORD_2);
  217. } else {
  218. glDisable(GL_TEXTURE_2D);
  219. glDisable(GL_MAP2_TEXTURE_COORD_2);
  220. }
  221. if (polygonFilled) {
  222. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  223. } else {
  224. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  225. }
  226. glShadeModel(GL_SMOOTH);
  227. switch (mapType) {
  228. case EVAL:
  229. switch (arrayType) {
  230. case ONE_D:
  231. glDisable(GL_MAP2_VERTEX_4);
  232. glEnable(GL_MAP1_VERTEX_4);
  233. DrawPoints1();
  234. DrawMapEval1(0.1/VORDER);
  235. break;
  236. case TWO_D:
  237. glDisable(GL_MAP1_VERTEX_4);
  238. glEnable(GL_MAP2_VERTEX_4);
  239. DrawPoints2();
  240. DrawMapEval2(0.1/VMAJOR_ORDER,0.1/VMINOR_ORDER);
  241. break;
  242. default:
  243. break;
  244. }
  245. break;
  246. case MESH:
  247. switch (arrayType) {
  248. case ONE_D:
  249. DrawPoints1();
  250. glDisable(GL_MAP2_VERTEX_4);
  251. glEnable (GL_MAP1_VERTEX_4);
  252. glColor3f(0.0, 0.0, 1.0);
  253. glMapGrid1d(40, 0.0, 1.0);
  254. if (mapPoint) {
  255. glPointSize(2);
  256. glEvalMesh1(GL_POINT, 0, 40);
  257. } else {
  258. glEvalMesh1(GL_LINE, 0, 40);
  259. }
  260. break;
  261. case TWO_D:
  262. DrawPoints2();
  263. glDisable(GL_MAP1_VERTEX_4);
  264. glEnable(GL_MAP2_VERTEX_4);
  265. glColor3f(0.0, 0.0, 1.0);
  266. glMapGrid2d(20, 0.0, 1.0, 20, 0.0, 1.0);
  267. if (mapPoint) {
  268. glPointSize(2);
  269. glEvalMesh2(GL_POINT, 0, 20, 0, 20);
  270. } else if (polygonFilled) {
  271. glEvalMesh2(GL_FILL, 0, 20, 0, 20);
  272. } else {
  273. glEvalMesh2(GL_LINE, 0, 20, 0, 20);
  274. }
  275. break;
  276. default:
  277. break;
  278. }
  279. break;
  280. default:
  281. break;
  282. }
  283. }
  284. static void Reshape(int width, int height)
  285. {
  286. glViewport(0, 0, (GLint)width, (GLint)height);
  287. glMatrixMode(GL_PROJECTION);
  288. glLoadIdentity();
  289. glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 10.0);
  290. glMatrixMode(GL_MODELVIEW);
  291. }
  292. static void Key2(int key, int x, int y)
  293. {
  294. switch (key) {
  295. case GLUT_KEY_LEFT:
  296. rotY -= 30;
  297. break;
  298. case GLUT_KEY_RIGHT:
  299. rotY += 30;
  300. break;
  301. case GLUT_KEY_UP:
  302. rotX -= 30;
  303. break;
  304. case GLUT_KEY_DOWN:
  305. rotX += 30;
  306. break;
  307. default:
  308. return;
  309. }
  310. glutPostRedisplay();
  311. }
  312. static void Key(unsigned char key, int x, int y)
  313. {
  314. switch (key) {
  315. case 27:
  316. exit(1);
  317. case '1':
  318. arrayType = ONE_D;
  319. break;
  320. case '2':
  321. arrayType = TWO_D;
  322. break;
  323. case 'e':
  324. mapType = EVAL;
  325. break;
  326. case 'm':
  327. mapType = MESH;
  328. break;
  329. case 'f':
  330. polygonFilled = !polygonFilled;
  331. break;
  332. case 'p':
  333. mapPoint = !mapPoint;
  334. break;
  335. case 'c':
  336. colorType = !colorType;
  337. break;
  338. case 't':
  339. textureType = !textureType;
  340. break;
  341. case 'l':
  342. lighting =! lighting;
  343. if (lighting) {
  344. glEnable(GL_LIGHTING);
  345. glEnable(GL_LIGHT0);
  346. glEnable(GL_AUTO_NORMAL);
  347. } else {
  348. glDisable(GL_LIGHTING);
  349. glDisable(GL_LIGHT0);
  350. glDisable(GL_AUTO_NORMAL);
  351. }
  352. break;
  353. default:
  354. return;
  355. }
  356. glutPostRedisplay();
  357. }
  358. static void Draw(void)
  359. {
  360. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  361. glPushMatrix();
  362. glTranslatef(0.0, 0.0 , translateZ);
  363. glRotatef(rotX, 1, 0, 0);
  364. glRotatef(rotY, 0, 1, 0);
  365. RenderEval();
  366. glPopMatrix();
  367. glFlush();
  368. if (doubleBuffer) {
  369. glutSwapBuffers();
  370. }
  371. }
  372. static GLenum Args(int argc, char **argv)
  373. {
  374. GLint i;
  375. doubleBuffer = GL_FALSE;
  376. for (i = 1; i < argc; i++) {
  377. if (strcmp(argv[i], "-sb") == 0) {
  378. doubleBuffer = GL_FALSE;
  379. } else if (strcmp(argv[i], "-db") == 0) {
  380. doubleBuffer = GL_TRUE;
  381. } else {
  382. printf("%s (Bad option).\n", argv[i]);
  383. return GL_FALSE;
  384. }
  385. }
  386. return GL_TRUE;
  387. }
  388. int main(int argc, char **argv)
  389. {
  390. GLenum type;
  391. glutInit(&argc, argv);
  392. if (Args(argc, argv) == GL_FALSE) {
  393. exit(1);
  394. }
  395. glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300);
  396. type = GLUT_RGB | GLUT_DEPTH;
  397. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  398. glutInitDisplayMode(type);
  399. if (glutCreateWindow("Evaluator Test") == GL_FALSE) {
  400. exit(1);
  401. }
  402. Init();
  403. glutReshapeFunc(Reshape);
  404. glutKeyboardFunc(Key);
  405. glutSpecialFunc(Key2);
  406. glutDisplayFunc(Draw);
  407. glutMainLoop();
  408. return 0;
  409. }