GS2.c 63 KB

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