GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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 2008/2010)
13  */
14 
15 #include <grass/glocale.h>
16 #include <grass/nviz.h>
17 
18 /*!
19  \brief Set light position
20 
21  \param data nviz data
22  \param num light num (starts at 1)
23  \param x,y,z,w position, model coordinates
24 
25  \return 1
26  */
27 int Nviz_set_light_position(nv_data * data, int num,
28  double x, double y, double z, double w)
29 {
30  /*
31  double xpos, ypos;
32  xpos = x;
33  xpos = (xpos < 0) ? 0 : (xpos > 1.0) ? 1.0 : xpos;
34  ypos = 1.0 - y;
35  ypos = (ypos < 0) ? 0 : (ypos > 1.0) ? 1.0 : ypos;
36 
37  if (x < 0.0 || x > 1.0 || y < 0.0 || y > 1.0) {
38  G_debug(1, "Invalid light position coordinates (%f,%f), using %f,%f",
39  x, y, xpos, 1.0 - ypos);
40  }
41  */
42 
43  data->light[num].id = num;
44  data->light[num].x = x;
45  data->light[num].y = y;
46  data->light[num].z = z;
47  data->light[num].w = w;
48 
49  G_debug(1, "Nviz_set_light_position(): num = %d x = %f y = %f z = %f w = %f",
50  num, x, y, z, w);
51  GS_setlight_position(num, x, y, z, w);
52 
53  return 1;
54 }
55 
56 /*!
57  \brief Set light brightness
58 
59  \param data nviz data
60  \param num light num (starts at 1)
61  \param value brightness value
62  */
63 int Nviz_set_light_bright(nv_data * data, int num, double value)
64 {
65  double r, g, b;
66 
67  data->light[num].brt = value;
68 
69  r = data->light[num].r * data->light[num].brt;
70  g = data->light[num].g * data->light[num].brt;
71  b = data->light[num].b * data->light[num].brt;
72 
73  G_debug(1, "Nviz_set_light_bright(): num = %d value = %f r = %f g = %f b = %f",
74  num, value, r, g, b);
75  GS_setlight_color(num, r, g, b);
76 
77  return 1;
78 }
79 
80 /*!
81  \brief Set light color
82 
83  \param data nviz data
84  \param num light num (starts at 1)
85  \param red,green,blue RGB values (0-255)
86  */
87 int Nviz_set_light_color(nv_data * data, int num,
88  int red, int green, int blue)
89 {
90  double r, g, b;
91 
92  data->light[num].r = red / 255.;
93  data->light[num].g = green / 255.;
94  data->light[num].b = blue / 255.;
95 
96  r = data->light[num].r * data->light[num].brt;
97  g = data->light[num].g * data->light[num].brt;
98  b = data->light[num].b * data->light[num].brt;
99 
100  G_debug(1, "Nviz_set_light_color(): num = %d r = %d/%f g = %d/%f b = %d/%f",
101  num, red, r, green, g, blue, b);
102  GS_setlight_color(num, r, g, b);
103 
104  return 1;
105 }
106 
107 /*!
108  \brief Set light ambient
109 
110  \param data nviz data
111  \param num light num (starts at 1)
112  \param value ambient value (same for R/G/B) (0-1)
113  */
114 int Nviz_set_light_ambient(nv_data * data, int num, double value)
115 {
116  data->light[num].ar = value;
117  data->light[num].ag = value;
118  data->light[num].ab = value;
119 
120  G_debug(1, "Nviz_set_light_ambient(): num = %d value = %f",
121  num, value);
122  GS_setlight_ambient(num, value, value, value);
123 
124  return 1;
125 }
126 
127 /*!
128  \brief Init new light
129 
130  \param data nviz data
131  \param num light num (starts at 1)
132  */
133 int Nviz_init_light(nv_data * data, int num)
134 {
135  G_debug(1, "Nviz_init_light(): num = %d", num);
136  if (num > MAX_LIGHTS) {
137  return 0;
138  }
139 
140  data->light[num].id = 0;
141  data->light[num].brt = 0.8;
142  data->light[num].ar = 0.3;
143  data->light[num].ag = 0.3;
144  data->light[num].ab = 0.3;
145  data->light[num].r = 1.0;
146  data->light[num].b = 1.0;
147  data->light[num].g = 1.0;
148  data->light[num].x = 1.0;
149  data->light[num].y = 1.0;
150  data->light[num].z = 1.0;
151  data->light[num].w = 1.0;
152 
153  return 1;
154 }
155 
156 /*!
157  \brief Define new light
158 
159  \param data nviz data
160 
161  \return 1 on success
162  \return 0 on failure
163 */
165 {
166  int num;
167 
168  num = GS_new_light();
169 
170  if (num < 1) {
171  G_warning(_("Unable to define new light"));
172  return 0;
173  }
174 
175  Nviz_init_light(data, num);
176 
177  return 1;
178 }
179 
180 /*!
181  \brief Draw lighting model
182 
183  \param data nviz data
184 */
186 {
188  GS_ready_draw();
190  GS_done_draw();
192 }
void GS_setlight_ambient(int, float, float, float)
Set light ambient.
Definition: gs2.c:402
void Nviz_draw_model(nv_data *data)
Draw lighting model.
Definition: lights.c:185
float w
Definition: nviz.h:75
float r
Definition: nviz.h:73
float b
Definition: nviz.h:73
int id
Definition: nviz.h:71
void GS_ready_draw(void)
Definition: gs2.c:2488
#define MAX_LIGHTS
Definition: ogsf.h:44
float z
Definition: nviz.h:75
float ar
Definition: nviz.h:74
int GS_new_light(void)
Add new model light.
Definition: gs2.c:268
int Nviz_init_light(nv_data *data, int num)
Init new light.
Definition: lights.c:133
void GS_draw_lighting_model(void)
Draw lighting model.
Definition: gs2.c:941
#define x
int Nviz_set_light_color(nv_data *data, int num, int red, int green, int blue)
Set light color.
Definition: lights.c:87
float g
Definition: nviz.h:73
int Nviz_set_light_bright(nv_data *data, int num, double value)
Set light brightness.
Definition: lights.c:63
int Nviz_set_light_ambient(nv_data *data, int num, double value)
Set light ambient.
Definition: lights.c:114
double b
Definition: r_raster.c:39
void GS_setlight_color(int, float, float, float)
Set light color.
Definition: gs2.c:358
float ab
Definition: nviz.h:74
float g
Definition: named_colr.c:8
float x
Definition: nviz.h:75
float y
Definition: nviz.h:75
float brt
Definition: nviz.h:72
void GS_done_draw(void)
Draw done, swap buffers.
Definition: gs2.c:2501
void GS_setlight_position(int, float, float, float, int)
Set light position.
Definition: gs2.c:309
light_data light[MAX_LIGHTS]
Definition: nviz.h:113
#define GSD_BACK
Definition: ogsf.h:102
float ag
Definition: nviz.h:74
Definition: nviz.h:101
int Nviz_set_light_position(nv_data *data, int num, double x, double y, double z, double w)
Set light position.
Definition: lights.c:27
void G_warning(const char *,...) __attribute__((format(printf
#define GSD_FRONT
Definition: ogsf.h:101
#define _(str)
Definition: glocale.h:10
int Nviz_new_light(nv_data *data)
Define new light.
Definition: lights.c:164
void GS_set_draw(int)
Sets which buffer to draw to.
Definition: gs2.c:2462
int G_debug(int, const char *,...) __attribute__((format(printf
double r
Definition: r_raster.c:39