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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
/* Project MAGIC
John DiCamillo Software Consulting
Copyright © 1994-1997. All Rights Reserved.
SUBSYSTEM: Magic.exe Application
FILE: Editor.cpp
AUTHOR: John DiCamillo
OVERVIEW
========
Source file for implementation of Selector
*/
#include "stdafx.h"
#include "Editor.h"
#include "MagicDoc.h"
#include "ModelView.h"
#include "Selection.h"
// +----------------------------------------------------------------------+
static float project_u(Vec3& v, int style)
{
switch (style) {
case 0: return v.x; // PLAN
case 1: return v.x; // FRONT
case 2: return v.z; // SIDE
}
return v.x;
}
static float project_v(Vec3& v, int style)
{
switch (style) {
case 0: return -v.y; // PLAN
case 1: return -v.z; // FRONT
case 2: return -v.y; // SIDE
}
return -v.y;
}
static float project_u_cylindrical(Vec3& v, int axis)
{
float t = 0.0f;
switch (axis) {
// PLAN
case 0: if (v.x == 0)
return 0.0f;
t = v.y/v.x;
return (float) atan(t);
// FRONT
case 1: if (v.x == 0)
return 0.0f;
t = v.z/v.x;
return (float) atan(t);
// SIDE
case 2: return (float) atan2(v.z, v.y); // SIDE
}
return project_u(v, axis);
}
static float project_v_cylindrical(Vec3& v, int axis)
{
switch (axis) {
case 0: return v.z; // PLAN
case 1: return v.y; // FRONT
case 2: return v.x; // SIDE
}
return project_v(v, axis);
}
void
Editor::ApplyMaterial(Material* material, List<Poly>& polys,
int mapping, int axis, float scale_u, float scale_v,
int flip, int mirror, int rotate)
{
// save state:
EditCommand* command = new EditCommand("ApplyMaterial", document);
document->Exec(command);
// do the job:
if (mapping == MAP_CYLINDRICAL) {
ApplyMaterialCylindrical(material, polys, axis, scale_u, scale_v, flip, mirror, rotate);
return;
}
if (mapping == MAP_SPHERICAL) {
ApplyMaterialSpherical(material, polys, axis, scale_u, scale_v, flip, mirror, rotate);
return;
}
VertexSet* vset = polys.first()->vertex_set;
Vec3* loc = vset->loc;
float min_u = 100000.0f, max_u = -100000.0f;
float min_v = 100000.0f, max_v = -100000.0f;
ListIter<Poly> iter = polys;
// compute range and scale:
if (mapping == MAP_PLANAR) {
while (++iter) {
Poly* poly = iter.value();
for (int i = 0; i < poly->nverts; i++) {
int v = poly->verts[i];
float u0 = project_u(loc[v], axis);
float v0 = project_v(loc[v], axis);
if (u0 < min_u) min_u = u0;
if (v0 < min_v) min_v = v0;
if (u0 > max_u) max_u = u0;
if (v0 > max_v) max_v = v0;
}
}
}
float base_u = 0.0f;
float base_v = 0.0f;
if (max_u != min_u) base_u = 1.0f / (max_u - min_u);
if (max_v != min_v) base_v = 1.0f / (max_v - min_v);
iter.reset();
// assign texture id and coordinates:
while (++iter) {
Poly* poly = iter.value();
poly->material = material;
if (mapping == MAP_NONE)
continue;
for (int i = 0; i < poly->nverts; i++) {
int v = poly->verts[i];
// planar projection
if (mapping == MAP_PLANAR) {
if (!rotate) {
if (mirror)
vset->tu[v] = (1.0f - base_u * (project_u(loc[v], axis) - min_u)) * scale_u;
else
vset->tu[v] = (project_u(loc[v], axis) - min_u) * scale_u * base_u;
if (flip)
vset->tv[v] = (1.0f - base_v * (project_v(loc[v], axis) - min_v)) * scale_v;
else
vset->tv[v] = (project_v(loc[v], axis) - min_v) * scale_v * base_v;
}
else {
if (!mirror)
vset->tv[v] = (1.0f - base_u * (project_u(loc[v], axis) - min_u)) * scale_u;
else
vset->tv[v] = (project_u(loc[v], axis) - min_u) * scale_u * base_u;
if (flip)
vset->tu[v] = (1.0f - base_v * (project_v(loc[v], axis) - min_v)) * scale_v;
else
vset->tu[v] = (project_v(loc[v], axis) - min_v) * scale_v * base_v;
}
}
// stretch to fit
else if (mapping == MAP_STRETCH) {
if (scale_u < 0.001) scale_u = 1;
if (scale_v < 0.001) scale_v = 1;
if (!rotate) {
if (mirror)
vset->tu[v] = scale_u * (float) (i < 1 || i > 2);
else
vset->tu[v] = scale_u * (float) (i > 0 && i < 3);
if (flip)
vset->tv[v] = scale_v * (float) (i <= 1);
else
vset->tv[v] = scale_v * (float) (i > 1);
}
else {
if (!mirror)
vset->tv[v] = scale_v * (float) (i < 1 || i > 2);
else
vset->tv[v] = scale_v * (float) (i > 0 && i < 3);
if (flip)
vset->tu[v] = scale_u * (float) (i <= 1);
else
vset->tu[v] = scale_u * (float) (i > 1);
}
}
}
}
Resegment();
}
void
Editor::ApplyMaterialCylindrical(Material* material, List<Poly>& polys,
int axis, float scale_u, float scale_v,
int flip, int mirror, int rotate)
{
VertexSet* vset = polys.first()->vertex_set;
Vec3* loc = vset->loc;
float min_u = 100000.0f, max_u = -100000.0f;
float min_v = 100000.0f, max_v = -100000.0f;
ListIter<Poly> iter = polys;
// compute range and scale:
while (++iter) {
Poly* poly = iter.value();
for (int i = 0; i < poly->nverts; i++) {
int v = poly->verts[i];
float u0 = project_u_cylindrical(loc[v], axis);
float v0 = project_v_cylindrical(loc[v], axis);
if (u0 < min_u) min_u = u0;
if (v0 < min_v) min_v = v0;
if (u0 > max_u) max_u = u0;
if (v0 > max_v) max_v = v0;
}
}
float base_u = 0.0f;
float base_v = 0.0f;
if (max_u != min_u) base_u = 1.0f / (max_u - min_u);
if (max_v != min_v) base_v = 1.0f / (max_v - min_v);
iter.reset();
// assign texture id and coordinates:
while (++iter) {
Poly* poly = iter.value();
poly->material = material;
for (int i = 0; i < poly->nverts; i++) {
int v = poly->verts[i];
float u0 = project_u_cylindrical(loc[v], axis);
float v0 = project_v_cylindrical(loc[v], axis);
if (!rotate) {
if (mirror)
vset->tu[v] = (1.0f - base_u * (u0 - min_u)) * scale_u;
else
vset->tu[v] = (u0 - min_u) * scale_u * base_u;
if (flip)
vset->tv[v] = (1.0f - base_v * (v0 - min_v)) * scale_v;
else
vset->tv[v] = (v0 - min_v) * scale_v * base_v;
}
else {
if (!mirror)
vset->tv[v] = (1.0f - base_u * (u0 - min_u)) * scale_u;
else
vset->tv[v] = (u0 - min_u) * scale_u * base_u;
if (flip)
vset->tu[v] = (1.0f - base_v * (v0 - min_v)) * scale_v;
else
vset->tu[v] = (v0 - min_v) * scale_v * base_v;
}
}
}
Resegment();
}
void
Editor::ApplyMaterialSpherical(Material* material, List<Poly>& polys,
int axis, float scale_u, float scale_v,
int flip, int mirror, int rotate)
{
}
// +----------------------------------------------------------------------+
static int mcomp(const void* a, const void* b)
{
Poly* pa = (Poly*) a;
Poly* pb = (Poly*) b;
if (pa->sortval == pb->sortval)
return 0;
if (pa->sortval < pb->sortval)
return -1;
return 1;
}
void
Editor::Resegment()
{
if (model) {
ListIter<Surface> iter = model->GetSurfaces();
while (++iter) {
Surface* surface = iter.value();
Poly* polys = surface->GetPolys();
int npolys = surface->NumPolys();
for (int n = 0; n < npolys; n++) {
Poly* p = polys + n;
Material* m = p->material;
int sortval = model->GetMaterials().index(m) + 1;
if (p->sortval != sortval)
p->sortval = sortval;
}
// destroy the old segments and video data:
VideoPrivateData* video_data = surface->GetVideoPrivateData();
surface->SetVideoPrivateData(0);
surface->GetSegments().destroy();
delete video_data;
// sort the polys by material index:
qsort((void*) polys, npolys, sizeof(Poly), mcomp);
// create new cohesive segments:
Segment* segment = 0;
for (n = 0; n < npolys; n++) {
if (segment && segment->material == polys[n].material) {
segment->npolys++;
}
else {
segment = 0;
}
if (!segment) {
segment = new Segment;
segment->npolys = 1;
segment->polys = polys + n;
segment->material = segment->polys->material;
surface->GetSegments().append(segment);
}
}
}
}
}
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
EditCommand::EditCommand(const char* n, MagicDoc* d)
: Command(n, d), model1(0), model2(0)
{
}
EditCommand::~EditCommand()
{
delete model1;
delete model2;
}
// +----------------------------------------------------------------------+
void
EditCommand::Do()
{
if (document) {
Solid* solid = document->GetSolid();
// first application:
if (!model2) {
if (!model1)
model1 = new Model(*solid->GetModel());
}
// re-do:
else {
solid->GetModel()->operator=(*model2);
}
}
}
// +----------------------------------------------------------------------+
void
EditCommand::Undo()
{
if (document && model1) {
Solid* solid = document->GetSolid();
// save current state for later re-do:
if (!model2)
model2 = new Model(*solid->GetModel());
solid->GetModel()->operator=(*model1);
}
}
|