Code

Merge from fe-moved
[inkscape.git] / src / display / nr-light.cpp
1 #define __NR_LIGHT_CPP__
3 /*
4  * Light rendering helpers
5  *
6  * Author:
7  *   Jean-Rene Reinhard <jr@komite.net>
8  *
9  * Copyright (C) 2006 Jean-Rene Reinhard
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include <cmath>
16 #include "libnr/nr-pixops.h"
17 #include "display/nr-light.h"
18 #include "display/nr-3dutils.h"
19 #include "filters/distantlight.h"
20 #include "filters/pointlight.h"
21 #include "filters/spotlight.h"
23 namespace NR {
24    
25 DistantLight::DistantLight(SPFeDistantLight *light, guint32 lighting_color) {
26     color = lighting_color;
27     azimuth = M_PI / 180 * light->azimuth;
28     elevation = M_PI / 180 * light->elevation;
29 }
31 DistantLight::~DistantLight() {}
33 void DistantLight::light_vector(Fvector &v) {
34     v[X_3D] = std::cos(azimuth)*std::cos(elevation);
35     v[Y_3D] = std::sin(azimuth)*std::cos(elevation);
36     v[Z_3D] = std::sin(elevation);
37
39 void DistantLight::light_components(Fvector &lc) {
40     lc[LIGHT_RED] = NR_RGBA32_R(color);
41     lc[LIGHT_GREEN] = NR_RGBA32_G(color);
42     lc[LIGHT_BLUE] = NR_RGBA32_B(color);
43 }
45 PointLight::PointLight(SPFePointLight *light, guint32 lighting_color, const Matrix &trans) {
46     color = lighting_color;
47     l_x = light->x;
48     l_y = light->y;
49     l_z = light->z;
50     convert_coord(l_x, l_y, l_z, trans);
51 }
53 PointLight::~PointLight() {}
55 void PointLight::light_vector(Fvector &v, gdouble x, gdouble y, gdouble z) {
56     v[X_3D] = l_x - x;
57     v[Y_3D] = l_y - y;
58     v[Z_3D] = l_z - z;
59     normalize_vector(v);
60
62 void PointLight::light_components(Fvector &lc) {
63     lc[LIGHT_RED] = NR_RGBA32_R(color);
64     lc[LIGHT_GREEN] = NR_RGBA32_G(color);
65     lc[LIGHT_BLUE] = NR_RGBA32_B(color);
66 }
68 SpotLight::SpotLight(SPFeSpotLight *light, guint32 lighting_color, const Matrix &trans) {
69     gdouble p_x, p_y, p_z;
70     color = lighting_color;
71     l_x = light->x;
72     l_y = light->y;
73     l_z = light->z;
74     p_x = light->pointsAtX;
75     p_y = light->pointsAtY;
76     p_z = light->pointsAtZ;
77     cos_lca = std::cos(M_PI / 180 * light->limitingConeAngle);
78     speExp = light->specularExponent;
79     convert_coord(l_x, l_y, l_z, trans);
80     convert_coord(p_x, p_y, p_z, trans);
81     S[X_3D] = p_x - l_x;
82     S[Y_3D] = p_y - l_y;
83     S[Z_3D] = p_z - l_z;
84     normalize_vector(S);
85     
86 }
88 SpotLight::~SpotLight() {}
90 void SpotLight::light_vector(Fvector &v, gdouble x, gdouble y, gdouble z) {
91     v[X_3D] = l_x - x;
92     v[Y_3D] = l_y - y;
93     v[Z_3D] = l_z - z;
94     normalize_vector(v);
95
97 void SpotLight::light_components(Fvector &lc, const Fvector &L) {
98     gdouble spmod = (-1) * scalar_product(L, S);
99     if (spmod <= cos_lca)
100         spmod = 0;
101     else
102         spmod = std::pow(spmod, speExp);
103     lc[LIGHT_RED] = spmod * NR_RGBA32_R(color);
104     lc[LIGHT_GREEN] = spmod * NR_RGBA32_G(color);
105     lc[LIGHT_BLUE] = spmod * NR_RGBA32_B(color);
108 } /* namespace NR */
110 /*
111   Local Variables:
112   mode:c++
113   c-file-style:"stroustrup"
114   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
115   indent-tabs-mode:nil
116   fill-column:99
117   End:
118 */
119 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :