Debian Codegraph @

similar/editor/texture.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Texture map assignment.
23 *
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <math.h>
30 #include <string.h>
31 #include "segment.h"
32 #include "seguvs.h"
33 #include "editor.h"
34 #include "editor/esegment.h"
35 #include "maths.h"
36 #include "kdefs.h"
37
38 #include "compiler-range_for.h"
39
40 static uvl compute_uv_side_center(const unique_segment &segp, sidenum_t sidenum);
41 static void rotate_uv_points_on_side(unique_segment &segp, sidenum_t sidenum, const std::array<fix, 4> &rotmat, const uvl &uvcenter);
42
43 // -----------------------------------------------------------
44 int TexFlipX()
45 {
46 const auto uvcenter = compute_uv_side_center(Cursegp, Curside);
47 std::array<fix, 4> rotmat;
48 // Create a rotation matrix
49 rotmat[0] = -0xffff;
50 rotmat[1] = 0;
51 rotmat[2] = 0;
52 rotmat[3] = 0xffff;
53
54 rotate_uv_points_on_side(Cursegp, Curside, rotmat, uvcenter);
55
56 Update_flags |= UF_WORLD_CHANGED;
57
58 return 1;
59 }
60
61 // -----------------------------------------------------------
62 int TexFlipY()
63 {
64 const auto uvcenter = compute_uv_side_center(Cursegp, Curside);
65 std::array<fix, 4> rotmat;
66 // Create a rotation matrix
67 rotmat[0] = 0xffff;
68 rotmat[1] = 0;
69 rotmat[2] = 0;
70 rotmat[3] = -0xffff;
71
72 rotate_uv_points_on_side(Cursegp, Curside, rotmat, uvcenter);
73
74 Update_flags |= UF_WORLD_CHANGED;
75
76 return 1;
77 }
78
79 // -----------------------------------------------------------
80 static int DoTexSlideLeft(int value)
81 {
82 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
83 auto &Vertices = LevelSharedVertexState.get_vertices();
84 uvl duvl03;
85 fix dist;
86 auto &vp = Side_to_verts[Curside];
87 auto &uvls = Cursegp->unique_segment::sides[Curside].uvls;
88
89 auto &vcvertptr = Vertices.vcptr;
90 dist = vm_vec_dist(vcvertptr(Cursegp->verts[vp[side_relative_vertnum::_3]]), vcvertptr(Cursegp->verts[vp[side_relative_vertnum::_0]]));
91 dist *= value;
92 if (dist < F1_0/(64*value))
93 dist = F1_0/(64*value);
94
95 auto &u3 = uvls[side_relative_vertnum::_3];
96 auto &u0 = uvls[side_relative_vertnum::_0];
97 duvl03.u = fixdiv(u3.u - u0.u, dist);
98 duvl03.v = fixdiv(u3.v - u0.v, dist);
99
100 range_for (auto &v, uvls)
101 {
102 v.u -= duvl03.u;
103 v.v -= duvl03.v;
104 }
105
106 Update_flags |= UF_WORLD_CHANGED;
107
108 return 1;
109 }
110
111 int TexSlideLeft()
112 {
113 return DoTexSlideLeft(3);
114 }
115
116 int TexSlideLeftBig()
117 {
118 return DoTexSlideLeft(1);
119 }
120
121 // -----------------------------------------------------------
122 static int DoTexSlideUp(int value)
123 {
124 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
125 auto &Vertices = LevelSharedVertexState.get_vertices();
126 uvl duvl03;
127 fix dist;
128 auto &vp = Side_to_verts[Curside];
129 auto &uvls = Cursegp->unique_segment::sides[Curside].uvls;
130
131 auto &vcvertptr = Vertices.vcptr;
132 dist = vm_vec_dist(vcvertptr(Cursegp->verts[vp[side_relative_vertnum::_1]]), vcvertptr(Cursegp->verts[vp[side_relative_vertnum::_0]]));
133 dist *= value;
134
135 if (dist < F1_0/(64*value))
136 dist = F1_0/(64*value);
137
138 auto &u1 = uvls[side_relative_vertnum::_1];
139 auto &u0 = uvls[side_relative_vertnum::_0];
140 duvl03.u = fixdiv(u1.u - u0.u, dist);
141 duvl03.v = fixdiv(u1.v - u0.v, dist);
142
143 range_for (auto &v, uvls)
144 {
145 v.u -= duvl03.u;
146 v.v -= duvl03.v;
147 }
148
149 Update_flags |= UF_WORLD_CHANGED;
150
151 return 1;
152 }
153
154 int TexSlideUp()
155 {
156 return DoTexSlideUp(3);
157 }
158
159 int TexSlideUpBig()
160 {
161 return DoTexSlideUp(1);
162 }
163
164
165 // -----------------------------------------------------------
166 static int DoTexSlideDown(int value)
167 {
168 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
169 auto &Vertices = LevelSharedVertexState.get_vertices();
170 uvl duvl03;
171 fix dist;
172 auto &vp = Side_to_verts[Curside];
173 auto &uvls = Cursegp->unique_segment::sides[Curside].uvls;
174
175 auto &vcvertptr = Vertices.vcptr;
176 dist = vm_vec_dist(vcvertptr(Cursegp->verts[vp[side_relative_vertnum::_1]]), vcvertptr(Cursegp->verts[vp[side_relative_vertnum::_0]]));
177 dist *= value;
178 if (dist < F1_0/(64*value))
179 dist = F1_0/(64*value);
180
181 auto &u1 = uvls[side_relative_vertnum::_1];
182 auto &u0 = uvls[side_relative_vertnum::_0];
183 duvl03.u = fixdiv(u1.u - u0.u, dist);
184 duvl03.v = fixdiv(u1.v - u0.v, dist);
185
186 range_for (auto &v, uvls)
187 {
188 v.u += duvl03.u;
189 v.v += duvl03.v;
190 }
191
192 Update_flags |= UF_WORLD_CHANGED;
193
194 return 1;
195 }
196
197 int TexSlideDown()
198 {
199 return DoTexSlideDown(3);
200 }
201
202 int TexSlideDownBig()
203 {
204 return DoTexSlideDown(1);
205 }
206
207 // -----------------------------------------------------------
208 // Compute the center of the side in u,v coordinates.
209 static uvl compute_uv_side_center(const unique_segment &segp, const sidenum_t sidenum)
210 {
211 uvl uvcenter{};
212 range_for (auto &v, segp.sides[sidenum].uvls)
213 {
214 uvcenter.u += v.u;
215 uvcenter.v += v.v;
216 }
217 uvcenter.u /= 4;
218 uvcenter.v /= 4;
219 return uvcenter;
220 }
221
222 // -----------------------------------------------------------
223 // rotate point *uv by matrix rotmat, return *uvrot
224 static uvl rotate_uv_point(const std::array<fix, 4> &rotmat, const uvl &uv, const uvl &uvcenter)
225 {
226 const auto centered_u = uv.u - uvcenter.u;
227 const auto centered_v = uv.v - uvcenter.v;
228 return {
229 fixmul(centered_u, rotmat[0]) + fixmul(centered_v, rotmat[1]) + uvcenter.u,
230 fixmul(centered_u, rotmat[2]) + fixmul(centered_v, rotmat[3]) + uvcenter.v,
231 0
232 };
233 }
234
235 // -----------------------------------------------------------
236 // Compute the center of the side in u,v coordinates.
237 static void rotate_uv_points_on_side(unique_segment &segp, const sidenum_t sidenum, const std::array<fix, 4> &rotmat, const uvl &uvcenter)
238 {
239 range_for (auto &v, segp.sides[sidenum].uvls)
240 {
241 v = rotate_uv_point(rotmat, v, uvcenter);
242 }
243 }
244
245 // -----------------------------------------------------------
246 // ang is in 0..ffff = 0..359.999 degrees
247 // rotmat is filled in with 4 fixes
248 static std::array<fix, 4> create_2d_rotation_matrix(const fixang ang)
249 {
250 const auto &&a = fix_sincos(ang);
251 const auto &sinang = a.sin;
252 const auto &cosang = a.cos;
253 return {{
254 cosang,
255 sinang,
256 -sinang,
257 cosang
258 }};
259 }
260
261
262 // -----------------------------------------------------------
263 static int DoTexRotateLeft(int value)
264 {
265 const auto uvcenter = compute_uv_side_center(Cursegp, Curside);
266 // Create a rotation matrix
267 const auto rotmat = create_2d_rotation_matrix(-F1_0/value);
268
269 rotate_uv_points_on_side(Cursegp, Curside, rotmat, uvcenter);
270
271 Update_flags |= UF_WORLD_CHANGED;
272
273 return 1;
274 }
275
276 int TexRotateLeft()
277 {
278 return DoTexRotateLeft(192);
279 }
280
281 int TexRotateLeftBig()
282 {
283 return DoTexRotateLeft(64);
284 }
285
286
287 // -----------------------------------------------------------
288 static int DoTexSlideRight(int value)
289 {
290 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
291 auto &Vertices = LevelSharedVertexState.get_vertices();
292 uvl duvl03;
293 fix dist;
294 auto &vp = Side_to_verts[Curside];
295 auto &uvls = Cursegp->unique_segment::sides[Curside].uvls;
296
297 auto &vcvertptr = Vertices.vcptr;
298 dist = vm_vec_dist(vcvertptr(Cursegp->verts[vp[side_relative_vertnum::_3]]), vcvertptr(Cursegp->verts[vp[side_relative_vertnum::_0]]));
299 dist *= value;
300 if (dist < F1_0/(64*value))
301 dist = F1_0/(64*value);
302
303 auto &u3 = uvls[side_relative_vertnum::_3];
304 auto &u0 = uvls[side_relative_vertnum::_0];
305 duvl03.u = fixdiv(u3.u - u0.u, dist);
306 duvl03.v = fixdiv(u3.v - u0.v, dist);
307
308 range_for (auto &v, uvls)
309 {
310 v.u += duvl03.u;
311 v.v += duvl03.v;
312 }
313
314 Update_flags |= UF_WORLD_CHANGED;
315
316 return 1;
317 }
318
319 int TexSlideRight()
320 {
321 return DoTexSlideRight(3);
322 }
323
324 int TexSlideRightBig()
325 {
326 return DoTexSlideRight(1);
327 }
328
329 // -----------------------------------------------------------
330 static int DoTexRotateRight(int value)
331 {
332 const auto uvcenter = compute_uv_side_center(Cursegp, Curside);
333
334 // Create a rotation matrix
335 const auto rotmat = create_2d_rotation_matrix(F1_0/value);
336
337 rotate_uv_points_on_side(Cursegp, Curside, rotmat, uvcenter);
338
339 Update_flags |= UF_WORLD_CHANGED;
340
341 return 1;
342 }
343
344 int TexRotateRight()
345 {
346 return DoTexRotateRight(192);
347 }
348
349 int TexRotateRightBig()
350 {
351 return DoTexRotateRight(64);
352 }
353
354 // -----------------------------------------------------------
355 int TexSelectActiveEdge()
356 {
357 return 1;
358 }
359
360 // -----------------------------------------------------------
361 int TexRotate90Degrees()
362 {
363 const auto uvcenter = compute_uv_side_center(Cursegp, Curside);
364
365 // Create a rotation matrix
366 const auto rotmat = create_2d_rotation_matrix(F1_0/4);
367
368 rotate_uv_points_on_side(Cursegp, Curside, rotmat, uvcenter);
369
370 Update_flags |= UF_WORLD_CHANGED;
371
372 return 1;
373 }
374
375 // -----------------------------------------------------------
376 int TexSetDefault()
377 {
378 Num_tilings = 1;
379
380 Stretch_scale_x = F1_0;
381 Stretch_scale_y = F1_0;
382
383 assign_default_uvs_to_side(Cursegp,Curside);
384
385 Update_flags |= UF_GAME_VIEW_CHANGED;
386 return 1;
387 }
388
389 // -----------------------------------------------------------
390 int TexIncreaseTiling()
391 {
392
393 Num_tilings++;
394 assign_default_uvs_to_side(Cursegp, Curside);
395 Update_flags |= UF_GAME_VIEW_CHANGED;
396
397 return 1;
398 }
399
400 // -----------------------------------------------------------
401 int TexDecreaseTiling()
402 {
403
404 if (--Num_tilings < 1)
405 Num_tilings = 1;
406
407 assign_default_uvs_to_side(Cursegp, Curside);
408 Update_flags |= UF_GAME_VIEW_CHANGED;
409
410 return 1;
411 }
412
413
414 // direction = -1 or 1 depending on direction
415 static int TexStretchCommon(int direction)
416 {
417 const auto sptr = (Curedge == side_relative_vertnum::_0 || Curedge == side_relative_vertnum::_2)
418 ? &Stretch_scale_x
419 : &Stretch_scale_y;
420
421 *sptr += direction*F1_0/64;
422
423 if (*sptr < F1_0/16)
424 *sptr = F1_0/16;
425
426 if (*sptr > 2*F1_0)
427 *sptr = 2*F1_0;
428
429 stretch_uvs_from_curedge(Cursegp, Curside);
430
431 editor_status_fmt("Stretch scale = %7.4f, use Set Default to return to 1.0", f2fl(*sptr));
432
433 Update_flags |= UF_GAME_VIEW_CHANGED;
434 return 1;
435
436 }
437
438 int TexStretchDown(void)
439 {
440 return TexStretchCommon(-1);
441
442 }
443
444 int TexStretchUp(void)
445 {
446 return TexStretchCommon(1);
447
448 }
449