GRASS 8 Programmer's Manual 8.6.0dev(2026)-55de52a352
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 SPDX-FileCopyrightText: 2008, 2010-2011 GRASS Development Team
10 SPDX-License-Identifier: GPL-2.0-or-later
11
12 \author Updated/modified by Martin Landa <landa.martin gmail.com> (Google SoC
13 2008/2010) \author Textures by Anna Kratochvilova
14 */
15
16#include <grass/nviz.h>
17
18#ifndef GL_CLAMP_TO_EDGE
19#define GL_CLAMP_TO_EDGE 0x812F
20#endif
21
22static int sort_surfs_max(int *, int *, int *, int);
23
24/*!
25 \brief Draw all loaded surfaces
26
27 \param dc nviz data
28
29 \return 1
30 */
32{
33 int i, nsurfs;
35 int *surf_list;
36 float x, y, z;
37 int num, w;
38
39 /* Get position for Light 1 */
40 num = 1;
41 x = dc->light[num].x;
42 y = dc->light[num].y;
43 z = dc->light[num].z;
44 w = dc->light[num].z;
45
47
48 sort_surfs_max(surf_list, sortSurfs, sorti, nsurfs);
49
51
52 /* re-initialize lights */
53 GS_setlight_position(num, x, y, z, w);
54 num = 2;
55 GS_setlight_position(num, 0., 0., 1., 0);
56
57 for (i = 0; i < nsurfs; i++) {
59 }
60
61 /* GS_draw_cplane_fence params will change - surfs aren't used anymore */
62 for (i = 0; i < MAX_CPLANES; i++) {
63 if (dc->cp_on[i])
65 }
66
67 return 1;
68}
69
70/*!
71 \brief Sorts surfaces by max elevation, lowest to highest.
72
73 Puts ordered id numbers in id_sort, leaving id_orig unchanged.
74 Puts ordered indices of surfaces from id_orig in indices.
75
76 \param surf pointer to surface array
77 \param id_sort
78 \param indices
79 \param num
80
81 \return 1
82 */
83int sort_surfs_max(int *surf, int *id_sort, int *indices, int num)
84{
85 int i, j;
86 float maxvals[MAX_SURFS];
87 float tmp, max = 0., tmin, tmax, tmid;
88
89 for (i = 0; i < num; i++) {
91 if (i == 0)
92 max = tmax;
93 else
94 max = max < tmax ? tmax : max;
95 maxvals[i] = tmax;
96 }
97
98 for (i = 0; i < num; i++) {
99 tmp = maxvals[0];
100 indices[i] = 0;
101 for (j = 0; j < num; j++) {
102 if (maxvals[j] < tmp) {
103 tmp = maxvals[j];
104 indices[i] = j;
105 }
106 }
107
108 maxvals[indices[i]] = max + 1;
109 id_sort[i] = surf[indices[i]];
110 }
111
112 return 1;
113}
114
115/*!
116 \brief Draw all loaded vector sets (lines)
117
118 \return 1
119 */
121{
122 /* GS_set_cancel(0); */
123
124 /* in case transparency is set */
126
128
130
131 GS_done_draw();
132
134
135 /* GS_set_cancel(0); */
136
137 return 1;
138}
139
140/*!
141 \brief Draw all loaded vector point sets
142
143 \return 1
144 */
146{
147 int i;
148 int *site_list, nsites;
149
151
152 /* in case transparency is set */
154
156
157 for (i = 0; i < nsites; i++) {
159 }
161
162 GS_done_draw();
163
165
166 return 1;
167}
168
169/*!
170 \brief Draw all loaded volume sets
171
172 \return 1
173 */
175{
176 int *vol_list, nvols, i;
177
179
180 /* in case transparency is set */
182
184
185 for (i = 0; i < nvols; i++) {
187 }
188
190
191 GS_done_draw();
192
194
195 return 1;
196}
197
198/*!
199 \brief Draw all map objects (in full resolution) and decorations
200
201 \param data nviz data
202 */
204{
205 int i;
207
208 draw_surf = 1;
209 draw_vect = 1;
210 draw_site = 1;
211 draw_vol = 1;
212 /*
213 draw_north_arrow = 0;
214 arrow_x = 0;
215 draw_label = 0;
216 draw_legend = 0;
217 draw_fringe = 0;
218 draw_scalebar = 0;
219 draw_bar_x = 0;
220 */
221
222 GS_set_draw(GSD_BACK); /* needs to be BACK to avoid flickering */
223
225
226 GS_clear(data->bgcolor);
227
228 if (draw_surf)
229 Nviz_draw_all_surf(data);
230
231 if (draw_vect)
233
234 if (draw_site)
236
237 if (draw_vol)
239
240 for (i = 0; i < data->num_fringes; i++) {
241 struct fringe_data *f = data->fringe[i];
242
243 GS_draw_fringe(f->id, f->color, f->elev, f->where);
244 }
245
246 /* North Arrow */
247 if (data->draw_arrow) {
248 gsd_north_arrow(data->arrow->where, data->arrow->size, 0,
249 data->arrow->color, data->arrow->color);
250 }
251
252 /* scale bar */
253 for (i = 0; i < data->num_scalebars; i++) {
254 if (data->scalebar[i]) {
255 struct scalebar_data *s = data->scalebar[i];
256
257 gsd_scalebar_v2(s->where, s->size, 0, s->color, s->color);
258 }
259 }
260 GS_done_draw();
262
263 return 1;
264}
265
266/*!
267 \brief Draw all surfaces in wireframe (quick mode)
268
269 Draw modes:
270 - DRAW_QUICK_SURFACE
271 - DRAW_QUICK_VLINES
272 - DRAW_QUICK_VPOINTS
273 - DRAW_QUICK_VOLUME
274
275 \param data nviz data
276 \param draw_mode draw mode
277
278 \return 1
279 */
280int Nviz_draw_quick(nv_data *data, int draw_mode)
281{
283
285
286 GS_clear(data->bgcolor);
287
288 /* draw surfaces */
289 if (draw_mode & DRAW_QUICK_SURFACE)
291
292 /* draw vector lines */
293 if (draw_mode & DRAW_QUICK_VLINES)
295
296 /* draw vector points */
297 if (draw_mode & DRAW_QUICK_VPOINTS)
299
300 /* draw volumes */
301 if (draw_mode & DRAW_QUICK_VOLUME) {
303 }
304
305 GS_done_draw();
306
307 return 1;
308}
309
310/*!
311 \brief Load image into texture
312
313 \param image_data image data
314 \param width, height image screen size
315 \param alpha has alpha channel
316 */
348
349/*!
350 \brief Set ortho view for drawing images
351
352 \param width, height image screen size
353 */
354void Nviz_set_2D(int width, int height)
355{
356 glEnable(GL_BLEND); /* images are transparent */
358
361 glOrtho(0, width, 0, height, -1, 1);
362
363 /* set coordinate system from upper left corner */
364 glScalef(1, -1, 1);
365 glTranslatef(0, -height, 0);
366
369}
370
371/*!
372 \brief Draw image as texture
373
374 \param x, y image coordinates
375 \param width, height image size
376 \param texture_id texture id
377 */
378void Nviz_draw_image(int x, int y, int width, int height, int texture_id)
379{
382
384
386
387 glTexCoord2d(0.0, 1.0);
388 glVertex2d(x, y);
389 glTexCoord2d(0.0, 0.0);
390 glVertex2d(x, y + height);
391 glTexCoord2d(1.0, 0.0);
392 glVertex2d(x + width, y + height);
393 glTexCoord2d(1.0, 1.0);
394 glVertex2d(x + width, y);
395
396 glEnd();
397
398 GS_done_draw();
400}
401
402/*!
403 \brief Delete texture
404
405 \param texture_id texture id
406 */
408{
409 GLuint t[1];
410
411 t[0] = texture_id;
413}
414
415/*!
416 \brief Get maximum texture size
417
418 */
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
int * GP_get_site_list(int *)
Get list of point sets.
Definition gp2.c:100
int GS_get_zextents(int, float *, float *, float *)
Get z-extent for a single surface.
Definition gs2.c:2661
void GS_set_draw(int)
Sets which buffer to draw to.
Definition gs2.c:2458
void GS_alldraw_wire(void)
Draw all wires.
Definition gs2.c:1916
void GVL_draw_vol(int)
Draw volume set.
Definition gvl2.c:404
void GS_draw_surf(int)
Draw surface.
Definition gs2.c:1861
void GS_setlight_position(int, float, float, float, int)
Set light position.
Definition gs2.c:305
int GS_draw_cplane_fence(int, int, int)
Draw cplace fence ?
Definition gs2.c:3170
void GV_alldraw_vect(void)
Draw all loaded vector sets.
Definition gv2.c:504
int * GS_get_surf_list(int *)
Get surface list.
Definition gs2.c:1528
void GVL_alldraw_wire(void)
Draw all volume sets in wire mode.
Definition gvl2.c:457
void GS_ready_draw(void)
Definition gs2.c:2484
int gsd_north_arrow(float *, float, GLuint, unsigned long, unsigned long)
Draw North Arrow.
Definition gsd_objs.c:847
int gsd_scalebar_v2(float *, float, GLuint, unsigned long, unsigned long)
Draw Scalebar (as lines)
Definition gsd_objs.c:1278
void GS_clear(int)
Clear view.
Definition gs2.c:3413
void GS_done_draw(void)
Draw done, swap buffers.
Definition gs2.c:2497
void GS_draw_fringe(int, unsigned long, float, int *)
Draw fringe around data (surface) at selected corners.
Definition gs2.c:816
int * GVL_get_vol_list(int *)
Get list of loaded volume sets.
Definition gvl2.c:177
void GP_alldraw_site(void)
Draw all available point sets.
Definition gp2.c:613
void GP_draw_site(int)
Draw point set.
Definition gp2.c:579
#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:317
void Nviz_get_max_texture(int *size)
Get maximum texture size.
Definition nviz/draw.c:419
int Nviz_draw_all_vect(void)
Draw all loaded vector sets (lines)
Definition nviz/draw.c:120
void Nviz_draw_image(int x, int y, int width, int height, int texture_id)
Draw image as texture.
Definition nviz/draw.c:378
int Nviz_draw_all_surf(nv_data *dc)
Draw all loaded surfaces.
Definition nviz/draw.c:31
void Nviz_set_2D(int width, int height)
Set ortho view for drawing images.
Definition nviz/draw.c:354
int Nviz_draw_quick(nv_data *data, int draw_mode)
Draw all surfaces in wireframe (quick mode)
Definition nviz/draw.c:280
int Nviz_draw_all(nv_data *data)
Draw all map objects (in full resolution) and decorations.
Definition nviz/draw.c:203
void Nviz_del_texture(int texture_id)
Delete texture.
Definition nviz/draw.c:407
int Nviz_draw_all_vol(void)
Draw all loaded volume sets.
Definition nviz/draw.c:174
#define GL_CLAMP_TO_EDGE
Definition nviz/draw.c:19
int Nviz_draw_all_site(void)
Draw all loaded vector point sets.
Definition nviz/draw.c:145
#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:48
#define GSD_FRONT
Definition ogsf.h:105
#define GSD_BOTH
Definition ogsf.h:107
#define MAX_SURFS
Definition ogsf.h:41
#define GSD_BACK
Definition ogsf.h:106
double t
Definition r_raster.c:37
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