Code

9ff3922e8d1c2b049b8d10ab7da40e0c18e76872
[inkscape.git] / src / display / nr-filter-diffuselighting.cpp
1 /*
2  * feDiffuseLighting renderer
3  *
4  * Authors:
5  *   Niko Kiirala <niko@kiirala.com>
6  *   Jean-Rene Reinhard <jr@komite.net>
7  *
8  * Copyright (C) 2007 authors
9  *
10  * Released under GNU GPL, read the file 'COPYING' for more information
11  */
13 #include <glib/gmessages.h>
15 #include "display/nr-3dutils.h"
16 #include "display/nr-arena-item.h"
17 #include "display/nr-filter-diffuselighting.h"
18 #include "display/nr-filter-getalpha.h"
19 #include "display/nr-filter-slot.h"
20 #include "display/nr-filter-units.h"
21 #include "display/nr-filter-utils.h"
22 #include "display/nr-light.h"
23 #include "libnr/nr-blit.h"
24 #include "libnr/nr-pixblock.h"
25 #include "libnr/nr-matrix.h"
26 #include "libnr/nr-rect-l.h"
27 #include "color.h"
29 namespace NR {
31 FilterDiffuseLighting::FilterDiffuseLighting() 
32 {
33     light_type = NO_LIGHT;
34     diffuseConstant = 1;
35     surfaceScale = 1;
36     lighting_color = 0xffffffff;
37 }
39 FilterPrimitive * FilterDiffuseLighting::create() {
40     return new FilterDiffuseLighting();
41 }
43 FilterDiffuseLighting::~FilterDiffuseLighting()
44 {}
46 #define COMPUTE_INTER(inter, N, L, kd) \
47 do {\
48     (inter) = (kd) * scalar_product((N), (L)); \
49     if ((inter) < 0) (inter) = 0; \
50 }while(0)
53 int FilterDiffuseLighting::render(FilterSlot &slot, FilterUnits const &units) {
54     NRPixBlock *in = filter_get_alpha(slot.get(_input));
55     NRPixBlock *out = new NRPixBlock;
57     int w = in->area.x1 - in->area.x0;
58     int h = in->area.y1 - in->area.y0;
59     int x0 = in->area.x0;
60     int y0 = in->area.y0;
61     int i, j;
62     //As long as FilterRes and kernel unit is not supported we hardcode the
63     //default value
64     int dx = 1; //TODO setup
65     int dy = 1; //TODO setup
66     //surface scale
67     Matrix trans = units.get_matrix_primitiveunits2pb();
68     gdouble ss = surfaceScale * trans[0];
69     gdouble kd = diffuseConstant; //diffuse lighting constant
71     Fvector L, N, LC;
72     gdouble inter;
74     nr_pixblock_setup_fast(out, in->mode,
75             in->area.x0, in->area.y0, in->area.x1, in->area.y1,
76             true);
77     unsigned char *data_i = NR_PIXBLOCK_PX (in);
78     unsigned char *data_o = NR_PIXBLOCK_PX (out);
79     //No light, nothing to do
80     switch (light_type) {
81         case DISTANT_LIGHT:  
82             //the light vector is constant
83             {
84             DistantLight *dl = new DistantLight(light.distant, lighting_color);
85             dl->light_vector(L);
86             dl->light_components(LC);
87             //finish the work
88             for (i = 0, j = 0; i < w*h; i++) {
89                 compute_surface_normal(N, ss, in, i / w, i % w, dx, dy);
90                 COMPUTE_INTER(inter, N, L, kd);
92                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_RED]);
93                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_GREEN]);
94                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_BLUE]);
95                 data_o[j++] = 255;
96             }
97             out->empty = FALSE;
98             delete dl;
99             }
100             break;
101         case POINT_LIGHT:
102             {
103             PointLight *pl = new PointLight(light.point, lighting_color, trans);
104             pl->light_components(LC);
105         //TODO we need a reference to the filter to determine primitiveUnits
106         //if objectBoundingBox is used, use a different matrix for light_vector
107         // UPDATE: trans is now correct matrix from primitiveUnits to
108         // pixblock coordinates
109             //finish the work
110             for (i = 0, j = 0; i < w*h; i++) {
111                 compute_surface_normal(N, ss, in, i / w, i % w, dx, dy);
112                 pl->light_vector(L,
113                         i % w + x0,
114                         i / w + y0,
115                         ss * (double) data_i[4*i+3]/ 255);
116                 COMPUTE_INTER(inter, N, L, kd);
118                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_RED]);
119                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_GREEN]);
120                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_BLUE]);
121                 data_o[j++] = 255;
122             }
123             out->empty = FALSE;
124             delete pl;
125             }
126             break;
127         case SPOT_LIGHT:
128             {
129             SpotLight *sl = new SpotLight(light.spot, lighting_color, trans);
130         //TODO we need a reference to the filter to determine primitiveUnits
131         //if objectBoundingBox is used, use a different matrix for light_vector
132         // UPDATE: trans is now correct matrix from primitiveUnits to
133         // pixblock coordinates
134             //finish the work
135             for (i = 0, j = 0; i < w*h; i++) {
136                 compute_surface_normal(N, ss, in, i / w, i % w, dx, dy);
137                 sl->light_vector(L,
138                     i % w + x0,
139                     i / w + y0,
140                     ss * (double) data_i[4*i+3]/ 255);
141                 sl->light_components(LC, L);
142                 COMPUTE_INTER(inter, N, L, kd);
143                 
144                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_RED]);
145                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_GREEN]);
146                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_BLUE]);
147                 data_o[j++] = 255;
148             }
149             out->empty = FALSE;
150             delete sl;
151             }
152             break;
153         //else unknown light source, doing nothing
154         case NO_LIGHT:
155         default:
156             {
157             if (light_type != NO_LIGHT)
158                 g_warning("unknown light source %d", light_type);
159             for (i = 0; i < w*h; i++) {
160                 data_o[4*i+3] = 255;
161             }
162             out->empty = false;
163             }
164     }
165         
166     //finishing
167     slot.set(_output, out);
168     nr_pixblock_release(in);
169     delete in;
170     return 0;
173 FilterTraits FilterDiffuseLighting::get_input_traits() {
174     return TRAIT_PARALLER;
177 } /* namespace NR */
179 /*
180   Local Variables:
181   mode:c++
182   c-file-style:"stroustrup"
183   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
184   indent-tabs-mode:nil
185   fill-column:99
186   End:
187 */
188 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :