/
blockgroup.cc
326 lines (246 loc) · 6.38 KB
/
blockgroup.cc
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
#include "stage.hh"
#include "worldfile.hh"
#include <libgen.h> // for dirname(3)
#include <limits.h> // for _POSIX_PATH_MAX
#undef DEBUG
using namespace Stg;
using namespace std;
BlockGroup::BlockGroup()
: displaylist(0),
blocks(),
minx(0),
maxx(0),
miny(0),
maxy(0)
{ /* empty */ }
BlockGroup::~BlockGroup()
{
Clear();
}
void BlockGroup::AppendBlock( Block* block )
{
blocks.push_back( block );
}
void BlockGroup::Clear()
{
FOR_EACH( it, blocks )
delete *it;
blocks.clear();
}
void BlockGroup::AppendTouchingModels( std::set<Model*>& v )
{
FOR_EACH( it, blocks )
(*it)->AppendTouchingModels( v );
}
Model* BlockGroup::TestCollision()
{
Model* hitmod = NULL;
FOR_EACH( it, blocks )
if( (hitmod = (*it)->TestCollision()))
break; // bail on the earliest collision
return hitmod; // NULL if no collision
}
// establish the min and max of all the blocks, so we can scale this
// group later
void BlockGroup::CalcSize()
{
// assuming the blocks currently fit in a square +/- one billion units
//double minx, miny, maxx, maxy, minz, maxz;
minx = miny = billion;
maxx = maxy = -billion;
size.z = 0.0; // grow to largest z we see
FOR_EACH( it, blocks )
{
// examine all the points in the polygon
Block* block = *it;
FOR_EACH( it, block->pts )
{
if( it->x < minx ) minx = it->x;
if( it->y < miny ) miny = it->y;
if( it->x > maxx ) maxx = it->x;
if( it->y > maxy ) maxy = it->y;
}
size.z = std::max( block->local_z.max, size.z );
}
// store these bounds for normalization purposes
size.x = maxx-minx;
size.y = maxy-miny;
offset.x = minx + size.x/2.0;
offset.y = miny + size.y/2.0;
offset.z = 0; // todo?
InvalidateModelPointCache();
}
void BlockGroup::Map( unsigned int layer )
{
FOR_EACH( it, blocks )
(*it)->Map(layer);
}
// defined in world.cc
//void SwitchSuperRegionLock( SuperRegion* current, SuperRegion* next );
void BlockGroup::UnMap( unsigned int layer )
{
FOR_EACH( it, blocks )
(*it)->UnMap(layer);
}
void BlockGroup::DrawSolid( const Geom & geom )
{
glPushMatrix();
Gl::pose_shift( geom.pose );
glScalef( geom.size.x / size.x,
geom.size.y / size.y,
geom.size.z / size.z );
glTranslatef( -offset.x, -offset.y, -offset.z );
FOR_EACH( it, blocks )
(*it)->DrawSolid(false);
glPopMatrix();
}
void BlockGroup::DrawFootPrint( const Geom & geom )
{
glPushMatrix();
glScalef( geom.size.x / size.x,
geom.size.y / size.y,
geom.size.z / size.z );
glTranslatef( -offset.x, -offset.y, -offset.z );
FOR_EACH( it, blocks )
(*it)->DrawFootPrint();
glPopMatrix();
}
void BlockGroup::BuildDisplayList( Model* mod )
{
//puts( "build" );
if( ! mod->world->IsGUI() )
return;
//printf( "display list for model %s\n", mod->token );
if( displaylist == 0 )
{
displaylist = glGenLists(1);
CalcSize();
}
glNewList( displaylist, GL_COMPILE );
// render each block as a polygon extruded into Z
Geom geom = mod->GetGeom();
Gl::pose_shift( geom.pose );
glScalef( geom.size.x / size.x,
geom.size.y / size.y,
geom.size.z / size.z );
glTranslatef( -offset.x, -offset.y, -offset.z );
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0, 1.0);
mod->PushColor( mod->color );
//const bool topview = dynamic_cast<WorldGui*>(mod->world)->IsTopView();
FOR_EACH( it, blocks )
{
Block* blk = (*it);
if( (!blk->inherit_color) && (blk->color != mod->color) )
{
mod->PushColor( blk->color );
blk->DrawSolid(false);
mod->PopColor();
}
else
blk->DrawSolid(false);
}
mod->PopColor();
// outline each poly in a darker version of the same color
glDisable(GL_POLYGON_OFFSET_FILL);
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
glDepthMask(GL_FALSE);
Color c = mod->color;
c.r /= 2.0;
c.g /= 2.0;
c.b /= 2.0;
mod->PushColor( c );
FOR_EACH( it, blocks )
{
Block* blk = *it;
if( (!blk->inherit_color) && (blk->color != mod->color) )
{
Color c = blk->color;
c.r /= 2.0;
c.g /= 2.0;
c.b /= 2.0;
mod->PushColor( c );
blk->DrawSolid(false);
mod->PopColor();
}
else
blk->DrawSolid(false);
}
glDepthMask(GL_TRUE);
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
mod->PopColor();
glEndList();
}
void BlockGroup::CallDisplayList( Model* mod )
{
if( displaylist == 0 || mod->rebuild_displaylist )
{
BuildDisplayList( mod );
mod->rebuild_displaylist = 0;
}
glCallList( displaylist );
}
void BlockGroup::LoadBlock( Model* mod, Worldfile* wf, int entity )
{
AppendBlock( new Block( mod, wf, entity ));
CalcSize();
}
void BlockGroup::LoadBitmap( Model* mod, const std::string& bitmapfile, Worldfile* wf )
{
PRINT_DEBUG1( "attempting to load bitmap \"%s\n", bitmapfile );
std::string full;
if( bitmapfile[0] == '/' )
full = bitmapfile;
else
{
char* workaround_const = strdup(wf->filename.c_str());
full = std::string(dirname(workaround_const)) + "/" + bitmapfile;
free( workaround_const );
}
PRINT_DEBUG1( "attempting to load image %s", full );
std::vector<rotrect_t> rects;
if( rotrects_from_image_file( full,
rects ) )
{
PRINT_ERR1( "failed to load rects from image file \"%s\"",
full.c_str() );
return;
}
//printf( "found %d rects in \"%s\" at %p\n",
// rect_count, full, rects );
// TODO fix this
Color col( 1.0, 0.0, 1.0, 1.0 );
FOR_EACH( rect, rects )
{
std::vector<point_t> pts(4);
const double x = rect->pose.x;
const double y = rect->pose.y;
const double w = rect->size.x;
const double h = rect->size.y;
pts[0].x = x;
pts[0].y = y;
pts[1].x = x + w;
pts[1].y = y;
pts[2].x = x + w;
pts[2].y = y + h;
pts[3].x = x;
pts[3].y = y + h;
AppendBlock( new Block( mod,
pts,
0,1,
col,
true,
false) );
}
CalcSize();
}
void BlockGroup::Rasterize( uint8_t* data,
unsigned int width,
unsigned int height,
meters_t cellwidth,
meters_t cellheight )
{
FOR_EACH( it, blocks )
(*it)->Rasterize( data, width, height, cellwidth, cellheight );
}