Code

Warning cleanup
[inkscape.git] / src / display / nr-filter-specularlighting.cpp
1 /*
2  * feSpecularLighting 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>
14 #include <cmath>
16 #include "display/nr-3dutils.h"
17 #include "display/nr-arena-item.h"
18 #include "display/nr-filter-specularlighting.h"
19 #include "display/nr-filter-getalpha.h"
20 #include "display/nr-filter-slot.h"
21 #include "display/nr-filter-units.h"
22 #include "display/nr-filter-utils.h"
23 #include "display/nr-light.h"
24 #include "libnr/nr-blit.h"
25 #include "libnr/nr-pixblock.h"
26 #include "libnr/nr-matrix.h"
27 #include "libnr/nr-rect-l.h"
28 #include "color.h"
30 namespace NR {
32 FilterSpecularLighting::FilterSpecularLighting()
33 {
34     light_type = NO_LIGHT;
35     specularConstant = 1;
36     specularExponent = 1;
37     surfaceScale = 1;
38     lighting_color = 0xffffffff;
39 }
41 FilterPrimitive * FilterSpecularLighting::create() {
42     return new FilterSpecularLighting();
43 }
45 FilterSpecularLighting::~FilterSpecularLighting()
46 {}
48 //Investigating Phong Lighting model we should not take N.H but
49 //R.E which equals to 2*N.H^2 - 1
50 //replace the second line by
51 //gdouble scal = scalar_product((N), (H)); scal = 2 * scal * scal - 1;
52 //to get the expected formula
53 #define COMPUTE_INTER(inter, H, N, ks, speculaExponent) \
54 do {\
55     gdouble scal = scalar_product((N), (H)); \
56     if (scal <= 0)\
57         (inter) = 0;\
58     else\
59         (inter) = (ks) * std::pow(scal, (specularExponent));\
60 }while(0)
62 int FilterSpecularLighting::render(FilterSlot &slot, FilterUnits const &units) {
63     NRPixBlock *in = filter_get_alpha(slot.get(_input));
64     NRPixBlock *out = new NRPixBlock;
66     //Fvector *L = NULL; //vector to the light
68     int w = in->area.x1 - in->area.x0;
69     int h = in->area.y1 - in->area.y0;
70     int x0 = in->area.x0;
71     int y0 = in->area.y0;
72     int i, j;
73     //As long as FilterRes and kernel unit is not supported we hardcode the
74     //default value
75     int dx = 1; //TODO setup
76     int dy = 1; //TODO setup
77     //surface scale
78     Matrix trans = units.get_matrix_primitiveunits2pb();
79     gdouble ss = surfaceScale * trans[0];
80     gdouble ks = specularConstant; //diffuse lighting constant
81     Fvector L, N, LC, H;
82     gdouble inter;
84     nr_pixblock_setup_fast(out, in->mode,
85             in->area.x0, in->area.y0, in->area.x1, in->area.y1,
86             true);
87     unsigned char *data_i = NR_PIXBLOCK_PX (in);
88     unsigned char *data_o = NR_PIXBLOCK_PX (out);
89     //No light, nothing to do
90     switch (light_type) {
91         case DISTANT_LIGHT:
92             //the light vector is constant
93             {
94             DistantLight *dl = new DistantLight(light.distant, lighting_color);
95             dl->light_vector(L);
96             dl->light_components(LC);
97             normalized_sum(H, L, EYE_VECTOR);
98             //finish the work
99             for (i = 0, j = 0; i < w*h; i++) {
100                 compute_surface_normal(N, ss, in, i / w, i % w, dx, dy);
101                 COMPUTE_INTER(inter, N, H, ks, specularExponent);
103                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_RED]);
104                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_GREEN]);
105                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_BLUE]);
106                 data_o[j++] = MAX(MAX(data_o[j-3], data_o[j-2]), data_o[j-1]);
107             }
108             out->empty = FALSE;
109             delete dl;
110             }
111             break;
112         case POINT_LIGHT:
113             {
114             PointLight *pl = new PointLight(light.point, lighting_color, trans);
115             pl->light_components(LC);
116         //TODO we need a reference to the filter to determine primitiveUnits
117         //if objectBoundingBox is used, use a different matrix for light_vector
118         // UPDATE: trans is now correct matrix from primitiveUnits to
119         // pixblock coordinates
120             //finish the work
121             for (i = 0, j = 0; i < w*h; i++) {
122                 compute_surface_normal(N, ss, in, i / w, i % w, dx, dy);
123                 pl->light_vector(L,
124                         i % w + x0,
125                         i / w + y0,
126                         ss * (double) data_i[4*i+3]/ 255);
127                 normalized_sum(H, L, EYE_VECTOR);
128                 COMPUTE_INTER(inter, N, H, ks, specularExponent);
130                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_RED]);
131                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_GREEN]);
132                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_BLUE]);
133                 data_o[j++] = MAX(MAX(data_o[j-3], data_o[j-2]), data_o[j-1]);
134             }
135             out->empty = FALSE;
136             delete pl;
137             }
138             break;
139         case SPOT_LIGHT:
140             {
141             SpotLight *sl = new SpotLight(light.spot, lighting_color, trans);
142         //TODO we need a reference to the filter to determine primitiveUnits
143         //if objectBoundingBox is used, use a different matrix for light_vector
144         // UPDATE: trans is now correct matrix from primitiveUnits to
145         // pixblock coordinates
146             //finish the work
147             for (i = 0, j = 0; i < w*h; i++) {
148                 compute_surface_normal(N, ss, in, i / w, i % w, dx, dy);
149                 sl->light_vector(L,
150                     i % w + x0,
151                     i / w + y0,
152                     ss * (double) data_i[4*i+3]/ 255);
153                 sl->light_components(LC, L);
154                 normalized_sum(H, L, EYE_VECTOR);
155                 COMPUTE_INTER(inter, N, H, ks, specularExponent);
157                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_RED]);
158                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_GREEN]);
159                 data_o[j++] = CLAMP_D_TO_U8(inter * LC[LIGHT_BLUE]);
160                 data_o[j++] = MAX(MAX(data_o[j-3], data_o[j-2]), data_o[j-1]);
161             }
162             out->empty = FALSE;
163             delete sl;
164             }
165             break;
166         //else unknown light source, doing nothing
167         case NO_LIGHT:
168         default:
169             {
170             if (light_type != NO_LIGHT)
171                 g_warning("unknown light source %d", light_type);
172             out->empty = false;
173             }
174     }
176     //finishing
177     slot.set(_output, out);
178     nr_pixblock_release(in);
179     delete in;
180     return 0;
183 FilterTraits FilterSpecularLighting::get_input_traits() {
184     return TRAIT_PARALLER;
187 } /* namespace NR */
189 /*
190   Local Variables:
191   mode:c++
192   c-file-style:"stroustrup"
193   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
194   indent-tabs-mode:nil
195   fill-column:99
196   End:
197 */
198 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :