draw_polys_ogl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. #include <grass/gis.h>
  2. #include "vizual.h"
  3. /*
  4. ** the corner of the cube
  5. **
  6. **
  7. */
  8. /* this subroutine draws polygons from dspf file using flat shading */
  9. static float ZNexag = 1.0;
  10. void fill_data_cube();
  11. void get_vert_color();
  12. void set_ZNexag(exag)
  13. float exag;
  14. {
  15. ZNexag = exag;
  16. }
  17. void get_ZNexag(exag)
  18. float *exag;
  19. {
  20. *exag = ZNexag;
  21. }
  22. void fdraw_polys(D_spec)
  23. struct dspec *D_spec; /*structure containing interactive input */
  24. {
  25. int x, y, z;
  26. int t, p; /* LOOP COUNTER */
  27. double xadd, yadd, zadd;
  28. poly_info *Polyfax; /* local pointer */
  29. float tmp_vect[3];
  30. Cube_data Cube; /*structure containing poly info */
  31. cube_info *cubefax; /* local pointer */
  32. int curr;
  33. short color[3];
  34. cubefax = Cube.data;
  35. glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  36. glEnable(GL_COLOR_MATERIAL);
  37. t = D_spec->Thresh;
  38. for (z = 0; z < Headfax.zdim; z++) {
  39. zadd = z * Headfax.ydim;
  40. for (y = 0; y < Headfax.ydim; y++) {
  41. yadd = y * D_spec->yscale;
  42. for (x = 0; x < Headfax.xdim; x++) {
  43. if (!read_cube(&Cube, &Headfax))
  44. continue;
  45. if (!((x > (D_spec->B[X] - 1)) && (x < D_spec->E[X]) &&
  46. (y > (D_spec->B[Y] - 1)) && (y < D_spec->E[Y]) &&
  47. (z > (D_spec->B[Z] - 1)) && (z < D_spec->E[Z])))
  48. continue;
  49. xadd = x * D_spec->xscale;
  50. for (t = 0; t < Cube.n_thresh; t++) {
  51. if (cubefax[t].t_ndx == D_spec->Thresh) {
  52. curr = D_spec->Thresh;
  53. get_cat_color(Headfax.linefax.tvalue[curr],
  54. D_spec->ctable, color);
  55. glColor3sv(color);
  56. for (p = 0; p < cubefax[t].npoly; p++) {
  57. /* center data */
  58. Polyfax = &(cubefax[t].poly[p]);
  59. glBegin(GL_POLYGON);
  60. tmp_vect[0] =
  61. X_sign * G_sign * (Polyfax->n1[0] / 127. -
  62. 1.);
  63. tmp_vect[1] =
  64. X_sign * G_sign * (Polyfax->n1[1] / 127. -
  65. 1.);
  66. tmp_vect[2] =
  67. X_sign * G_sign * (Polyfax->n1[2] / 127. -
  68. 1.);
  69. glNormal3fv(tmp_vect);
  70. tmp_vect[0] =
  71. Polyfax->v1[0] / 255. * D_spec->xscale + xadd;
  72. tmp_vect[1] =
  73. Polyfax->v1[1] / 255. * D_spec->yscale + yadd;
  74. tmp_vect[2] =
  75. Polyfax->v1[2] / 255. * D_spec->zscale + zadd;
  76. glVertex3fv(tmp_vect);
  77. tmp_vect[0] =
  78. Polyfax->v2[0] / 255. * D_spec->xscale + xadd;
  79. tmp_vect[1] =
  80. Polyfax->v2[1] / 255. * D_spec->yscale + yadd;
  81. tmp_vect[2] =
  82. Polyfax->v2[2] / 255. * D_spec->zscale + zadd;
  83. glVertex3fv(tmp_vect);
  84. tmp_vect[0] =
  85. Polyfax->v3[0] / 255. * D_spec->xscale + xadd;
  86. tmp_vect[1] =
  87. Polyfax->v3[1] / 255. * D_spec->yscale + yadd;
  88. tmp_vect[2] =
  89. Polyfax->v3[2] / 255. * D_spec->zscale + zadd;
  90. glVertex3fv(tmp_vect);
  91. glEnd();
  92. }
  93. break; /* dont bother w/ other thresholds then */
  94. }
  95. }
  96. }
  97. }
  98. }
  99. glDisable(GL_COLOR_MATERIAL);
  100. }
  101. void normalize(v)
  102. float v[];
  103. {
  104. float len;
  105. len = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
  106. v[0] /= len;
  107. v[1] /= len;
  108. v[2] /= len;
  109. }
  110. /******************************** gdraw_polys *********************************/
  111. /* this subroutine draws polygons from a dspf file using 3 different normals */
  112. void gdraw_polys(D_spec)
  113. struct dspec *D_spec;
  114. {
  115. int x, y, z;
  116. int t, p; /* LOOP COUNTER */
  117. float xadd, yadd, zadd;
  118. poly_info *Polyfax; /* local pointer */
  119. cube_info *cubefax; /* local pointer */
  120. int curr;
  121. float xres, yres, zres, norm[3];
  122. Cube_data Cube; /*structure containing poly info */
  123. file_info chead;
  124. int color_on = 0;
  125. int xdim = 0, ydim = 0;
  126. float *slice[2];
  127. float *tmp;
  128. int i;
  129. int level, row[2], col[2];
  130. short color[3];
  131. short data[8][3];
  132. glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  133. glEnable(GL_COLOR_MATERIAL);
  134. /* a color data file has been requested */
  135. if (D_spec->cfile != NULL) {
  136. chead.datainfp = D_spec->cfile;
  137. /* TO CHANGE
  138. if (r3read_header (&chead) < 0)
  139. fprintf (stderr, "Can't read color file\n");
  140. else
  141. color_on = 1;
  142. */
  143. }
  144. if (color_on) {
  145. xdim = chead.xdim;
  146. ydim = chead.ydim;
  147. for (i = 0; i < 2; i++) {
  148. if ((slice[i] =
  149. (float *)G_malloc(sizeof(float) * xdim * ydim)) == NULL) {
  150. fprintf(stderr, "error in allocating memory\n");
  151. fprintf(stderr, "unable to use colortable\n");
  152. xdim = ydim = 0;
  153. color_on = 0;
  154. break;
  155. }
  156. }
  157. }
  158. curr = D_spec->Thresh;
  159. if (color_on) {
  160. level = get_level(&Headfax, &chead, 0);
  161. row[1] = get_row(&Headfax, &chead, 0);
  162. col[1] = get_col(&Headfax, &chead, 0);
  163. if (level < 0 || row[1] < 0 || col[1] < 0) {
  164. fprintf(stderr, "Bounds of colorfile do not match data file\n");
  165. color_on = 0;
  166. }
  167. else
  168. r3read_level(&chead, slice[1], level); /*read in data */
  169. }
  170. if (!color_on) {
  171. get_cat_color(Headfax.linefax.tvalue[curr], D_spec->ctable, color);
  172. glColor3sv(color);
  173. glColor3ub(color[0], color[1], color[2]);
  174. }
  175. xres = D_spec->xscale;
  176. yres = D_spec->yscale;
  177. zres = D_spec->zscale * ZNexag;
  178. cubefax = Cube.data;
  179. for (z = 0; z < Headfax.zdim; z++) {
  180. zadd = z * D_spec->zscale;
  181. if (color_on) {
  182. if (0 > (level = get_level(&Headfax, &chead, z + 1))) {
  183. fprintf(stderr, "LEVEL out of bounds for z = %d\n", z);
  184. /*
  185. color_on = 0;
  186. glColor3ub(0xff,0xff,0xff);
  187. */
  188. }
  189. else {
  190. tmp = slice[0];
  191. slice[0] = slice[1];
  192. slice[1] = tmp;
  193. r3read_level(&chead, slice[1], level); /*read in data */
  194. }
  195. }
  196. for (y = 0; y < Headfax.ydim; y++) {
  197. yadd = y * D_spec->yscale;
  198. if (color_on) {
  199. if (y)
  200. row[0] = row[1];
  201. else
  202. row[0] = get_row(&Headfax, &chead, 0);
  203. if (0 > (row[1] = get_row(&Headfax, &chead, y + 1))) {
  204. row[1] = row[0];
  205. fprintf(stderr, " ROW out of bounds for y = %d\n", y);
  206. /*
  207. color_on = 0;
  208. glColor3ub(0xff,0xff,0xff);
  209. */
  210. }
  211. }
  212. for (x = 0; x < Headfax.xdim; x++) {
  213. if (color_on) {
  214. if (x)
  215. col[0] = col[1];
  216. else
  217. col[1] = get_col(&Headfax, &chead, 0);
  218. if (0 > (col[1] = get_col(&Headfax, &chead, x + 1))) {
  219. col[1] = col[0];
  220. fprintf(stderr, " COL out of bounds for x = %d\n", x);
  221. /*
  222. color_on = 0;
  223. glColor3ub(0xff,0xff,0xff);
  224. */
  225. }
  226. }
  227. if (!read_cube(&Cube, &Headfax))
  228. continue;
  229. if (!((x > (D_spec->B[X] - 1)) && (x < D_spec->E[X]) &&
  230. (y > (D_spec->B[Y] - 1)) && (y < D_spec->E[Y]) &&
  231. (z > (D_spec->B[Z] - 1)) && (z < D_spec->E[Z])))
  232. continue;
  233. xadd = x * D_spec->xscale;
  234. for (t = 0; t < Cube.n_thresh; t++) {
  235. if (cubefax[t].t_ndx == D_spec->Thresh) {
  236. if (D_spec->in_out == INSIDE) {
  237. if (D_spec->Thresh == D_spec->low) {
  238. G_sign = -1;
  239. }
  240. else {
  241. G_sign = 1;
  242. }
  243. }
  244. else { /* outside */
  245. if (D_spec->Thresh == D_spec->low) {
  246. G_sign = 1;
  247. }
  248. else {
  249. G_sign = -1;
  250. }
  251. /* TODO. What happens for outside, w/ Thresh == max thresh? */
  252. }
  253. if (color_on && cubefax[t].npoly)
  254. fill_data_cube(data, slice, row, col,
  255. D_spec->ctable, xdim);
  256. for (p = 0; p < cubefax[t].npoly; p++) {
  257. /* center data */
  258. Polyfax = &(cubefax[t].poly[p]);
  259. {
  260. glBegin(GL_POLYGON);
  261. Polyfax->v1[0] = Polyfax->v1[0] / 255.0;
  262. Polyfax->v1[1] = Polyfax->v1[1] / 255.0;
  263. Polyfax->v1[2] = Polyfax->v1[2] / 255.0;
  264. if (color_on) {
  265. get_vert_color
  266. (data, Polyfax->v1, D_spec->ctable,
  267. color);
  268. glColor3sv(color);
  269. }
  270. norm[0] =
  271. (X_sign * G_sign *
  272. (Polyfax->n1[0] / 127. - 1.)) / xres;
  273. norm[1] =
  274. (X_sign * G_sign *
  275. (Polyfax->n1[1] / 127. - 1.)) / yres;
  276. norm[2] =
  277. (X_sign * G_sign *
  278. (Polyfax->n1[2] / 127. - 1.)) / zres;
  279. normalize(norm);
  280. glNormal3fv(norm);
  281. Polyfax->v1[0] =
  282. Polyfax->v1[0] * D_spec->xscale + xadd;
  283. Polyfax->v1[1] =
  284. Polyfax->v1[1] * D_spec->yscale + yadd;
  285. Polyfax->v1[2] =
  286. Polyfax->v1[2] * D_spec->zscale + zadd;
  287. glVertex3fv(Polyfax->v1);
  288. Polyfax->v2[0] = Polyfax->v2[0] / 255.0;
  289. Polyfax->v2[1] = Polyfax->v2[1] / 255.0;
  290. Polyfax->v2[2] = Polyfax->v2[2] / 255.0;
  291. if (color_on) {
  292. get_vert_color
  293. (D_spec, data, Polyfax->v2,
  294. D_spec->ctable, color);
  295. glColor3sv(color);
  296. }
  297. norm[0] =
  298. (X_sign * G_sign *
  299. (Polyfax->n2[0] / 127. - 1.)) / xres;
  300. norm[1] =
  301. (X_sign * G_sign *
  302. (Polyfax->n2[1] / 127. - 1.)) / yres;
  303. norm[2] =
  304. (X_sign * G_sign *
  305. (Polyfax->n2[2] / 127. - 1.)) / zres;
  306. normalize(norm);
  307. glNormal3fv(norm);
  308. Polyfax->v2[0] =
  309. Polyfax->v2[0] * D_spec->xscale + xadd;
  310. Polyfax->v2[1] =
  311. Polyfax->v2[1] * D_spec->yscale + yadd;
  312. Polyfax->v2[2] =
  313. Polyfax->v2[2] * D_spec->zscale + zadd;
  314. glVertex3fv(Polyfax->v2);
  315. Polyfax->v3[1] = Polyfax->v3[1] / 255.0;
  316. Polyfax->v3[2] = Polyfax->v3[2] / 255.0;
  317. Polyfax->v3[0] = Polyfax->v3[0] / 255.0;
  318. if (color_on) {
  319. get_vert_color
  320. (D_spec, data, Polyfax->v3,
  321. D_spec->ctable, color);
  322. glColor3sv(color);
  323. }
  324. norm[0] =
  325. (X_sign * G_sign *
  326. (Polyfax->n3[0] / 127. - 1.)) / xres;
  327. norm[1] =
  328. (X_sign * G_sign *
  329. (Polyfax->n3[1] / 127. - 1.)) / yres;
  330. norm[2] =
  331. (X_sign * G_sign *
  332. (Polyfax->n3[2] / 127. - 1.)) / zres;
  333. normalize(norm);
  334. glNormal3fv(norm);
  335. Polyfax->v3[0] =
  336. Polyfax->v3[0] * D_spec->xscale + xadd;
  337. Polyfax->v3[1] =
  338. Polyfax->v3[1] * D_spec->yscale + yadd;
  339. Polyfax->v3[2] =
  340. Polyfax->v3[2] * D_spec->zscale + zadd;
  341. glVertex3fv(Polyfax->v3);
  342. glEnd();
  343. }
  344. }
  345. break;
  346. }
  347. }
  348. }
  349. }
  350. }
  351. if (color_on) {
  352. G_free(slice[0]);
  353. G_free(slice[1]);
  354. }
  355. glDisable(GL_COLOR_MATERIAL);
  356. }
  357. int get_level(head, chead, z)
  358. file_info *head, *chead;
  359. int z;
  360. {
  361. int level;
  362. level = (z * head->tb_res + head->bottom - chead->bottom)
  363. / chead->tb_res;
  364. if (level < 0 || level >= chead->zdim)
  365. level = -1;
  366. return (level);
  367. }
  368. int get_row(head, chead, y)
  369. file_info *head, *chead;
  370. int y;
  371. {
  372. int row;
  373. row = (y * head->ns_res + head->south - chead->south)
  374. / chead->ns_res;
  375. if (row < 0 || row >= chead->ydim)
  376. row = -1;
  377. return (row);
  378. }
  379. int get_col(head, chead, x)
  380. file_info *head, *chead;
  381. int x;
  382. {
  383. int col;
  384. col = (x * head->ew_res + head->west - chead->west)
  385. / chead->ew_res;
  386. if (col < 0 || col >= chead->xdim)
  387. col = -1;
  388. return (col);
  389. }
  390. void fill_data_cube(data, slice, row, col, ctable, xdim)
  391. short data[8][3];
  392. float *slice[2];
  393. int row[2], col[2];
  394. struct color_entry *ctable;
  395. int xdim;
  396. {
  397. int x, y, z;
  398. int i = 0;
  399. float cat, *tmp;
  400. for (x = 0; x < 2; x++)
  401. for (y = 0; y < 2; y++)
  402. for (z = 0; z < 2; z++) {
  403. tmp = slice[z];
  404. cat = *(tmp + row[y] * xdim + col[x]);
  405. get_cat_color(cat, ctable, data[i]);
  406. i++;
  407. }
  408. }
  409. void get_vert_color(data, vert, ctable, color)
  410. short data[8][3];
  411. float vert[3];
  412. struct color_entry *ctable;
  413. short color[3];
  414. {
  415. short x[4][3], y[2][3];
  416. float dx, dy, dz;
  417. int i, j;
  418. dx = vert[0];
  419. dy = vert[1];
  420. dz = vert[2];
  421. if (dx == 0.0)
  422. for (i = 0; i < 4; i++)
  423. for (j = 0; j < 3; j++)
  424. x[i][j] = data[i][j];
  425. else if (dx == 1.0)
  426. for (i = 0; i < 4; i++)
  427. for (j = 0; j < 3; j++)
  428. x[i][j] = data[i + 4][j];
  429. else
  430. for (i = 0; i < 4; i++)
  431. for (j = 0; j < 3; j++)
  432. x[i][j] = (data[i][j] * (1 - dx) + data[i + 4][j] * dx);
  433. if (dy == 0.0)
  434. for (j = 0; j < 3; j++) {
  435. y[0][j] = x[0][j];
  436. y[1][j] = x[1][j];
  437. }
  438. if (dy == 1.0)
  439. for (j = 0; j < 3; j++) {
  440. y[0][j] = x[2][j];
  441. y[1][j] = x[3][j];
  442. }
  443. else
  444. for (i = 0; i < 2; i++)
  445. for (j = 0; j < 3; j++) {
  446. y[i][j] = (x[i][j] * (1 - dy) + x[i + 2][j] * dy);
  447. }
  448. if (dz == 0.0)
  449. for (j = 0; j < 3; j++)
  450. color[j] = y[0][j];
  451. else if (dz == 1.0)
  452. for (j = 0; j < 3; j++)
  453. color[j] = y[1][j];
  454. else
  455. for (j = 0; j < 3; j++)
  456. color[j] = (y[0][j] * (1 - dz) + y[1][j] * dz);
  457. }
  458. void print_color_table(ctable)
  459. struct color_entry ctable[];
  460. {
  461. int i;
  462. for (i = 0; ctable[i].color[0] > 0; i++) {
  463. fprintf(stderr, "%f:%hd:%hd:%hd",
  464. ctable[i].data,
  465. ctable[i].color[0], ctable[i].color[1], ctable[i].color[2]);
  466. }
  467. }