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
|
/* Starshatter OpenSource Distribution
Copyright (c) 1997-2004, Destroyer Studios LLC.
All Rights Reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name "Destroyer Studios" nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
SUBSYSTEM: Magic.exe
FILE: Grid.cpp
AUTHOR: John DiCamillo
OVERVIEW
========
Implementation of the Grid class
*/
#include "stdafx.h"
#include "Magic.h"
#include "MagicDoc.h"
#include "Grid.h"
#include "ActiveWindow.h"
#include "Color.h"
#include "Polygon.h"
#include "Scene.h"
#include "Screen.h"
#include "Solid.h"
#include "Video.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
const int MAJOR_COUNT = 64;
// +--------------------------------------------------------------------+
Grid::Grid()
: x_major(0), y_major(0), x_minor(0), y_minor(0),
bmp_plan(0), bmp_front(0), bmp_side(0)
{
strcpy(name, "Grid");
plane = GRID_XZ; // plan
show = true;
show_ref = true;
snap = true;
minor = true;
x_size = 4;
y_size = 4;
x_skip = 4;
y_skip = 4;
x_major = new Vec3[2*MAJOR_COUNT];
y_major = new Vec3[2*MAJOR_COUNT];
}
Grid::~Grid()
{
delete [] x_major;
delete [] y_major;
delete [] x_minor;
delete [] y_minor;
}
// +--------------------------------------------------------------------+
const char*
Grid::GetReferencePlan() const
{
if (bmp_plan)
return bmp_plan->GetFilename();
return "";
}
void
Grid::SetReferencePlan(const char* fname)
{
bmp_plan = 0;
if (fname && *fname)
LoadTexture(fname, bmp_plan);
}
// +--------------------------------------------------------------------+
const char*
Grid::GetReferenceFront() const
{
if (bmp_front)
return bmp_front->GetFilename();
return "";
}
void
Grid::SetReferenceFront(const char* fname)
{
bmp_front = 0;
if (fname && *fname)
LoadTexture(fname, bmp_front);
}
// +--------------------------------------------------------------------+
const char*
Grid::GetReferenceSide() const
{
if (bmp_side)
return bmp_side->GetFilename();
return "";
}
void
Grid::SetReferenceSide(const char* fname)
{
bmp_side = 0;
if (fname && *fname)
LoadTexture(fname, bmp_side);
}
// +--------------------------------------------------------------------+
void
Grid::Render(Video* video, DWORD flags)
{
Vec3 origin[4];
for (int i = 0; i < 4; i++)
origin[i] = loc;
const float EXTENT = (float) MAJOR_COUNT * 64.0f;
switch (plane) {
case GRID_XY: // FRONT
origin[0].x += -EXTENT;
origin[1].x += EXTENT;
origin[2].y += -EXTENT;
origin[3].y += EXTENT;
for (int i = 0; i < 2*MAJOR_COUNT; i += 2) {
float x = (float) (i-MAJOR_COUNT) * 64.0f;
float y = (float) (i-MAJOR_COUNT) * 64.0f;
x_major[i ] = loc + Vec3(x, -EXTENT, 0.0f);
x_major[i+1] = loc + Vec3(x, EXTENT, 0.0f);
y_major[i ] = loc + Vec3(-EXTENT, y, 0.0f);
y_major[i+1] = loc + Vec3( EXTENT, y, 0.0f);
}
break;
case GRID_XZ: // PLAN
origin[0].x += -EXTENT;
origin[1].x += EXTENT;
origin[2].z += -EXTENT;
origin[3].z += EXTENT;
for (int i = 0; i < 2*MAJOR_COUNT; i += 2) {
float x = (float) (i-MAJOR_COUNT) * 64.0f;
float z = (float) (i-MAJOR_COUNT) * 64.0f;
x_major[i ] = loc + Vec3(x, 0.0f, -EXTENT);
x_major[i+1] = loc + Vec3(x, 0.0f, EXTENT);
y_major[i ] = loc + Vec3(-EXTENT, 0.0f, z);
y_major[i+1] = loc + Vec3( EXTENT, 0.0f, z);
}
break;
case GRID_YZ: // SIDE
origin[0].y += -EXTENT;
origin[1].y += EXTENT;
origin[2].z += -EXTENT;
origin[3].z += EXTENT;
for (int i = 0; i < 2*MAJOR_COUNT; i += 2) {
float y = (float) (i-MAJOR_COUNT) * 64.0f;
float z = (float) (i-MAJOR_COUNT) * 64.0f;
x_major[i ] = loc + Vec3(0.0f, y, -EXTENT);
x_major[i+1] = loc + Vec3(0.0f, y, EXTENT);
y_major[i ] = loc + Vec3(0.0f, -EXTENT, z);
y_major[i+1] = loc + Vec3(0.0f, EXTENT, z);
}
break;
}
RenderReference(video);
video->SetRenderState(Video::FILL_MODE, Video::FILL_SOLID);
video->SetRenderState(Video::LIGHTING_ENABLE, FALSE);
video->SetRenderState(Video::Z_ENABLE, TRUE);
video->SetRenderState(Video::Z_WRITE_ENABLE, TRUE);
video->DrawLines(MAJOR_COUNT, x_major, Color::Gray, 1);
video->DrawLines(MAJOR_COUNT, y_major, Color::Gray, 1);
video->DrawLines( 2, origin, Color::DarkGray, 1);
}
// +--------------------------------------------------------------------+
void
Grid::RenderReference(Video* video)
{
if (!show_ref) return;
Bitmap* bmp = 0;
switch (plane) {
case GRID_XY: bmp = bmp_front; break;
case GRID_XZ: bmp = bmp_plan; break;
case GRID_YZ: bmp = bmp_side; break;
}
if (!bmp) return;
Material mtl;
VertexSet vset(4);
Poly poly;
Vec3 nrm;
float vx = (float) bmp->Width();
float vy = (float) bmp->Height();
float vz = -100.0f;
if (vx <= 256 || vy <= 256) {
vx *= 2.0f;
vy *= 2.0f;
}
switch (plane) {
case GRID_XY: // FRONT
nrm = Vec3(0.0f, 0.0f, 1.0f);
vset.loc[0] = loc + Vec3(-vx, -vy, vz);
vset.loc[1] = loc + Vec3( vx, -vy, vz);
vset.loc[2] = loc + Vec3( vx, vy, vz);
vset.loc[3] = loc + Vec3(-vx, vy, vz);
break;
case GRID_XZ: // PLAN
nrm = Vec3(0.0f, 1.0f, 0.0f);
vset.loc[0] = loc + Vec3(-vx, vz, vy);
vset.loc[1] = loc + Vec3( vx, vz, vy);
vset.loc[2] = loc + Vec3( vx, vz, -vy);
vset.loc[3] = loc + Vec3(-vx, vz, -vy);
break;
case GRID_YZ: // SIDE
nrm = Vec3(1.0f, 0.0f, 0.0f);
vset.loc[0] = loc + Vec3( vz, -vx, -vy);
vset.loc[1] = loc + Vec3( vz, vx, -vy);
vset.loc[2] = loc + Vec3( vz, vx, vy);
vset.loc[3] = loc + Vec3( vz, -vx, vy);
break;
default:
return;
}
mtl.Ka = Color::White;
mtl.Kd = Color::Black;
mtl.tex_diffuse = bmp;
vset.nrm[0] = nrm;
vset.nrm[1] = nrm;
vset.nrm[2] = nrm;
vset.nrm[3] = nrm;
vset.tu[0] = 0.0f;
vset.tv[0] = 0.0f;
vset.tu[1] = 1.0f;
vset.tv[1] = 0.0f;
vset.tu[2] = 1.0f;
vset.tv[2] = 1.0f;
vset.tu[3] = 0.0f;
vset.tv[3] = 1.0f;
poly.nverts = 4;
poly.verts[0] = 0;
poly.verts[1] = 1;
poly.verts[2] = 2;
poly.verts[3] = 3;
poly.material = &mtl;
poly.vertex_set = &vset;
video->SetRenderState(Video::FILL_MODE, Video::FILL_SOLID);
video->SetRenderState(Video::LIGHTING_ENABLE, TRUE);
video->SetRenderState(Video::Z_ENABLE, FALSE);
video->SetRenderState(Video::Z_WRITE_ENABLE, FALSE);
video->SetAmbient(Color::White);
video->DrawPolys(1, &poly);
}
// +----------------------------------------------------------------------+
CPoint&
Grid::Snap(CPoint& p)
{
p.x = snapto(p.x, x_size);
p.y = snapto(p.y, y_size);
return p;
}
// +----------------------------------------------------------------------+
int
Grid::snapto(int i, int dim)
{
if (!snap)
return i;
int n = i + dim/2;
int m = n % dim;
return (n - m);
}
// +----------------------------------------------------------------------+
void
Grid::SetSize(int x, int y)
{
x_size = x;
y_size = y ? y : x;
}
// +----------------------------------------------------------------------+
void
Grid::SetSkip(int x, int y)
{
x_skip = x;
y_skip = y ? y : x;
}
|