GS2.c 62 KB

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