5 #include "SunLightBlending.h"
7 #include "MarbleDebug.h"
8 #include "SunLocator.h"
9 #include "TextureTile.h"
10 #include "TileLoaderHelper.h"
11 #include "MarbleGlobal.h"
21 SunLightBlending::SunLightBlending(
const SunLocator * sunLocator )
23 m_sunLocator( sunLocator ),
24 m_levelZeroColumns( 0 ),
29 SunLightBlending::~SunLightBlending()
33 void SunLightBlending::blend(
QImage *
const tileImage, TextureTile
const *
const top )
const
35 if ( tileImage->
depth() != 32 )
40 const TileId
id = top->id();
41 const qreal global_width = tileImage->
width()
42 * TileLoaderHelper::levelToColumn( m_levelZeroColumns,
id.zoomLevel() );
43 const qreal global_height = tileImage->
height()
44 * TileLoaderHelper::levelToRow( m_levelZeroRows,
id.zoomLevel() );
45 const qreal lon_scale = 2*M_PI / global_width;
46 const qreal lat_scale = -M_PI / global_height;
47 const int tileHeight = tileImage->
height();
48 const int tileWidth = tileImage->
width();
51 const int n = maxDivisor( 30, tileWidth );
52 const int ipRight = n * (int)( tileWidth / n );
54 const QImage *nighttile = top->image();
56 for (
int cur_y = 0; cur_y < tileHeight; ++cur_y ) {
57 const qreal lat = lat_scale * (
id.y() * tileHeight + cur_y ) - 0.5*M_PI;
58 const qreal a = sin( ( lat+DEG2RAD * m_sunLocator->getLat() )/2.0 );
59 const qreal c = cos(lat)*cos( -DEG2RAD * m_sunLocator->getLat() );
61 QRgb* scanline = (QRgb*)tileImage->
scanLine( cur_y );
62 const QRgb* nscanline = (QRgb*)nighttile->
scanLine( cur_y );
64 qreal lastShade = -10.0;
68 while ( cur_x < tileWidth ) {
70 const bool interpolate = ( cur_x != 0 && cur_x < ipRight && cur_x + n < tileWidth );
75 const int check = cur_x + n;
76 const qreal checklon = lon_scale * (
id.x() * tileWidth + check );
77 shade = m_sunLocator->shading( checklon, a, c );
81 if ( shade == lastShade && shade == 1.0 ) {
87 if ( shade == lastShade && shade == 0.0 ) {
88 for (
int t = 0; t < n; ++t ) {
89 SunLocator::shadePixelComposite(*scanline, *nscanline, shade);
97 qreal lon = lon_scale * (
id.x() * tileWidth + cur_x);
98 for (
int t = 0; t < n ; ++t ) {
99 shade = m_sunLocator->shading( lon, a, c );
100 SunLocator::shadePixelComposite(*scanline, *nscanline, shade);
110 if ( cur_x < tileWidth ) {
111 qreal lon = lon_scale * (
id.x() * tileWidth + cur_x );
112 shade = m_sunLocator->shading( lon, a, c );
113 SunLocator::shadePixelComposite(*scanline, *nscanline, shade);
124 void SunLightBlending::setLevelZeroLayout(
int levelZeroColumns,
int levelZeroRows )
126 m_levelZeroColumns = levelZeroColumns;
127 m_levelZeroRows = levelZeroRows;
131 int SunLightBlending::maxDivisor(
int maximum,
int fullLength )
137 int nEvalMin = fullLength;
138 for (
int it = 1; it <= maximum; ++it ) {
142 int nEval = fullLength / it + fullLength % it;
143 if ( nEval < nEvalMin ) {