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 48 : Slot(int index = -1) : index(index), variants(nullptr), grass(nullptr) { reset(); }
100 14 : 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 50 : void reset()
148 : {
149 50 : smooth = -1;
150 50 : sts.clear();
151 50 : shader = nullptr;
152 50 : params.clear();
153 50 : loaded = false;
154 50 : texmask = 0;
155 50 : delete[] grass;
156 50 : grass = nullptr;
157 50 : grasstex = nullptr;
158 50 : thumbnail = nullptr;
159 50 : }
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;
192 : int changed; /// bitmask containing zero or more bits defined above in the `VSlot` enum values.
193 : std::vector<SlotShaderParam> params;
194 : bool linked;
195 : float scale;
196 : int rotation;
197 : vec angle;
198 : ivec2 offset;
199 : vec2 scroll;
200 : int layer;
201 : float alphafront, alphaback;
202 : vec colorscale;
203 : vec glowcolor;
204 : float refractscale;
205 : vec refractcolor;
206 :
207 43 : VSlot(Slot *slot = nullptr, int index = -1) : slot(slot), next(nullptr), index(index), changed(0)
208 : {
209 43 : reset();
210 43 : if(slot)
211 : {
212 40 : addvariant(slot);
213 : }
214 43 : }
215 :
216 : void addvariant(Slot *slot);
217 :
218 45 : void reset()
219 : {
220 45 : params.clear();
221 45 : linked = false;
222 45 : scale = 1;
223 45 : rotation = 0;
224 45 : angle = vec(0, sinf(0), cosf(0));
225 45 : offset = ivec2(0, 0);
226 45 : scroll = vec2(0, 0);
227 45 : layer = 0;
228 45 : alphafront = 0.5f;
229 45 : alphaback = 0;
230 45 : colorscale = vec(1, 1, 1);
231 45 : glowcolor = vec(1, 1, 1);
232 45 : refractscale = 0;
233 45 : refractcolor = vec(1, 1, 1);
234 45 : }
235 :
236 2 : void cleanup()
237 : {
238 2 : linked = false;
239 2 : }
240 :
241 : bool isdynamic() const;
242 : };
243 :
244 : /**
245 : * @param A derivative form of a slot intended for decal objects.
246 : */
247 : struct DecalSlot final : Slot, VSlot
248 : {
249 : float depth, fade;
250 :
251 7 : DecalSlot(int index = -1) : Slot(index), VSlot(this), depth(1), fade(0.5f) {}
252 :
253 1 : int type() const override final
254 : {
255 1 : return SlotType_Decal;
256 : }
257 :
258 : const char *name() const override final;
259 1 : const char *texturedir() const override final
260 : {
261 1 : return "media/decal";
262 : }
263 :
264 1 : VSlot &emptyvslot() override final
265 : {
266 1 : return *this;
267 : }
268 :
269 : int cancombine(int type) const override final;
270 : bool shouldpremul(int type) const override final;
271 :
272 1 : void reset()
273 : {
274 1 : Slot::reset();
275 1 : VSlot::reset();
276 1 : depth = 1;
277 1 : fade = 0.5f;
278 1 : }
279 :
280 1 : void cleanup()
281 : {
282 1 : Slot::cleanup();
283 1 : VSlot::cleanup();
284 1 : }
285 : };
286 :
287 : extern std::vector<Slot *> slots;
288 : extern std::vector<VSlot *> vslots;
289 : extern std::vector<int *> editingvslots;
290 :
291 : extern const char *getshaderparamname(const char *name, bool insert = true);
292 : extern void setldrnotexture();
293 :
294 : extern VSlot *findvslot(const Slot &slot, const VSlot &src, const VSlot &delta);
295 : extern VSlot *editvslot(const VSlot &src, const VSlot &delta);
296 : extern Slot &lookupslot(int slot, bool load = true);
297 : extern VSlot &lookupvslot(int slot, bool load = true);
298 : extern bool unpackvslot(ucharbuf &buf, VSlot &dst, bool delta);
299 : extern DecalSlot &lookupdecalslot(int slot, bool load = true);
300 :
301 : #endif /* SLOT_H_ */
|