123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376 |
- /*!
- \file lib/ogsf/gsd_surf.c
- \brief OGSF library - loading and manipulating surfaces
- GRASS OpenGL gsurf OGSF Library
- (C) 1999-2008 by the GRASS Development Team
- This program is free software under the
- GNU General Public License (>=v2).
- Read the file COPYING that comes with GRASS
- for details.
- \author Bill Brown USACERL (October 1993)
- \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
- */
- #include <stdlib.h>
- #include <grass/gis.h>
- #include <grass/glocale.h>
- #include <grass/ogsf.h>
- #include "gsget.h"
- #include "rowcol.h"
- /*
- #define CALC_AREA
- */
- /*
- #define DO_ARROW_SOLID
- #define DEBUG_ARROW ((row && !(row%surf->y_modw))&&(col && !(col%surf->x_modw)))
- */
- /*
- #define DO_ARROW
- */
- #define DEBUG_ARROW (0)
- /*!
- \brief MACROS for use in gsd_ortho_wall ONLY !!!
- */
- #define SET_SCOLOR(sf) \
- if(check_color[sf]) \
- { \
- tx = points[sf][i][X] - gsurfs[sf]->x_trans; \
- ty = points[sf][i][Y] - gsurfs[sf]->y_trans; \
- offset = XY2OFF(gsurfs[sf], tx, ty); \
- colors[sf] = gs_mapcolor(cobuf[sf], coloratt[sf], offset); \
- }
- static int transpoint_is_masked(geosurf *, Point3);
- static int get_point_below(Point3 **, geosurf **, int, int, int, int *);
- static int FCmode;
- /************************************************************************/
- /* Notes on exageration:
- vertical exageration is of two forms:
- 1) global exageration (from geoview struct)
- 2) vertical exageration for each surface (UN-IMPLEMENTED)
- */
- /************************************************************************/
- /* may need to add more parameters to tell it which window or off_screen
- * pixmap to draw into.
- */
- /*!
- \brief ADD
- \param surf surface (geosurf)
- \return
- \return -1 on error
- */
- int gsd_surf(geosurf * surf)
- {
- int desc, ret;
- G_debug(5, "gsd_surf(): id=%d", surf->gsurf_id);
- desc = ATT_TOPO;
- /* won't recalculate if update not needed, but may want to check
- to see if lights are on */
- gs_calc_normals(surf);
- switch (gs_get_att_src(surf, desc)) {
- case NOTSET_ATT:
- ret = (-1);
- break;
- case MAP_ATT:
- ret = (gsd_surf_map(surf)); /* changed to use test draw routine */
- #ifdef DO_ARROW
- gsd_norm_arrows(surf);
- /* Not ready yet - need to recalc normals for proper res
- gsd_wire_arrows(surf);
- */
- #endif
- break;
- case CONST_ATT:
- ret = (gsd_surf_const(surf, surf->att[desc].constant));
- break;
- case FUNC_ATT:
- ret = (gsd_surf_func(surf, surf->att[desc].user_func));
- break;
- default:
- ret = (-1);
- break;
- }
- return (ret);
- }
- /*!
- \brief ADD
- Using tmesh - not confident with qstrips portability
- \param surf surface (geosurf)
- \return
- */
- int gsd_surf_map_old(geosurf * surf)
- {
- int check_mask, check_color, check_transp;
- int check_material, check_emis, check_shin;
- typbuff *buff, *cobuff, *trbuff, *embuff, *shbuff;
- int xmod, ymod, row, col, cnt, xcnt, ycnt;
- long offset, y1off, y2off;
- float x1, x2, y1, y2, tx, ty, tz, ttr;
- float n[3], pt[4], xres, yres, ymax, zexag;
- int em_src, sh_src, trans_src, col_src, curcolor;
- gsurf_att *ematt, *shatt, *tratt, *coloratt;
- /* Viewport variables for accelerated drawing */
- GLdouble modelMatrix[16], projMatrix[16];
- GLint viewport[4];
- GLint window[4];
- #ifdef CALC_AREA
- float sz, mag, tedge1[3], tedge2[3], crossp[3], triv[3][3];
- double asurf = 0.0, axsurf = 0.0;
- #endif
- int zeros, dr1, dr2, dr3, dr4;
- int datarow1, datacol1, datarow2, datacol2;
- float kem, ksh, pkem, pksh;
- unsigned int ktrans;
- G_debug(3, "gsd_surf_map_old");
- /* avoid scaling by zero */
- GS_get_scale(&tx, &ty, &tz, 1);
- if (tz == 0.0) {
- return (gsd_surf_const(surf, 0.0));
- }
- /* else if (surf->z_exag == 0.0)
- {
- return(gsd_surf_const(surf, surf->z_min));
- }
- NOT YET IMPLEMENTED */
- buff = gs_get_att_typbuff(surf, ATT_TOPO, 0);
- cobuff = gs_get_att_typbuff(surf, ATT_COLOR, 0);
- gs_update_curmask(surf);
- check_mask = surf->curmask ? 1 : 0;
- /*
- checks ATT_TOPO & ATT_COLOR no_zero flags, make a mask from each,
- combine it/them with any current mask, put in surf->curmask:
- */
- xmod = surf->x_mod;
- ymod = surf->y_mod;
- xres = xmod * surf->xres;
- yres = ymod * surf->yres;
- ymax = (surf->rows - 1) * surf->yres;
- xcnt = VCOLS(surf);
- ycnt = VROWS(surf);
- /* Get viewport */
- gsd_getwindow(window, viewport, modelMatrix, projMatrix);
- /* adjust window */
- window[0] += (int)(yres * 2);
- window[1] -= (int)(yres * 2);
- window[2] -= (int)(xres * 2);
- window[3] += (int)(xres * 2);
- gsd_colormode(CM_DIFFUSE);
- gsd_pushmatrix();
- gsd_do_scale(1);
- gsd_translate(surf->x_trans, surf->y_trans, surf->z_trans);
- zexag = surf->z_exag;
- /* CURRENTLY ALWAYS 1.0 */
- #ifdef CALC_AREA
- sz = GS_global_exag();
- #endif
- /* TESTING */
- /*
- fprintf(stderr, "This machine has %d alpha bits\n", getgdesc(GD_BITS_NORM_DBL_ALPHA));
- fprintf(stderr, "GD_BLEND = %d \n", getgdesc(GD_BLEND));
- fprintf(stderr, "GD_CLIPPLANES = %d \n", getgdesc(GD_CLIPPLANES));
- */
- /* TODO: get rid of (define) these magic numbers scaling the attribute vals */
- check_transp = 0;
- tratt = &(surf->att[ATT_TRANSP]);
- ktrans = (255 << 24);
- trans_src = surf->att[ATT_TRANSP].att_src;
- if (CONST_ATT == trans_src && surf->att[ATT_TRANSP].constant != 0.0) {
- ktrans = (255 - (int)surf->att[ATT_TRANSP].constant) << 24;
- gsd_blend(1);
- gsd_zwritemask(0x0);
- }
- else if (MAP_ATT == trans_src) {
- trbuff = gs_get_att_typbuff(surf, ATT_TRANSP, 0);
- check_transp = trbuff ? 1 : 0;
- gsd_blend(1);
- gsd_zwritemask(0x0);
- }
- check_emis = 0;
- ematt = &(surf->att[ATT_EMIT]);
- kem = 0.0;
- pkem = 1.0;
- em_src = surf->att[ATT_EMIT].att_src;
- if (CONST_ATT == em_src) {
- kem = surf->att[ATT_EMIT].constant / 255.;
- }
- else if (MAP_ATT == em_src) {
- embuff = gs_get_att_typbuff(surf, ATT_EMIT, 0);
- check_emis = embuff ? 1 : 0;
- }
- check_shin = 0;
- shatt = &(surf->att[ATT_SHINE]);
- ksh = 0.0;
- pksh = 1.0;
- sh_src = surf->att[ATT_SHINE].att_src;
- if (CONST_ATT == sh_src) {
- ksh = surf->att[ATT_SHINE].constant / 255.;
- gsd_set_material(1, 0, ksh, kem, 0x0);
- }
- else if (MAP_ATT == sh_src) {
- shbuff = gs_get_att_typbuff(surf, ATT_SHINE, 0);
- check_shin = shbuff ? 1 : 0;
- }
- /* will need to check for color source of FUNC_ATT & NOTSET_ATT,
- or else use more general and inefficient gets */
- check_color = 1;
- coloratt = &(surf->att[ATT_COLOR]);
- col_src = surf->att[ATT_COLOR].att_src;
- if (col_src != MAP_ATT) {
- if (col_src == CONST_ATT) {
- curcolor = (int)surf->att[ATT_COLOR].constant;
- }
- else {
- curcolor = surf->wire_color;
- }
- check_color = 0;
- }
- check_material = (check_shin || check_emis || (kem && check_color));
- /* would also be good to check if colormap == surfmap, to increase speed */
- /* will also need to set check_transp, check_shine, etc & fix material */
- cnt = 0;
- for (row = 0; row < ycnt; row++) {
- if (GS_check_cancel()) {
- gsd_popmatrix();
- gsd_blend(0);
- gsd_zwritemask(0xffffffff);
- return (-1);
- }
- datarow1 = row * ymod;
- datarow2 = (row + 1) * ymod;
- y1 = ymax - row * yres;
- y2 = ymax - (row + 1) * yres;
- y1off = row * ymod * surf->cols;
- y2off = (row + 1) * ymod * surf->cols;
- gsd_bgntmesh();
- zeros = 0;
- dr1 = dr2 = dr3 = dr4 = 1;
- if (check_mask) {
- if (BM_get(surf->curmask, 0, datarow1)) {
- /*TL*/ ++zeros;
- dr1 = 0;
- }
- if (BM_get(surf->curmask, 0, datarow2)) {
- /*BL*/ ++zeros;
- dr2 = 0;
- }
- }
- if (dr1 && dr2) {
- offset = y1off; /* TL */
- FNORM(surf->norms[offset], n);
- pt[X] = 0;
- pt[Y] = y1;
- GET_MAPATT(buff, offset, pt[Z]);
- pt[Z] *= zexag;
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- if (check_transp) {
- GET_MAPATT(trbuff, offset, ttr);
- ktrans = (char)SCALE_ATT(tratt, ttr, 0, 255);
- ktrans = (char)(255 - ktrans) << 24;
- }
- gsd_litvert_func(n, ktrans | curcolor, pt);
- #ifdef CALC_AREA
- GS_v3eq(triv[cnt % 3], pt);
- #endif
- cnt++;
- offset = y2off; /* BL */
- FNORM(surf->norms[offset], n);
- pt[X] = 0;
- pt[Y] = y2;
- GET_MAPATT(buff, offset, pt[Z]);
- pt[Z] *= zexag;
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- if (check_transp) {
- GET_MAPATT(trbuff, offset, ttr);
- ktrans = (char)SCALE_ATT(tratt, ttr, 0, 255);
- ktrans = (char)(255 - ktrans) << 24;
- }
- if (check_material) {
- if (check_emis) {
- GET_MAPATT(embuff, offset, kem);
- kem = SCALE_ATT(ematt, kem, 0., 1.);
- }
- if (check_shin) {
- GET_MAPATT(shbuff, offset, ksh);
- ksh = SCALE_ATT(shatt, ksh, 0., 1.);
- }
- if (pksh != ksh || pkem != kem || (kem && check_color)) {
- /* expensive */
- pksh = ksh;
- pkem = kem;
- gsd_set_material(check_shin, check_emis, ksh, kem,
- curcolor);
- }
- }
- gsd_litvert_func(n, ktrans | curcolor, pt);
- #ifdef CALC_AREA
- GS_v3eq(triv[cnt % 3], pt);
- #endif
- cnt++;
- }
- for (col = 0; col < xcnt; col++) {
- datacol1 = col * xmod;
- datacol2 = (col + 1) * xmod;
- x1 = col * xres;
- x2 = (col + 1) * xres;
- zeros = 0;
- dr1 = dr2 = dr3 = dr4 = 1;
- if (check_mask) {
- if (BM_get(surf->curmask, datacol1, datarow1)) {
- /*TL*/ ++zeros;
- dr1 = 0;
- }
- if (BM_get(surf->curmask, datacol1, datarow2)) {
- /*BL*/ ++zeros;
- dr2 = 0;
- }
- if (BM_get(surf->curmask, datacol2, datarow2)) {
- /*BR*/ ++zeros;
- dr3 = 0;
- }
- if (BM_get(surf->curmask, datacol2, datarow1)) {
- /*TR*/ ++zeros;
- dr4 = 0;
- }
- if ((zeros > 1) && cnt) {
- gsd_endtmesh();
- cnt = 0;
- gsd_bgntmesh();
- continue;
- }
- }
- if (cnt > 252) {
- /* not needed! - no limit for tmesh */
- cnt = 0;
- gsd_endtmesh();
- gsd_bgntmesh();
- if (dr1) {
- offset = y1off + datacol1; /* TL */
- FNORM(surf->norms[offset], n);
- pt[X] = x1;
- pt[Y] = y1;
- GET_MAPATT(buff, offset, pt[Z]);
- pt[Z] *= zexag;
- if (gsd_checkpoint
- (pt, window, viewport, modelMatrix, projMatrix)) {
- gsd_endtmesh();
- cnt = 0;
- gsd_bgntmesh();
- continue;
- }
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- if (check_transp) {
- GET_MAPATT(trbuff, offset, ttr);
- ktrans = (char)SCALE_ATT(tratt, ttr, 0, 255);
- ktrans = (char)(255 - ktrans) << 24;
- }
- if (check_material) {
- if (check_emis) {
- GET_MAPATT(embuff, offset, kem);
- kem = SCALE_ATT(ematt, kem, 0., 1.);
- }
- if (check_shin) {
- GET_MAPATT(shbuff, offset, ksh);
- ksh = SCALE_ATT(shatt, ksh, 0., 1.);
- }
- if (pksh != ksh || pkem != kem
- || (kem && check_color)) {
- pksh = ksh;
- pkem = kem;
- gsd_set_material(check_shin, check_emis,
- ksh, kem, curcolor);
- }
- }
- gsd_litvert_func(n, ktrans | curcolor, pt);
- #ifdef CALC_AREA
- GS_v3eq(triv[cnt % 3], pt);
- #endif
- cnt++;
- }
- if (dr2) {
- offset = y2off + datacol1; /* BL */
- FNORM(surf->norms[offset], n);
- pt[X] = x1;
- pt[Y] = y2;
- GET_MAPATT(buff, offset, pt[Z]);
- pt[Z] *= zexag;
- if (gsd_checkpoint
- (pt, window, viewport, modelMatrix, projMatrix)) {
- gsd_endtmesh();
- cnt = 0;
- gsd_bgntmesh();
- continue;
- }
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- if (check_transp) {
- GET_MAPATT(trbuff, offset, ttr);
- ktrans = (char)SCALE_ATT(tratt, ttr, 0, 255);
- ktrans = (char)(255 - ktrans) << 24;
- }
- if (check_material) {
- if (check_emis) {
- GET_MAPATT(embuff, offset, kem);
- kem = SCALE_ATT(ematt, kem, 0., 1.);
- }
- if (check_shin) {
- GET_MAPATT(shbuff, offset, ksh);
- ksh = SCALE_ATT(shatt, ksh, 0., 1.);
- }
- if (pksh != ksh || pkem != kem
- || (kem && check_color)) {
- pksh = ksh;
- pkem = kem;
- gsd_set_material(check_shin, check_emis,
- ksh, kem, curcolor);
- }
- }
- gsd_litvert_func(n, ktrans | curcolor, pt);
- #ifdef CALC_AREA
- GS_v3eq(triv[cnt % 3], pt);
- #endif
- cnt++;
- }
- }
- if (dr4) {
- offset = y1off + datacol2; /* TR */
- FNORM(surf->norms[offset], n);
- pt[X] = x2;
- pt[Y] = y1;
- GET_MAPATT(buff, offset, pt[Z]);
- pt[Z] *= zexag;
- if (gsd_checkpoint
- (pt, window, viewport, modelMatrix, projMatrix)) {
- gsd_endtmesh();
- cnt = 0;
- gsd_bgntmesh();
- continue;
- }
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- if (check_transp) {
- GET_MAPATT(trbuff, offset, ttr);
- ktrans = (char)SCALE_ATT(tratt, ttr, 0, 255);
- ktrans = (char)(255 - ktrans) << 24;
- }
- if (check_material) {
- if (check_emis) {
- GET_MAPATT(embuff, offset, kem);
- kem = SCALE_ATT(ematt, kem, 0., 1.);
- }
- if (check_shin) {
- GET_MAPATT(shbuff, offset, ksh);
- ksh = SCALE_ATT(shatt, ksh, 0., 1.);
- }
- if (pksh != ksh || pkem != kem || (kem && check_color)) {
- pksh = ksh;
- pkem = kem;
- gsd_set_material(check_shin, check_emis,
- ksh, kem, curcolor);
- }
- }
- gsd_litvert_func(n, ktrans | curcolor, pt);
- #ifdef CALC_AREA
- GS_v3eq(triv[cnt % 3], pt);
- if (cnt > 1) {
- GS_v3eq(tedge1, triv[1]);
- GS_v3eq(tedge2, triv[2]);
- GS_v3sub(tedge1, triv[0]);
- GS_v3sub(tedge2, triv[1]);
- GS_v3cross(tedge1, tedge2, crossp);
- GS_v3mag(crossp, &mag);
- asurf += .5 * mag;
- tedge1[Z] *= sz;
- tedge2[Z] *= sz;
- GS_v3cross(tedge1, tedge2, crossp);
- GS_v3mag(crossp, &mag);
- axsurf += .5 * mag;
- }
- #endif
- cnt++;
- }
- if (dr3) {
- offset = y2off + datacol2; /* BR */
- FNORM(surf->norms[offset], n);
- pt[X] = x2;
- pt[Y] = y2;
- GET_MAPATT(buff, offset, pt[Z]);
- pt[Z] *= zexag;
- if (gsd_checkpoint
- (pt, window, viewport, modelMatrix, projMatrix)) {
- gsd_endtmesh();
- cnt = 0;
- gsd_bgntmesh();
- continue;
- }
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- if (check_transp) {
- GET_MAPATT(trbuff, offset, ttr);
- ktrans = (char)SCALE_ATT(tratt, ttr, 0, 255);
- ktrans = (char)(255 - ktrans) << 24;
- }
- if (check_material) {
- if (check_emis) {
- GET_MAPATT(embuff, offset, kem);
- kem = SCALE_ATT(ematt, kem, 0., 1.);
- }
- if (check_shin) {
- GET_MAPATT(shbuff, offset, ksh);
- ksh = SCALE_ATT(shatt, ksh, 0., 1.);
- }
- if (pksh != ksh || pkem != kem || (kem && check_color)) {
- pksh = ksh;
- pkem = kem;
- gsd_set_material(check_shin, check_emis,
- ksh, kem, curcolor);
- }
- }
- gsd_litvert_func(n, ktrans | curcolor, pt);
- #ifdef CALC_AREA
- GS_v3eq(triv[cnt % 3], pt);
- if (cnt > 1) {
- GS_v3eq(tedge1, triv[1]);
- GS_v3eq(tedge2, triv[2]);
- GS_v3sub(tedge1, triv[0]);
- GS_v3sub(tedge2, triv[1]);
- GS_v3cross(tedge1, tedge2, crossp);
- GS_v3mag(crossp, &mag);
- asurf += .5 * mag;
- tedge1[Z] *= sz;
- tedge2[Z] *= sz;
- GS_v3cross(tedge1, tedge2, crossp);
- GS_v3mag(crossp, &mag);
- axsurf += .5 * mag;
- }
- #endif
- cnt++;
- }
- } /* ea col */
- gsd_endtmesh();
- } /* ea row */
- gsd_popmatrix();
- gsd_blend(0);
- gsd_zwritemask(0xffffffff);
- show_colormode();
- #ifdef CALC_AREA
- G_debug(5, " Surface Area: %.12lf", asurf);
- G_debug(5, " Exaggerated Surface Area: %.12lf", axsurf);
- #endif
- return (0);
- }
- /*!
- \brief
- Using tmesh - not confident with qstrips portability
- \todo FIX: do_diff won't work right - needs normals - maybe
- calculate on the fly
- \param surf surface (geosurf)
- \param k
- \return
- */
- int gsd_surf_const(geosurf * surf, float k)
- {
- int do_diff, check_mask, check_color;
- typbuff *cobuff;
- int xmod, ymod, row, col, cnt, xcnt, ycnt;
- long offset, y1off, y2off;
- float x1, x2, y1, y2, tx, ty, tz;
- float n[3], pt[4], xres, yres, ymax, zexag;
- int col_src, curcolor;
- gsurf_att *coloratt;
- /* Viewport variables */
- GLdouble modelMatrix[16], projMatrix[16];
- GLint viewport[4];
- GLint window[4];
- int zeros, dr1, dr2, dr3, dr4;
- int datarow1, datacol1, datarow2, datacol2;
- unsigned int ktrans = 255;
- G_debug(5, "gsd_surf_const(): id=%d", surf->gsurf_id);
- if (GS_check_cancel()) {
- return (-1);
- }
- cobuff = gs_get_att_typbuff(surf, ATT_COLOR, 0);
- gs_update_curmask(surf);
- check_mask = surf->curmask ? 1 : 0;
- /*
- checks ATT_TOPO & ATT_COLOR no_zero flags, make a mask from each,
- combine it/them with any current mask, put in surf->curmask:
- */
- do_diff = (NULL != gsdiff_get_SDref());
- xmod = surf->x_mod;
- ymod = surf->y_mod;
- xres = xmod * surf->xres;
- yres = ymod * surf->yres;
- xcnt = VCOLS(surf);
- ycnt = VROWS(surf);
- ymax = (surf->rows - 1) * surf->yres;
- /* Get Viewport */
- gsd_getwindow(window, viewport, modelMatrix, projMatrix);
- /* adjust window */
- window[0] += (int)(yres * 2);
- window[1] -= (int)(yres * 2);
- window[2] -= (int)(xres * 2);
- window[3] += (int)(xres * 2);
- gsd_colormode(CM_DIFFUSE);
- gsd_pushmatrix();
- /* avoid scaling by zero */
- GS_get_scale(&tx, &ty, &tz, 1);
- if (tz == 0.0) {
- k = 0.0;
- gsd_do_scale(0);
- }
- else {
- gsd_do_scale(1);
- }
- gsd_translate(surf->x_trans, surf->y_trans, surf->z_trans);
- zexag = surf->z_exag;
- if (CONST_ATT == surf->att[ATT_TRANSP].att_src) {
- gsd_blend(1);
- ktrans = 255 - (int)surf->att[ATT_TRANSP].constant;
- gsd_zwritemask(0x0);
- }
- ktrans = (ktrans << 24);
- /* will need to check for color source of FUNC_ATT & NOTSET_ATT,
- or else use more general and inefficient gets */
- check_color = 1;
- coloratt = &(surf->att[ATT_COLOR]);
- col_src = surf->att[ATT_COLOR].att_src;
- if (col_src != MAP_ATT) {
- if (col_src == CONST_ATT) {
- curcolor = (int)surf->att[ATT_COLOR].constant;
- }
- else {
- curcolor = surf->wire_color;
- }
- check_color = 0;
- }
- /* CONSTANTS */
- pt[Z] = k * zexag;
- n[X] = n[Y] = 0.0;
- n[Z] = 1.0;
- /* just draw one polygon if no color mapped */
- /* fast, but specular reflection will prob. be poor */
- if (!check_color && !check_mask && !do_diff) {
- gsd_bgnpolygon();
- pt[X] = pt[Y] = 0;
- gsd_litvert_func(n, ktrans | curcolor, pt);
- pt[X] = xcnt * xres;
- gsd_litvert_func(n, ktrans | curcolor, pt);
- pt[Y] = ycnt * yres;
- gsd_litvert_func(n, ktrans | curcolor, pt);
- pt[X] = 0;
- gsd_litvert_func(n, ktrans | curcolor, pt);
- gsd_endpolygon();
- gsd_popmatrix();
- gsd_blend(0);
- gsd_zwritemask(0xffffffff);
- return (0);
- }
- cnt = 0;
- for (row = 0; row < ycnt; row++) {
- if (GS_check_cancel()) {
- gsd_popmatrix();
- gsd_blend(0);
- gsd_zwritemask(0xffffffff);
- return (-1);
- }
- datarow1 = row * ymod;
- datarow2 = (row + 1) * ymod;
- y1 = ymax - row * yres;
- y2 = ymax - (row + 1) * yres;
- y1off = row * ymod * surf->cols;
- y2off = (row + 1) * ymod * surf->cols;
- gsd_bgntmesh();
- zeros = 0;
- dr1 = dr2 = dr3 = dr4 = 1;
- if (check_mask) {
- if (BM_get(surf->curmask, 0, datarow1)) {
- /*TL*/ ++zeros;
- dr1 = 0;
- }
- if (BM_get(surf->curmask, 0, datarow2)) {
- /*BL*/ ++zeros;
- dr2 = 0;
- }
- }
- if (dr1 && dr2) {
- offset = y1off; /* TL */
- pt[X] = 0;
- pt[Y] = y1;
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- if (do_diff) {
- pt[Z] = gsdiff_do_SD(k * zexag, offset);
- }
- gsd_litvert_func(n, ktrans | curcolor, pt);
- cnt++;
- offset = y2off; /* BL */
- pt[X] = 0;
- pt[Y] = y2;
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- if (do_diff) {
- pt[Z] = gsdiff_do_SD(k * zexag, offset);
- }
- gsd_litvert_func(n, ktrans | curcolor, pt);
- cnt++;
- }
- for (col = 0; col < xcnt; col++) {
- datacol1 = col * xmod;
- datacol2 = (col + 1) * xmod;
- x1 = col * xres;
- x2 = (col + 1) * xres;
- zeros = 0;
- dr1 = dr2 = dr3 = dr4 = 1;
- if (check_mask) {
- if (BM_get(surf->curmask, datacol1, datarow1)) {
- /*TL*/ ++zeros;
- dr1 = 0;
- }
- if (BM_get(surf->curmask, datacol1, datarow2)) {
- /*BL*/ ++zeros;
- dr2 = 0;
- }
- if (BM_get(surf->curmask, datacol2, datarow2)) {
- /*BR*/ ++zeros;
- dr3 = 0;
- }
- if (BM_get(surf->curmask, datacol2, datarow1)) {
- /*TR*/ ++zeros;
- dr4 = 0;
- }
- if ((zeros > 1) && cnt) {
- gsd_endtmesh();
- cnt = 0;
- gsd_bgntmesh();
- continue;
- }
- }
- if (cnt > 250) {
- cnt = 0;
- gsd_endtmesh();
- gsd_bgntmesh();
- if (dr1) {
- offset = y1off + datacol1; /* TL */
- pt[X] = x1;
- pt[Y] = y1;
- if (gsd_checkpoint
- (pt, window, viewport, modelMatrix, projMatrix)) {
- gsd_endtmesh();
- cnt = 0;
- gsd_bgntmesh();
- continue;
- }
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- if (do_diff) {
- pt[Z] = gsdiff_do_SD(k * zexag, offset);
- }
- gsd_litvert_func(n, ktrans | curcolor, pt);
- cnt++;
- }
- if (dr2) {
- offset = y2off + datacol1; /* BL */
- pt[X] = x1;
- pt[Y] = y2;
- if (gsd_checkpoint
- (pt, window, viewport, modelMatrix, projMatrix)) {
- gsd_endtmesh();
- cnt = 0;
- gsd_bgntmesh();
- continue;
- }
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- if (do_diff) {
- pt[Z] = gsdiff_do_SD(k * zexag, offset);
- }
- gsd_litvert_func(n, ktrans | curcolor, pt);
- cnt++;
- }
- }
- if (dr4) {
- offset = y1off + datacol2; /* TR */
- pt[X] = x2;
- pt[Y] = y1;
- if (gsd_checkpoint
- (pt, window, viewport, modelMatrix, projMatrix)) {
- gsd_endtmesh();
- cnt = 0;
- gsd_bgntmesh();
- continue;
- }
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- if (do_diff) {
- pt[Z] = gsdiff_do_SD(k * zexag, offset);
- }
- gsd_litvert_func(n, ktrans | curcolor, pt);
- cnt++;
- }
- if (dr3) {
- offset = y2off + datacol2; /* BR */
- pt[X] = x2;
- pt[Y] = y2;
- if (gsd_checkpoint
- (pt, window, viewport, modelMatrix, projMatrix)) {
- gsd_endtmesh();
- cnt = 0;
- gsd_bgntmesh();
- continue;
- }
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- if (do_diff) {
- pt[Z] = gsdiff_do_SD(k * zexag, offset);
- }
- gsd_litvert_func(n, ktrans | curcolor, pt);
- cnt++;
- }
- } /* ea col */
- gsd_endtmesh();
- } /* ea row */
- gsd_popmatrix();
- gsd_blend(0);
- gsd_zwritemask(0xffffffff);
- return (0);
- }
- /*!
- \brief Define user function
- Not yet supported
- \param gs surface (geosurf)
- \param user_func user function
- \return 1
- */
- int gsd_surf_func(geosurf * gs, int (*user_func) ())
- {
- return (1);
- }
- /*!
- \brief ADD
- \param npts1
- \param npts2
- \param surf1 first surface (geosurf)
- \param surf2 second surface (geosurf)
- \param points1
- \param points2
- \param norm
- \return 1
- */
- int gsd_triangulated_wall(int npts1, int npts2, geosurf * surf1,
- geosurf * surf2, Point3 * points1, Point3 * points2,
- float *norm)
- {
- int i, i1, i2, nlong, offset, col_src;
- int check_color1, check_color2, color1, color2;
- typbuff *cobuf1, *cobuf2;
- gsurf_att *coloratt1, *coloratt2;
- check_color1 = check_color2 = 1;
- col_src = surf1->att[ATT_COLOR].att_src;
- if (col_src != MAP_ATT) {
- if (col_src == CONST_ATT) {
- color1 = (int)surf1->att[ATT_COLOR].constant;
- }
- else {
- color1 = surf1->wire_color;
- }
- check_color1 = 0;
- }
- coloratt1 = &(surf1->att[ATT_COLOR]);
- cobuf1 = gs_get_att_typbuff(surf1, ATT_COLOR, 0);
- col_src = surf2->att[ATT_COLOR].att_src;
- if (col_src != MAP_ATT) {
- if (col_src == CONST_ATT) {
- color2 = (int)surf2->att[ATT_COLOR].constant;
- }
- else {
- color2 = surf2->wire_color;
- }
- check_color2 = 0;
- }
- coloratt2 = &(surf2->att[ATT_COLOR]);
- cobuf2 = gs_get_att_typbuff(surf2, ATT_COLOR, 0);
- gsd_colormode(CM_DIFFUSE);
- gsd_pushmatrix();
- gsd_do_scale(1);
- gsd_bgntmesh();
- for (nlong = (npts1 > npts2 ? npts1 : npts2), i = 0; i < nlong; i++) {
- i1 = i * npts1 / nlong;
- i2 = i * npts2 / nlong;
- offset = XY2OFF(surf1, points1[i1][X], points1[i1][Y]);
- if (check_color1) {
- color1 = gs_mapcolor(cobuf1, coloratt1, offset);
- }
- offset = XY2OFF(surf1, points2[i2][X], points2[i2][Y]);
- if (check_color2) {
- color2 = gs_mapcolor(cobuf2, coloratt2, offset);
- }
- /* start with long line to ensure triangle */
- if (npts1 > npts2) {
- points1[i1][X] += surf1->x_trans;
- points1[i1][Y] += surf1->y_trans;
- points1[i1][Z] += surf1->z_trans;
- gsd_litvert_func(norm, color1, points1[i1]);
- points2[i2][X] += surf2->x_trans;
- points2[i2][Y] += surf2->y_trans;
- points2[i2][Z] += surf2->z_trans;
- gsd_litvert_func(norm, color2, points2[i2]);
- }
- else {
- points2[i2][X] += surf2->x_trans;
- points2[i2][Y] += surf2->y_trans;
- points2[i2][Z] += surf2->z_trans;
- gsd_litvert_func(norm, color2, points2[i2]);
- points1[i1][X] += surf1->x_trans;
- points1[i1][Y] += surf1->y_trans;
- points1[i1][Z] += surf1->z_trans;
- gsd_litvert_func(norm, color1, points1[i1]);
- }
- }
- gsd_endtmesh();
- gsd_popmatrix();
- return (1);
- }
- /*!
- \brief ADD
- \param mode
- */
- void gsd_setfc(int mode)
- {
- FCmode = mode;
- return;
- }
- /*!
- \brief ADD
- \return
- */
- int gsd_getfc(void)
- {
- return (FCmode);
- }
- /*!
- \brief ADD
- \param surf surface (geosurf)
- \param point
- \return
- */
- static int transpoint_is_masked(geosurf * surf, Point3 point)
- {
- Point3 tp;
- tp[X] = point[X] - surf->x_trans;
- tp[Y] = point[Y] - surf->y_trans;
- return (gs_point_is_masked(surf, tp));
- }
- /*!
- \brief ADD
- \param points
- \param gsurf
- \param ptn
- \param cursurf
- \param numsurfs
- \param belowsurf
- \return 0 if there is no surface below the current,
- \return -1 if the current surface is masked,
- \return 1 if the surface below the current surface is not masked
- (belowsurf is assigned)
- */
- static int get_point_below(Point3 ** points, geosurf ** gsurfs, int ptn,
- int cursurf, int numsurfs, int *belowsurf)
- {
- int n, found = -1;
- float nearz = 0.0, diff;
- if (gsurfs[cursurf]->curmask) {
- if (transpoint_is_masked(gsurfs[cursurf], points[cursurf][ptn])) {
- return (-1);
- }
- }
- for (n = 0; n < numsurfs; ++n) {
- diff = points[cursurf][ptn][Z] - points[n][ptn][Z];
- if (diff > 0) {
- if (!nearz || diff < nearz) {
- if (gsurfs[n]->curmask) {
- if (transpoint_is_masked(gsurfs[n], points[n][ptn])) {
- continue;
- }
- }
- nearz = diff;
- found = n;
- }
- }
- /* else if (diff == 0.0 && n != cursurf)
- {
- if (gsurfs[n]->curmask)
- {
- if (transpoint_is_masked(gsurfs[n], points[n][ptn]))
- {
- continue;
- }
- }
- nearz=diff;
- found = n;
- break;
- }
- */
- }
- if (found != -1) {
- *belowsurf = found;
- return (1);
- }
- return (0);
- }
- /*
- #define CPDEBUG
- */
- /*!
- \brief ADD
- \param np
- \param ns
- \param gsurfs
- \param points
- \param norm
- \return
- */
- int gsd_ortho_wall(int np, int ns, geosurf ** gsurfs, Point3 ** points,
- float *norm)
- {
- int n, i, offset, col_src, check_color[MAX_SURFS];
- int color, colors[MAX_SURFS], nocolor;
- typbuff *cobuf[MAX_SURFS];
- gsurf_att *coloratt[MAX_SURFS];
- nocolor = FCmode == FC_GREY ? 1 : 0;
- if (!nocolor) {
- for (n = 0; n < ns; ++n) {
- check_color[n] = 1;
- col_src = gsurfs[n]->att[ATT_COLOR].att_src;
- if (col_src != MAP_ATT) {
- if (col_src == CONST_ATT) {
- colors[n] = (int)gsurfs[n]->att[ATT_COLOR].constant;
- }
- else {
- colors[n] = gsurfs[n]->wire_color;
- }
- check_color[n] = 0;
- }
- coloratt[n] = &(gsurfs[n]->att[ATT_COLOR]);
- cobuf[n] = gs_get_att_typbuff(gsurfs[n], ATT_COLOR, 0);
- }
- }
- #ifdef CPDEBUG
- {
- GS_set_draw(GSD_BOTH);
- }
- #endif
- /* changed from CM_DIFFUSE - July 25, 2005
- * should display proper color for cut planes
- */
- gsd_colormode(CM_COLOR);
- /* actually ought to write a GS_set_fencetransp() */
- if (nocolor) {
- color = 0x80808080;
- gsd_blend(1);
- gsd_zwritemask(0x0);
- }
- gsd_pushmatrix();
- gsd_do_scale(1);
- /* using segs_intersect here with segments projected to
- the 2d clipping plane */
- {
- float tx, ty;
- int bn, bnl, ctop, cbot, ctopl, cbotl, bsret;
- Point3 xing;
- if (nocolor) {
- ctop = cbot = ctopl = cbotl = color;
- }
- for (n = 0; n < ns; ++n) {
- for (i = 0; i < np; i++) {
- if (0 <
- (bsret =
- get_point_below(points, gsurfs, i, n, ns, &bn))) {
- gsd_bgntmesh();
- if (!nocolor) {
- SET_SCOLOR(n);
- SET_SCOLOR(bn);
- if (FCmode == FC_ABOVE) {
- ctop = cbot = colors[n];
- }
- else if (FCmode == FC_BELOW) {
- ctop = cbot = colors[bn];
- }
- else {
- cbot = colors[bn];
- ctop = colors[n];
- }
- }
- if (i) {
- /* need to find crossing? */
- if (!transpoint_is_masked(gsurfs[n], points[n][i - 1])
- && !transpoint_is_masked(gsurfs[bn],
- points[bn][i - 1])) {
- if (1 ==
- segs_intersect(0.0, points[n][i - 1][Z], 1.0,
- points[n][i][Z], 0.0,
- points[bn][i - 1][Z], 1.0,
- points[bn][i][Z], &tx, &ty)) {
- xing[Z] = ty;
- xing[Y] = points[n][i - 1][Y] + tx *
- (points[n][i][Y] - points[n][i - 1][Y]);
- xing[X] = points[n][i - 1][X] + tx *
- (points[n][i][X] - points[n][i - 1][X]);
- gsd_litvert_func(norm, ctop, xing);
- xing[Z] = points[bn][i - 1][Z] + tx *
- (points[bn][i][Z] - points[bn][i - 1][Z]);
- gsd_litvert_func(norm, cbot, xing);
- }
- }
- }
- gsd_litvert_func(norm, ctop, points[n][i]);
- gsd_litvert_func(norm, cbot, points[bn][i]);
- i++;
- bnl = -1;
- while (i < np && 0 < (bsret =
- get_point_below(points, gsurfs, i,
- n, ns, &bn))) {
- #ifdef CPDEBUG
- {
- int lower = 0;
- if (GS_check_cancel()) {
- break;
- }
- }
- #endif
- if (!nocolor) {
- ctopl = ctop;
- cbotl = cbot;
- SET_SCOLOR(n);
- SET_SCOLOR(bn);
- if (FCmode == FC_ABOVE) {
- ctop = cbot = colors[n];
- }
- else if (FCmode == FC_BELOW) {
- ctop = cbot = colors[bn];
- }
- else {
- cbot = colors[bn];
- ctop = colors[n];
- }
- }
- /*
- IF UPPER crossing :
- (crossing is between current & new lower surf)
- IF XING going DOWN:
- - plot crossing point (color previous upper)
- - endtmesh/bgntmesh
- - plot crossing point (color current upper)
- - plot "equivalent" point below (color current lower)
- IF XING going UP:
- - plot crossing point (color previous upper)
- - plot "equivalent" point below (color previous lower)
- - endtmesh/bgntmesh
- - plot crossing point (color current upper)
- ELSE IF LOWER crossing:
- (crossing between new & previous lower surfs):
- - plot "equivalent" point above (color previous upper)
- - plot crossing below (color previous lower)
- - endtmesh/bgntmesh
- - plot "equivalent" point above (color current upper)
- - plot crossing below (color current lower)
- */
- if (bnl >= 0 && bnl != bn) {
- /* crossing */
- float z1, z2;
- int upper = 0;
- if (!transpoint_is_masked(gsurfs[n],
- points[n][i - 1]) &&
- !transpoint_is_masked(gsurfs[bnl],
- points[bnl][i - 1]) &&
- !transpoint_is_masked(gsurfs[bn],
- points[bn][i - 1])) {
- if (1 == segs_intersect(0.0,
- points[n][i - 1][Z],
- 1.0, points[n][i][Z],
- 0.0,
- points[bn][i - 1][Z],
- 1.0, points[bn][i][Z],
- &tx, &ty)) {
- /* crossing going up */
- G_debug(5,
- "crossing going up at surf %d no. %d",
- n, i);
- upper = 1;
- xing[Z] = ty;
- xing[Y] = points[n][i - 1][Y] + tx *
- (points[n][i][Y] -
- points[n][i - 1][Y]);
- xing[X] =
- points[n][i - 1][X] +
- tx * (points[n][i][X] -
- points[n][i - 1][X]);
- gsd_litvert_func(norm, ctopl, xing);
- z1 = xing[Z];
- xing[Z] = points[bnl][i - 1][Z] + tx *
- (points[bnl][i][Z] -
- points[bnl][i - 1][Z]);
- gsd_litvert_func(norm, cbotl, xing);
- xing[Z] = z1;
- gsd_endtmesh();
- gsd_bgntmesh();
- gsd_litvert_func(norm, ctop, xing);
- }
- else if (1 == segs_intersect(0.0,
- points[n][i -
- 1][Z],
- 1.0,
- points[n][i][Z],
- 0.0,
- points[bnl][i -
- 1]
- [Z], 1.0,
- points[bnl][i]
- [Z], &tx, &ty)) {
- /* crossing going down */
- G_debug(5,
- "crossing going down at surf %d no. %d",
- n, i);
- upper = 1;
- xing[Z] = ty;
- xing[Y] = points[n][i - 1][Y] + tx *
- (points[n][i][Y] -
- points[n][i - 1][Y]);
- xing[X] =
- points[n][i - 1][X] +
- tx * (points[n][i][X] -
- points[n][i - 1][X]);
- gsd_litvert_func(norm, ctopl, xing);
- z1 = xing[Z];
- xing[Z] = points[bnl][i - 1][Z] + tx *
- (points[bnl][i][Z] -
- points[bnl][i - 1][Z]);
- gsd_litvert_func(norm, cbotl, xing);
- xing[Z] = z1;
- gsd_endtmesh();
- gsd_bgntmesh();
- gsd_litvert_func(norm, ctop, xing);
- xing[Z] = points[bn][i - 1][Z] + tx *
- (points[bn][i][Z] -
- points[bn][i - 1][Z]);
- gsd_litvert_func(norm, cbot, xing);
- }
- }
- if (!upper &&
- !transpoint_is_masked(gsurfs[bn],
- points[bn][i - 1]) &&
- !transpoint_is_masked(gsurfs[bnl],
- points[bnl][i - 1])) {
- if (1 == segs_intersect(0.0,
- points[bn][i - 1][Z],
- 1.0, points[bn][i][Z],
- 0.0,
- points[bnl][i - 1][Z],
- 1.0,
- points[bnl][i][Z],
- &tx, &ty)) {
- #ifdef CPDEBUG
- {
- lower = 1;
- }
- #endif
- G_debug(5,
- "lower crossing at surf %d no. %d between surfs %d & %d",
- n, i, bn, bnl);
- xing[Z] = ty;
- xing[Y] = points[bn][i - 1][Y] + tx *
- (points[bn][i][Y] -
- points[bn][i - 1][Y]);
- xing[X] = points[bn][i - 1][X] + tx *
- (points[bn][i][X] -
- points[bn][i - 1][X]);
- z2 = xing[Z];
- z1 = xing[Z] = points[n][i - 1][Z] + tx *
- (points[n][i][Z] -
- points[n][i - 1][Z]);
- gsd_litvert_func(norm, ctopl, xing);
- xing[Z] = z2;
- gsd_litvert_func(norm, cbotl, xing);
- gsd_endtmesh();
- gsd_bgntmesh();
- xing[Z] = z1;
- gsd_litvert_func(norm, ctop, xing);
- xing[Z] = z2;
- gsd_litvert_func(norm, cbot, xing);
- }
- }
- #ifdef CPDEBUG
- {
- if (!upper && !lower) {
- G_debug(5,
- "Crossing NOT found or masked:");
- G_debug(5,
- " current surf: %d [ %.2f %.2f %.2f -> %.2f %.2f %f",
- n, points[n][i - 1][X],
- points[n][i - 1][Y],
- points[n][i - 1][Z],
- points[n][i][X], points[n][i][Y],
- points[n][i][Z]);
- G_debug(5,
- " below surf: %d [ %.2f %.2f %.2f -> %.2f %.2f %f\n",
- bn, points[bn][i - 1][X],
- points[bn][i - 1][Y],
- points[bn][i - 1][Z],
- points[bn][i][X],
- points[bn][i][Y],
- points[bn][i][Z]);
- G_debug(5, gs
- " last below surf: %d [ %.2f %.2f %.2f -> %.2f %.2f %f\n",
- bnl, points[bnl][i - 1][X],
- points[bnl][i - 1][Y],
- points[bnl][i - 1][Z],
- points[bnl][i][X],
- points[bnl][i][Y],
- points[bnl][i][Z]);
- }
- }
- #endif
- }
- gsd_litvert_func(norm, ctop, points[n][i]);
- gsd_litvert_func(norm, cbot, points[bn][i]);
- bnl = bn;
- i++;
- }
- if (i < np) {
- /* need to find crossing? */
- if (!transpoint_is_masked(gsurfs[n], points[n][i - 1])
- && !transpoint_is_masked(gsurfs[bn],
- points[bn][i - 1])) {
- if (1 ==
- segs_intersect(0.0, points[n][i - 1][Z], 1.0,
- points[n][i][Z], 0.0,
- points[bn][i - 1][Z], 1.0,
- points[bn][i][Z], &tx, &ty)) {
- xing[Z] = ty;
- xing[Y] = points[n][i - 1][Y] + tx *
- (points[n][i][Y] - points[n][i - 1][Y]);
- xing[X] = points[n][i - 1][X] + tx *
- (points[n][i][X] - points[n][i - 1][X]);
- gsd_litvert_func(norm, ctop, xing);
- }
- i--;
- }
- }
- gsd_endtmesh();
- }
- }
- }
- }
- gsd_colormode(CM_DIFFUSE); /* set colormode back to DIFFUSE */
- gsd_popmatrix();
- gsd_blend(0);
- gsd_zwritemask(0xffffffff);
- return (1);
- }
- /*!
- \brief ADD
- bgn,end should already be in world modeling coords, but have to
- be reverse-translated to apply to each surface
- \param bgn,end 2d line for cutting plane
- \param norm indicates which way wall faces
- \return
- */
- int gsd_wall(float *bgn, float *end, float *norm)
- {
- geosurf *gsurfs[MAX_SURFS];
- Point3 *points[MAX_SURFS], *tmp;
- int nsurfs, ret, npts, npts1, n, i, err = 0;
- float bgn1[2], end1[2];
- if (norm[Z] > 0.0001 || norm[Z] < -.0001) {
- return (0); /* can't do tilted wall yet */
- }
- if (FCmode == FC_OFF) {
- return (0);
- }
- nsurfs = gs_getall_surfaces(gsurfs);
- for (n = 0; n < nsurfs; n++) {
- /* get drape points for surf */
- bgn1[X] = bgn[X] - gsurfs[n]->x_trans;
- bgn1[Y] = bgn[Y] - gsurfs[n]->y_trans;
- end1[X] = end[X] - gsurfs[n]->x_trans;
- end1[Y] = end[Y] - gsurfs[n]->y_trans;
- tmp = gsdrape_get_allsegments(gsurfs[n], bgn1, end1, &npts1);
- if (n) {
- if (npts != npts1) {
- G_warning(_("Cut-plane points mis-match between surfaces. "
- "Check resolution(s)."));
- err = 1;
- nsurfs = n;
- break;
- }
- }
- npts = npts1;
- if (n == nsurfs - 1) {
- /* last surf - don't need to copy */
- points[n] = tmp;
- for (i = 0; i < npts1; i++) {
- /* DOING translation here! */
- points[n][i][X] += gsurfs[n]->x_trans;
- points[n][i][Y] += gsurfs[n]->y_trans;
- points[n][i][Z] += gsurfs[n]->z_trans;
- }
- break;
- }
- /* allocate space in points and copy tmp to points */
- points[n] = (Point3 *) G_calloc(npts1, sizeof(Point3)); /* G_fatal_error */
- for (i = 0; i < npts1; i++) {
- GS_v3eq(points[n][i], tmp[i]);
- /* DOING translation here! */
- points[n][i][X] += gsurfs[n]->x_trans;
- points[n][i][Y] += gsurfs[n]->y_trans;
- points[n][i][Z] += gsurfs[n]->z_trans;
- }
- } /* done for */
- if (err) {
- for (n = 0; n < nsurfs; n++) {
- if (points[n]) {
- G_free(points[n]);
- }
- }
- return (0);
- }
- ret = gsd_ortho_wall(npts, nsurfs, gsurfs, points, norm);
- for (n = 0; n < nsurfs - 1; n++) {
- /* don't free last - it's constant */
- G_free(points[n]);
- }
- return (ret);
- }
- /*!
- \brief ADD
- Need to do Zexag scale of normal for arrow direction, drawing
- routine unexags z for arrow
- \param surf surface (geosurf)
- \return
- */
- int gsd_norm_arrows(geosurf * surf)
- {
- typbuff *buff, *cobuff;
- int check_mask, check_color;
- int xmod, ymod, row, col, cnt, xcnt, ycnt;
- long offset, y1off, y2off;
- float x1, x2, y1, y2, tx, ty, tz, sz;
- float n[3], pt[4], xres, yres, ymax, zexag;
- int col_src, curcolor;
- gsurf_att *coloratt;
- int zeros, dr1, dr2, dr3, dr4;
- int datarow1, datacol1, datarow2, datacol2;
- G_debug(3, "gsd_norm_arrows");
- /* avoid scaling by zero */
- GS_get_scale(&tx, &ty, &tz, 1);
- if (tz == 0.0) {
- return (0);
- }
- sz = GS_global_exag();
- /*
- checks ATT_TOPO & ATT_COLOR no_zero flags, make a mask from each,
- combine it/them with any current mask, put in surf->curmask:
- */
- gs_update_curmask(surf);
- check_mask = surf->curmask ? 1 : 0;
- check_color = 1;
- coloratt = &(surf->att[ATT_COLOR]);
- col_src = surf->att[ATT_COLOR].att_src;
- if (col_src != MAP_ATT) {
- if (col_src == CONST_ATT) {
- curcolor = (int)surf->att[ATT_COLOR].constant;
- }
- else {
- curcolor = surf->wire_color;
- }
- check_color = 0;
- }
- buff = gs_get_att_typbuff(surf, ATT_TOPO, 0);
- cobuff = gs_get_att_typbuff(surf, ATT_COLOR, 0);
- xmod = surf->x_mod;
- ymod = surf->y_mod;
- xres = xmod * surf->xres;
- yres = ymod * surf->yres;
- ymax = (surf->rows - 1) * surf->yres;
- xcnt = VCOLS(surf);
- ycnt = VROWS(surf);
- gsd_pushmatrix();
- gsd_do_scale(1);
- gsd_translate(surf->x_trans, surf->y_trans, surf->z_trans);
- zexag = surf->z_exag;
- /* CURRENTLY ALWAYS 1.0 */
- #ifdef DO_ARROW_SOLID
- gsd_colormode(CM_DIFFUSE);
- #else
- gsd_colormode(CM_COLOR);
- #endif
- cnt = 0;
- for (row = 0; row < ycnt; row++) {
- if (GS_check_cancel()) {
- gsd_popmatrix();
- return (-1);
- }
- datarow1 = row * ymod;
- datarow2 = (row + 1) * ymod;
- y1 = ymax - row * yres;
- y2 = ymax - (row + 1) * yres;
- y1off = row * ymod * surf->cols;
- y2off = (row + 1) * ymod * surf->cols;
- zeros = 0;
- dr1 = dr2 = dr3 = dr4 = 1;
- if (check_mask) {
- if (BM_get(surf->curmask, 0, datarow1)) {
- /*TL*/ ++zeros;
- dr1 = 0;
- }
- if (BM_get(surf->curmask, 0, datarow2)) {
- /*BL*/ ++zeros;
- dr2 = 0;
- }
- }
- if (dr1 && dr2) {
- offset = y1off; /* TL */
- FNORM(surf->norms[offset], n);
- pt[X] = 0;
- pt[Y] = y2;
- GET_MAPATT(buff, offset, pt[Z]);
- pt[Z] *= zexag;
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- #ifdef DO_ARROW_SOLID
- gsd_3darrow(pt, curcolor, xres * 2, xres / 2, n, sz);
- #else
- if (DEBUG_ARROW) {
- gsd_arrow(pt, 0x000000, xres * 2, n, sz, surf);
- }
- #endif
- cnt++;
- offset = y2off; /* BL */
- FNORM(surf->norms[offset], n);
- pt[X] = 0;
- pt[Y] = y2;
- GET_MAPATT(buff, offset, pt[Z]);
- pt[Z] *= zexag;
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- #ifdef DO_ARROW_SOLID
- gsd_3darrow(pt, curcolor, xres * 2, xres / 2, n, sz);
- #else
- if (DEBUG_ARROW) {
- gsd_arrow(pt, 0x000000, xres * 2, n, sz, surf);
- }
- #endif
- cnt++;
- }
- for (col = 0; col < xcnt; col++) {
- datacol1 = col * xmod;
- datacol2 = (col + 1) * xmod;
- x1 = col * xres;
- x2 = (col + 1) * xres;
- zeros = 0;
- dr1 = dr2 = dr3 = dr4 = 1;
- if (check_mask) {
- if (BM_get(surf->curmask, datacol1, datarow1)) {
- /*TL*/ ++zeros;
- dr1 = 0;
- }
- if (BM_get(surf->curmask, datacol1, datarow2)) {
- /*BL*/ ++zeros;
- dr2 = 0;
- }
- if (BM_get(surf->curmask, datacol2, datarow2)) {
- /*BR*/ ++zeros;
- dr3 = 0;
- }
- if (BM_get(surf->curmask, datacol2, datarow1)) {
- /*TR*/ ++zeros;
- dr4 = 0;
- }
- if ((zeros > 1) && cnt) {
- cnt = 0;
- continue;
- }
- }
- if (dr4) {
- offset = y1off + datacol2; /* TR */
- FNORM(surf->norms[offset], n);
- pt[X] = x2;
- pt[Y] = y1;
- GET_MAPATT(buff, offset, pt[Z]);
- pt[Z] *= zexag;
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- #ifdef DO_ARROW_SOLID
- gsd_3darrow(pt, curcolor, xres * 2, xres / 2, n, sz);
- #else
- if (DEBUG_ARROW) {
- gsd_arrow(pt, 0x000000, xres * 2, n, sz, surf);
- }
- #endif
- cnt++;
- }
- if (dr3) {
- offset = y2off + datacol2; /* BR */
- FNORM(surf->norms[offset], n);
- pt[X] = x2;
- pt[Y] = y2;
- GET_MAPATT(buff, offset, pt[Z]);
- pt[Z] *= zexag;
- if (check_color) {
- curcolor = gs_mapcolor(cobuff, coloratt, offset);
- }
- #ifdef DO_ARROW_SOLID
- gsd_3darrow(pt, curcolor, xres * 2, xres / 2, n, sz);
- #else
- if (DEBUG_ARROW) {
- gsd_arrow(pt, 0x000000, xres * 2, n, sz, surf);
- }
- #endif
- cnt++;
- }
- } /* ea col */
- } /* ea row */
- gsd_popmatrix();
- return (1);
- }
- /*!
- \brief Draw surface using triangle fan instead of strip
- Optimized by getting rid of BM_get mask check - GET_MAPPATT does same
- and returns zero if masked
- Only do in window check on Fan center(v0) to further optimize -- this runs
- the risk of trimming points in view !!
- \param surf surface (geosurf)
- \return
- */
- int gsd_surf_map(geosurf * surf)
- {
- int check_mask, check_color, check_transp;
- int check_material, check_emis, check_shin;
- typbuff *buff, *cobuff, *trbuff, *embuff, *shbuff;
- int xmod, ymod;
- int row, col, cnt, xcnt, ycnt;
- long y1off, y2off, y3off;
- long offset2[10];
- float pt2[10][2];
- int ii;
- float x1, x2, x3, y1, y2, y3, tx, ty, tz, ttr;
- float n[3], pt[4], xres, yres, ymax, zexag;
- int em_src, sh_src, trans_src, col_src, curcolor;
- gsurf_att *ematt, *shatt, *tratt, *coloratt;
- /* Viewport variables for accelerated drawing */
- GLdouble modelMatrix[16], projMatrix[16];
- GLint viewport[4];
- GLint window[4];
- int cnt1 = 0, cnt2 = 0;
- int datarow1, datacol1, datarow2, datacol2, datarow3, datacol3;
- float kem, ksh, pkem, pksh;
- unsigned int ktrans;
- int step_val = 2; /* should always be factor of 2 for fan */
- int start_val = 1; /* one half of step_val */
- /* avoid scaling by zero */
- GS_get_scale(&tx, &ty, &tz, 1);
- if (tz == 0.0) {
- return (gsd_surf_const(surf, 0.0));
- }
- /* else if (surf->z_exag == 0.0)
- {
- return(gsd_surf_const(surf, surf->z_min));
- }
- NOT YET IMPLEMENTED */
- buff = gs_get_att_typbuff(surf, ATT_TOPO, 0);
- cobuff = gs_get_att_typbuff(surf, ATT_COLOR, 0);
- gs_update_curmask(surf);
- check_mask = surf->curmask ? 1 : 0;
- /*
- checks ATT_TOPO & ATT_COLOR no_zero flags, make a mask from each,
- combine it/them with any current mask, put in surf->curmask:
- */
- xmod = surf->x_mod;
- ymod = surf->y_mod;
- xres = xmod * surf->xres;
- yres = ymod * surf->yres;
- ymax = (surf->rows - 1) * surf->yres;
- xcnt = VCOLS(surf);
- ycnt = VROWS(surf);
- /* Get viewport */
- gsd_getwindow(window, viewport, modelMatrix, projMatrix);
- gsd_colormode(CM_DIFFUSE);
- gsd_pushmatrix();
- gsd_do_scale(1);
- gsd_translate(surf->x_trans, surf->y_trans, surf->z_trans);
- zexag = surf->z_exag;
- /* adjust window */
- window[0] += (int)(yres * 4 * zexag);
- window[1] -= (int)(yres * 4 * zexag);
- window[2] -= (int)(xres * 4 * zexag);
- window[3] += (int)(xres * 4 * zexag);
- /* CURRENTLY ALWAYS 1.0 */
- #ifdef CALC_AREA
- sz = GS_global_exag();
- #endif
- /* TODO: get rid of (define) these magic numbers scaling the attribute vals */
- check_transp = 0;
- tratt = &(surf->att[ATT_TRANSP]);
- ktrans = (255 << 24);
- trans_src = surf->att[ATT_TRANSP].att_src;
- if (CONST_ATT == trans_src && surf->att[ATT_TRANSP].constant != 0.0) {
- ktrans = (255 - (int)surf->att[ATT_TRANSP].constant) << 24;
- gsd_blend(1);
- gsd_zwritemask(0x0);
- }
- else if (MAP_ATT == trans_src) {
- trbuff = gs_get_att_typbuff(surf, ATT_TRANSP, 0);
- check_transp = trbuff ? 1 : 0;
- gsd_blend(1);
- gsd_zwritemask(0x0);
- }
- check_emis = 0;
- ematt = &(surf->att[ATT_EMIT]);
- kem = 0.0;
- pkem = 1.0;
- em_src = surf->att[ATT_EMIT].att_src;
- if (CONST_ATT == em_src) {
- kem = surf->att[ATT_EMIT].constant / 255.;
- }
- else if (MAP_ATT == em_src) {
- embuff = gs_get_att_typbuff(surf, ATT_EMIT, 0);
- check_emis = embuff ? 1 : 0;
- }
- check_shin = 0;
- shatt = &(surf->att[ATT_SHINE]);
- ksh = 0.0;
- pksh = 1.0;
- sh_src = surf->att[ATT_SHINE].att_src;
- if (CONST_ATT == sh_src) {
- ksh = surf->att[ATT_SHINE].constant / 255.;
- gsd_set_material(1, 0, ksh, kem, 0x0);
- }
- else if (MAP_ATT == sh_src) {
- shbuff = gs_get_att_typbuff(surf, ATT_SHINE, 0);
- check_shin = shbuff ? 1 : 0;
- }
- /* will need to check for color source of FUNC_ATT & NOTSET_ATT,
- or else use more general and inefficient gets */
- check_color = 1;
- coloratt = &(surf->att[ATT_COLOR]);
- col_src = surf->att[ATT_COLOR].att_src;
- if (col_src != MAP_ATT) {
- if (col_src == CONST_ATT) {
- curcolor = (int)surf->att[ATT_COLOR].constant;
- }
- else {
- curcolor = surf->wire_color;
- }
- check_color = 0;
- }
- check_material = (check_shin || check_emis || (kem && check_color));
- /* would also be good to check if colormap == surfmap, to increase speed */
- /* will also need to set check_transp, check_shine, etc & fix material */
- cnt = 0;
- for (row = start_val; row < ycnt; row += step_val) {
- if (GS_check_cancel()) {
- gsd_popmatrix();
- gsd_blend(0);
- gsd_zwritemask(0xffffffff);
- return (-1);
- }
- /*
- if (row == 201 && new_fan == 0) {
- xmod *= 2;
- ymod *= 2;
- xres = xmod * surf->xres;
- yres = ymod * surf->yres;
- step_val *= 2;
- new_fan = 1;
- row -= 1;
- row /= 2;
- }
- */
- datarow1 = row * ymod;
- datarow2 = (row - (step_val / 2)) * ymod;
- datarow3 = (row + (step_val / 2)) * ymod;
- y1 = ymax - row * yres;
- y2 = ymax - (row - (step_val / 2)) * yres;
- y3 = ymax - (row + (step_val / 2)) * yres;
- y1off = row * ymod * surf->cols;
- y2off = (row - (step_val / 2)) * ymod * surf->cols;
- y3off = (row + (step_val / 2)) * ymod * surf->cols;
- for (col = start_val; col < xcnt; col += step_val) {
- datacol1 = col * xmod;
- datacol2 = (col - (step_val / 2)) * xmod;
- datacol3 = (col + (step_val / 2)) * xmod;
- x1 = col * xres;
- x2 = (col - (step_val / 2)) * xres;
- x3 = (col + (step_val / 2)) * xres;
- /* 0 */
- /*
- if (check_mask) {
- if (BM_get(surf->curmask, datacol1, datarow1))
- continue;
- }
- */
- cnt1++;
- /* Do not need BM_get because GET_MAPATT calls
- * same and returns zero if masked
- */
- offset2[0] = y1off + datacol1; /* fan center */
- pt2[0][X] = x1;
- pt2[0][Y] = y1; /* fan center */
- pt[X] = pt2[0][X];
- pt[Y] = pt2[0][Y];
- if (!GET_MAPATT(buff, offset2[0], pt[Z]))
- continue; /* masked */
- else {
- pt[Z] *= zexag;
- if (gsd_checkpoint
- (pt, window, viewport, modelMatrix, projMatrix))
- continue;
- }
- offset2[1] = y2off + datacol2;
- offset2[2] = y2off + datacol1;
- offset2[3] = y2off + datacol3;
- offset2[4] = y1off + datacol3;
- offset2[5] = y3off + datacol3;
- offset2[6] = y3off + datacol1;
- offset2[7] = y3off + datacol2;
- offset2[8] = y1off + datacol2;
- offset2[9] = y2off + datacol2; /* repeat 1st corner to close */
- pt2[1][X] = x2;
- pt2[1][Y] = y2;
- pt2[2][X] = x1;
- pt2[2][Y] = y2;
- pt2[3][X] = x3;
- pt2[3][Y] = y2;
- pt2[4][X] = x3;
- pt2[4][Y] = y1;
- pt2[5][X] = x3;
- pt2[5][Y] = y3;
- pt2[6][X] = x1;
- pt2[6][Y] = y3;
- pt2[7][X] = x2;
- pt2[7][Y] = y3;
- pt2[8][X] = x2;
- pt2[8][Y] = y1;
- pt2[9][X] = x2;
- pt2[9][Y] = y2; /* repeat 1st corner to close */
- /* Run through triangle fan */
- gsd_bgntfan();
- for (ii = 0; ii < 10; ii++) {
- if (ii > 0) {
- pt[X] = pt2[ii][X];
- pt[Y] = pt2[ii][Y];
- if (!GET_MAPATT(buff, offset2[ii], pt[Z]))
- continue;
- pt[Z] *= zexag;
- }
- FNORM(surf->norms[offset2[ii]], n);
- if (check_color)
- curcolor = gs_mapcolor(cobuff, coloratt, offset2[ii]);
- if (check_transp) {
- GET_MAPATT(trbuff, offset2[ii], ttr);
- ktrans = (char)SCALE_ATT(tratt, ttr, 0, 255);
- ktrans = (char)(255 - ktrans) << 24;
- }
- if (check_material) {
- if (check_emis) {
- GET_MAPATT(embuff, offset2[ii], kem);
- kem = SCALE_ATT(ematt, kem, 0., 1.);
- }
- if (check_shin) {
- GET_MAPATT(shbuff, offset2[ii], ksh);
- ksh = SCALE_ATT(shatt, ksh, 0., 1.);
- }
- if (pksh != ksh || pkem != kem || (kem && check_color)) {
- pksh = ksh;
- pkem = kem;
- gsd_set_material(check_shin, check_emis,
- ksh, kem, curcolor);
- }
- }
- gsd_litvert_func(n, ktrans | curcolor, pt);
- } /* close ii loop */
- gsd_endtfan();
- cnt2++;
- } /* end col */
- } /* end row */
- gsd_popmatrix();
- gsd_blend(0);
- gsd_zwritemask(0xffffffff);
- return (0);
- }
|