GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
Loading...
Searching...
No Matches
nviz/draw.c
Go to the documentation of this file.
1/*!
2 \file lib/nviz/draw.c
3
4 \brief Nviz library -- Draw map objects to GLX context
5
6 Based on visualization/nviz/src/draw.c and
7 visualization/nviz/src/togl_flythrough.c
8
9 (C) 2008, 2010-2011 by the GRASS Development Team
10 This program is free software under the GNU General Public License
11 (>=v2). Read the file COPYING that comes with GRASS for details.
12
13 \author Updated/modified by Martin Landa <landa.martin gmail.com> (Google SoC
14 2008/2010) \author Textures by Anna Kratochvilova
15 */
16
17#include <grass/nviz.h>
18
19#ifndef GL_CLAMP_TO_EDGE
20#define GL_CLAMP_TO_EDGE 0x812F
21#endif
22
23static int sort_surfs_max(int *, int *, int *, int);
24
25/*!
26 \brief Draw all loaded surfaces
27
28 \param dc nviz data
29
30 \return 1
31 */
33{
34 int i, nsurfs;
36 int *surf_list;
37 float x, y, z;
38 int num, w;
39
40 /* Get position for Light 1 */
41 num = 1;
42 x = dc->light[num].x;
43 y = dc->light[num].y;
44 z = dc->light[num].z;
45 w = dc->light[num].z;
46
48
49 sort_surfs_max(surf_list, sortSurfs, sorti, nsurfs);
50
52
53 /* re-initialize lights */
54 GS_setlight_position(num, x, y, z, w);
55 num = 2;
56 GS_setlight_position(num, 0., 0., 1., 0);
57
58 for (i = 0; i < nsurfs; i++) {
60 }
61
62 /* GS_draw_cplane_fence params will change - surfs aren't used anymore */
63 for (i = 0; i < MAX_CPLANES; i++) {
64 if (dc->cp_on[i])
66 }
67
68 return 1;
69}
70
71/*!
72 \brief Sorts surfaces by max elevation, lowest to highest.
73
74 Puts ordered id numbers in id_sort, leaving id_orig unchanged.
75 Puts ordered indices of surfaces from id_orig in indices.
76
77 \param surf pointer to surface array
78 \param id_sort
79 \param indices
80 \param num
81
82 \return 1
83 */
84int sort_surfs_max(int *surf, int *id_sort, int *indices, int num)
85{
86 int i, j;
87 float maxvals[MAX_SURFS];
88 float tmp, max = 0., tmin, tmax, tmid;
89
90 for (i = 0; i < num; i++) {
92 if (i == 0)
93 max = tmax;
94 else
95 max = max < tmax ? tmax : max;
96 maxvals[i] = tmax;
97 }
98
99 for (i = 0; i < num; i++) {
100 tmp = maxvals[0];
101 indices[i] = 0;
102 for (j = 0; j < num; j++) {
103 if (maxvals[j] < tmp) {
104 tmp = maxvals[j];
105 indices[i] = j;
106 }
107 }
108
109 maxvals[indices[i]] = max + 1;
110 id_sort[i] = surf[indices[i]];
111 }
112
113 return 1;
114}
115
116/*!
117 \brief Draw all loaded vector sets (lines)
118
119 \return 1
120 */
122{
123 /* GS_set_cancel(0); */
124
125 /* in case transparency is set */
127
129
131
132 GS_done_draw();
133
135
136 /* GS_set_cancel(0); */
137
138 return 1;
139}
140
141/*!
142 \brief Draw all loaded vector point sets
143
144 \return 1
145 */
147{
148 int i;
149 int *site_list, nsites;
150
152
153 /* in case transparency is set */
155
157
158 for (i = 0; i < nsites; i++) {
160 }
162
163 GS_done_draw();
164
166
167 return 1;
168}
169
170/*!
171 \brief Draw all loaded volume sets
172
173 \return 1
174 */
176{
177 int *vol_list, nvols, i;
178
180
181 /* in case transparency is set */
183
185
186 for (i = 0; i < nvols; i++) {
188 }
189
191
192 GS_done_draw();
193
195
196 return 1;
197}
198
199/*!
200 \brief Draw all map objects (in full resolution) and decorations
201
202 \param data nviz data
203 */
205{
206 int i;
208
209 draw_surf = 1;
210 draw_vect = 1;
211 draw_site = 1;
212 draw_vol = 1;
213 /*
214 draw_north_arrow = 0;
215 arrow_x = 0;
216 draw_label = 0;
217 draw_legend = 0;
218 draw_fringe = 0;
219 draw_scalebar = 0;
220 draw_bar_x = 0;
221 */
222
223 GS_set_draw(GSD_BACK); /* needs to be BACK to avoid flickering */
224
226
227 GS_clear(data->bgcolor);
228
229 if (draw_surf)
230 Nviz_draw_all_surf(data);
231
232 if (draw_vect)
234
235 if (draw_site)
237
238 if (draw_vol)
240
241 for (i = 0; i < data->num_fringes; i++) {
242 struct fringe_data *f = data->fringe[i];
243
244 GS_draw_fringe(f->id, f->color, f->elev, f->where);
245 }
246
247 /* North Arrow */
248 if (data->draw_arrow) {
249 gsd_north_arrow(data->arrow->where, data->arrow->size, 0,
250 data->arrow->color, data->arrow->color);
251 }
252
253 /* scale bar */
254 for (i = 0; i < data->num_scalebars; i++) {
255 if (data->scalebar[i]) {
256 struct scalebar_data *s = data->scalebar[i];
257
258 gsd_scalebar_v2(s->where, s->size, 0, s->color, s->color);
259 }
260 }
261 GS_done_draw();
263
264 return 1;
265}
266
267/*!
268 \brief Draw all surfaces in wireframe (quick mode)
269
270 Draw modes:
271 - DRAW_QUICK_SURFACE
272 - DRAW_QUICK_VLINES
273 - DRAW_QUICK_VPOINTS
274 - DRAW_QUICK_VOLUME
275
276 \param data nviz data
277 \param draw_mode draw mode
278
279 \return 1
280 */
281int Nviz_draw_quick(nv_data *data, int draw_mode)
282{
284
286
287 GS_clear(data->bgcolor);
288
289 /* draw surfaces */
290 if (draw_mode & DRAW_QUICK_SURFACE)
292
293 /* draw vector lines */
294 if (draw_mode & DRAW_QUICK_VLINES)
296
297 /* draw vector points */
298 if (draw_mode & DRAW_QUICK_VPOINTS)
300
301 /* draw volumes */
302 if (draw_mode & DRAW_QUICK_VOLUME) {
304 }
305
306 GS_done_draw();
307
308 return 1;
309}
310
311/*!
312 \brief Load image into texture
313
314 \param image_data image data
315 \param width, height image screen size
316 \param alpha has alpha channel
317 */
349
350/*!
351 \brief Set ortho view for drawing images
352
353 \param width, height image screen size
354 */
355void Nviz_set_2D(int width, int height)
356{
357 glEnable(GL_BLEND); /* images are transparent */
359
362 glOrtho(0, width, 0, height, -1, 1);
363
364 /* set coordinate system from upper left corner */
365 glScalef(1, -1, 1);
366 glTranslatef(0, -height, 0);
367
370}
371
372/*!
373 \brief Draw image as texture
374
375 \param x, y image coordinates
376 \param width, height image size
377 \param texture_id texture id
378 */
379void Nviz_draw_image(int x, int y, int width, int height, int texture_id)
380{
383
385
387
388 glTexCoord2d(0.0, 1.0);
389 glVertex2d(x, y);
390 glTexCoord2d(0.0, 0.0);
391 glVertex2d(x, y + height);
392 glTexCoord2d(1.0, 0.0);
393 glVertex2d(x + width, y + height);
394 glTexCoord2d(1.0, 1.0);
395 glVertex2d(x + width, y);
396
397 glEnd();
398
399 GS_done_draw();
401}
402
403/*!
404 \brief Delete texture
405
406 \param texture_id texture id
407 */
409{
410 GLuint t[1];
411
412 t[0] = texture_id;
414}
415
416/*!
417 \brief Get maximum texture size
418
419 */
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:147
int * GP_get_site_list(int *)
Get list of point sets.
Definition gp2.c:102
int GS_get_zextents(int, float *, float *, float *)
Get z-extent for a single surface.
Definition gs2.c:2665
void GS_set_draw(int)
Sets which buffer to draw to.
Definition gs2.c:2462
void GS_alldraw_wire(void)
Draw all wires.
Definition gs2.c:1920
void GVL_draw_vol(int)
Draw volume set.
Definition gvl2.c:408
void GS_draw_surf(int)
Draw surface.
Definition gs2.c:1865
void GS_setlight_position(int, float, float, float, int)
Set light position.
Definition gs2.c:309
int GS_draw_cplane_fence(int, int, int)
Draw cplace fence ?
Definition gs2.c:3174
void GV_alldraw_vect(void)
Draw all loaded vector sets.
Definition gv2.c:506
int * GS_get_surf_list(int *)
Get surface list.
Definition gs2.c:1532
void GVL_alldraw_wire(void)
Draw all volume sets in wire mode.
Definition gvl2.c:461
void GS_ready_draw(void)
Definition gs2.c:2488
int gsd_north_arrow(float *, float, GLuint, unsigned long, unsigned long)
Draw North Arrow.
Definition gsd_objs.c:851
int gsd_scalebar_v2(float *, float, GLuint, unsigned long, unsigned long)
Draw Scalebar (as lines)
Definition gsd_objs.c:1282
void GS_clear(int)
Clear view.
Definition gs2.c:3417
void GS_done_draw(void)
Draw done, swap buffers.
Definition gs2.c:2501
void GS_draw_fringe(int, unsigned long, float, int *)
Draw fringe around data (surface) at selected corners.
Definition gs2.c:820
int * GVL_get_vol_list(int *)
Get list of loaded volume sets.
Definition gvl2.c:181
void GP_alldraw_site(void)
Draw all available point sets.
Definition gp2.c:615
void GP_draw_site(int)
Draw point set.
Definition gp2.c:581
#define max(x, y)
Definition draw2.c:30
int Nviz_load_image(GLubyte *image_data, int width, int height, int alpha)
Load image into texture.
Definition nviz/draw.c:318
void Nviz_get_max_texture(int *size)
Get maximum texture size.
Definition nviz/draw.c:420
int Nviz_draw_all_vect(void)
Draw all loaded vector sets (lines)
Definition nviz/draw.c:121
void Nviz_draw_image(int x, int y, int width, int height, int texture_id)
Draw image as texture.
Definition nviz/draw.c:379
int Nviz_draw_all_surf(nv_data *dc)
Draw all loaded surfaces.
Definition nviz/draw.c:32
void Nviz_set_2D(int width, int height)
Set ortho view for drawing images.
Definition nviz/draw.c:355
int Nviz_draw_quick(nv_data *data, int draw_mode)
Draw all surfaces in wireframe (quick mode)
Definition nviz/draw.c:281
int Nviz_draw_all(nv_data *data)
Draw all map objects (in full resolution) and decorations.
Definition nviz/draw.c:204
void Nviz_del_texture(int texture_id)
Delete texture.
Definition nviz/draw.c:408
int Nviz_draw_all_vol(void)
Draw all loaded volume sets.
Definition nviz/draw.c:175
#define GL_CLAMP_TO_EDGE
Definition nviz/draw.c:20
int Nviz_draw_all_site(void)
Draw all loaded vector point sets.
Definition nviz/draw.c:146
#define DRAW_QUICK_VOLUME
Definition nviz.h:55
#define DRAW_QUICK_VPOINTS
Definition nviz.h:54
#define DRAW_QUICK_SURFACE
Definition nviz.h:52
#define DRAW_QUICK_VLINES
Definition nviz.h:53
#define MAX_CPLANES
Definition ogsf.h:47
#define GSD_FRONT
Definition ogsf.h:104
#define GSD_BOTH
Definition ogsf.h:106
#define MAX_SURFS
Definition ogsf.h:40
#define GSD_BACK
Definition ogsf.h:105
double t
Definition r_raster.c:39
float elev
Definition nviz.h:79
unsigned long color
Definition nviz.h:78
int id
Definition nviz.h:77
int where[4]
Definition nviz.h:80
Definition nviz.h:96
struct scalebar_data ** scalebar
Definition nviz.h:119
int num_fringes
Definition nviz.h:110
struct fringe_data ** fringe
Definition nviz.h:111
struct arrow_data * arrow
Definition nviz.h:115
int bgcolor
Definition nviz.h:122
int draw_arrow
Definition nviz.h:114
int num_scalebars
Definition nviz.h:118
float where[3]
Definition nviz.h:93
unsigned long color
Definition nviz.h:91
float size
Definition nviz.h:92
#define x