Line data Source code
1 : /**
2 : * @file slot.h
3 : * @brief Texture slot objects
4 : *
5 : * The texture slot objects expose the data structures used to organize geometry
6 : * texture information and encode it in a level. A vector containing a series of
7 : * slot objects is associated with every cube object, allowing every cube face
8 : * to reference a slot "palette" containing many texture + shader combinations
9 : * without requiring much data stored on the client.
10 : *
11 : * Furthermore, a `VSlot` object is defined, which defines a virtual texture slot.
12 : * These are derived from a "real" texture slot, and can have one or more shader
13 : * modifiers applied to it: color, scale, rotation etc.
14 : */
15 :
16 : #ifndef SLOT_H_
17 : #define SLOT_H_
18 :
19 : /**
20 : * @brief Bitmask variables for a VSlot.changed field.
21 : *
22 : * These variables determine what about a virtual texture slot has been changed.
23 : * Use them by bit shifting by the value of the enum, e.g.
24 : * ```
25 : * slot.changed = 1 << VSlot_ShParam;
26 : * ```
27 : */
28 : enum
29 : {
30 : VSlot_ShParam = 0,
31 : VSlot_Scale,
32 : VSlot_Rotation,
33 : VSlot_Offset,
34 : VSlot_Scroll,
35 : VSlot_Layer,
36 : VSlot_Alpha,
37 : VSlot_Color,
38 : VSlot_Reserved, // used by RE
39 : VSlot_Refract,
40 : VSlot_Detail,
41 : VSlot_Angle,
42 : VSlot_Num
43 : };
44 :
45 : struct SlotShaderParam
46 : {
47 : enum
48 : {
49 : REUSE = 1<<0
50 : };
51 :
52 : const char *name;
53 : size_t loc;
54 : int flags;
55 : float val[4];
56 : };
57 :
58 : struct Texture;
59 : class Shader;
60 : struct VSlot; //both slot and vslot depend on each other
61 :
62 : /**
63 : * @brief A representation of a texture inside the engine.
64 : *
65 : * A texture slot represents the texture and shader information for a single
66 : * application onto an octree face. This includes a diffuse texture, as well as
67 : * other shader-defined textures such as normal, parallax, and specular maps if
68 : * they are part of the shader configuration of the slot object.
69 : */
70 : struct Slot
71 : {
72 : enum
73 : {
74 : SlotType_Octa,
75 : SlotType_Material,
76 : SlotType_Decal
77 : };
78 :
79 : struct Tex
80 : {
81 : int type;
82 : Texture *t;
83 : string name;
84 : int combined;
85 :
86 1 : Tex() : t(nullptr), combined(-1) {}
87 : };
88 :
89 : int index, smooth;
90 : std::vector<Tex> sts;
91 : Shader *shader;
92 : std::vector<SlotShaderParam> params;
93 : VSlot *variants;
94 : bool loaded;
95 : uint texmask;
96 : char *grass;
97 : Texture *grasstex, *thumbnail;
98 :
99 45 : Slot(int index = -1) : index(index), variants(nullptr), grass(nullptr) { reset(); }
100 11 : virtual ~Slot() {}
101 :
102 1 : virtual int type() const
103 : {
104 1 : return SlotType_Octa;
105 : }
106 :
107 : virtual const char *name() const;
108 1 : virtual const char *texturedir() const
109 : {
110 1 : return "media/texture";
111 : }
112 :
113 : virtual VSlot &emptyvslot();
114 :
115 : virtual int cancombine(int type) const;
116 3 : virtual bool shouldpremul(int) const
117 : {
118 3 : return false;
119 : }
120 :
121 : /**
122 : * @brief Attempts to find a tex with index type.
123 : *
124 : * Searches for a tex with the type passed, searching only for the part of the
125 : * sts vector after `last`. If none is found, returns -1.
126 : *
127 : * @param type the type to seach for
128 : * @param last the last index to not look at
129 : *
130 : * @return the index where the type was found
131 : */
132 : int findtextype(int type, int last = -1) const;
133 :
134 : void load(int index, Slot::Tex &t);
135 : void load();
136 :
137 : /**
138 : * @brief Returns a new texture that is a thumbnail of the slot's texture.
139 : *
140 : * Creates a new texture on the heap representing a scaled-down version of
141 : * the slot's diffuse map.
142 : *
143 : * @return a pointer to the generated Texture containing the thumbnail
144 : */
145 : Texture *loadthumbnail();
146 :
147 46 : void reset()
148 : {
149 46 : smooth = -1;
150 46 : sts.clear();
151 46 : shader = nullptr;
152 46 : params.clear();
153 46 : loaded = false;
154 46 : texmask = 0;
155 46 : delete[] grass;
156 46 : grass = nullptr;
157 46 : grasstex = nullptr;
158 46 : thumbnail = nullptr;
159 46 : }
160 :
161 2 : void cleanup()
162 : {
163 2 : loaded = false;
164 2 : grasstex = nullptr;
165 2 : thumbnail = nullptr;
166 3 : for(uint i = 0; i < sts.size(); i++)
167 : {
168 1 : Tex &t = sts[i];
169 1 : t.t = nullptr;
170 1 : t.combined = -1;
171 : }
172 2 : }
173 : };
174 :
175 : /**
176 : * @brief A virtual texture slot.
177 : *
178 : * A virtual texture slot ("VSlot") is a derivative of a standard texture slot;
179 : * it contains modification information (such as rotation, translation, tinting,
180 : * and modifications to other shader settings) but not the fundamental attributes
181 : * of a texture.
182 : *
183 : * Each VSlot must point to a normal slot to have any meaning; it is from this slot
184 : * object where the VSlot gets the attributes of its texture files and shader
185 : * definition information.
186 : */
187 : struct VSlot
188 : {
189 : Slot *slot;
190 : VSlot *next;
191 : int index, changed;
192 : std::vector<SlotShaderParam> params;
193 : bool linked;
194 : float scale;
195 : int rotation;
196 : vec angle;
197 : ivec2 offset;
198 : vec2 scroll;
199 : int layer;
200 : float alphafront, alphaback;
201 : vec colorscale;
202 : vec glowcolor;
203 : float refractscale;
204 : vec refractcolor;
205 :
206 41 : VSlot(Slot *slot = nullptr, int index = -1) : slot(slot), next(nullptr), index(index), changed(0)
207 : {
208 41 : reset();
209 41 : if(slot)
210 : {
211 40 : addvariant(slot);
212 : }
213 41 : }
214 :
215 : void addvariant(Slot *slot);
216 :
217 42 : void reset()
218 : {
219 42 : params.clear();
220 42 : linked = false;
221 42 : scale = 1;
222 42 : rotation = 0;
223 42 : angle = vec(0, sinf(0), cosf(0));
224 42 : offset = ivec2(0, 0);
225 42 : scroll = vec2(0, 0);
226 42 : layer = 0;
227 42 : alphafront = 0.5f;
228 42 : alphaback = 0;
229 42 : colorscale = vec(1, 1, 1);
230 42 : glowcolor = vec(1, 1, 1);
231 42 : refractscale = 0;
232 42 : refractcolor = vec(1, 1, 1);
233 42 : }
234 :
235 2 : void cleanup()
236 : {
237 2 : linked = false;
238 2 : }
239 :
240 : bool isdynamic() const;
241 : };
242 :
243 : /**
244 : * @param A derivative form of a slot intended for decal objects.
245 : */
246 : struct DecalSlot final : Slot, VSlot
247 : {
248 : float depth, fade;
249 :
250 7 : DecalSlot(int index = -1) : Slot(index), VSlot(this), depth(1), fade(0.5f) {}
251 :
252 1 : int type() const override final
253 : {
254 1 : return SlotType_Decal;
255 : }
256 :
257 : const char *name() const override final;
258 1 : const char *texturedir() const override final
259 : {
260 1 : return "media/decal";
261 : }
262 :
263 1 : VSlot &emptyvslot() override final
264 : {
265 1 : return *this;
266 : }
267 :
268 : int cancombine(int type) const override final;
269 : bool shouldpremul(int type) const override final;
270 :
271 1 : void reset()
272 : {
273 1 : Slot::reset();
274 1 : VSlot::reset();
275 1 : depth = 1;
276 1 : fade = 0.5f;
277 1 : }
278 :
279 1 : void cleanup()
280 : {
281 1 : Slot::cleanup();
282 1 : VSlot::cleanup();
283 1 : }
284 : };
285 :
286 : extern std::vector<Slot *> slots;
287 : extern std::vector<VSlot *> vslots;
288 : extern std::vector<int *> editingvslots;
289 :
290 : extern const char *getshaderparamname(const char *name, bool insert = true);
291 : extern void setldrnotexture();
292 :
293 : extern VSlot *findvslot(const Slot &slot, const VSlot &src, const VSlot &delta);
294 : extern VSlot *editvslot(const VSlot &src, const VSlot &delta);
295 : extern Slot &lookupslot(int slot, bool load = true);
296 : extern VSlot &lookupvslot(int slot, bool load = true);
297 : extern bool unpackvslot(ucharbuf &buf, VSlot &dst, bool delta);
298 : extern DecalSlot &lookupdecalslot(int slot, bool load = true);
299 :
300 : #endif /* SLOT_H_ */
|