Merge "Fix svace issue (strerror -> strerror_r)" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / environment-definition.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
18 // CLASS HEADER
19 #include <dali-scene3d/public-api/loader/environment-definition.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/environment-variable.h>
23 #include <dali/devel-api/adaptor-framework/image-loading.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-scene3d/public-api/loader/environment-map-loader.h>
27 #include <dali-scene3d/public-api/loader/utils.h>
28
29 namespace
30 {
31 #define TOKEN_STRING(x) #x
32 std::string GetDaliImagePath()
33 {
34   return (nullptr == DALI_IMAGE_DIR) ? Dali::EnvironmentVariable::GetEnvironmentVariable(TOKEN_STRING(DALI_IMAGE_DIR)) : DALI_IMAGE_DIR;
35 }
36
37 static constexpr float DEFAULT_INTENSITY = 1.0f;
38 } // unnamed namespace
39
40 namespace Dali::Scene3D::Loader
41 {
42 namespace
43 {
44 const char* PRE_COMPUTED_BRDF_TEXTURE_FILE_NAME = "brdfLUT.png";
45 }
46
47 EnvironmentDefinition::RawData
48 EnvironmentDefinition::LoadRaw(const std::string& environmentsPath) const
49 {
50   RawData raw;
51   auto    loadFn = [&environmentsPath](const std::string& path, EnvironmentMapData& environmentMapData) {
52     if(path.empty())
53     {
54       environmentMapData.mPixelData.resize(6);
55       for(auto& face : environmentMapData.mPixelData)
56       {
57         face.push_back(PixelData::New(new uint8_t[3]{0xff, 0xff, 0xff}, 3, 1, 1, Pixel::RGB888, PixelData::DELETE_ARRAY));
58       }
59       environmentMapData.SetEnvironmentMapType(Dali::Scene3D::EnvironmentMapType::CUBEMAP);
60     }
61     else if(!LoadEnvironmentMap(environmentsPath + path, environmentMapData))
62     {
63       ExceptionFlinger(ASSERT_LOCATION) << "Failed to load cubemap texture from '" << path << "'.";
64     }
65   };
66
67   loadFn(mDiffuseMapPath, raw.mDiffuse);
68   loadFn(mSpecularMapPath, raw.mSpecular);
69
70   if(mUseBrdfTexture)
71   {
72     Devel::PixelBuffer pixelBuffer = LoadImageFromFile(GetDaliImagePath() + PRE_COMPUTED_BRDF_TEXTURE_FILE_NAME);
73     if(pixelBuffer)
74     {
75       raw.mBrdf = Devel::PixelBuffer::Convert(pixelBuffer);
76     }
77   }
78   return raw;
79 }
80
81 EnvironmentDefinition::Textures EnvironmentDefinition::Load(RawData&& raw) const
82 {
83   Textures textures;
84
85   // This texture should have 6 faces and only one mipmap
86   if(!raw.mDiffuse.mPixelData.empty())
87   {
88     textures.mDiffuse = raw.mDiffuse.GetTexture();
89   }
90
91   // This texture should have 6 faces and 6 mipmaps
92   if(!raw.mSpecular.mPixelData.empty())
93   {
94     textures.mSpecular             = raw.mSpecular.GetTexture();
95     textures.mSpecularMipmapLevels = raw.mSpecular.GetMipmapLevels();
96   }
97
98   if(raw.mBrdf)
99   {
100     textures.mBrdf = Texture::New(TextureType::TEXTURE_2D, raw.mBrdf.GetPixelFormat(), raw.mBrdf.GetWidth(), raw.mBrdf.GetHeight());
101     textures.mBrdf.Upload(raw.mBrdf);
102   }
103   return textures;
104 }
105
106 float EnvironmentDefinition::GetDefaultIntensity()
107 {
108   return DEFAULT_INTENSITY;
109 }
110
111 } // namespace Dali::Scene3D::Loader