GS2.c 61 KB

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