GS2.c 63 KB

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