1 
2 //
3 // This source file is part of appleseed.
4 // Visit https://appleseedhq.net/ for additional information and resources.
5 //
6 // This software is released under the MIT license.
7 //
8 // Copyright (c) 2010-2013 Francois Beaune, Jupiter Jazz Limited
9 // Copyright (c) 2014-2018 Francois Beaune, The appleseedhq Organization
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 // THE SOFTWARE.
28 //
29 
30 #pragma once
31 
32 // appleseed.renderer headers.
33 #include "renderer/modeling/entity/entity.h"
34 #include "renderer/modeling/scene/containers.h"
35 
36 // appleseed.foundation headers.
37 #include "foundation/math/transform.h"
38 #include "foundation/platform/compiler.h"
39 #include "foundation/platform/types.h"
40 #include "foundation/utility/autoreleaseptr.h"
41 #include "foundation/utility/uid.h"
42 
43 // appleseed.main headers.
44 #include "main/dllsymbol.h"
45 
46 // Standard headers.
47 #include <cassert>
48 
49 // Forward declarations.
50 namespace foundation    { class DictionaryArray; }
51 namespace renderer      { class ParamArray; }
52 namespace renderer      { class Texture; }
53 
54 namespace renderer
55 {
56 
57 //
58 // Texture modes.
59 //
60 
61 enum TextureAddressingMode
62 {
63     TextureAddressingClamp,
64     TextureAddressingWrap
65 };
66 
67 enum TextureFilteringMode
68 {
69     TextureFilteringNearest,
70     TextureFilteringBilinear,
71     TextureFilteringBicubic,
72     TextureFilteringFeline,             // Reference: http://www.hpl.hp.com/techreports/Compaq-DEC/WRL-99-1.pdf
73     TextureFilteringEWA
74 };
75 
76 enum TextureAlphaMode
77 {
78     TextureAlphaModeAlphaChannel,
79     TextureAlphaModeLuminance,
80     TextureAlphaModeDetect
81 };
82 
83 
84 //
85 // An instance of a texture.
86 //
87 // todo: allow to specify the lighting conditions of a texture.
88 //
89 
90 class APPLESEED_DLLSYMBOL TextureInstance
91   : public Entity
92 {
93   public:
94     // Return the unique ID of this class of entities.
95     static foundation::UniqueID get_class_uid();
96 
97     // Delete this instance.
98     void release() override;
99 
100     // Compute and return the unique signature of this instance.
101     foundation::uint64 compute_signature() const override;
102 
103     // Return the name of the instantiated texture.
104     const char* get_texture_name() const;
105 
106     // Return the texture transform.
107     const foundation::Transformf& get_transform() const;
108 
109     // Return the modes.
110     TextureAddressingMode get_addressing_mode() const;
111     TextureFilteringMode get_filtering_mode() const;
112     TextureAlphaMode get_alpha_mode() const;
113 
114     // Find the texture bound to this instance.
115     Texture* find_texture() const;
116 
117     // Texture binding.
118     void unbind_texture();
119     void bind_texture(const TextureContainer& textures);
120     void check_texture() const;
121 
122     // Return the texture bound to this instance.
123     Texture& get_texture() const;
124 
125     // Return the effective (detected) alpha mode.
126     // A texture must be bound to this instance.
127     TextureAlphaMode get_effective_alpha_mode() const;
128 
129   private:
130     friend class TextureInstanceFactory;
131 
132     struct Impl;
133     Impl* impl;
134 
135     TextureAddressingMode   m_addressing_mode;
136     TextureFilteringMode    m_filtering_mode;
137     TextureAlphaMode        m_alpha_mode;
138     TextureAlphaMode        m_effective_alpha_mode;
139     Texture*                m_texture;
140 
141     // Constructor.
142     TextureInstance(
143         const char*                     name,
144         const ParamArray&               params,
145         const char*                     texture_name,
146         const foundation::Transformf&   transform);
147 
148     // Destructor.
149     ~TextureInstance() override;
150 };
151 
152 
153 //
154 // Texture instance factory.
155 //
156 
157 class APPLESEED_DLLSYMBOL TextureInstanceFactory
158 {
159   public:
160     // Return a set of input metadata for texture instance entities.
161     static foundation::DictionaryArray get_input_metadata();
162 
163     // Create a new texture instance.
164     static foundation::auto_release_ptr<TextureInstance> create(
165         const char*                     name,
166         const ParamArray&               params,
167         const char*                     texture_name,
168         const foundation::Transformf&   transform = foundation::Transformf::identity());
169 };
170 
171 
172 //
173 // TextureInstance class implementation.
174 //
175 
get_addressing_mode()176 inline TextureAddressingMode TextureInstance::get_addressing_mode() const
177 {
178     return m_addressing_mode;
179 }
180 
get_filtering_mode()181 inline TextureFilteringMode TextureInstance::get_filtering_mode() const
182 {
183     return m_filtering_mode;
184 }
185 
get_alpha_mode()186 inline TextureAlphaMode TextureInstance::get_alpha_mode() const
187 {
188     return m_alpha_mode;
189 }
190 
get_texture()191 inline Texture& TextureInstance::get_texture() const
192 {
193     assert(m_texture);
194 
195     return *m_texture;
196 }
197 
get_effective_alpha_mode()198 inline TextureAlphaMode TextureInstance::get_effective_alpha_mode() const
199 {
200     assert(m_texture);
201 
202     return m_effective_alpha_mode;
203 }
204 
205 }   // namespace renderer
206