Modify default shader according to the gltf pbr spec
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / public-api / environment-definition.cpp
1 /*
2  * Copyright (c) 2022 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 // EXTERNAL INCLUDES
19 #include <dali/devel-api/adaptor-framework/environment-variable.h>
20 #include <dali/devel-api/adaptor-framework/image-loading.h>
21
22 // INTERNAL INCLUDES
23 #include "dali-scene-loader/public-api/environment-definition.h"
24 #include "dali-scene-loader/public-api/utils.h"
25
26 namespace
27 {
28 #define TOKEN_STRING(x) #x
29 std::string GetDaliImagePath()
30 {
31   return (nullptr == DALI_IMAGE_DIR) ? Dali::EnvironmentVariable::GetEnvironmentVariable(TOKEN_STRING(DALI_IMAGE_DIR)) : DALI_IMAGE_DIR;
32 }
33 } // unnamed namespace
34
35 namespace Dali
36 {
37 namespace SceneLoader
38 {
39 namespace
40 {
41 const std::string PRE_COMPUTED_BRDF_TEXTURE_FILE_NAME = "brdfLUT.png";
42 }
43
44 EnvironmentDefinition::RawData
45 EnvironmentDefinition::LoadRaw(const std::string& environmentsPath) const
46 {
47   RawData raw;
48   auto    loadFn = [&environmentsPath](const std::string& path, CubeData& cd) {
49     if(path.empty())
50     {
51       cd.data.resize(6);
52       for(auto& face : cd.data)
53       {
54         face.push_back(PixelData::New(new uint8_t[3]{0xff, 0xff, 0xff}, 3, 1, 1, Pixel::RGB888, PixelData::DELETE_ARRAY));
55       }
56     }
57     else if(!LoadCubeMapData(environmentsPath + path, cd))
58     {
59       ExceptionFlinger(ASSERT_LOCATION) << "Failed to load cubemap texture from '" << path << "'.";
60     }
61   };
62
63   loadFn(mDiffuseMapPath, raw.mDiffuse);
64   loadFn(mSpecularMapPath, raw.mSpecular);
65
66   if(mUseBrdfTexture)
67   {
68     Devel::PixelBuffer pixelBuffer = LoadImageFromFile(GetDaliImagePath() + PRE_COMPUTED_BRDF_TEXTURE_FILE_NAME);
69     if(pixelBuffer)
70     {
71       raw.mBrdf = Devel::PixelBuffer::Convert(pixelBuffer);
72     }
73   }
74   return raw;
75 }
76
77 EnvironmentDefinition::Textures EnvironmentDefinition::Load(RawData&& raw) const
78 {
79   Textures textures;
80
81   // This texture should have 6 faces and only one mipmap
82   if(!raw.mDiffuse.data.empty())
83   {
84     textures.mDiffuse = raw.mDiffuse.CreateTexture();
85   }
86
87   // This texture should have 6 faces and 6 mipmaps
88   if(!raw.mSpecular.data.empty())
89   {
90     textures.mSpecular = raw.mSpecular.CreateTexture();
91   }
92
93   if(raw.mBrdf)
94   {
95     textures.mBrdf = Texture::New(TextureType::TEXTURE_2D, raw.mBrdf.GetPixelFormat(), raw.mBrdf.GetWidth(), raw.mBrdf.GetHeight());
96     textures.mBrdf.Upload(raw.mBrdf);
97   }
98   return textures;
99 }
100
101 } // namespace SceneLoader
102 } // namespace Dali