summaryrefslogtreecommitdiffstats
path: root/src/graphics/common/texture.h
blob: 83986ea93a178d6db8f1fb72a894ed3a9b2173d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// * This file is part of the COLOBOT source code
// * Copyright (C) 2012, Polish Portal of Colobot (PPC)
// *
// * This program is free software: you can redistribute it and/or modify
// * it under the terms of the GNU General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see  http://www.gnu.org/licenses/.

// texture.h

#pragma once

namespace Gfx {

/**
  \enum TexImgFormat
  \brief Format of image data */
enum TexImgFormat
{
    TEX_IMG_RGB,
    TEX_IMG_BGR,
    TEX_IMG_RGBA,
    TEX_IMG_BGRA
};

/**
  \enum TexMinFilter
  \brief Minification texture filter

  Corresponds to OpenGL modes but should translate to DirectX too. */
enum TexMinFilter
{
    TEX_MIN_FILTER_NEAREST,
    TEX_MIN_FILTER_LINEAR,
    TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST,
    TEX_MIN_FILTER_LINEAR_MIPMAP_NEAREST,
    TEX_MIN_FILTER_NEAREST_MIPMAP_LINEAR,
    TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR
};

/**
  \enum TexMagFilter
  \brief Magnification texture filter */
enum TexMagFilter
{
    TEX_MAG_FILTER_NEAREST,
    TEX_MAG_FILTER_LINEAR
};

/**
  \enum TexWrapMode
  \brief Wrapping mode for texture coords */
enum TexWrapMode
{
    TEX_WRAP_CLAMP,
    TEX_WRAP_REPEAT
};

/**
  \enum TexMixOperation
  \brief Multitexture mixing operation
 */
enum TexMixOperation
{
    TEX_MIX_OPER_MODULATE,
    TEX_MIX_OPER_ADD
};

/**
  \enum TexMixArgument
  \brief Multitexture mixing argument
  */
enum TexMixArgument
{
    TEX_MIX_ARG_CURRENT,
    TEX_MIX_ARG_TEXTURE,
    TEX_MIX_ARG_DIFFUSE,
    TEX_MIX_ARG_FACTOR
};

/**
  \struct TextureCreateParams
  \brief Parameters for texture creation
  */
struct TextureCreateParams
{
    //! Whether to generate mipmaps
    bool mipmap;
    //! Format of source image data
    Gfx::TexImgFormat format;
    //! Minification filter
    Gfx::TexMinFilter minFilter;
    //! Magnification filter
    Gfx::TexMagFilter magFilter;
    //! Wrap S coord mode
    Gfx::TexWrapMode wrapS;
    //! Wrap T coord mode
    Gfx::TexWrapMode wrapT;

    //! Constructor; calls LoadDefault()
    TextureCreateParams()
        { LoadDefault(); }

    //! Loads the default values
    inline void LoadDefault()
    {
        format = Gfx::TEX_IMG_RGB;
        mipmap = false;

        minFilter = Gfx::TEX_MIN_FILTER_NEAREST;
        magFilter = Gfx::TEX_MAG_FILTER_NEAREST;

        wrapS = Gfx::TEX_WRAP_REPEAT;
        wrapT = Gfx::TEX_WRAP_REPEAT;
    }
};

/**
  \struct TextureParams
  \brief Parameters for texture creation
 */
struct TextureParams
{
    //! Mixing operation done on color values
    Gfx::TexMixOperation colorOperation;
    //! 1st argument of color operations
    Gfx::TexMixArgument colorArg1;
    //! 2nd argument of color operations
    Gfx::TexMixArgument colorArg2;
    //! Mixing operation done on alpha values
    Gfx::TexMixOperation alphaOperation;
    //! 1st argument of alpha operations
    Gfx::TexMixArgument alphaArg1;
    //! 2nd argument of alpha operations
    Gfx::TexMixArgument alphaArg2;

    //! Constructor; calls LoadDefault()
    TextureParams()
        { LoadDefault(); }

    //! Loads the default values
    inline void LoadDefault()
    {
        colorOperation = Gfx::TEX_MIX_OPER_MODULATE;
        colorArg1 = Gfx::TEX_MIX_ARG_CURRENT;
        colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE;

        alphaOperation = Gfx::TEX_MIX_OPER_MODULATE;
        alphaArg1 = Gfx::TEX_MIX_ARG_CURRENT;
        alphaArg2 = Gfx::TEX_MIX_ARG_TEXTURE;
    }
};

/** \struct Texture*/
struct Texture
{
    //! Whether the texture was loaded
    bool valid;
    //! Id of the texture in graphics engine
    unsigned int id;
    //! Width of texture
    int width;
    //! Height of texture
    int height;
    //! Whether the texture has alpha channel
    bool alpha;

    Texture()
    {
        valid = false;
        id = 0;
        width = height = 0;
        alpha = false;
    }

    //! Comparator for use in texture maps and sets
    inline bool operator<(const Gfx::Texture &other) const
    {
        // Invalid textures are always "less than" every other texture

        if ( (!valid) && (!other.valid) )
            return false;

        if (!valid)
            return true;

        if (!other.valid)
            return false;

        return id < other.id;
    }

    //! Comparator
    inline bool operator==(const Gfx::Texture &other) const
    {
        if (valid != other.valid)
            return false;
        if ( (!valid) && (!other.valid) )
            return true;

        return id == other.id;
    }
};

}; // namespace Gfx