blockgroup.cc
Go to the documentation of this file.
1 
2 #include "stage.hh"
3 #include "worldfile.hh"
4 
5 #include <libgen.h> // for dirname(3)
6 #include <limits.h> // for _POSIX_PATH_MAX
7 
8 #undef DEBUG
9 
10 using namespace Stg;
11 using namespace std;
12 
14  : displaylist(0),
15  blocks(),
16  minx(0),
17  maxx(0),
18  miny(0),
19  maxy(0)
20 { /* empty */ }
21 
23 {
24  Clear();
25 }
26 
28 {
29  blocks.push_back( block );
30 }
31 
33 {
34  FOR_EACH( it, blocks )
35  delete *it;
36 
37  blocks.clear();
38 }
39 
40 void BlockGroup::AppendTouchingModels( std::set<Model*>& v )
41 {
42  FOR_EACH( it, blocks )
43  (*it)->AppendTouchingModels( v );
44 }
45 
47 {
48  Model* hitmod = NULL;
49 
50  FOR_EACH( it, blocks )
51  if( (hitmod = (*it)->TestCollision()))
52  break; // bail on the earliest collision
53 
54  return hitmod; // NULL if no collision
55 }
56 
57 
58 // establish the min and max of all the blocks, so we can scale this
59 // group later
61 {
62  // assuming the blocks currently fit in a square +/- one billion units
63  //double minx, miny, maxx, maxy, minz, maxz;
64  minx = miny = billion;
65  maxx = maxy = -billion;
66 
67  size.z = 0.0; // grow to largest z we see
68 
69  FOR_EACH( it, blocks )
70  {
71  // examine all the points in the polygon
72  Block* block = *it;
73 
74  FOR_EACH( it, block->pts )
75  {
76  if( it->x < minx ) minx = it->x;
77  if( it->y < miny ) miny = it->y;
78  if( it->x > maxx ) maxx = it->x;
79  if( it->y > maxy ) maxy = it->y;
80  }
81 
82  size.z = std::max( block->local_z.max, size.z );
83  }
84 
85  // store these bounds for normalization purposes
86  size.x = maxx-minx;
87  size.y = maxy-miny;
88 
89  offset.x = minx + size.x/2.0;
90  offset.y = miny + size.y/2.0;
91  offset.z = 0; // todo?
92 
94 }
95 
96 
97 void BlockGroup::Map( unsigned int layer )
98 {
99  FOR_EACH( it, blocks )
100  (*it)->Map(layer);
101 }
102 
103 // defined in world.cc
104 //void SwitchSuperRegionLock( SuperRegion* current, SuperRegion* next );
105 
106 void BlockGroup::UnMap( unsigned int layer )
107 {
108  FOR_EACH( it, blocks )
109  (*it)->UnMap(layer);
110 }
111 
112 void BlockGroup::DrawSolid( const Geom & geom )
113 {
114  glPushMatrix();
115 
116  Gl::pose_shift( geom.pose );
117 
118  glScalef( geom.size.x / size.x,
119  geom.size.y / size.y,
120  geom.size.z / size.z );
121 
122  glTranslatef( -offset.x, -offset.y, -offset.z );
123 
124  FOR_EACH( it, blocks )
125  (*it)->DrawSolid(false);
126 
127  glPopMatrix();
128 }
129 
130 void BlockGroup::DrawFootPrint( const Geom & geom )
131 {
132  glPushMatrix();
133 
134  glScalef( geom.size.x / size.x,
135  geom.size.y / size.y,
136  geom.size.z / size.z );
137 
138  glTranslatef( -offset.x, -offset.y, -offset.z );
139 
140  FOR_EACH( it, blocks )
141  (*it)->DrawFootPrint();
142 
143  glPopMatrix();
144 }
145 
147 {
148  //puts( "build" );
149 
150  if( ! mod->world->IsGUI() )
151  return;
152 
153  //printf( "display list for model %s\n", mod->token );
154  if( displaylist == 0 )
155  {
156  displaylist = glGenLists(1);
157  CalcSize();
158  }
159 
160 
161  glNewList( displaylist, GL_COMPILE );
162 
163 
164  // render each block as a polygon extruded into Z
165 
166  Geom geom = mod->GetGeom();
167 
168  Gl::pose_shift( geom.pose );
169 
170  glScalef( geom.size.x / size.x,
171  geom.size.y / size.y,
172  geom.size.z / size.z );
173 
174  glTranslatef( -offset.x, -offset.y, -offset.z );
175 
176  glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
177  glEnable(GL_POLYGON_OFFSET_FILL);
178  glPolygonOffset(1.0, 1.0);
179 
180  mod->PushColor( mod->color );
181 
182  //const bool topview = dynamic_cast<WorldGui*>(mod->world)->IsTopView();
183 
184  FOR_EACH( it, blocks )
185  {
186  Block* blk = (*it);
187 
188  if( (!blk->inherit_color) && (blk->color != mod->color) )
189  {
190  mod->PushColor( blk->color );
191  blk->DrawSolid(false);
192  mod->PopColor();
193  }
194  else
195  blk->DrawSolid(false);
196  }
197 
198  mod->PopColor();
199 
200  // outline each poly in a darker version of the same color
201 
202  glDisable(GL_POLYGON_OFFSET_FILL);
203 
204  glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
205  glDepthMask(GL_FALSE);
206 
207  Color c = mod->color;
208  c.r /= 2.0;
209  c.g /= 2.0;
210  c.b /= 2.0;
211  mod->PushColor( c );
212 
213  FOR_EACH( it, blocks )
214  {
215  Block* blk = *it;
216 
217  if( (!blk->inherit_color) && (blk->color != mod->color) )
218  {
219  Color c = blk->color;
220  c.r /= 2.0;
221  c.g /= 2.0;
222  c.b /= 2.0;
223  mod->PushColor( c );
224  blk->DrawSolid(false);
225  mod->PopColor();
226  }
227  else
228  blk->DrawSolid(false);
229  }
230 
231  glDepthMask(GL_TRUE);
232  glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
233 
234  mod->PopColor();
235 
236  glEndList();
237 }
238 
240 {
241  if( displaylist == 0 || mod->rebuild_displaylist )
242  {
243  BuildDisplayList( mod );
244  mod->rebuild_displaylist = 0;
245  }
246 
247  glCallList( displaylist );
248 }
249 
250 void BlockGroup::LoadBlock( Model* mod, Worldfile* wf, int entity )
251 {
252  AppendBlock( new Block( mod, wf, entity ));
253  CalcSize();
254 }
255 
256 void BlockGroup::LoadBitmap( Model* mod, const std::string& bitmapfile, Worldfile* wf )
257 {
258  PRINT_DEBUG1( "attempting to load bitmap \"%s\n", bitmapfile );
259 
260  std::string full;
261 
262  if( bitmapfile[0] == '/' )
263  full = bitmapfile;
264  else
265  {
266  char* workaround_const = strdup(wf->filename.c_str());
267  full = std::string(dirname(workaround_const)) + "/" + bitmapfile;
268  free( workaround_const );
269  }
270 
271  PRINT_DEBUG1( "attempting to load image %s", full );
272 
273  std::vector<rotrect_t> rects;
274  if( rotrects_from_image_file( full,
275  rects ) )
276  {
277  PRINT_ERR1( "failed to load rects from image file \"%s\"",
278  full.c_str() );
279  return;
280  }
281 
282  //printf( "found %d rects in \"%s\" at %p\n",
283  // rect_count, full, rects );
284 
285  // TODO fix this
286  Color col( 1.0, 0.0, 1.0, 1.0 );
287 
288  FOR_EACH( rect, rects )
289  {
290  std::vector<point_t> pts(4);
291 
292  const double x = rect->pose.x;
293  const double y = rect->pose.y;
294  const double w = rect->size.x;
295  const double h = rect->size.y;
296 
297  pts[0].x = x;
298  pts[0].y = y;
299  pts[1].x = x + w;
300  pts[1].y = y;
301  pts[2].x = x + w;
302  pts[2].y = y + h;
303  pts[3].x = x;
304  pts[3].y = y + h;
305 
306  AppendBlock( new Block( mod,
307  pts,
308  0,1,
309  col,
310  true,
311  false) );
312  }
313 
314  CalcSize();
315 }
316 
317 
318 void BlockGroup::Rasterize( uint8_t* data,
319  unsigned int width,
320  unsigned int height,
321  meters_t cellwidth,
322  meters_t cellheight )
323 {
324  FOR_EACH( it, blocks )
325  (*it)->Rasterize( data, width, height, cellwidth, cellheight );
326 }
Color color
Definition: stage.hh:1275
virtual void PushColor(Color col)
Definition: stage.hh:2189
Bounds local_z
z extent in local coords
Definition: stage.hh:1274
Model class
Definition: stage.hh:1742
meters_t y
Definition: stage.hh:485
meters_t x
Definition: stage.hh:485
double max
largest value in range, initially zero
Definition: stage.hh:435
The Stage library uses its own namespace.
Definition: canvas.hh:8
void CallDisplayList(Model *mod)
Definition: blockgroup.cc:239
meters_t x
Definition: stage.hh:228
Model * TestCollision()
Definition: model.cc:800
void AppendBlock(Block *block)
Definition: blockgroup.cc:27
meters_t minx
Definition: stage.hh:1316
meters_t z
Definition: stage.hh:485
const double billion
Definition: stage.hh:145
Size size
extent
Definition: stage.hh:395
bool inherit_color
Definition: stage.hh:1276
virtual void PopColor()
Definition: stage.hh:2191
void DrawSolid(bool topview)
Definition: block.cc:368
void DrawFootPrint(const Geom &geom)
Definition: blockgroup.cc:130
void InvalidateModelPointCache()
Definition: stage.hh:1356
std::vector< point_t > pts
points defining a polygonx
Definition: stage.hh:1272
char * dirname(char *path)
Definition: dirname.c:31
double r
Definition: stage.hh:200
int rotrects_from_image_file(const std::string &filename, std::vector< rotrect_t > &rects)
Definition: stage.cc:102
point3_t offset
Definition: stage.hh:1315
std::string filename
Definition: worldfile.hh:350
void LoadBitmap(Model *mod, const std::string &bitmapfile, Worldfile *wf)
Definition: blockgroup.cc:256
Pose pose
position
Definition: stage.hh:394
void LoadBlock(Model *mod, Worldfile *wf, int entity)
Definition: blockgroup.cc:250
double meters_t
Definition: stage.hh:174
meters_t maxx
Definition: stage.hh:1316
meters_t y
Definition: stage.hh:228
void pose_shift(const Pose &pose)
Definition: gl.cc:13
meters_t z
Definition: stage.hh:228
double b
Definition: stage.hh:200
void Map(unsigned int layer)
Definition: blockgroup.cc:97
double g
Definition: stage.hh:200
virtual bool IsGUI() const
Definition: stage.hh:1128
void Rasterize(uint8_t *data, unsigned int width, unsigned int height, meters_t cellwidth, meters_t cellheight)
Definition: blockgroup.cc:318
#define PRINT_DEBUG1(m, a)
Definition: stage.hh:667
World * world
Definition: stage.hh:2017
bool rebuild_displaylist
iff true, regenerate block display list before redraw
Definition: stage.hh:1953
#define PRINT_ERR1(m, a)
Definition: stage.hh:626
void UnMap(unsigned int layer)
Definition: blockgroup.cc:106
friend class Block
Definition: stage.hh:1306
void DrawSolid(const Geom &geom)
Definition: blockgroup.cc:112
meters_t maxy
Definition: stage.hh:1316
std::vector< Block * > blocks
Definition: stage.hh:1313
#define FOR_EACH(I, C)
Definition: stage.hh:616
void AppendTouchingModels(std::set< Model * > &touchers)
Definition: blockgroup.cc:40
Model * TestCollision()
Definition: blockgroup.cc:46
Color color
Definition: stage.hh:1859
void BuildDisplayList(Model *mod)
Definition: blockgroup.cc:146
meters_t miny
Definition: stage.hh:1316
Geom GetGeom() const
Definition: stage.hh:2378


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Mon Jun 10 2019 15:06:09