GS2.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499
  1. /*!
  2. \file GS2.c
  3. \brief OGSF library - loading and manipulating surfaces (higher level functions)
  4. GRASS OpenGL gsurf OGSF Library
  5. Plans for handling color maps:
  6. NOW:
  7. if able to load as unsigned char, make lookup table containing palette
  8. otherwise, load directly as packed color, set lookup = NULL
  9. MAYBE LATER:
  10. if able to load as POSITIVE short, make lookup table containing palette
  11. - may want to calculate savings first (ie, numcells > 32768)
  12. (not exactly, it's Friday & time to go home - figure it later)
  13. otherwise, load directly as packed color, set lookup = NULL
  14. MESSY! - need to fix up!
  15. (C) 1999-2008 by the GRASS Development Team
  16. This program is free software under the
  17. GNU General Public License (>=v2).
  18. Read the file COPYING that comes with GRASS
  19. for details.
  20. \author Bill Brown USACERL (1993)
  21. \author Pierre de Mouveaux <p_de_mouveaux hotmail.com> (updated October 1999)
  22. \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
  23. */
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <math.h>
  27. #include <grass/config.h>
  28. #if defined(OPENGL_X11) || defined(OPENGL_WINDOWS)
  29. #include <GL/gl.h>
  30. #include <GL/glu.h>
  31. #elif defined(OPENGL_AQUA)
  32. #include <OpenGL/gl.h>
  33. #include <OpenGL/glu.h>
  34. #endif
  35. #include <grass/gis.h>
  36. #include <grass/raster.h>
  37. #include <grass/ogsf.h>
  38. #include <grass/glocale.h>
  39. #include "gsget.h"
  40. #include "rowcol.h"
  41. #include "rgbpack.h"
  42. /* Hack to make NVIZ2.2 query functions.("What's Here" and "Look at")
  43. * to work.
  44. * Uses gs_los_intersect1() instead of gs_los_intersect().
  45. * Pierre de Mouveaux - 31 oct. 1999. p_de_mouveaux@hotmail.com.
  46. */
  47. #define NVIZ_HACK 1
  48. int gsd_getViewport(GLint *, GLint *);
  49. /* array of surface ids */
  50. static int Surf_ID[MAX_SURFS];
  51. static int Next_surf = 0;
  52. static int SDref_surf = 0;
  53. /* attributes array */
  54. static float Default_const[MAX_ATTS];
  55. static float Default_nulls[MAX_ATTS];
  56. /* largest dimension */
  57. static float Longdim;
  58. /* N, S, W, E */
  59. static float Region[4];
  60. static geoview Gv;
  61. static geodisplay Gd;
  62. static struct Cell_head wind;
  63. static int Buffermode;
  64. static int Numlights = 0;
  65. static int Resetlight = 1;
  66. static int Modelshowing = 0;
  67. void void_func(void)
  68. {
  69. return;
  70. }
  71. /*!
  72. \brief Initialize OGSF library
  73. Get region settings - wind
  74. Set Region (NSWE array) and compute scale
  75. */
  76. void GS_libinit(void)
  77. {
  78. static int first = 1;
  79. G_get_set_window(&wind);
  80. Region[0] = wind.north;
  81. Region[1] = wind.south;
  82. Region[2] = wind.west;
  83. Region[3] = wind.east;
  84. /* scale largest dimension to GS_UNIT_SIZE */
  85. if ((wind.east - wind.west) > (wind.north - wind.south)) {
  86. Longdim = (wind.east - wind.west);
  87. }
  88. else {
  89. Longdim = (wind.north - wind.south);
  90. }
  91. Gv.scale = GS_UNIT_SIZE / Longdim;
  92. G_debug(1, "GS_libinit(): n=%f s=%f w=%f e=%f scale=%f first=%d",
  93. Region[0], Region[1], Region[2], Region[3], Gv.scale, first);
  94. Cxl_func = void_func;
  95. Swap_func = void_func;
  96. if (first) {
  97. gs_init();
  98. }
  99. first = 0;
  100. return;
  101. }
  102. /*!
  103. \brief Get largest dimension
  104. \param[out] dim dimension
  105. \return 1
  106. */
  107. int GS_get_longdim(float *dim)
  108. {
  109. *dim = Longdim;
  110. G_debug(3, "GS_get_longdim(): dim=%g", *dim);
  111. return (1);
  112. }
  113. /*!
  114. \brief Get 2D region extent
  115. \param[out] n,s,w,e extent values
  116. \return 1
  117. */
  118. int GS_get_region(float *n, float *s, float *w, float *e)
  119. {
  120. *n = Region[0];
  121. *s = Region[1];
  122. *w = Region[2];
  123. *e = Region[3];
  124. return (1);
  125. }
  126. /*!
  127. \brief Set default attributes for map objects
  128. \param defs attributes array (dim MAX_ATTS)
  129. \param null_defs null attributes array (dim MAX_ATTS)
  130. */
  131. void GS_set_att_defaults(float *defs, float *null_defs)
  132. {
  133. int i;
  134. G_debug(3, "GS_set_att_defaults");
  135. for (i = 0; i < MAX_ATTS; i++) {
  136. Default_const[i] = defs[i];
  137. Default_nulls[i] = null_defs[i];
  138. }
  139. return;
  140. }
  141. /*!
  142. Check if surface exists
  143. \param id surface id
  144. \return 0 not found
  145. \return 1 found
  146. */
  147. int GS_surf_exists(int id)
  148. {
  149. int i, found = 0;
  150. G_debug(3, "GS_surf_exists(): id=%d", id);
  151. if (NULL == gs_get_surf(id)) {
  152. return (0);
  153. }
  154. for (i = 0; i < Next_surf && !found; i++) {
  155. if (Surf_ID[i] == id) {
  156. found = 1;
  157. }
  158. }
  159. return (found);
  160. }
  161. /*!
  162. \brief Add new surface
  163. Note that origin has 1/2 cell added to represent center of cells
  164. because library assumes that east - west = (cols - 1) * ew_res,
  165. since left and right columns are on the edges.
  166. \return surface id
  167. \return -1 on error (MAX_SURFS exceded)
  168. */
  169. int GS_new_surface(void)
  170. {
  171. geosurf *ns;
  172. G_debug(3, "GS_new_surface():");
  173. if (Next_surf < MAX_SURFS) {
  174. ns = gs_get_new_surface();
  175. gs_init_surf(ns, wind.west + wind.ew_res / 2.,
  176. wind.south + wind.ns_res / 2., wind.rows, wind.cols,
  177. wind.ew_res, wind.ns_res);
  178. gs_set_defaults(ns, Default_const, Default_nulls);
  179. /* make default shine current */
  180. gs_set_att_src(ns, ATT_SHINE, CONST_ATT);
  181. Surf_ID[Next_surf] = ns->gsurf_id;
  182. ++Next_surf;
  183. G_debug(3, " id=%d", ns->gsurf_id);
  184. return (ns->gsurf_id);
  185. }
  186. return (-1);
  187. }
  188. void GS_set_light_reset(int i)
  189. {
  190. Resetlight = i;
  191. if (i)
  192. Numlights = 0;
  193. }
  194. int GS_get_light_reset(void)
  195. {
  196. return Resetlight;
  197. }
  198. /*!
  199. \brief Add new model light
  200. \return light model id
  201. \return -1 on error (MAX_LIGHTS exceded)
  202. */
  203. int GS_new_light(void)
  204. {
  205. int i;
  206. if (GS_get_light_reset()) {
  207. GS_set_light_reset(0);
  208. for (i = 0; i < MAX_LIGHTS; i++) {
  209. Gv.lights[i].position[X] = Gv.lights[i].position[Y] = 0.0;
  210. Gv.lights[i].position[Z] = 1.0;
  211. Gv.lights[i].position[W] = 0.0; /* infinite */
  212. Gv.lights[i].color[0] = Gv.lights[i].color[1] =
  213. Gv.lights[i].color[2] = 1.0;
  214. Gv.lights[i].ambient[0] = Gv.lights[i].ambient[1] =
  215. Gv.lights[i].ambient[2] = 0.2;
  216. Gv.lights[i].shine = 32.0;
  217. }
  218. gsd_init_lightmodel();
  219. }
  220. if (Numlights < MAX_LIGHTS) {
  221. gsd_deflight(Numlights + 1, &(Gv.lights[Numlights]));
  222. gsd_switchlight(Numlights + 1, 1);
  223. return ++Numlights;
  224. }
  225. return -1;
  226. }
  227. /*!
  228. \brief Set light position
  229. \bug I think lights array doesnt match sgi_light array
  230. \param num light id (starts with 1)
  231. \param xpos,ypos,zpos coordinates (model)
  232. \param local local coordinate (for viewport)
  233. */
  234. void GS_setlight_position(int num, float xpos, float ypos, float zpos,
  235. int local)
  236. {
  237. if (num) {
  238. num -= 1;
  239. if (num < Numlights) {
  240. Gv.lights[num].position[X] = xpos;
  241. Gv.lights[num].position[Y] = ypos;
  242. Gv.lights[num].position[Z] = zpos;
  243. Gv.lights[num].position[W] = (float)local;
  244. gsd_deflight(num + 1, &(Gv.lights[num]));
  245. }
  246. }
  247. return;
  248. }
  249. /*!
  250. \brief Get light position
  251. \param num light id (starts at 1)
  252. \param[out] xpos,ypos,zpos coordinates
  253. \param[out] local ?
  254. */
  255. void GS_getlight_position(int num, float *xpos, float *ypos, float *zpos,
  256. int *local)
  257. {
  258. if (num) {
  259. num -= 1;
  260. if (num < Numlights) {
  261. *xpos = Gv.lights[num].position[X];
  262. *ypos = Gv.lights[num].position[Y];
  263. *zpos = Gv.lights[num].position[Z];
  264. *local = (int)Gv.lights[num].position[W];
  265. }
  266. }
  267. return;
  268. }
  269. /*!
  270. \brief Set light color
  271. \param num light id (starts at 1)
  272. \param red,green,blue color values (from 0.0 to 1.0)
  273. */
  274. void GS_setlight_color(int num, float red, float green, float blue)
  275. {
  276. if (num) {
  277. num -= 1;
  278. if (num < Numlights) {
  279. Gv.lights[num].color[0] = red;
  280. Gv.lights[num].color[1] = green;
  281. Gv.lights[num].color[2] = blue;
  282. gsd_deflight(num + 1, &(Gv.lights[num]));
  283. }
  284. }
  285. return;
  286. }
  287. /*!
  288. \brief Get light color
  289. \param num light id (starts at 1)
  290. \param[out] red,green,blue color values
  291. */
  292. void GS_getlight_color(int num, float *red, float *green, float *blue)
  293. {
  294. if (num) {
  295. num -= 1;
  296. if (num < Numlights) {
  297. *red = Gv.lights[num].color[0];
  298. *green = Gv.lights[num].color[1];
  299. *blue = Gv.lights[num].color[2];
  300. }
  301. }
  302. return;
  303. }
  304. /*!
  305. \brief Set light ambient
  306. Red, green, blue from 0.0 to 1.0
  307. \param num light id (starts at 1)
  308. \param red,green,blue color values
  309. */
  310. void GS_setlight_ambient(int num, float red, float green, float blue)
  311. {
  312. if (num) {
  313. num -= 1;
  314. if (num < Numlights) {
  315. Gv.lights[num].ambient[0] = red;
  316. Gv.lights[num].ambient[1] = green;
  317. Gv.lights[num].ambient[2] = blue;
  318. gsd_deflight(num + 1, &(Gv.lights[num]));
  319. }
  320. }
  321. return;
  322. }
  323. /*!
  324. \brief Get light ambient
  325. \param num light id (starts at 1)
  326. \param[out] red,green,blue color values
  327. */
  328. void GS_getlight_ambient(int num, float *red, float *green, float *blue)
  329. {
  330. if (num) {
  331. num -= 1;
  332. if (num < Numlights) {
  333. *red = Gv.lights[num].ambient[0];
  334. *green = Gv.lights[num].ambient[1];
  335. *blue = Gv.lights[num].ambient[2];
  336. }
  337. }
  338. return;
  339. }
  340. /*!
  341. \brief Switch off all lights
  342. */
  343. void GS_lights_off(void)
  344. {
  345. int i;
  346. for (i = 0; i < Numlights; i++) {
  347. gsd_switchlight(i + 1, 0);
  348. }
  349. return;
  350. }
  351. /*!
  352. \brief Switch on all lights
  353. */
  354. void GS_lights_on(void)
  355. {
  356. int i;
  357. for (i = 0; i < Numlights; i++) {
  358. gsd_switchlight(i + 1, 1);
  359. }
  360. return;
  361. }
  362. /*!
  363. \brief Switch on/off light
  364. \param num light id (starts at 1)
  365. \param on non-zero for 'on' otherwise 'off'
  366. */
  367. void GS_switchlight(int num, int on)
  368. {
  369. if (num) {
  370. num -= 1;
  371. if (num < Numlights) {
  372. gsd_switchlight(num + 1, on);
  373. }
  374. }
  375. return;
  376. }
  377. /*!
  378. \brief Check if transparency is set
  379. \return 0 transparency not set
  380. \return 1 transparency is set
  381. */
  382. int GS_transp_is_set(void)
  383. {
  384. return (gs_att_is_set(NULL, ATT_TRANSP) || (FC_GREY == gsd_getfc()));
  385. }
  386. /*!
  387. \brief Retrieves coordinates for lighting model position, at center of view
  388. \param pos[out] coordinates
  389. */
  390. void GS_get_modelposition1(float pos[])
  391. {
  392. /* TODO: Still needs work to handle other cases */
  393. /* this is a quick hack to get lighting adjustments debugged */
  394. /*
  395. GS_v3dir(Gv.from_to[FROM], Gv.from_to[TO], center);
  396. GS_v3mult(center, 1000);
  397. GS_v3add(center, Gv.from_to[FROM]);
  398. */
  399. gs_get_datacenter(pos);
  400. gs_get_data_avg_zmax(&(pos[Z]));
  401. G_debug(1, "GS_get_modelposition1(): model position: %f %f %f",
  402. pos[X], pos[Y], pos[Z]);
  403. return;
  404. }
  405. /*!
  406. \brief Retrieves coordinates for lighting model position, at center of view
  407. Position at nearclip * 2: tried nearclip + siz, but since need to
  408. know position to calculate size, have two dependent variables
  409. (nearclip * 2) from eye.
  410. \param siz[out] size
  411. \param pos[out] coordinates (X, Y, Z)
  412. */
  413. void GS_get_modelposition(float *siz, float *pos)
  414. {
  415. float dist, near_h, dir[3];
  416. dist = 2. * Gd.nearclip;
  417. near_h = 2.0 * tan(4.0 * atan(1.) * Gv.fov / 3600.) * dist;
  418. *siz = near_h / 8.0;
  419. /* prevent clipping - would only happen if fov > ~127 degrees, at
  420. fov = 2.0 * atan(2.0) */
  421. if (*siz > Gd.nearclip) {
  422. *siz = Gd.nearclip;
  423. }
  424. GS_v3dir(Gv.from_to[FROM], Gv.from_to[TO], dir);
  425. pos[X] = Gv.from_to[FROM][X] + dir[X] * dist;
  426. pos[Y] = Gv.from_to[FROM][Y] + dir[Y] * dist;
  427. pos[Z] = Gv.from_to[FROM][Z] + dir[Z] * dist;
  428. return;
  429. }
  430. /*!
  431. \brief Set decoration, north arrow ??
  432. \todo scale used to calculate len of arrow still needs work
  433. needs go function that returns center / eye distance
  434. gsd_get_los function is not working correctly ??
  435. \param pt point value in true world coordinates (?)
  436. \param id surface id
  437. \param[out] pos2 output coordinates
  438. */
  439. void GS_set_Narrow(int *pt, int id, float *pos2)
  440. {
  441. geosurf *gs;
  442. float x, y, z;
  443. GLdouble modelMatrix[16], projMatrix[16];
  444. GLint viewport[4];
  445. if (GS_get_selected_point_on_surface(pt[X], pt[Y], &id, &x, &y, &z)) {
  446. gs = gs_get_surf(id);
  447. if (gs) {
  448. z = gs->zmax;
  449. pos2[X] = (float)x - gs->ox + gs->x_trans;
  450. pos2[Y] = (float)y - gs->oy + gs->y_trans;
  451. pos2[Z] = (float)z + gs->z_trans;
  452. return;
  453. }
  454. }
  455. else {
  456. gs = gs_get_surf(id);
  457. /* Need to get model matrix, etc
  458. * to run gluUnProject
  459. */
  460. gsd_pushmatrix();
  461. gsd_do_scale(1);
  462. glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
  463. glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
  464. glGetIntegerv(GL_VIEWPORT, viewport);
  465. if (gs) {
  466. GLdouble out_near[3], out_far[3];
  467. GLdouble factor;
  468. GLdouble out[3];
  469. z = (float)gs->zmax + gs->z_trans;
  470. gluUnProject((GLdouble) pt[X], (GLdouble) pt[Y], (GLdouble) 0.,
  471. modelMatrix, projMatrix, viewport,
  472. &out_near[X], &out_near[Y], &out_near[Z]);
  473. gluUnProject((GLdouble) pt[X], (GLdouble) pt[Y], (GLdouble) 1.,
  474. modelMatrix, projMatrix, viewport,
  475. &out_far[X], &out_far[Y], &out_far[Z]);
  476. glPopMatrix();
  477. factor = (out_near[Z] - z) / (out_near[Z] - out_far[Z]);
  478. out[X] = out_near[X] - ((out_near[X] - out_far[X]) * factor);
  479. out[Y] = out_near[Y] - ((out_near[Y] - out_far[Y]) * factor);
  480. out[Z] = z;
  481. pos2[X] = (float)out[X];
  482. pos2[Y] = (float)out[Y];
  483. pos2[Z] = (float)out[Z];
  484. return;
  485. }
  486. }
  487. return;
  488. }
  489. /*!
  490. \brief Draw place marker
  491. Used to display query point for raster queries.
  492. \param id surface id
  493. \param pt point, X, Y value in true world coordinates
  494. */
  495. void GS_draw_X(int id, float *pt)
  496. {
  497. geosurf *gs;
  498. Point3 pos;
  499. float siz;
  500. gvstyle style;
  501. if ((gs = gs_get_surf(id))) {
  502. GS_get_longdim(&siz);
  503. style.size = siz / 200.;
  504. pos[X] = pt[X] - gs->ox;
  505. pos[Y] = pt[Y] - gs->oy;
  506. _viewcell_tri_interp(gs, pos);
  507. gsd_pushmatrix();
  508. gsd_do_scale(1);
  509. gsd_translate(gs->x_trans, gs->y_trans, gs->z_trans);
  510. gsd_linewidth(1);
  511. if (CONST_ATT == gs_get_att_src(gs, ATT_TOPO)) {
  512. pos[Z] = gs->att[ATT_TOPO].constant;
  513. gs = NULL; /* tells gpd_obj to use given Z val */
  514. }
  515. style.color = Gd.bgcol;
  516. style.symbol = ST_GYRO;
  517. gpd_obj(gs, &style, pos);
  518. gsd_flush();
  519. gsd_popmatrix();
  520. }
  521. return;
  522. }
  523. /*!
  524. \brief Draw line on surface
  525. \param id surface id
  526. \param x1,y1,x2,y2 line nodes
  527. */
  528. void GS_draw_line_onsurf(int id, float x1, float y1, float x2, float y2)
  529. {
  530. float p1[2], p2[2];
  531. geosurf *gs;
  532. if ((gs = gs_get_surf(id))) {
  533. p1[X] = x1 - gs->ox;
  534. p1[Y] = y1 - gs->oy;
  535. p2[X] = x2 - gs->ox;
  536. p2[Y] = y2 - gs->oy;
  537. gsd_pushmatrix();
  538. gsd_do_scale(1);
  539. gsd_translate(gs->x_trans, gs->y_trans, gs->z_trans);
  540. gsd_linewidth(1);
  541. gsd_color_func(GS_default_draw_color());
  542. gsd_line_onsurf(gs, p1, p2);
  543. gsd_popmatrix();
  544. gsd_flush();
  545. }
  546. return;
  547. }
  548. /*!
  549. \brief Draw multiline on surface
  550. Like above but limits points in line to n or points found in segment,
  551. whichever is smaller.
  552. \param id surface id
  553. \param x1,y1,x2,y2 line nodes
  554. \return number of points used
  555. */
  556. int GS_draw_nline_onsurf(int id, float x1, float y1, float x2, float y2,
  557. float *lasp, int n)
  558. {
  559. float p1[2], p2[2];
  560. geosurf *gs;
  561. int ret = 0;
  562. if ((gs = gs_get_surf(id))) {
  563. p1[X] = x1 - gs->ox;
  564. p1[Y] = y1 - gs->oy;
  565. p2[X] = x2 - gs->ox;
  566. p2[Y] = y2 - gs->oy;
  567. gsd_pushmatrix();
  568. gsd_do_scale(1);
  569. gsd_translate(gs->x_trans, gs->y_trans, gs->z_trans);
  570. gsd_linewidth(1);
  571. gsd_color_func(GS_default_draw_color());
  572. ret = gsd_nline_onsurf(gs, p1, p2, lasp, n);
  573. gsd_surf2real(gs, lasp);
  574. gsd_popmatrix();
  575. gsd_flush();
  576. }
  577. return (ret);
  578. }
  579. /*!
  580. \brief Draw flow-line on surace
  581. This is slow - should be moved to gs_ but GS_ good for testing
  582. and useful for app programmer
  583. \param id surface id
  584. \param x,y coordinates of flow-line
  585. */
  586. void GS_draw_flowline_at_xy(int id, float x, float y)
  587. {
  588. geosurf *gs;
  589. float nv[3], pdir[2], mult;
  590. float p1[2], p2[2], next[2];
  591. int i = 0;
  592. if ((gs = gs_get_surf(id))) {
  593. p1[X] = x;
  594. p1[Y] = y;
  595. /* multiply by 1.5 resolutions to ensure a crossing ? */
  596. mult = .1 * (VXRES(gs) > VYRES(gs) ? VXRES(gs) : VYRES(gs));
  597. GS_coordpair_repeats(p1, p1, 50);
  598. while (1 == GS_get_norm_at_xy(id, p1[X], p1[Y], nv)) {
  599. if (nv[Z] == 1.0) {
  600. if (pdir[X] == 0.0 && pdir[Y] == 0.0) {
  601. break;
  602. }
  603. p2[X] = p1[X] + (pdir[X] * mult);
  604. p2[Y] = p1[Y] + (pdir[Y] * mult);
  605. }
  606. else {
  607. /* use previous direction */
  608. GS_v2norm(nv);
  609. p2[X] = p1[X] + (nv[X] * mult);
  610. p2[Y] = p1[Y] + (nv[Y] * mult);
  611. pdir[X] = nv[X];
  612. pdir[Y] = nv[Y];
  613. }
  614. if (i > 2000) {
  615. break;
  616. }
  617. if (GS_coordpair_repeats(p1, p2, 0)) {
  618. break;
  619. }
  620. /* Think about this: */
  621. /* degenerate line means edge or level edge ? */
  622. /* next is filled with last point drawn */
  623. if (2 > GS_draw_nline_onsurf(id, p1[X], p1[Y],
  624. p2[X], p2[Y], next, 3)) {
  625. break;
  626. }
  627. p1[X] = next[X];
  628. p1[Y] = next[Y];
  629. }
  630. G_debug(3, "GS_draw_flowline_at_xy(): dir: %f %f", nv[X], nv[Y]);
  631. }
  632. return;
  633. }
  634. /*!
  635. \brief Draw fringe around data (surface) at selected corners
  636. \param id surface id
  637. \param clr color
  638. \param elev elevation value
  639. \param where nw/ne/sw/se edges - 0 (turn off) 1 (turn on)
  640. */
  641. void GS_draw_fringe(int id, unsigned long clr, float elev, int *where)
  642. {
  643. geosurf *gs;
  644. G_debug(3, "GS_draw_fringe(): id: %d clr: %ld elev %f edges: %d %d %d %d",
  645. id, clr, elev, where[0], where[1], where[2], where[3]);
  646. if ((gs = gs_get_surf(id)))
  647. gsd_display_fringe(gs, clr, elev, where);
  648. }
  649. /*!
  650. \brief Draw legend
  651. \todo add legend from list option
  652. make font loading more flexible
  653. \param name legend name
  654. \param fontbase font-base
  655. \param size ?
  656. \param flags legend flags
  657. \param range values range
  658. \param pt ?
  659. */
  660. int GS_draw_legend(const char *name, GLuint fontbase, int size, int *flags,
  661. float *range, int *pt)
  662. {
  663. int list_no;
  664. list_no = gsd_put_legend(name, fontbase, size, flags, range, pt);
  665. return (list_no);
  666. }
  667. /*!
  668. \brief Draw pre-defined list
  669. Uses glFlush() to ensure all drawing is complete
  670. before returning
  671. \param list_id list id
  672. */
  673. void GS_draw_list(GLuint list_id)
  674. {
  675. gsd_calllist(list_id);
  676. glFlush();
  677. return;
  678. }
  679. /*!
  680. \brief Draw all glLists
  681. Uses glFlush() to ensure all drawing is complete
  682. before returning
  683. */
  684. void GS_draw_all_list(void)
  685. {
  686. gsd_calllists(0); /* not sure if 0 is right - MN */
  687. glFlush();
  688. return;
  689. }
  690. /*!
  691. \brief Delete pre-defined list
  692. \param list_id list id
  693. */
  694. void GS_delete_list(GLuint list_id)
  695. {
  696. gsd_deletelist(list_id, 1);
  697. return;
  698. }
  699. /*!
  700. \brief Draw lighting model
  701. */
  702. void GS_draw_lighting_model1(void)
  703. {
  704. static float center[3];
  705. float tcenter[3];
  706. if (!Modelshowing) {
  707. GS_get_modelposition1(center);
  708. }
  709. GS_v3eq(tcenter, center);
  710. gsd_zwritemask(0x0);
  711. gsd_backface(1);
  712. gsd_colormode(CM_AD);
  713. gsd_shademodel(DM_GOURAUD);
  714. gsd_pushmatrix();
  715. gsd_do_scale(1);
  716. if (Gv.vert_exag) {
  717. tcenter[Z] *= Gv.vert_exag;
  718. gsd_scale(1.0, 1.0, 1. / Gv.vert_exag);
  719. }
  720. gsd_drawsphere(tcenter, 0xDDDDDD, (float)(Longdim / 10.));
  721. gsd_popmatrix();
  722. Modelshowing = 1;
  723. gsd_backface(0);
  724. gsd_zwritemask(0xffffffff);
  725. return;
  726. }
  727. /*!
  728. \brief Draw lighting model
  729. Just turn off any cutting planes and draw it just outside near
  730. clipping plane, since lighting is infinite now
  731. */
  732. void GS_draw_lighting_model(void)
  733. {
  734. static float center[3], size;
  735. float tcenter[3], tsize;
  736. int i, wason[MAX_CPLANES];
  737. gsd_get_cplanes_state(wason);
  738. for (i = 0; i < MAX_CPLANES; i++) {
  739. if (wason[i]) {
  740. gsd_cplane_off(i);
  741. }
  742. }
  743. if (!Modelshowing) {
  744. GS_get_modelposition(&size, center);
  745. }
  746. GS_v3eq(tcenter, center);
  747. tsize = size;
  748. gsd_zwritemask(0x0);
  749. gsd_backface(1);
  750. gsd_colormode(CM_DIFFUSE);
  751. gsd_shademodel(DM_GOURAUD);
  752. gsd_pushmatrix();
  753. gsd_drawsphere(tcenter, 0xDDDDDD, tsize);
  754. gsd_popmatrix();
  755. Modelshowing = 1;
  756. gsd_backface(0);
  757. gsd_zwritemask(0xffffffff);
  758. for (i = 0; i < MAX_CPLANES; i++) {
  759. if (wason[i]) {
  760. gsd_cplane_on(i);
  761. }
  762. }
  763. gsd_flush();
  764. return;
  765. }
  766. /*!
  767. \brief Update current mask
  768. May be called to update total mask for a surface at convenient times
  769. instead of waiting until ready to redraw surface
  770. \param id surface id
  771. \return ?
  772. */
  773. int GS_update_curmask(int id)
  774. {
  775. geosurf *gs;
  776. gs = gs_get_surf(id);
  777. return (gs_update_curmask(gs));
  778. }
  779. /*!
  780. \brief Check if point is masked ?
  781. \param id surface id
  782. \param pt point
  783. \return 1 masked
  784. \return 0 not masked
  785. \return -1 on error, invalid surface id
  786. */
  787. int GS_is_masked(int id, float *pt)
  788. {
  789. geosurf *gs;
  790. Point3 tmp;
  791. if ((gs = gs_get_surf(id))) {
  792. tmp[X] = pt[X] - gs->ox;
  793. tmp[Y] = pt[Y] - gs->oy;
  794. return (gs_point_is_masked(gs, tmp));
  795. }
  796. return (-1);
  797. }
  798. /*!
  799. \brief Unset Scaled Difference surface
  800. */
  801. void GS_unset_SDsurf(void)
  802. {
  803. gsdiff_set_SDref(NULL);
  804. SDref_surf = 0;
  805. return;
  806. }
  807. /*!
  808. \brief Set surface as Scaled Difference surface
  809. \param id surface id
  810. \return 1 on success
  811. \return 0 on error, invalid surface id
  812. */
  813. int GS_set_SDsurf(int id)
  814. {
  815. geosurf *gs;
  816. if ((gs = gs_get_surf(id))) {
  817. gsdiff_set_SDref(gs);
  818. SDref_surf = id;
  819. return (1);
  820. }
  821. return (0);
  822. }
  823. /*!
  824. \brief Set ?
  825. \param scale scale value
  826. \return 1
  827. */
  828. int GS_set_SDscale(float scale)
  829. {
  830. gsdiff_set_SDscale(scale);
  831. return (1);
  832. }
  833. /*!
  834. \brief Get ?
  835. \param[out] id ?
  836. \return 1 on success
  837. \return 0 on error
  838. */
  839. int GS_get_SDsurf(int *id)
  840. {
  841. geosurf *gs;
  842. if ((gs = gsdiff_get_SDref())) {
  843. *id = SDref_surf;
  844. return (1);
  845. }
  846. return (0);
  847. }
  848. /*!
  849. \brief Get ?
  850. \param[out] scale value
  851. \return 1
  852. */
  853. int GS_get_SDscale(float *scale)
  854. {
  855. *scale = gsdiff_get_SDscale();
  856. return (1);
  857. }
  858. /*!
  859. \brief Update normals
  860. \param id surface id
  861. \return ?
  862. */
  863. int GS_update_normals(int id)
  864. {
  865. geosurf *gs;
  866. gs = gs_get_surf(id);
  867. return (gs_calc_normals(gs));
  868. }
  869. /*!
  870. \brief Get attributes
  871. \param id surface id
  872. \param att
  873. \param[out] set
  874. \param[out] constant
  875. \param[out] mapname
  876. \return 1 on success
  877. \return -1 on error (invalid surface id)
  878. */
  879. int GS_get_att(int id, int att, int *set, float *constant, char *mapname)
  880. {
  881. int src;
  882. geosurf *gs;
  883. gs = gs_get_surf(id);
  884. if (gs) {
  885. if (-1 != (src = gs_get_att_src(gs, att))) {
  886. *set = src;
  887. if (src == CONST_ATT) {
  888. *constant = gs->att[att].constant;
  889. }
  890. else if (src == MAP_ATT) {
  891. strcpy(mapname, gsds_get_name(gs->att[att].hdata));
  892. }
  893. return (1);
  894. }
  895. return (-1);
  896. }
  897. return (-1);
  898. }
  899. /*!
  900. \brief Get surface category on given position
  901. Prints "no data" or a description (i.e., "coniferous forest") to
  902. <i>catstr</i>. Usually call after GS_get_selected_point_on_surface().
  903. Define <i>att</i> as MAP_ATT
  904. \todo Allocate catstr using G_store()
  905. \param id surface id
  906. \param att attribute id (MAP_ATT)
  907. \param catstr cat string (must be allocated, dim?)
  908. \param x,y real coordinates
  909. \return -1 if no category info or point outside of window
  910. \return 1 on success
  911. */
  912. int GS_get_cat_at_xy(int id, int att, char *catstr, float x, float y)
  913. {
  914. int offset, drow, dcol, vrow, vcol;
  915. float ftmp, pt[3];
  916. typbuff *buff;
  917. geosurf *gs;
  918. *catstr = '\0';
  919. gs = gs_get_surf(id);
  920. if (NULL == gs) {
  921. return -1;
  922. }
  923. pt[X] = x;
  924. pt[Y] = y;
  925. gsd_real2surf(gs, pt);
  926. if (gs_point_is_masked(gs, pt)) {
  927. return -1;
  928. }
  929. if (!in_vregion(gs, pt)) {
  930. return -1;
  931. }
  932. if (MAP_ATT != gs_get_att_src(gs, att)) {
  933. sprintf(catstr, _("no category info"));
  934. return -1;
  935. }
  936. buff = gs_get_att_typbuff(gs, att, 0);
  937. vrow = Y2VROW(gs, pt[Y]);
  938. vcol = X2VCOL(gs, pt[X]);
  939. drow = VROW2DROW(gs, vrow);
  940. dcol = VCOL2DCOL(gs, vcol);
  941. offset = DRC2OFF(gs, drow, dcol);
  942. if (GET_MAPATT(buff, offset, ftmp)) {
  943. return
  944. (Gs_get_cat_label(gsds_get_name(gs->att[att].hdata),
  945. drow, dcol, catstr));
  946. }
  947. sprintf(catstr, _("no data"));
  948. return 1;
  949. }
  950. /*!
  951. \brief Get surface normal at x,y (real coordinates)
  952. Usually call after GS_get_selected_point_on_surface()
  953. \param id surface id
  954. \param x,y real coordinates
  955. \param[out] nv surface normal
  956. \return -1 if point outside of window or masked
  957. \return 1 on success
  958. */
  959. int GS_get_norm_at_xy(int id, float x, float y, float *nv)
  960. {
  961. int offset, drow, dcol, vrow, vcol;
  962. float pt[3];
  963. geosurf *gs;
  964. gs = gs_get_surf(id);
  965. if (NULL == gs) {
  966. return (-1);
  967. }
  968. if (gs->norm_needupdate) {
  969. gs_calc_normals(gs);
  970. }
  971. pt[X] = x;
  972. pt[Y] = y;
  973. gsd_real2surf(gs, pt);
  974. if (gs_point_is_masked(gs, pt)) {
  975. return (-1);
  976. }
  977. if (!in_vregion(gs, pt)) {
  978. return (-1);
  979. }
  980. vrow = Y2VROW(gs, pt[Y]);
  981. vcol = X2VCOL(gs, pt[X]);
  982. drow = VROW2DROW(gs, vrow);
  983. dcol = VCOL2DCOL(gs, vcol);
  984. offset = DRC2OFF(gs, drow, dcol);
  985. if (gs->norms) {
  986. FNORM(gs->norms[offset], nv);
  987. }
  988. else {
  989. /* otherwise must be a constant */
  990. nv[0] = 0.0;
  991. nv[1] = 0.0;
  992. nv[2] = 1.0;
  993. }
  994. return (1);
  995. }
  996. /*!
  997. \brief Get RGB color at given point
  998. Colors are translated to rgb and returned as Rxxx Gxxx Bxxx Usually
  999. call after GS_get_selected_point_on_surface().
  1000. Prints NULL or the value (i.e., "921.5") to valstr
  1001. \param id surface id
  1002. \param att attribute id
  1003. \param[out] valstr value string (allocated, dim?)
  1004. \param x,y real coordinates
  1005. \return -1 if point outside of window or masked
  1006. \return 1 on success
  1007. */
  1008. int GS_get_val_at_xy(int id, int att, char *valstr, float x, float y)
  1009. {
  1010. int offset, drow, dcol, vrow, vcol;
  1011. float ftmp, pt[3];
  1012. typbuff *buff;
  1013. geosurf *gs;
  1014. *valstr = '\0';
  1015. gs = gs_get_surf(id);
  1016. if (NULL == gs) {
  1017. return -1;
  1018. }
  1019. pt[X] = x;
  1020. pt[Y] = y;
  1021. gsd_real2surf(gs, pt);
  1022. if (gs_point_is_masked(gs, pt)) {
  1023. return -1;
  1024. }
  1025. if (!in_vregion(gs, pt)) {
  1026. return (-1);
  1027. }
  1028. if (CONST_ATT == gs_get_att_src(gs, att)) {
  1029. if (att == ATT_COLOR) {
  1030. int r, g, b, i;
  1031. i = gs->att[att].constant;
  1032. sprintf(valstr, "R%d G%d B%d",
  1033. INT_TO_RED(i, r), INT_TO_GRN(i, g), INT_TO_BLU(i, b));
  1034. }
  1035. else {
  1036. sprintf(valstr, "%f", gs->att[att].constant);
  1037. }
  1038. return 1;
  1039. }
  1040. else if (MAP_ATT != gs_get_att_src(gs, att)) {
  1041. return -1;
  1042. }
  1043. buff = gs_get_att_typbuff(gs, att, 0);
  1044. vrow = Y2VROW(gs, pt[Y]);
  1045. vcol = X2VCOL(gs, pt[X]);
  1046. drow = VROW2DROW(gs, vrow);
  1047. dcol = VCOL2DCOL(gs, vcol);
  1048. offset = DRC2OFF(gs, drow, dcol);
  1049. if (GET_MAPATT(buff, offset, ftmp)) {
  1050. if (att == ATT_COLOR) {
  1051. int r, g, b, i;
  1052. i = gs_mapcolor(gs_get_att_typbuff(gs, ATT_COLOR, 0),
  1053. &(gs->att[ATT_COLOR]), offset);
  1054. sprintf(valstr, "R%d G%d B%d",
  1055. INT_TO_RED(i, r), INT_TO_GRN(i, g), INT_TO_BLU(i, b));
  1056. }
  1057. else {
  1058. sprintf(valstr, "%f", ftmp);
  1059. }
  1060. return (1);
  1061. }
  1062. sprintf(valstr, "NULL");
  1063. return (1);
  1064. }
  1065. /*!
  1066. \brief Unset attribute
  1067. \param id surface id
  1068. \param att attribute id
  1069. \return ?
  1070. */
  1071. int GS_unset_att(int id, int att)
  1072. {
  1073. geosurf *gs;
  1074. gs = gs_get_surf(id);
  1075. gs->mask_needupdate = 1;
  1076. return (gs_set_att_src(gs, att, NOTSET_ATT));
  1077. }
  1078. /*!
  1079. \brief Set attribute constant
  1080. \param id surface id
  1081. \param att attribute id
  1082. \param constant value
  1083. \return ?
  1084. */
  1085. int GS_set_att_const(int id, int att, float constant)
  1086. {
  1087. geosurf *gs;
  1088. int ret;
  1089. gs = gs_get_surf(id);
  1090. ret = (gs_set_att_const(gs, att, constant));
  1091. Gs_update_attrange(gs, att);
  1092. return (ret);
  1093. }
  1094. /*!
  1095. \brief Set mask mode
  1096. Mask attribute special: constant is set to indicate invert or no
  1097. \param id surface id
  1098. \param mode id
  1099. \return mode id
  1100. \return -1 on error (invalid surface id)
  1101. */
  1102. int GS_set_maskmode(int id, int mode)
  1103. {
  1104. geosurf *gs;
  1105. gs = gs_get_surf(id);
  1106. if (gs) {
  1107. gs->att[ATT_MASK].constant = mode;
  1108. gs->mask_needupdate = 1;
  1109. return (mode);
  1110. }
  1111. return (-1);
  1112. }
  1113. /*!
  1114. \brief Get mask mode
  1115. \param id surface id
  1116. \param[out] mode id
  1117. \return 1 on success
  1118. \return -1 on error (invalid surface id)
  1119. */
  1120. int GS_get_maskmode(int id, int *mode)
  1121. {
  1122. geosurf *gs;
  1123. gs = gs_get_surf(id);
  1124. if (gs) {
  1125. *mode = gs->att[ATT_MASK].constant;
  1126. return (1);
  1127. }
  1128. return (-1);
  1129. }
  1130. /*!
  1131. \brief Set client data
  1132. \param id surface id
  1133. \param clientd pointer to client data struct
  1134. \return 1 on success
  1135. \return -1 on error (invalid surface id)
  1136. */
  1137. int GS_Set_ClientData(int id, void *clientd)
  1138. {
  1139. geosurf *gs;
  1140. gs = gs_get_surf(id);
  1141. if (gs) {
  1142. gs->clientdata = clientd;
  1143. return (1);
  1144. }
  1145. return (-1);
  1146. }
  1147. /*!
  1148. \brief Get client data
  1149. \param id surface id
  1150. \return pointer to client data
  1151. \return NULL on error
  1152. */
  1153. void *GS_Get_ClientData(int id)
  1154. {
  1155. geosurf *gs;
  1156. gs = gs_get_surf(id);
  1157. if (gs) {
  1158. return (gs->clientdata);
  1159. }
  1160. return (NULL);
  1161. }
  1162. /*!
  1163. \brief Get number of surfaces
  1164. \return number of surfaces
  1165. */
  1166. int GS_num_surfs(void)
  1167. {
  1168. return (gs_num_surfaces());
  1169. }
  1170. /*!
  1171. \brief Get surface list
  1172. Must be freed when not neeed!
  1173. \param[out] numsurf number of available surfaces
  1174. \return pointer to surface array
  1175. \return NULL on error
  1176. */
  1177. int *GS_get_surf_list(int *numsurfs)
  1178. {
  1179. int i, *ret;
  1180. *numsurfs = Next_surf;
  1181. if (Next_surf) {
  1182. ret = (int *)G_malloc(Next_surf * sizeof(int));
  1183. for (i = 0; i < Next_surf; i++) {
  1184. ret[i] = Surf_ID[i];
  1185. }
  1186. return (ret);
  1187. }
  1188. return (NULL);
  1189. }
  1190. /*!
  1191. \brief Delete surface
  1192. \param id surface id
  1193. \return 1 on success
  1194. \return -1 on error
  1195. */
  1196. int GS_delete_surface(int id)
  1197. {
  1198. int i, j, found;
  1199. found = FALSE;
  1200. G_debug(1, "GS_delete_surface(): id=%d", id);
  1201. if (GS_surf_exists(id)) {
  1202. gs_delete_surf(id);
  1203. for (i = 0; i < Next_surf && !found; i++) {
  1204. if (Surf_ID[i] == id) {
  1205. found = TRUE;
  1206. for (j = i; j < Next_surf; j++) {
  1207. Surf_ID[j] = Surf_ID[j + 1];
  1208. }
  1209. }
  1210. }
  1211. gv_update_drapesurfs();
  1212. if (found) {
  1213. --Next_surf;
  1214. return 1;
  1215. }
  1216. }
  1217. return -1;
  1218. }
  1219. /*!
  1220. \brief Load raster map as attribute
  1221. \param id surface id
  1222. \param filename filename
  1223. \param att attribute descriptor
  1224. \return -1 on error (invalid surface id)
  1225. \return ?
  1226. */
  1227. int GS_load_att_map(int id, const char *filename, int att)
  1228. {
  1229. geosurf *gs;
  1230. unsigned int changed;
  1231. unsigned int atty;
  1232. const char *mapset;
  1233. struct Cell_head rast_head;
  1234. int reuse, begin, hdata, ret, neg, has_null;
  1235. typbuff *tbuff;
  1236. G_debug(3, "GS_load_att_map(): map=%s", filename);
  1237. reuse = ret = neg = has_null = 0;
  1238. gs = gs_get_surf(id);
  1239. if (NULL == gs) {
  1240. return -1;
  1241. }
  1242. gs->mask_needupdate = (ATT_MASK == att || ATT_TOPO == att ||
  1243. (gs->nz_topo && ATT_TOPO == att) ||
  1244. (gs->nz_color && ATT_COLOR == att));
  1245. gs_set_att_src(gs, att, MAP_ATT);
  1246. /* Check against maps already loaded in memory */
  1247. /* if to be color attribute:
  1248. - if packed color for another surface, OK to reuse
  1249. - if unchanged, ok to reuse IF it's of type char (will have lookup)
  1250. */
  1251. begin = hdata = 1;
  1252. /* Get MAPSET to ensure names are fully qualified */
  1253. mapset = G_find_raster2(filename, "");
  1254. if (mapset == NULL) {
  1255. /* Check for valid filename */
  1256. G_warning("Raster map <%s> not found", filename);
  1257. return -1;
  1258. }
  1259. /* Check to see if map is in Region */
  1260. Rast_get_cellhd(filename, mapset, &rast_head);
  1261. if (rast_head.north <= wind.south ||
  1262. rast_head.south >= wind.north ||
  1263. rast_head.east <= wind.west || rast_head.west >= wind.east) {
  1264. G_warning(_("Raster map <%s> is outside of current region. Load failed."),
  1265. G_fully_qualified_name(filename, mapset));
  1266. }
  1267. while (!reuse && (0 < hdata)) {
  1268. changed = CF_COLOR_PACKED;
  1269. atty = ATTY_FLOAT | ATTY_CHAR | ATTY_INT | ATTY_SHORT | ATTY_MASK;
  1270. if (0 < (hdata = gsds_findh(filename, &changed, &atty, begin))) {
  1271. G_debug(3, "GS_load_att_map(): %s already has data handle %d.CF=%x",
  1272. filename, hdata, changed);
  1273. /* handle found */
  1274. if (ATT_COLOR == att) {
  1275. if ((changed == CF_COLOR_PACKED) ||
  1276. (!changed && atty == ATTY_CHAR)) {
  1277. reuse = 1;
  1278. }
  1279. }
  1280. else if (atty == ATTY_MASK && att != ATT_MASK) {
  1281. reuse = 0;
  1282. /* should also free mask data & share new - but need backward
  1283. reference? */
  1284. }
  1285. else if (!changed) {
  1286. reuse = 1;
  1287. }
  1288. }
  1289. begin = 0;
  1290. }
  1291. if (reuse) {
  1292. gs->att[att].hdata = hdata;
  1293. gs_set_att_type(gs, att, atty); /* ?? */
  1294. /* free lookup & set to NULL! */
  1295. if (atty == ATTY_INT) {
  1296. if (gs->att[att].lookup) {
  1297. free(gs->att[att].lookup);
  1298. gs->att[att].lookup = NULL;
  1299. }
  1300. }
  1301. /* TODO: FIX THIS stuff with lookup sharing! */
  1302. G_debug(3, "GS_load_att_map(): %s is being reused. hdata=%d",
  1303. filename, hdata);
  1304. }
  1305. else {
  1306. G_debug(3, "GS_load_att_map(): %s not loaded in correct form - loading now",
  1307. filename);
  1308. /* not loaded - need to get new dataset handle */
  1309. gs->att[att].hdata = gsds_newh(filename);
  1310. tbuff = gs_get_att_typbuff(gs, att, 1);
  1311. /* TODO: Provide mechanism for loading certain attributes at
  1312. specified sizes, allow to scale or cap, or scale non-zero */
  1313. if (ATT_MASK == att) {
  1314. atty = ATTY_MASK;
  1315. }
  1316. else {
  1317. atty = Gs_numtype(filename, &neg);
  1318. }
  1319. #ifdef MAYBE_LATER
  1320. if (att == ATT_COLOR && atty == ATTY_SHORT) {
  1321. atty = (neg ? ATTY_INT : ATTY_SHORT);
  1322. }
  1323. #endif
  1324. if (att == ATT_COLOR && atty == ATTY_SHORT) {
  1325. atty = ATTY_INT;
  1326. }
  1327. if (0 == gs_malloc_att_buff(gs, att, ATTY_NULL)) {
  1328. G_fatal_error(_("GS_load_att_map(): Out of memory. Unable to load map"));
  1329. }
  1330. switch (atty) {
  1331. case ATTY_MASK:
  1332. if (0 == gs_malloc_att_buff(gs, att, ATTY_MASK)) {
  1333. G_fatal_error(_("GS_load_att_map(): Out of memory. Unable to load map"));
  1334. }
  1335. ret = Gs_loadmap_as_bitmap(&wind, filename, tbuff->bm);
  1336. break;
  1337. case ATTY_CHAR:
  1338. if (0 == gs_malloc_att_buff(gs, att, ATTY_CHAR)) {
  1339. G_fatal_error(_("GS_load_att_map(): Out of memory. Unable to load map"));
  1340. }
  1341. ret = Gs_loadmap_as_char(&wind, filename, tbuff->cb,
  1342. tbuff->nm, &has_null);
  1343. break;
  1344. case ATTY_SHORT:
  1345. if (0 == gs_malloc_att_buff(gs, att, ATTY_SHORT)) {
  1346. G_fatal_error(_("GS_load_att_map(): Out of memory. Unable to load map"));
  1347. }
  1348. ret = Gs_loadmap_as_short(&wind, filename, tbuff->sb,
  1349. tbuff->nm, &has_null);
  1350. break;
  1351. case ATTY_FLOAT:
  1352. if (0 == gs_malloc_att_buff(gs, att, ATTY_FLOAT)) {
  1353. G_fatal_error(_("GS_load_att_map(): Out of memory. Unable to load map"));
  1354. }
  1355. ret = Gs_loadmap_as_float(&wind, filename, tbuff->fb,
  1356. tbuff->nm, &has_null);
  1357. break;
  1358. case ATTY_INT:
  1359. default:
  1360. if (0 == gs_malloc_att_buff(gs, att, ATTY_INT)) {
  1361. G_fatal_error(_("GS_load_att_map(): Out of memory. Unable to load map"));
  1362. }
  1363. ret = Gs_loadmap_as_int(&wind, filename, tbuff->ib,
  1364. tbuff->nm, &has_null);
  1365. break;
  1366. } /* Done with switch */
  1367. if (ret == -1) {
  1368. gsds_free_data_buff(gs->att[att].hdata, ATTY_NULL);
  1369. return -1;
  1370. }
  1371. G_debug(4, " has_null=%d", has_null);
  1372. if (!has_null) {
  1373. gsds_free_data_buff(gs->att[att].hdata, ATTY_NULL);
  1374. }
  1375. else {
  1376. gs_update_curmask(gs);
  1377. }
  1378. } /* end if not reuse */
  1379. if (ATT_COLOR == att) {
  1380. #ifdef MAYBE_LATER
  1381. if (ATTY_INT == atty) {
  1382. Gs_pack_colors(filename, tbuff->ib, gs->rows, gs->cols);
  1383. gsds_set_changed(gs->att[att].hdata, CF_COLOR_PACKED);
  1384. gs->att[att].lookup = NULL;
  1385. }
  1386. else {
  1387. gs_malloc_lookup(gs, att);
  1388. Gs_build_lookup(filename, gs->att[att].lookup);
  1389. }
  1390. #else
  1391. if (ATTY_CHAR == atty) {
  1392. if (!gs->att[att].lookup) {
  1393. /* might already exist if reusing */
  1394. gs_malloc_lookup(gs, att);
  1395. Gs_build_256lookup(filename, gs->att[att].lookup);
  1396. }
  1397. }
  1398. else if (ATTY_FLOAT == atty) {
  1399. if (!reuse) {
  1400. if (0 == gs_malloc_att_buff(gs, att, ATTY_INT)) {
  1401. G_fatal_error(_("GS_load_att_map(): Out of memory. Unable to load map"));
  1402. }
  1403. Gs_pack_colors_float(filename, tbuff->fb, tbuff->ib,
  1404. gs->rows, gs->cols);
  1405. gsds_set_changed(gs->att[att].hdata, CF_COLOR_PACKED);
  1406. gsds_free_data_buff(gs->att[att].hdata, ATTY_FLOAT);
  1407. gs->att[att].lookup = NULL;
  1408. }
  1409. }
  1410. else {
  1411. if (!reuse) {
  1412. Gs_pack_colors(filename, tbuff->ib, gs->rows, gs->cols);
  1413. gsds_set_changed(gs->att[att].hdata, CF_COLOR_PACKED);
  1414. gs->att[att].lookup = NULL;
  1415. }
  1416. }
  1417. #endif
  1418. }
  1419. if (ATT_TOPO == att) {
  1420. gs_init_normbuff(gs);
  1421. /* S_DIFF: should also check here to see if this surface is a
  1422. reference surface for scaled differences, if so update references
  1423. to it */
  1424. }
  1425. if (ret < 0) {
  1426. G_warning(_("Loading failed"));
  1427. }
  1428. if (-1 == Gs_update_attrange(gs, att)) {
  1429. G_warning(_("Error finding range"));
  1430. }
  1431. return ret;
  1432. }
  1433. /*!
  1434. \brief Draw surface
  1435. \param id surface id
  1436. */
  1437. void GS_draw_surf(int id)
  1438. {
  1439. geosurf *gs;
  1440. G_debug(3, "GS_draw_surf(): id=%d", id);
  1441. gs = gs_get_surf(id);
  1442. if (gs) {
  1443. gsd_shademodel(gs->draw_mode & DM_GOURAUD);
  1444. if (gs->draw_mode & DM_POLY) {
  1445. gsd_surf(gs);
  1446. }
  1447. if (gs->draw_mode & DM_WIRE) {
  1448. gsd_wire_surf(gs);
  1449. }
  1450. /* TODO: write wire/poly draw routines */
  1451. if (gs->draw_mode & DM_WIRE_POLY) {
  1452. gsd_surf(gs);
  1453. gsd_wire_surf(gs);
  1454. }
  1455. }
  1456. return;
  1457. }
  1458. /*!
  1459. \brief Draw surface wire
  1460. Overrides draw_mode for fast display
  1461. \param id surface id
  1462. */
  1463. void GS_draw_wire(int id)
  1464. {
  1465. geosurf *gs;
  1466. G_debug(3, "GS_draw_wire(): id=%d", id);
  1467. gs = gs_get_surf(id);
  1468. if (gs) {
  1469. gsd_wire_surf(gs);
  1470. }
  1471. return;
  1472. }
  1473. /*!
  1474. \brief Draw all wires
  1475. Overrides draw_mode for fast display
  1476. */
  1477. void GS_alldraw_wire(void)
  1478. {
  1479. geosurf *gs;
  1480. int i;
  1481. for (i = 0; i < Next_surf; i++) {
  1482. if ((gs = gs_get_surf(Surf_ID[i]))) {
  1483. gsd_wire_surf(gs);
  1484. }
  1485. }
  1486. return;
  1487. }
  1488. /*!
  1489. \brief Draw all surfaces
  1490. */
  1491. void GS_alldraw_surf(void)
  1492. {
  1493. int i;
  1494. for (i = 0; i < Next_surf; i++) {
  1495. GS_draw_surf(Surf_ID[i]);
  1496. }
  1497. return;
  1498. }
  1499. /*!
  1500. \brief Set Z exag for surface
  1501. \param id surface id
  1502. \param exag z-exag value
  1503. */
  1504. void GS_set_exag(int id, float exag)
  1505. {
  1506. geosurf *gs;
  1507. G_debug(3, "GS_set_exag");
  1508. gs = gs_get_surf(id);
  1509. if (gs) {
  1510. if (gs->z_exag != exag) {
  1511. gs->norm_needupdate = 1;
  1512. }
  1513. gs->z_exag = exag;
  1514. }
  1515. return;
  1516. }
  1517. /*!
  1518. \brief Set global z-exag value
  1519. \param exag exag value to be set up
  1520. */
  1521. void GS_set_global_exag(float exag)
  1522. {
  1523. G_debug(3, "GS_set_global_exag");
  1524. Gv.vert_exag = exag;
  1525. /* GL_NORMALIZE */
  1526. /* Only need to update norms gs_norms.c
  1527. * if exag is used in norm equation which
  1528. * it is not! If GL_NORMALIZE is disabled
  1529. * will need to include.
  1530. gs_setall_norm_needupdate();
  1531. */
  1532. return;
  1533. }
  1534. /*!
  1535. \brief Get global z-exag value
  1536. \return value
  1537. */
  1538. float GS_global_exag(void)
  1539. {
  1540. G_debug(3, "GS_global_exag(): %g", Gv.vert_exag);
  1541. return (Gv.vert_exag);
  1542. }
  1543. /*!
  1544. \brief Set wire color
  1545. \todo error-handling
  1546. \param id surface id
  1547. \param colr color value
  1548. */
  1549. void GS_set_wire_color(int id, int colr)
  1550. {
  1551. geosurf *gs;
  1552. G_debug(3, "GS_set_wire_color");
  1553. gs = gs_get_surf(id);
  1554. if (gs) {
  1555. gs->wire_color = colr;
  1556. }
  1557. return;
  1558. }
  1559. /*!
  1560. \brief Get wire color
  1561. \param id surface id
  1562. \param[out] colr color value
  1563. \return 1 on success
  1564. \return -1 on error
  1565. */
  1566. int GS_get_wire_color(int id, int *colr)
  1567. {
  1568. geosurf *gs;
  1569. gs = gs_get_surf(id);
  1570. if (gs) {
  1571. *colr = gs->wire_color;
  1572. return (1);
  1573. }
  1574. return (-1);
  1575. }
  1576. /*!
  1577. \brief Set all draw-modes
  1578. \param mode mode id
  1579. \return 0 on success
  1580. \return -1 on error
  1581. */
  1582. int GS_setall_drawmode(int mode)
  1583. {
  1584. int i;
  1585. for (i = 0; i < Next_surf; i++) {
  1586. if (0 != GS_set_drawmode(Surf_ID[i], mode)) {
  1587. return (-1);
  1588. }
  1589. }
  1590. return (0);
  1591. }
  1592. /*!
  1593. \brief Set draw mode
  1594. \param id surface id
  1595. \param mode mode type(s)
  1596. \return 0 on success
  1597. \return -1 on error (invalid surface id)
  1598. */
  1599. int GS_set_drawmode(int id, int mode)
  1600. {
  1601. geosurf *gs;
  1602. G_debug(3, "GS_set_drawmode(): id=%d mode=%d", id, mode);
  1603. gs = gs_get_surf(id);
  1604. if (gs) {
  1605. gs->draw_mode = mode;
  1606. return (0);
  1607. }
  1608. return (-1);
  1609. }
  1610. /*!
  1611. \brief Get draw mode
  1612. \param id surface id
  1613. \param[out] mode mode id
  1614. \return 1 on success
  1615. \return -1 on error (invalid surface id)
  1616. */
  1617. int GS_get_drawmode(int id, int *mode)
  1618. {
  1619. geosurf *gs;
  1620. gs = gs_get_surf(id);
  1621. if (gs) {
  1622. *mode = gs->draw_mode;
  1623. return (1);
  1624. }
  1625. return (-1);
  1626. }
  1627. /*!
  1628. \brief Set no-zero ?
  1629. \param id surface id
  1630. \param att attribute id
  1631. \param mode mode id
  1632. */
  1633. void GS_set_nozero(int id, int att, int mode)
  1634. {
  1635. geosurf *gs;
  1636. G_debug(3, "GS_set_nozero");
  1637. gs = gs_get_surf(id);
  1638. if (gs) {
  1639. if (att == ATT_TOPO) {
  1640. gs->nz_topo = mode;
  1641. gs->mask_needupdate = 1;
  1642. }
  1643. if (att == ATT_COLOR) {
  1644. gs->nz_color = mode;
  1645. gs->mask_needupdate = 1;
  1646. }
  1647. }
  1648. return;
  1649. }
  1650. /*!
  1651. \brief Get no-zero ?
  1652. \param id surface id
  1653. \param att attribute id
  1654. \param[out] mode mode id
  1655. \return -1 on error (invalid surface id)
  1656. \return 1 on success
  1657. */
  1658. int GS_get_nozero(int id, int att, int *mode)
  1659. {
  1660. geosurf *gs;
  1661. G_debug(3, "GS_set_nozero");
  1662. gs = gs_get_surf(id);
  1663. if (gs) {
  1664. if (att == ATT_TOPO) {
  1665. *mode = gs->nz_topo;
  1666. }
  1667. else if (att == ATT_COLOR) {
  1668. *mode = gs->nz_color;
  1669. }
  1670. else {
  1671. return (-1);
  1672. }
  1673. return (1);
  1674. }
  1675. return (-1);
  1676. }
  1677. /*!
  1678. \brief Set all draw resolutions
  1679. \param xres,yres x/y resolution value
  1680. \param xwire,ywire x/y wire value
  1681. \return 0 on success
  1682. \return -1 on error
  1683. */
  1684. int GS_setall_drawres(int xres, int yres, int xwire, int ywire)
  1685. {
  1686. int i;
  1687. for (i = 0; i < Next_surf; i++) {
  1688. if (0 != GS_set_drawres(Surf_ID[i], xres, yres, xwire, ywire)) {
  1689. return (-1);
  1690. }
  1691. }
  1692. return (0);
  1693. }
  1694. /*!
  1695. \brief Set draw resolution for surface
  1696. \param id surface id
  1697. \param xres,yres x/y resolution value
  1698. \param xwire,ywire x/y wire value
  1699. \return -1 on error
  1700. \return 0 on success
  1701. */
  1702. int GS_set_drawres(int id, int xres, int yres, int xwire, int ywire)
  1703. {
  1704. geosurf *gs;
  1705. G_debug(3, "GS_set_drawres() id=%d xyres=%d/%d xywire=%d/%d",
  1706. id, xres, yres, xwire, ywire);
  1707. if (xres < 1 || yres < 1 || xwire < 1 || ywire < 1) {
  1708. return (-1);
  1709. }
  1710. gs = gs_get_surf(id);
  1711. if (gs) {
  1712. if (gs->x_mod != xres || gs->y_mod != yres) {
  1713. gs->norm_needupdate = 1;
  1714. }
  1715. gs->x_mod = xres;
  1716. gs->y_mod = yres;
  1717. gs->x_modw = xwire;
  1718. gs->y_modw = ywire;
  1719. }
  1720. return (0);
  1721. }
  1722. /*!
  1723. \brief Get draw resolution of surface
  1724. \param id surface id
  1725. \param[out] xres,yres x/y resolution value
  1726. \param[out] xwire,ywire x/y wire value
  1727. */
  1728. void GS_get_drawres(int id, int *xres, int *yres, int *xwire, int *ywire)
  1729. {
  1730. geosurf *gs;
  1731. G_debug(3, "GS_get_drawres");
  1732. gs = gs_get_surf(id);
  1733. if (gs) {
  1734. *xres = gs->x_mod;
  1735. *yres = gs->y_mod;
  1736. *xwire = gs->x_modw;
  1737. *ywire = gs->y_modw;
  1738. }
  1739. return;
  1740. }
  1741. /*!
  1742. \brief Get dimension of surface
  1743. \param id surface id
  1744. \param[out] rows,cols number of rows/cols
  1745. */
  1746. void GS_get_dims(int id, int *rows, int *cols)
  1747. {
  1748. geosurf *gs;
  1749. gs = gs_get_surf(id);
  1750. if (gs) {
  1751. *rows = gs->rows;
  1752. *cols = gs->cols;
  1753. }
  1754. return;
  1755. }
  1756. /*!
  1757. \brief Get exag-value guess
  1758. Use no_zero range because if zero IS data, then range won't be that
  1759. much off (it's just a GUESS, after all), but if zero is NO data, could
  1760. drastically affect guess
  1761. \param id surface id
  1762. \param[out] exag exag value
  1763. \return 1 on success
  1764. \return -1 on error
  1765. */
  1766. int GS_get_exag_guess(int id, float *exag)
  1767. {
  1768. geosurf *gs;
  1769. float guess;
  1770. gs = gs_get_surf(id);
  1771. guess = 1.0;
  1772. /* if gs is type const return guess = 1.0 */
  1773. if (CONST_ATT == gs_get_att_src(gs, ATT_TOPO)) {
  1774. *exag = guess;
  1775. return (1);
  1776. }
  1777. if (gs) {
  1778. if (gs->zrange_nz == 0.0) {
  1779. *exag = 0.0;
  1780. return (1);
  1781. }
  1782. G_debug(3, "GS_get_exag_guess(): %f %f", gs->zrange_nz, Longdim);
  1783. while (gs->zrange_nz * guess / Longdim >= .25) {
  1784. guess *= .1;
  1785. G_debug(3, "GS_get_exag_guess(): %f", guess);
  1786. }
  1787. while (gs->zrange_nz * guess / Longdim < .025) {
  1788. guess *= 10.;
  1789. G_debug(3, "GS_get_exag_guess(): %f", guess);
  1790. }
  1791. *exag = guess;
  1792. return (1);
  1793. }
  1794. return (-1);
  1795. }
  1796. /*!
  1797. \brief Get Z extents for all loaded surfaces
  1798. Treating zeros as "no data"
  1799. \param[out] min min value
  1800. \param[out] max max value
  1801. */
  1802. void GS_get_zrange_nz(float *min, float *max)
  1803. {
  1804. int i, first = 1;
  1805. geosurf *gs;
  1806. for (i = 0; i < Next_surf; i++) {
  1807. if ((gs = gs_get_surf(Surf_ID[i]))) {
  1808. if (first) {
  1809. first = 0;
  1810. *min = gs->zmin_nz;
  1811. *max = gs->zmax_nz;
  1812. }
  1813. if (gs->zmin_nz < *min) {
  1814. *min = gs->zmin_nz;
  1815. }
  1816. if (gs->zmax_nz > *max) {
  1817. *max = gs->zmax_nz;
  1818. }
  1819. }
  1820. }
  1821. G_debug(3, "GS_get_zrange_nz(): min=%g max=%g", *min, *max);
  1822. return;
  1823. }
  1824. /*!
  1825. \brief Set translation (surface position)
  1826. \param id surface id
  1827. \param xtrans,ytrans,ztrans translation values
  1828. */
  1829. void GS_set_trans(int id, float xtrans, float ytrans, float ztrans)
  1830. {
  1831. geosurf *gs;
  1832. gs = gs_get_surf(id);
  1833. if (gs) {
  1834. gs->x_trans = xtrans;
  1835. gs->y_trans = ytrans;
  1836. gs->z_trans = ztrans;
  1837. }
  1838. G_debug(3, "GS_set_trans(): id=%d, x=%f, y=%f, z=%f",
  1839. id, xtrans, ytrans, ztrans);
  1840. return;
  1841. }
  1842. /*!
  1843. \brief Get translation values (surface position)
  1844. \param id surface id
  1845. \param[out] xtrans,ytrans,ztrans trans values
  1846. */
  1847. void GS_get_trans(int id, float *xtrans, float *ytrans, float *ztrans)
  1848. {
  1849. geosurf *gs;
  1850. gs = gs_get_surf(id);
  1851. if (gs) {
  1852. *xtrans = gs->x_trans;
  1853. *ytrans = gs->y_trans;
  1854. *ztrans = gs->z_trans;
  1855. }
  1856. G_debug(3, "GS_get_trans: id=%d, x=%f, y=%f, z=%f",
  1857. id, *xtrans, *ytrans, *ztrans);
  1858. return;
  1859. }
  1860. /*!
  1861. \brief Get default draw color
  1862. \return color value
  1863. */
  1864. unsigned int GS_default_draw_color(void)
  1865. {
  1866. G_debug(3, "GS_default_draw_color");
  1867. return ((unsigned int)Gd.bgcol);
  1868. }
  1869. /*!
  1870. \brief Get background color
  1871. \return color value
  1872. */
  1873. unsigned int GS_background_color(void)
  1874. {
  1875. return ((unsigned int)Gd.bgcol);
  1876. }
  1877. /*!
  1878. \brief Sets which buffer to draw to
  1879. \param where GSD_BOTH, GSD_FRONT, GSD_BACK
  1880. */
  1881. void GS_set_draw(int where)
  1882. {
  1883. Buffermode = where;
  1884. switch (where) {
  1885. case GSD_BOTH:
  1886. gsd_frontbuffer(1);
  1887. gsd_backbuffer(1);
  1888. break;
  1889. case GSD_FRONT:
  1890. gsd_frontbuffer(1);
  1891. gsd_backbuffer(0);
  1892. break;
  1893. case GSD_BACK:
  1894. default:
  1895. gsd_frontbuffer(0);
  1896. gsd_backbuffer(1);
  1897. break;
  1898. }
  1899. return;
  1900. }
  1901. /*
  1902. \brief Ready to draw
  1903. */
  1904. void GS_ready_draw(void)
  1905. {
  1906. G_debug(3, "GS_ready_draw");
  1907. gsd_set_view(&Gv, &Gd);
  1908. return;
  1909. }
  1910. /*!
  1911. \brief Draw done, swap buffers
  1912. */
  1913. void GS_done_draw(void)
  1914. {
  1915. G_debug(3, "GS_done_draw");
  1916. if (GSD_BACK == Buffermode) {
  1917. gsd_swapbuffers();
  1918. }
  1919. gsd_flush();
  1920. return;
  1921. }
  1922. /*!
  1923. \brief Set focus
  1924. \param realto real coordinates to
  1925. */
  1926. void GS_set_focus(float *realto)
  1927. {
  1928. G_debug(3, "GS_set_focus(): %f,%f,%f", realto[0], realto[1], realto[2]);
  1929. Gv.infocus = 1;
  1930. GS_v3eq(Gv.real_to, realto);
  1931. gsd_set_view(&Gv, &Gd);
  1932. return;
  1933. }
  1934. /*!
  1935. \brief Set real focus
  1936. \param realto real coordinates to
  1937. */
  1938. void GS_set_focus_real(float *realto)
  1939. {
  1940. G_get_set_window(&wind);
  1941. realto[X] = realto[X] - wind.west - (wind.ew_res / 2.);
  1942. realto[Y] = realto[Y] - wind.south - (wind.ns_res / 2.);
  1943. Gv.infocus = 1;
  1944. GS_v3eq(Gv.real_to, realto);
  1945. gsd_set_view(&Gv, &Gd);
  1946. return;
  1947. }
  1948. /*!
  1949. \brief Get focus
  1950. OK to call with NULL argument if just want to check state
  1951. \param realto real coordinates to
  1952. \return ?
  1953. */
  1954. int GS_get_focus(float *realto)
  1955. {
  1956. G_debug(3, "GS_get_focus");
  1957. if (Gv.infocus) {
  1958. if (realto) {
  1959. GS_v3eq(realto, Gv.real_to);
  1960. }
  1961. }
  1962. return (Gv.infocus);
  1963. }
  1964. /*!
  1965. \brief Set focus to map center
  1966. \param id surface id
  1967. */
  1968. void GS_set_focus_center_map(int id)
  1969. {
  1970. float center[3];
  1971. geosurf *gs;
  1972. G_debug(3, "GS_set_focus_center_map");
  1973. gs = gs_get_surf(id);
  1974. if (gs) {
  1975. center[X] = (gs->xmax - gs->xmin) / 2.;
  1976. center[Y] = (gs->ymax - gs->ymin) / 2.;
  1977. center[Z] = (gs->zmax_nz + gs->zmin_nz) / 2.;
  1978. /* not yet working
  1979. buff = gs_get_att_typbuff(gs, ATT_TOPO, 0);
  1980. offset = gs->rows*gs->cols/2 + gs->cols/2;
  1981. if (buff)
  1982. {
  1983. if (GET_MAPATT(buff, offset, tmp))
  1984. {
  1985. center[Z] = tmp;
  1986. }
  1987. }
  1988. */
  1989. GS_set_focus(center);
  1990. }
  1991. }
  1992. /*!
  1993. \brief Move viewpoint
  1994. \param pt 'from' model coordinates
  1995. */
  1996. void GS_moveto(float *pt)
  1997. {
  1998. float ft[3];
  1999. G_debug(3, "GS_moveto(): %f,%f,%f", pt[0], pt[1], pt[2]);
  2000. if (Gv.infocus) {
  2001. GS_v3eq(Gv.from_to[FROM], pt);
  2002. /*
  2003. GS_v3eq(Gv.from_to[TO], Gv.real_to);
  2004. */
  2005. GS_v3normalize(Gv.from_to[FROM], Gv.from_to[TO]);
  2006. /* update inclination, look_dir if we're keeping these */
  2007. }
  2008. else {
  2009. GS_v3eq(ft, Gv.from_to[TO]);
  2010. GS_v3sub(ft, Gv.from_to[FROM]);
  2011. GS_v3eq(Gv.from_to[FROM], pt);
  2012. GS_v3eq(Gv.from_to[TO], pt);
  2013. GS_v3add(Gv.from_to[TO], ft);
  2014. }
  2015. return;
  2016. }
  2017. /*!
  2018. \brief Move position to (real)
  2019. \param pt point real coordinates
  2020. */
  2021. void GS_moveto_real(float *pt)
  2022. {
  2023. gsd_real2model(pt);
  2024. GS_moveto(pt);
  2025. return;
  2026. }
  2027. /*!
  2028. \brief Get z-extent for a single surface
  2029. \param id surface id
  2030. \param[out] min min z-value
  2031. \param[out] max max z-value
  2032. \param[out] mid middle z-value
  2033. \return -1 on error (invalid surface id)
  2034. \return ?
  2035. */
  2036. int GS_get_zextents(int id, float *min, float *max, float *mid)
  2037. {
  2038. geosurf *gs;
  2039. if (NULL == (gs = gs_get_surf(id))) {
  2040. return (-1);
  2041. }
  2042. G_debug(3, "GS_get_zextents(): id=%d", id);
  2043. return (gs_get_zextents(gs, min, max, mid));
  2044. }
  2045. /*!
  2046. \brief Get z-extent for all loaded surfaces
  2047. \param[out] min min z-value
  2048. \param[out] max max z-value
  2049. \param doexag use z-exaggeration
  2050. \return 1 on success
  2051. \return -1 on error
  2052. */
  2053. int GS_get_zrange(float *min, float *max, int doexag)
  2054. {
  2055. int ret_surf, ret_vol;
  2056. float surf_min, surf_max;
  2057. float vol_min, vol_max;
  2058. ret_surf = gs_get_zrange(&surf_min, &surf_max);
  2059. ret_vol = gvl_get_zrange(&vol_min, &vol_max);
  2060. if (ret_surf > 0 && ret_vol > 0) {
  2061. *min = (surf_min < vol_min) ? surf_min : vol_min;
  2062. *max = (surf_max < vol_max) ? surf_max : vol_max;
  2063. }
  2064. else if (ret_surf > 0) {
  2065. *min = surf_min;
  2066. *max = surf_max;
  2067. }
  2068. else if (ret_vol > 0) {
  2069. *min = vol_min;
  2070. *max = vol_max;
  2071. }
  2072. if (doexag) {
  2073. *min *= Gv.vert_exag;
  2074. *max *= Gv.vert_exag;
  2075. }
  2076. G_debug(3, "GS_get_zrange(): min=%g max=%g", *min, *max);
  2077. return ((ret_surf > 0 || ret_vol > 0) ? (1) : (-1));
  2078. }
  2079. /*!
  2080. \brief Get viewpoint 'from' position
  2081. \param[out] fr from model coordinates
  2082. */
  2083. void GS_get_from(float *fr)
  2084. {
  2085. GS_v3eq(fr, Gv.from_to[FROM]);
  2086. G_debug(3, "GS_get_from(): %f,%f,%f", fr[0], fr[1], fr[2]);
  2087. return;
  2088. }
  2089. /*!
  2090. \brief Get viewpoint 'from' real coordinates
  2091. \param[out] fr 'from' real coordinates
  2092. */
  2093. void GS_get_from_real(float *fr)
  2094. {
  2095. GS_v3eq(fr, Gv.from_to[FROM]);
  2096. gsd_model2real(fr);
  2097. return;
  2098. }
  2099. /*!
  2100. \brief Get 'to' real coordinates
  2101. \param[out] to 'to' real coordinates
  2102. */
  2103. void GS_get_to_real(float *to)
  2104. {
  2105. float realto[3];
  2106. G_get_set_window(&wind);
  2107. GS_get_focus(realto);
  2108. to[X] = realto[X] + wind.west + (wind.ew_res / 2.);
  2109. to[Y] = realto[Y] + wind.south + (wind.ns_res / 2.);
  2110. to[Z] = realto[Z];
  2111. return;
  2112. }
  2113. /*!
  2114. \brief Get zoom setup
  2115. \param[out] a,b,c,d current viewport settings
  2116. \param[out] maxx,maxy max viewport size
  2117. */
  2118. void GS_zoom_setup(int *a, int *b, int *c, int *d, int *maxx, int *maxy)
  2119. {
  2120. GLint tmp[4];
  2121. GLint num[2];
  2122. gsd_getViewport(tmp, num);
  2123. *a = tmp[0];
  2124. *b = tmp[1];
  2125. *c = tmp[2];
  2126. *d = tmp[3];
  2127. *maxx = num[0];
  2128. *maxy = num[1];
  2129. return;
  2130. }
  2131. /*!
  2132. \brief Get 'to' model coordinates
  2133. \todo need set_to? - just use viewdir?
  2134. \param[out] to 'to' model coordinates
  2135. */
  2136. void GS_get_to(float *to)
  2137. {
  2138. G_debug(3, "GS_get_to");
  2139. GS_v3eq(to, Gv.from_to[TO]);
  2140. return;
  2141. }
  2142. /*!
  2143. \brief Get viewdir
  2144. \param[out] dir viewdir value
  2145. */
  2146. void GS_get_viewdir(float *dir)
  2147. {
  2148. GS_v3dir(Gv.from_to[FROM], Gv.from_to[TO], dir);
  2149. return;
  2150. }
  2151. /*!
  2152. \brief Set viewdir
  2153. Automatically turns off focus
  2154. \param dir viewdir value
  2155. */
  2156. void GS_set_viewdir(float *dir)
  2157. {
  2158. float tmp[3];
  2159. GS_v3eq(tmp, dir);
  2160. GS_v3norm(tmp);
  2161. GS_v3eq(Gv.from_to[TO], Gv.from_to[FROM]);
  2162. GS_v3add(Gv.from_to[TO], tmp);
  2163. GS_set_nofocus();
  2164. gsd_set_view(&Gv, &Gd);
  2165. return;
  2166. }
  2167. /*!
  2168. \brief Set field of view
  2169. \param fov fov value
  2170. */
  2171. void GS_set_fov(int fov)
  2172. {
  2173. Gv.fov = fov;
  2174. return;
  2175. }
  2176. /*!
  2177. \brief Get fied of view
  2178. \return field of view, in 10ths of degrees
  2179. */
  2180. int GS_get_fov(void)
  2181. {
  2182. return (Gv.fov);
  2183. }
  2184. /*!
  2185. \brief Get twist value
  2186. 10ths of degrees off twelve o'clock
  2187. */
  2188. int GS_get_twist(void)
  2189. {
  2190. return (Gv.twist);
  2191. }
  2192. /*!
  2193. \brief Set viewpoint twist value
  2194. 10ths of degrees off twelve o'clock
  2195. \param t tenths of degrees clockwise from 12:00.
  2196. */
  2197. void GS_set_twist(int t)
  2198. {
  2199. Gv.twist = t;
  2200. return;
  2201. }
  2202. /*!
  2203. \brief Set rotation params
  2204. */
  2205. void GS_set_rotation(double angle, double x, double y, double z)
  2206. {
  2207. Gv.rotate.rot_angle = angle;
  2208. Gv.rotate.rot_axes[0] = x;
  2209. Gv.rotate.rot_axes[1] = y;
  2210. Gv.rotate.rot_axes[2] = z;
  2211. Gv.rotate.do_rot = 1;
  2212. return;
  2213. }
  2214. /*!
  2215. \brief Stop scene rotation
  2216. */
  2217. void GS_unset_rotation(void)
  2218. {
  2219. Gv.rotate.do_rot = 0;
  2220. }
  2221. /*!
  2222. \brief Reset scene rotation
  2223. */
  2224. void GS_init_rotation(void)
  2225. {
  2226. int i;
  2227. for (i = 0; i < 16; i++) {
  2228. if (i == 0 || i == 5 || i == 10 || i == 15)
  2229. Gv.rotate.rotMatrix[i] = 1.0;
  2230. else
  2231. Gv.rotate.rotMatrix[i] = 0.0;
  2232. }
  2233. Gv.rotate.rot_angle = 0.0;
  2234. Gv.rotate.rot_axes[0] = 0.0;
  2235. Gv.rotate.rot_axes[1] = 0.0;
  2236. Gv.rotate.rot_axes[2] = 0.0;
  2237. Gv.rotate.do_rot = 0;
  2238. }
  2239. /*!
  2240. * \brief Get rotation matrix
  2241. */
  2242. void GS_get_rotation_matrix(double *matrix)
  2243. {
  2244. int i;
  2245. for (i = 0; i < 16; i++) {
  2246. matrix[i] = Gv.rotate.rotMatrix[i];
  2247. }
  2248. }
  2249. /*!
  2250. * \brief Set rotation matrix
  2251. */
  2252. void GS_set_rotation_matrix(double *matrix)
  2253. {
  2254. int i;
  2255. for (i = 0; i < 16; i++) {
  2256. Gv.rotate.rotMatrix[i] = matrix[i];
  2257. }
  2258. }
  2259. /*!
  2260. \brief Unset focus
  2261. */
  2262. void GS_set_nofocus(void)
  2263. {
  2264. G_debug(3, "GS_set_nofocus");
  2265. Gv.infocus = 0;
  2266. return;
  2267. }
  2268. /*!
  2269. \brief Set focus
  2270. Make sure that the center of view is set
  2271. */
  2272. void GS_set_infocus(void)
  2273. {
  2274. G_debug(3, "GS_set_infocus");
  2275. Gv.infocus = 1;
  2276. return;
  2277. }
  2278. /*!
  2279. \brief Set viewport
  2280. \param left,right,bottom,top viewport extent values
  2281. */
  2282. void GS_set_viewport(int left, int right, int bottom, int top)
  2283. {
  2284. G_debug(3, "GS_set_viewport(): left=%d, right=%d, "
  2285. "bottom=%d, top=%d", left, right, bottom, top);
  2286. gsd_viewport(left, right, bottom, top);
  2287. return;
  2288. }
  2289. /*!
  2290. \brief Send screen coords sx and sy, lib traces through surfaces; sets
  2291. new center to point of nearest intersection.
  2292. If no intersection, uses line of sight with length of current view
  2293. ray (eye to center) to set new center.
  2294. Reset center of view to screen coordinates sx, sy.
  2295. \param sx,sy screen coordinates
  2296. \return 1 on success
  2297. \return 0 on error (invalid surface id)
  2298. */
  2299. int GS_look_here(int sx, int sy)
  2300. {
  2301. float x, y, z, len, los[2][3];
  2302. Point3 realto, dir;
  2303. int id;
  2304. geosurf *gs;
  2305. if (GS_get_selected_point_on_surface(sx, sy, &id, &x, &y, &z)) {
  2306. gs = gs_get_surf(id);
  2307. if (gs) {
  2308. realto[X] = x - gs->ox + gs->x_trans;
  2309. realto[Y] = y - gs->oy + gs->y_trans;
  2310. realto[Z] = z + gs->z_trans;
  2311. GS_set_focus(realto);
  2312. return (1);
  2313. }
  2314. }
  2315. else {
  2316. if (gsd_get_los(los, (short)sx, (short)sy)) {
  2317. len = GS_distance(Gv.from_to[FROM], Gv.real_to);
  2318. GS_v3dir(los[FROM], los[TO], dir);
  2319. GS_v3mult(dir, len);
  2320. realto[X] = Gv.from_to[FROM][X] + dir[X];
  2321. realto[Y] = Gv.from_to[FROM][Y] + dir[Y];
  2322. realto[Z] = Gv.from_to[FROM][Z] + dir[Z];
  2323. GS_set_focus(realto);
  2324. return (1);
  2325. }
  2326. }
  2327. return (0);
  2328. }
  2329. /*!
  2330. \brief Get selected point of surface
  2331. Given screen coordinates sx and sy, find closest intersection of
  2332. view ray with surfaces and return coordinates of intersection in x, y,
  2333. z, and identifier of surface in id.
  2334. \param sx,sy screen coordinates
  2335. \param[out] id surface id
  2336. \param[out] x,y,z point on surface (model coordinates?)
  2337. \returns 0 if no intersections found
  2338. \return number of intersections
  2339. */
  2340. int GS_get_selected_point_on_surface(int sx, int sy, int *id, float *x,
  2341. float *y, float *z)
  2342. {
  2343. float los[2][3], find_dist[MAX_SURFS], closest;
  2344. Point3 point, tmp, finds[MAX_SURFS];
  2345. int surfs[MAX_SURFS], i, iclose, numhits = 0;
  2346. geosurf *gs;
  2347. /* returns surface-world coords */
  2348. gsd_get_los(los, (short)sx, (short)sy);
  2349. if (!gs_setlos_enterdata(los)) {
  2350. G_debug(3, "gs_setlos_enterdata(los): returns false");
  2351. return (0);
  2352. }
  2353. for (i = 0; i < Next_surf; i++) {
  2354. G_debug(3, "id=%d", i);
  2355. gs = gs_get_surf(Surf_ID[i]);
  2356. /* los_intersect expects surf-world coords (xy transl, no scaling) */
  2357. #if NVIZ_HACK
  2358. if (gs_los_intersect1(Surf_ID[i], los, point)) {
  2359. #else
  2360. if (gs_los_intersect(Surf_ID[i], los, point)) {
  2361. #endif
  2362. if (!gs_point_is_masked(gs, point)) {
  2363. GS_v3eq(tmp, point);
  2364. tmp[X] += gs->x_trans;
  2365. tmp[Y] += gs->y_trans;
  2366. tmp[Z] += gs->z_trans;
  2367. find_dist[numhits] = GS_distance(los[FROM], tmp);
  2368. gsd_surf2real(gs, point);
  2369. GS_v3eq(finds[numhits], point);
  2370. surfs[numhits] = Surf_ID[i];
  2371. numhits++;
  2372. }
  2373. }
  2374. }
  2375. for (i = iclose = 0; i < numhits; i++) {
  2376. closest = find_dist[iclose];
  2377. if (find_dist[i] < closest) {
  2378. iclose = i;
  2379. }
  2380. }
  2381. if (numhits) {
  2382. *x = finds[iclose][X];
  2383. *y = finds[iclose][Y];
  2384. *z = finds[iclose][Z];
  2385. *id = surfs[iclose];
  2386. }
  2387. G_debug(3, "NumHits %d, next %d", numhits, Next_surf);
  2388. return (numhits);
  2389. }
  2390. /*!
  2391. \brief Set cplace rotation
  2392. \param num cplace id
  2393. \param dx,dy,dz rotation values
  2394. */
  2395. void GS_set_cplane_rot(int num, float dx, float dy, float dz)
  2396. {
  2397. gsd_cplane_setrot(num, dx, dy, dz);
  2398. return;
  2399. }
  2400. /*!
  2401. \brief Set cplace trans
  2402. \param num cplace id
  2403. \param dx,dy,dz rotation values
  2404. */
  2405. void GS_set_cplane_trans(int num, float dx, float dy, float dz)
  2406. {
  2407. gsd_cplane_settrans(num, dx, dy, dz);
  2408. return;
  2409. }
  2410. /*!
  2411. \brief Draw cplace
  2412. \param num cplace id
  2413. */
  2414. void GS_draw_cplane(int num)
  2415. {
  2416. geosurf *gsurfs[MAX_SURFS];
  2417. int nsurfs;
  2418. nsurfs = gs_num_surfaces();
  2419. if (2 == nsurfs) {
  2420. /* testing */
  2421. gs_getall_surfaces(gsurfs);
  2422. gsd_draw_cplane_fence(gsurfs[0], gsurfs[1], num);
  2423. }
  2424. else {
  2425. gsd_draw_cplane(num);
  2426. }
  2427. return;
  2428. }
  2429. /*!
  2430. \brief Draw cplace fence ?
  2431. \param hs1,hs2
  2432. \param num cplane id
  2433. \return 0 on error
  2434. \return 1 on success
  2435. */
  2436. int GS_draw_cplane_fence(int hs1, int hs2, int num)
  2437. {
  2438. geosurf *gs1, *gs2;
  2439. if (NULL == (gs1 = gs_get_surf(hs1))) {
  2440. return (0);
  2441. }
  2442. if (NULL == (gs2 = gs_get_surf(hs2))) {
  2443. return (0);
  2444. }
  2445. gsd_draw_cplane_fence(gs1, gs2, num);
  2446. return (1);
  2447. }
  2448. /*!
  2449. \brief Draw all cplace fences ?
  2450. */
  2451. void GS_alldraw_cplane_fences(void)
  2452. {
  2453. int onstate[MAX_CPLANES], i;
  2454. gsd_get_cplanes_state(onstate);
  2455. for (i = 0; i < MAX_CPLANES; i++) {
  2456. if (onstate[i]) {
  2457. GS_draw_cplane_fence(Surf_ID[0], Surf_ID[1], i);
  2458. }
  2459. }
  2460. return;
  2461. }
  2462. /*!
  2463. \brief Set cplace
  2464. \param num cplane id
  2465. */
  2466. void GS_set_cplane(int num)
  2467. {
  2468. gsd_cplane_on(num);
  2469. return;
  2470. }
  2471. /*!
  2472. \brief Unset clip place (turn off)
  2473. \param num cplane id
  2474. */
  2475. void GS_unset_cplane(int num)
  2476. {
  2477. gsd_cplane_off(num);
  2478. return;
  2479. }
  2480. /*!
  2481. \brief Get axis scale
  2482. \param sx,sy,sz x/y/z scale values
  2483. \param doexag use vertical exaggeration
  2484. */
  2485. void GS_get_scale(float *sx, float *sy, float *sz, int doexag)
  2486. {
  2487. float zexag;
  2488. zexag = doexag ? Gv.vert_exag : 1.;
  2489. *sx = *sy = Gv.scale;
  2490. *sz = Gv.scale * zexag;
  2491. return;
  2492. }
  2493. /*!
  2494. \brief Set fence color
  2495. \param mode mode id
  2496. */
  2497. void GS_set_fencecolor(int mode)
  2498. {
  2499. gsd_setfc(mode);
  2500. return;
  2501. }
  2502. /*!
  2503. \brief Get fence color
  2504. \return color value
  2505. */
  2506. int GS_get_fencecolor(void)
  2507. {
  2508. return gsd_getfc();
  2509. }
  2510. /*!
  2511. \brief Measure distance "as the ball rolls" between two points on
  2512. surface
  2513. \param hs surface id
  2514. \param x1,y1,x2,y2 two points on surface
  2515. \param[out] dist measured distance
  2516. \param use_exag use exag. surface
  2517. \return 0 on error or if one or more points is not in region
  2518. \return distance following terrain
  2519. */
  2520. int GS_get_distance_alongsurf(int hs, float x1, float y1, float x2, float y2,
  2521. float *dist, int use_exag)
  2522. {
  2523. geosurf *gs;
  2524. float p1[2], p2[2];
  2525. gs = gs_get_surf(hs);
  2526. if (gs == NULL) {
  2527. return 0;
  2528. }
  2529. p1[X] = x1;
  2530. p1[Y] = y1;
  2531. p2[X] = x2;
  2532. p2[Y] = y2;
  2533. gsd_real2surf(gs, p1);
  2534. gsd_real2surf(gs, p2);
  2535. G_debug(3, "GS_get_distance_alongsurf(): hs=%d p1=%f,%f p2=%f,%f",
  2536. hs, x1, y1, x2, y2);
  2537. return gs_distance_onsurf(gs, p1, p2, dist, use_exag);
  2538. }
  2539. /*!
  2540. \brief Save 3d view
  2541. \param vname view file name
  2542. \param surfid surface id
  2543. \return ?
  2544. */
  2545. int GS_save_3dview(const char *vname, int surfid)
  2546. {
  2547. return (Gs_save_3dview(vname, &Gv, &Gd, &wind, gs_get_surf(surfid)));
  2548. }
  2549. /*!
  2550. \brief Load 3d view
  2551. \param vname view file name
  2552. \param surfid surface id
  2553. \return ?
  2554. */
  2555. int GS_load_3dview(const char *vname, int surfid)
  2556. {
  2557. return (Gs_load_3dview(vname, &Gv, &Gd, &wind, gs_get_surf(surfid)));
  2558. /* what to do about lights - I guess, delete all &
  2559. create any that exist in 3dview file */
  2560. }
  2561. /************************************************************************
  2562. * Following routines use Graphics Library
  2563. ************************************************************************/
  2564. /*!
  2565. \brief Init viewpoint
  2566. \todo allow to set center?
  2567. */
  2568. void GS_init_view(void)
  2569. {
  2570. static int first = 1;
  2571. G_debug(3, "GS_init_view");
  2572. if (first) {
  2573. first = 0;
  2574. glMatrixMode(GL_MODELVIEW);
  2575. /* OGLXXX doublebuffer: use GLX_DOUBLEBUFFER in attriblist */
  2576. /* glxChooseVisual(*dpy, screen, *attriblist); */
  2577. /* OGLXXX
  2578. * ZMIN not needed -- always 0.
  2579. * ZMAX not needed -- always 1.
  2580. * getgdesc other posiblilties:
  2581. * glxGetConfig();
  2582. * glxGetCurrentContext();
  2583. * glxGetCurrentDrawable();
  2584. * GLint gdtmp;
  2585. * getgdesc other posiblilties:
  2586. * glxGetConfig();
  2587. * glxGetCurrentContext();
  2588. * glxGetCurrentDrawable();
  2589. * GLint gdtmp;
  2590. * glDepthRange params must be scaled to [0, 1]
  2591. */
  2592. glDepthRange(0.0, 1.0);
  2593. glEnable(GL_DEPTH_TEST);
  2594. glDepthFunc(GL_LEQUAL);
  2595. /* } */
  2596. /* replace these with something meaningful */
  2597. Gv.fov = 450;
  2598. Gv.twist = 0;
  2599. GS_init_rotation();
  2600. Gv.from_to[FROM][X] = Gv.from_to[FROM][Y] =
  2601. Gv.from_to[FROM][Z] = GS_UNIT_SIZE / 2.;
  2602. Gv.from_to[TO][X] = GS_UNIT_SIZE / 2.;
  2603. Gv.from_to[TO][Y] = GS_UNIT_SIZE / 2.;
  2604. Gv.from_to[TO][Z] = 0.;
  2605. Gv.from_to[TO][W] = Gv.from_to[FROM][W] = 1.;
  2606. Gv.real_to[W] = 1.;
  2607. Gv.vert_exag = 1.;
  2608. GS_v3eq(Gv.real_to, Gv.from_to[TO]);
  2609. GS_v3normalize(Gv.from_to[FROM], Gv.from_to[TO]);
  2610. /*
  2611. Gd.nearclip = 50;
  2612. Gd.farclip = 10000.;
  2613. */
  2614. Gd.nearclip = 10.;
  2615. Gd.farclip = 10000.;
  2616. Gd.aspect = (float)GS_get_aspect();
  2617. GS_set_focus(Gv.real_to);
  2618. }
  2619. return;
  2620. }
  2621. /*!
  2622. \brief Clear view
  2623. \param col color value
  2624. */
  2625. void GS_clear(int col)
  2626. {
  2627. G_debug(3, "GS_clear");
  2628. col = col | 0xFF000000;
  2629. /* OGLXXX
  2630. * change glClearDepth parameter to be in [0, 1]
  2631. * ZMAX not needed -- always 1.
  2632. * getgdesc other posiblilties:
  2633. * glxGetConfig();
  2634. * glxGetCurrentContext();
  2635. * glxGetCurrentDrawable();
  2636. * GLint gdtmp;
  2637. */
  2638. glClearDepth(1.0);
  2639. glClearColor(((float)((col) & 0xff)) / 255.,
  2640. (float)((col) >> 8 & 0xff) / 255.,
  2641. (float)((col) >> 16 & 0xff) / 255.,
  2642. (float)((col) >> 24 & 0xff) / 255.);
  2643. glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  2644. Gd.bgcol = col;
  2645. Modelshowing = 0;
  2646. gsd_flush();
  2647. return;
  2648. }
  2649. /*!
  2650. \brief Get aspect value
  2651. \return aspect value
  2652. */
  2653. double GS_get_aspect(void)
  2654. {
  2655. int left, right, bottom, top;
  2656. GLint tmp[4];
  2657. /* OGLXXX
  2658. * get GL_VIEWPORT:
  2659. * You can probably do better than this.
  2660. */
  2661. glGetIntegerv(GL_VIEWPORT, tmp);
  2662. left = tmp[0];
  2663. right = tmp[0] + tmp[2] - 1;
  2664. bottom = tmp[1];
  2665. top = tmp[1] + tmp[3] - 1;
  2666. G_debug(3, "GS_get_aspect(): left=%d, right=%d, top=%d, bottom=%d",
  2667. left, right, top, bottom);
  2668. return ((double)(right - left) / (top - bottom));
  2669. }
  2670. /*!
  2671. \brief Check for transparency
  2672. Disabled.
  2673. \return 1
  2674. */
  2675. int GS_has_transparency(void)
  2676. {
  2677. /* OGLXXX
  2678. * getgdesc other posiblilties:
  2679. * glxGetConfig();
  2680. * glxGetCurrentContext();
  2681. * glxGetCurrentDrawable();
  2682. * GLint gdtmp;
  2683. * blending is ALWAYS supported.
  2684. * This function returns whether it is enabled.
  2685. * return((glGetIntegerv(GL_BLEND, &gdtmp), gdtmp));
  2686. */
  2687. return (1);
  2688. }