Raytracing to determine whether a pixel is lit or not
[tecorrec.git] / geo / tcLambertianShading.cpp
blobb36d86707552d90186be0b09410afd884b50a690
1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * Tecorrec is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * Tecorrec is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with Tecorrec. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file tcLambertianShading.cpp
22 * @brief Shading channel using lambertian reflectance.
25 #include "tcLambertianShading.h"
27 #include <QObject>
30 * Constructors + destructor
33 /// Primary constructor.
34 tcLambertianShading::tcLambertianShading(tcChannel* reference, tcSrtmModel* dem, tcGeoImageData* imagery)
35 : tcChannelDem(dem, imagery,
36 QObject::tr("Lambertian Shading"),
37 QObject::tr("Uses lambertian shading and elevation data to shade pixels. "
38 "Cast shadows are not taken into account. "
39 "Self shadows are set to zero."))
40 , m_referenceChannel(reference)
44 /// Destructor.
45 tcLambertianShading::~tcLambertianShading()
50 * Interface for derived class to implement
53 void tcLambertianShading::roundPortion(double* x1, double* y1, double* x2, double* y2)
55 m_referenceChannel->roundPortion(x1,y1,x2,y2);
58 tcAbstractPixelData* tcLambertianShading::loadPortion(double x1, double y1, double x2, double y2)
60 Reference<tcAbstractPixelData> channelData = m_referenceChannel->loadPortion(x1, y1, x2, y2);
61 int width = channelData->width();
62 int height = channelData->height();
64 // Create a new pixel buffer
65 tcPixelData<float>* data = new tcPixelData<float>(width, height);
66 for (int j = 0; j < height; ++j)
68 for (int i = 0; i < width; ++i)
70 int index = j*width + i;
71 // Transform coordinates
72 maths::Vector<2,float> coord ( x1 + (x2-x1)*i / (width - 1),
73 1.0f - y1 - (y2-y1)*j / (height - 1));
74 tcGeo geoCoord = geoAt(coord);
76 // Get some elevation data
77 maths::Vector<3,float> normal = normalAt(geoCoord);
78 maths::Vector<3,float> light = lightDirectionAt(geoCoord);
79 // Find dot product between normal and light vectors and limit it to 0.0
80 float lambert = normal * light;
81 data->buffer()[index] = (lambert >= 0.0f ? lambert : 0.0f);
84 return data;