1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2020, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
20 /**
21  * \file graphics/core/texture.h
22  * \brief Texture struct and related enums
23  */
24 
25 #pragma once
26 
27 
28 #include "graphics/core/color.h"
29 
30 #include "math/intpoint.h"
31 
32 
33 // Graphics module namespace
34 namespace Gfx
35 {
36 
37 
38 /**
39  * \enum TexImgFormat
40  * \brief Format of image data
41  */
42 enum TexImgFormat
43 {
44     //! Try to determine automatically (may not work)
45     TEX_IMG_AUTO,
46     //! RGB triplet, 3 bytes
47     TEX_IMG_RGB,
48     //! BGR triplet, 3 bytes
49     TEX_IMG_BGR,
50     //! RGBA triplet, 4 bytes
51     TEX_IMG_RGBA,
52     //! BGRA triplet, 4 bytes
53     TEX_IMG_BGRA
54 };
55 
56 /**
57  * \enum TexFilter
58  * \brief General texture filtering mode
59  *
60  * Corresponds to typical options in game graphics settings.
61  */
62 enum TexFilter
63 {
64     TEX_FILTER_NEAREST,
65     TEX_FILTER_BILINEAR,
66     TEX_FILTER_TRILINEAR
67 };
68 
69 /**
70  * \enum TexMinFilter
71  * \brief Texture minification filter
72  *
73  * Corresponds to OpenGL modes but should translate to DirectX too.
74  */
75 enum TexMinFilter
76 {
77     TEX_MIN_FILTER_NEAREST,
78     TEX_MIN_FILTER_LINEAR,
79     TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST,
80     TEX_MIN_FILTER_LINEAR_MIPMAP_NEAREST,
81     TEX_MIN_FILTER_NEAREST_MIPMAP_LINEAR,
82     TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR
83 };
84 
85 /**
86  * \enum TexMagFilter
87  * \brief Texture magnification filter
88  */
89 enum TexMagFilter
90 {
91     TEX_MAG_FILTER_NEAREST,
92     TEX_MAG_FILTER_LINEAR
93 };
94 
95 /**
96  * \enum TexWrapMode
97  * \brief Wrapping mode for texture coords
98  */
99 enum TexWrapMode
100 {
101     TEX_WRAP_CLAMP,
102     TEX_WRAP_CLAMP_TO_BORDER,
103     TEX_WRAP_REPEAT
104 };
105 
106 /**
107  * \enum TexMixOperation
108  * \brief Multitexture mixing operation
109  */
110 enum TexMixOperation
111 {
112     //! Default operation on default params (modulate on computed & texture)
113     TEX_MIX_OPER_DEFAULT,
114     //! = Arg1
115     TEX_MIX_OPER_REPLACE,
116     //! = Arg1 * Arg2
117     TEX_MIX_OPER_MODULATE,
118     //! = Arg1 + Arg2
119     TEX_MIX_OPER_ADD,
120     //! = Arg1 - Arg2
121     TEX_MIX_OPER_SUBTRACT
122 };
123 
124 /**
125  * \enum TexMixArgument
126  * \brief Multitexture mixing argument
127  */
128 enum TexMixArgument
129 {
130     //! Color from current texture
131     TEX_MIX_ARG_TEXTURE,
132     //! Color from texture unit 0
133     TEX_MIX_ARG_TEXTURE_0,
134     //! Color from texture unit 1
135     TEX_MIX_ARG_TEXTURE_1,
136     //! Color from texture unit 2
137     TEX_MIX_ARG_TEXTURE_2,
138     //! Color from texture unit 3
139     TEX_MIX_ARG_TEXTURE_3,
140     //! Color computed by previous texture unit (current in DirectX; previous in OpenGL)
141     TEX_MIX_ARG_COMPUTED_COLOR,
142     //! (Source) color of textured fragment (diffuse in DirectX; primary color in OpenGL)
143     TEX_MIX_ARG_SRC_COLOR,
144     //! Constant color (texture factor in DirectX; texture env color in OpenGL)
145     TEX_MIX_ARG_FACTOR
146 };
147 
148 /**
149  * \struct TextureCreateParams
150  * \brief Parameters for texture creation
151  *
152  * These params define how particular texture is created and later displayed.
153  * They must be specified at texture creation time and cannot be changed later.
154  */
155 struct TextureCreateParams
156 {
157     //! Whether to generate mipmaps
158     bool mipmap = false;
159     //! Format of source image data
160     TexImgFormat format = TEX_IMG_RGB;
161     //! General texture filtering mode
162     TexFilter filter = TEX_FILTER_NEAREST;
163     //! Pad the image to nearest power of 2 dimensions
164     bool padToNearestPowerOfTwo = false;
165 
166     //! Loads the default values
LoadDefaultTextureCreateParams167     void LoadDefault()
168     {
169         *this = TextureCreateParams();
170     }
171 };
172 
173 /**
174  * \struct TextureStageParams
175  * \brief Parameters for a texture unit
176  *
177  * These params define the behavior of texturing units (stages).
178  * They can be changed freely and are features of graphics engine, not any particular texture.
179  */
180 struct TextureStageParams
181 {
182     //! Mixing operation done on color values
183     TexMixOperation colorOperation = TEX_MIX_OPER_DEFAULT;
184     //! 1st argument of color operations
185     TexMixArgument colorArg1 = TEX_MIX_ARG_COMPUTED_COLOR;
186     //! 2nd argument of color operations
187     TexMixArgument colorArg2 = TEX_MIX_ARG_TEXTURE;
188     //! Mixing operation done on alpha values
189     TexMixOperation alphaOperation = TEX_MIX_OPER_DEFAULT;
190     //! 1st argument of alpha operations
191     TexMixArgument alphaArg1 = TEX_MIX_ARG_COMPUTED_COLOR;
192     //! 2nd argument of alpha operations
193     TexMixArgument alphaArg2 = TEX_MIX_ARG_TEXTURE;
194     //! Wrap mode for 1st tex coord
195     TexWrapMode    wrapS = TEX_WRAP_REPEAT;
196     //! Wrap mode for 2nd tex coord
197     TexWrapMode    wrapT = TEX_WRAP_REPEAT;
198     //! Constant color factor (for TEX_MIX_ARG_FACTOR)
199     Color          factor;
200 
201     //! Loads the default values
LoadDefaultTextureStageParams202     void LoadDefault()
203     {
204         *this = TextureStageParams();
205     }
206 };
207 
208 /**
209  * \struct Texture
210  * \brief Info about a texture
211  *
212  * Identifies (through id) a texture created in graphics engine.
213  * Also contains some additional data.
214  */
215 struct Texture
216 {
217     //! ID of the texture in graphics engine; 0 = invalid texture
218     unsigned int id = 0;
219     //! Size of texture
220     Math::IntPoint size;
221     //! Original size of texture (as loaded from image)
222     Math::IntPoint originalSize;
223     //! Whether the texture has alpha channel
224     bool alpha = false;
225 
226     //! Returns whether the texture is valid (ID != 0)
ValidTexture227     bool Valid() const
228     {
229         return id != 0;
230     }
231 
232     //! Sets the ID to invalid value (0)
SetInvalidTexture233     void SetInvalid()
234     {
235         id = 0;
236     }
237 
238     //! Comparator for use in texture maps and sets
239     bool operator<(const Texture &other) const
240     {
241         // Invalid textures are always "less than" every other texture
242 
243         if ( (! Valid()) && (! other.Valid()) )
244             return false;
245 
246         if (! Valid())
247             return true;
248 
249         if (! other.Valid())
250             return false;
251 
252         return id < other.id;
253     }
254 
255     //! Comparator
256     bool operator==(const Texture &other) const
257     {
258         if (Valid() != other.Valid())
259             return false;
260         if ( (! Valid()) && (! other.Valid()) )
261             return true;
262 
263         return id == other.id;
264     }
265 };
266 
267 
268 } // namespace Gfx
269 
270