tecorrec: Check for OpenGL and add includes/libs
[tecorrec.git] / geo / tcRaytracedShadowMap.cpp
blobe4091f7705c92c1833254bfc6ffa6b24b16d2a9e
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 tcRaytracedShadowMap.cpp
22 * @brief Shadow map using ray tracing of DEM.
25 #include "tcRaytracedShadowMap.h"
26 #include "tcGeoImageData.h"
28 #include <QObject>
31 * Constructors + destructor
34 /// Primary constructor.
35 tcRaytracedShadowMap::tcRaytracedShadowMap(tcChannel* reference, tcSrtmModel* dem, tcGeoImageData* imagery)
36 : tcChannelDem(dem, imagery,
37 tr("Raytraced Shadow Map"),
38 tr("Uses ray tracing and elevation data to highlight shadows."))
39 , m_referenceChannel(reference)
40 , m_resolution()
41 , m_texToGeo(imagery->texToGeo())
45 /// Primary constructor.
46 tcRaytracedShadowMap::tcRaytracedShadowMap(const tcGeo& resolution, tcSrtmModel* dem, tcGeoImageData* imagery)
47 : tcChannelDem(dem, imagery,
48 tr("Raytraced Shadow Map"),
49 tr("Uses ray tracing and elevation data to highlight shadows."))
50 , m_referenceChannel(0)
51 , m_resolution(resolution)
52 , m_texToGeo(imagery->texToGeo())
56 /// Destructor.
57 tcRaytracedShadowMap::~tcRaytracedShadowMap()
62 * Interface for derived class to implement
65 void tcRaytracedShadowMap::roundPortion(double* x1, double* y1, double* x2, double* y2)
67 //m_referenceChannel->roundPortion(x1,y1,x2,y2);
70 tcAbstractPixelData* tcRaytracedShadowMap::loadPortion(double x1, double y1, double x2, double y2, bool changed)
72 int width;
73 int height;
74 if (0 != m_referenceChannel)
76 Reference<tcAbstractPixelData> channelData = m_referenceChannel->loadPortion(x1, y1, x2, y2, changed);
77 width = channelData->width();
78 height = channelData->height();
80 else
82 tcGeo diag = (tcGeo)(m_texToGeo * maths::Vector<2,double>(x1, y1) - m_texToGeo * maths::Vector<2,double>(x2,y2));
83 maths::Vector<2,double> res = diag / m_resolution;
84 width = abs((int)res[0]);
85 height = abs((int)res[1]);
86 // ensure resw and resh are powers of 2
87 for (int i = 1; i < 20; ++i)
89 if (0 == (width & ~((1<<i)-1)))
91 width = 1<<i;
92 break;
95 for (int i = 1; i < 20; ++i)
97 if (0 == (height & ~((1<<i)-1)))
99 height = 1<<i;
100 break;
105 // Create a new pixel buffer
106 tcPixelData<float>* data = new tcPixelData<float>(width, height);
107 startProcessing(tr("Raytracing"));
108 for (int j = 0; j < height; ++j)
110 progress((float)j/(height-1));
111 for (int i = 0; i < width; ++i)
113 int index = j*width + i;
114 // Transform coordinates
115 maths::Vector<2,float> coord (x1 + (x2-x1)*i / (width - 1),
116 y1 + (y2-y1)*j / (height - 1));
117 tcGeo geoCoord = geoAt(coord);
118 float lit = litAt(geoCoord, true);
120 data->buffer()[index] = lit;
123 endProcessing();
124 return data;