1 /*
2  * Copyright 2011-2020 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "render/image_sky.h"
18 
19 #include "sky_model.h"
20 
21 #include "util/util_image.h"
22 #include "util/util_logging.h"
23 #include "util/util_path.h"
24 #include "util/util_task.h"
25 
26 CCL_NAMESPACE_BEGIN
27 
SkyLoader(float sun_elevation,float altitude,float air_density,float dust_density,float ozone_density)28 SkyLoader::SkyLoader(float sun_elevation,
29                      float altitude,
30                      float air_density,
31                      float dust_density,
32                      float ozone_density)
33     : sun_elevation(sun_elevation),
34       altitude(altitude),
35       air_density(air_density),
36       dust_density(dust_density),
37       ozone_density(ozone_density)
38 {
39 }
40 
~SkyLoader()41 SkyLoader::~SkyLoader(){};
42 
load_metadata(ImageMetaData & metadata)43 bool SkyLoader::load_metadata(ImageMetaData &metadata)
44 {
45   metadata.width = 512;
46   metadata.height = 128;
47   metadata.channels = 3;
48   metadata.depth = 1;
49   metadata.type = IMAGE_DATA_TYPE_FLOAT4;
50   metadata.compress_as_srgb = false;
51   return true;
52 }
53 
load_pixels(const ImageMetaData & metadata,void * pixels,const size_t,const bool)54 bool SkyLoader::load_pixels(const ImageMetaData &metadata,
55                             void *pixels,
56                             const size_t /*pixels_size*/,
57                             const bool /*associate_alpha*/)
58 {
59   /* definitions */
60   int width = metadata.width;
61   int height = metadata.height;
62   float *pixel_data = (float *)pixels;
63 
64   /* precompute sky texture */
65   const int rows_per_task = divide_up(1024, width);
66   parallel_for(blocked_range<size_t>(0, height, rows_per_task),
67                [&](const blocked_range<size_t> &r) {
68                  SKY_nishita_skymodel_precompute_texture(pixel_data,
69                                                          metadata.channels,
70                                                          r.begin(),
71                                                          r.end(),
72                                                          width,
73                                                          height,
74                                                          sun_elevation,
75                                                          altitude,
76                                                          air_density,
77                                                          dust_density,
78                                                          ozone_density);
79                });
80 
81   return true;
82 }
83 
name() const84 string SkyLoader::name() const
85 {
86   return "sky_nishita";
87 }
88 
equals(const ImageLoader &) const89 bool SkyLoader::equals(const ImageLoader & /*other*/) const
90 {
91   return false;
92 }
93 
94 CCL_NAMESPACE_END
95