GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
lights.c
Go to the documentation of this file.
1 /*!
2  \file lib/nviz/lights.c
3 
4  \brief Nviz library -- Change lighting settings
5 
6  Based on visualization/nviz/src/lights.c
7 
8  (C) 2008, 2010 by the GRASS Development Team
9  This program is free software under the GNU General Public License
10  (>=v2). Read the file COPYING that comes with GRASS for details.
11 
12  \author Updated/modified by Martin Landa <landa.martin gmail.com> (Google SoC
13  2008/2010)
14  */
15 
16 #include <grass/glocale.h>
17 #include <grass/nviz.h>
18 
19 /*!
20  \brief Set light position
21 
22  \param data nviz data
23  \param num light num (starts at 1)
24  \param x,y,z,w position, model coordinates
25 
26  \return 1
27  */
28 int Nviz_set_light_position(nv_data *data, int num, double x, double y,
29  double z, double w)
30 {
31  /*
32  double xpos, ypos;
33  xpos = x;
34  xpos = (xpos < 0) ? 0 : (xpos > 1.0) ? 1.0 : xpos;
35  ypos = 1.0 - y;
36  ypos = (ypos < 0) ? 0 : (ypos > 1.0) ? 1.0 : ypos;
37 
38  if (x < 0.0 || x > 1.0 || y < 0.0 || y > 1.0) {
39  G_debug(1, "Invalid light position coordinates (%f,%f), using %f,%f",
40  x, y, xpos, 1.0 - ypos);
41  }
42  */
43 
44  data->light[num].id = num;
45  data->light[num].x = x;
46  data->light[num].y = y;
47  data->light[num].z = z;
48  data->light[num].w = w;
49 
50  G_debug(1,
51  "Nviz_set_light_position(): num = %d x = %f y = %f z = %f w = %f",
52  num, x, y, z, w);
53  GS_setlight_position(num, x, y, z, w);
54 
55  return 1;
56 }
57 
58 /*!
59  \brief Set light brightness
60 
61  \param data nviz data
62  \param num light num (starts at 1)
63  \param value brightness value
64  */
65 int Nviz_set_light_bright(nv_data *data, int num, double value)
66 {
67  double r, g, b;
68 
69  data->light[num].brt = value;
70 
71  r = data->light[num].r * data->light[num].brt;
72  g = data->light[num].g * data->light[num].brt;
73  b = data->light[num].b * data->light[num].brt;
74 
75  G_debug(1,
76  "Nviz_set_light_bright(): num = %d value = %f r = %f g = %f b = %f",
77  num, value, r, g, b);
78  GS_setlight_color(num, r, g, b);
79 
80  return 1;
81 }
82 
83 /*!
84  \brief Set light color
85 
86  \param data nviz data
87  \param num light num (starts at 1)
88  \param red,green,blue RGB values (0-255)
89  */
90 int Nviz_set_light_color(nv_data *data, int num, int red, int green, int blue)
91 {
92  double r, g, b;
93 
94  data->light[num].r = red / 255.;
95  data->light[num].g = green / 255.;
96  data->light[num].b = blue / 255.;
97 
98  r = data->light[num].r * data->light[num].brt;
99  g = data->light[num].g * data->light[num].brt;
100  b = data->light[num].b * data->light[num].brt;
101 
102  G_debug(1, "Nviz_set_light_color(): num = %d r = %d/%f g = %d/%f b = %d/%f",
103  num, red, r, green, g, blue, b);
104  GS_setlight_color(num, r, g, b);
105 
106  return 1;
107 }
108 
109 /*!
110  \brief Set light ambient
111 
112  \param data nviz data
113  \param num light num (starts at 1)
114  \param value ambient value (same for R/G/B) (0-1)
115  */
116 int Nviz_set_light_ambient(nv_data *data, int num, double value)
117 {
118  data->light[num].ar = value;
119  data->light[num].ag = value;
120  data->light[num].ab = value;
121 
122  G_debug(1, "Nviz_set_light_ambient(): num = %d value = %f", num, value);
123  GS_setlight_ambient(num, value, value, value);
124 
125  return 1;
126 }
127 
128 /*!
129  \brief Init new light
130 
131  \param data nviz data
132  \param num light num (starts at 1)
133  */
134 int Nviz_init_light(nv_data *data, int num)
135 {
136  G_debug(1, "Nviz_init_light(): num = %d", num);
137  if (num > MAX_LIGHTS) {
138  return 0;
139  }
140 
141  data->light[num].id = 0;
142  data->light[num].brt = 0.8;
143  data->light[num].ar = 0.3;
144  data->light[num].ag = 0.3;
145  data->light[num].ab = 0.3;
146  data->light[num].r = 1.0;
147  data->light[num].b = 1.0;
148  data->light[num].g = 1.0;
149  data->light[num].x = 1.0;
150  data->light[num].y = 1.0;
151  data->light[num].z = 1.0;
152  data->light[num].w = 1.0;
153 
154  return 1;
155 }
156 
157 /*!
158  \brief Define new light
159 
160  \param data nviz data
161 
162  \return 1 on success
163  \return 0 on failure
164  */
166 {
167  int num;
168 
169  num = GS_new_light();
170 
171  if (num < 1) {
172  G_warning(_("Unable to define new light"));
173  return 0;
174  }
175 
176  Nviz_init_light(data, num);
177 
178  return 1;
179 }
180 
181 /*!
182  \brief Draw lighting model
183 
184  \param data nviz data [unused]
185  */
187 {
189  GS_ready_draw();
191  GS_done_draw();
193 }
void G_warning(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
void GS_set_draw(int)
Sets which buffer to draw to.
Definition: gs2.c:2459
void GS_setlight_color(int, float, float, float)
Set light color.
Definition: gs2.c:356
int GS_new_light(void)
Add new model light.
Definition: gs2.c:268
void GS_setlight_position(int, float, float, float, int)
Set light position.
Definition: gs2.c:309
void GS_draw_lighting_model(void)
Draw lighting model.
Definition: gs2.c:934
void GS_ready_draw(void)
Definition: gs2.c:2485
void GS_done_draw(void)
Draw done, swap buffers.
Definition: gs2.c:2498
void GS_setlight_ambient(int, float, float, float)
Set light ambient.
Definition: gs2.c:400
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition: gis.h:47
#define _(str)
Definition: glocale.h:10
void Nviz_draw_model(nv_data *data UNUSED)
Draw lighting model.
Definition: lights.c:186
int Nviz_set_light_ambient(nv_data *data, int num, double value)
Set light ambient.
Definition: lights.c:116
int Nviz_set_light_color(nv_data *data, int num, int red, int green, int blue)
Set light color.
Definition: lights.c:90
int Nviz_init_light(nv_data *data, int num)
Init new light.
Definition: lights.c:134
int Nviz_new_light(nv_data *data)
Define new light.
Definition: lights.c:165
int Nviz_set_light_bright(nv_data *data, int num, double value)
Set light brightness.
Definition: lights.c:65
int Nviz_set_light_position(nv_data *data, int num, double x, double y, double z, double w)
Set light position.
Definition: lights.c:28
float g
Definition: named_colr.c:7
#define GSD_FRONT
Definition: ogsf.h:104
#define MAX_LIGHTS
Definition: ogsf.h:46
#define GSD_BACK
Definition: ogsf.h:105
double b
Definition: r_raster.c:39
double r
Definition: r_raster.c:39
float ab
Definition: nviz.h:73
float ag
Definition: nviz.h:73
float w
Definition: nviz.h:74
float brt
Definition: nviz.h:71
float b
Definition: nviz.h:72
float x
Definition: nviz.h:74
float ar
Definition: nviz.h:73
float y
Definition: nviz.h:74
float r
Definition: nviz.h:72
int id
Definition: nviz.h:70
float z
Definition: nviz.h:74
float g
Definition: nviz.h:72
Definition: nviz.h:97
light_data light[MAX_LIGHTS]
Definition: nviz.h:108
#define x