GS2.c 62 KB

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