Skip to content
Newer
Older
100644 341 lines (265 sloc) 7.86 KB
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
1
ec56d04 header clean up
rtv authored Jan 22, 2009
2 #include "region.hh"
3 #include "worldfile.hh"
ec21888 removed world size limitation
rtv authored Feb 1, 2008
4
ec56d04 header clean up
rtv authored Jan 22, 2009
5 using namespace Stg;
ec21888 removed world size limitation
rtv authored Feb 1, 2008
6
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
7 /** Create a new block. A model's body is a list of these
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
8 blocks. The point data is copied, so pts can safely be freed
9 after calling this.*/
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
10 Block::Block( Model* mod,
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
11 stg_point_t* pts,
12 size_t pt_count,
13 stg_meters_t zmin,
14 stg_meters_t zmax,
15 stg_color_t color,
16 bool inherit_color
17 ) :
18 mod( mod ),
19 pt_count( pt_count ),
20 pts( (stg_point_t*)g_memdup( pts, pt_count * sizeof(stg_point_t)) ),
21 color( color ),
22 inherit_color( inherit_color ),
23 rendered_cells( g_ptr_array_sized_new(32) ),
24 candidate_cells( g_ptr_array_sized_new(32) )
25 // _gpts( NULL )
26 {
27 assert( mod );
28 assert( pt_count > 0 );
29 assert( pts );
30
31 local_z.min = zmin;
32 local_z.max = zmax;
33
34 // add this block's global coords array to a global list
35 //g_ptr_array_add( global_verts, this );
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
36 }
37
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
38 /** A from-file constructor */
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
39 Block::Block( Model* mod,
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
40 Worldfile* wf,
41 int entity)
42 : mod( mod ),
43 pt_count(0),
03e148e code tidying - eliminated all -Wall warnings
rtv authored Dec 5, 2008
44 pts(NULL),
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
45 color(0),
46 inherit_color(true),
47 rendered_cells( g_ptr_array_sized_new(32) ),
48 candidate_cells( g_ptr_array_sized_new(32) )
49 // _gpts( NULL )
50 {
51 assert(mod);
52 assert(wf);
53 assert(entity);
54
55 Load( wf, entity );
56
57 // add this block's global coords array to a global list
58 //g_ptr_array_add( global_verts, this );
59 }
60
61
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
62 Block::~Block()
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
63 {
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
64 if( mapped ) UnMap();
65
66 stg_points_destroy( pts );
67
68 g_ptr_array_free( rendered_cells, TRUE );
69 g_ptr_array_free( candidate_cells, TRUE );
70
71 //free( _gpts );
72 //g_ptr_array_remove( global_verts, this );
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
73 }
74
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
75 stg_color_t Block::GetColor()
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
76 {
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
77 return( inherit_color ? mod->color : color );
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
78 }
79
80
81
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
82 Model* Block::TestCollision()
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
83 {
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
84 //printf( "model %s block %p test collision...\n", mod->Token(), this );
85
86 // find the set of cells we would render into given the current global pose
87 GenerateCandidateCells();
88
89 // for every cell we may be rendered into
03e148e code tidying - eliminated all -Wall warnings
rtv authored Dec 5, 2008
90 for( unsigned int i=0; i<candidate_cells->len; i++ )
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
91 {
92 Cell* cell = (Cell*)g_ptr_array_index(candidate_cells, i);
93
94 // for every rendered into that cell
95 for( GSList* it = cell->list; it; it=it->next )
96 {
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
97 Block* testblock = (Block*)it->data;
98 Model* testmod = testblock->mod;
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
99
100 //printf( " testing block %p of model %s\n", testblock, testmod->Token() );
101
102 // if the tested model is an obstacle and it's not attached to this model
103 if( (testmod != this->mod) &&
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
104 testmod->vis.obstacle_return &&
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
105 !mod->IsRelated( testmod ))
106 {
107 //puts( "HIT");
108 return testmod; // bail immediately with the bad news
109 }
110 }
111 }
112
113 //printf( "model %s block %p collision done. no hits.\n", mod->Token(), this );
114 return NULL; // no hit
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
115 }
116
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
117
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
118 void Block::RemoveFromCellArray( GPtrArray* ptrarray )
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
119 {
120 for( unsigned int i=0; i<ptrarray->len; i++ )
121 ((Cell*)g_ptr_array_index(ptrarray, i))->RemoveBlock( this );
122 }
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
123
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
124 void Block::AddToCellArray( GPtrArray* ptrarray )
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
125 {
126 for( unsigned int i=0; i<ptrarray->len; i++ )
127 ((Cell*)g_ptr_array_index(ptrarray, i))->AddBlock( this );
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
128 }
129
c68a660 more vertex arrays
rtv authored May 23, 2008
130
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
131 // used as a callback to gather an array of cells in a polygon
132 void AppendCellToPtrArray( Cell* c, GPtrArray* a )
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
133 {
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
134 g_ptr_array_add( a, c );
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
135 }
136
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
137 // used as a callback to gather an array of cells in a polygon
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
138 void AddBlockToCell( Cell* c, Block* block )
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
139 {
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
140 c->AddBlock( block );
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
141 }
142
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
143 void Block::Map()
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
144 {
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
145 // TODO - if called often, we may not need to generate each time
146 GenerateCandidateCells();
147 SwitchToTestedCells();
148 return;
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
149
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
150 mapped = true;
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
151 }
152
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
153 void Block::UnMap()
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
154 {
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
155 RemoveFromCellArray( rendered_cells );
156
157 g_ptr_array_set_size( rendered_cells, 0 );
158 mapped = false;
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
159 }
160
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
161 void Block::SwitchToTestedCells()
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
162 {
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
163 RemoveFromCellArray( rendered_cells );
164 AddToCellArray( candidate_cells );
165
166 // switch current and candidate cell pointers
167 GPtrArray* tmp = rendered_cells;
168 rendered_cells = candidate_cells;
169 candidate_cells = tmp;
170
171 mapped = true;
172 }
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
173
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
174 void Block::GenerateCandidateCells()
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
175
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
176 {
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
177 Pose gpose = mod->GetGlobalPose();
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
178
eaba4cb geometry fixes & graphics tweaks
rtv authored Nov 19, 2008
179 // add local offset
180 gpose = pose_sum( gpose, mod->geom.pose );
181
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
182 Size bgsize = mod->blockgroup.GetSize();
03e148e code tidying - eliminated all -Wall warnings
rtv authored Dec 5, 2008
183 stg_point3_t bgoffset = mod->blockgroup.GetOffset();
184
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
185 stg_point3_t scale;
03e148e code tidying - eliminated all -Wall warnings
rtv authored Dec 5, 2008
186 scale.x = mod->geom.size.x / bgsize.x;
187 scale.y = mod->geom.size.y / bgsize.y;
188 scale.z = mod->geom.size.z / bgsize.z;
189
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
190
191 g_ptr_array_set_size( candidate_cells, 0 );
192
193 // compute the global location of the first point
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
194 Pose local( (pts[0].x - bgoffset.x) * scale.x ,
03e148e code tidying - eliminated all -Wall warnings
rtv authored Dec 5, 2008
195 (pts[0].y - bgoffset.y) * scale.y,
196 -bgoffset.z,
d60514d geometry & graphics fixes. added normalization of blocks to fit insid…
rtv authored Dec 2, 2008
197 0 );
198
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
199 Pose first_gpose, last_gpose;
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
200 first_gpose = last_gpose = pose_sum( gpose, local );
201
202 // store the block's absolute z bounds at this rendering
203 global_z.min = (scale.z * local_z.min) + last_gpose.z;
204 global_z.max = (scale.z * local_z.max) + last_gpose.z;
205
206 // now loop from the the second to the last
03e148e code tidying - eliminated all -Wall warnings
rtv authored Dec 5, 2008
207 for( unsigned int p=1; p<pt_count; p++ )
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
208 {
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
209 Pose local( (pts[p].x - bgoffset.x) * scale.x ,
03e148e code tidying - eliminated all -Wall warnings
rtv authored Dec 5, 2008
210 (pts[p].y - bgoffset.y) * scale.y,
211 -bgoffset.z,
d60514d geometry & graphics fixes. added normalization of blocks to fit insid…
rtv authored Dec 2, 2008
212 0 );
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
213
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
214 Pose gpose2 = pose_sum( gpose, local );
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
215
216 // and render the shape of the block into the global cells
217 mod->world->ForEachCellInLine( last_gpose.x, last_gpose.y,
218 gpose2.x, gpose2.y,
219 (stg_cell_callback_t)AppendCellToPtrArray,
220 candidate_cells );
221 last_gpose = gpose2;
222 }
223
224 // close the polygon
225 mod->world->ForEachCellInLine( last_gpose.x, last_gpose.y,
226 first_gpose.x, first_gpose.y,
227 (stg_cell_callback_t)AppendCellToPtrArray,
228 candidate_cells );
229
230 mapped = true;
231 }
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
232
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
233
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
234 void Block::DrawTop()
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
235 {
236 // draw the top of the block - a polygon at the highest vertical
237 // extent
238 glBegin( GL_POLYGON);
03e148e code tidying - eliminated all -Wall warnings
rtv authored Dec 5, 2008
239 for( unsigned int i=0; i<pt_count; i++ )
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
240 glVertex3f( pts[i].x, pts[i].y, local_z.max );
241 glEnd();
242 }
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
243
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
244 void Block::DrawSides()
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
245 {
246 // construct a strip that wraps around the polygon
247 glBegin(GL_QUAD_STRIP);
248 for( unsigned int p=0; p<pt_count; p++)
249 {
250 glVertex3f( pts[p].x, pts[p].y, local_z.max );
251 glVertex3f( pts[p].x, pts[p].y, local_z.min );
252 }
253 // close the strip
254 glVertex3f( pts[0].x, pts[0].y, local_z.max );
255 glVertex3f( pts[0].x, pts[0].y, local_z.min );
256 glEnd();
257 }
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
258
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
259 void Block::DrawFootPrint()
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
260 {
261 glBegin(GL_POLYGON);
262 for( unsigned int p=0; p<pt_count; p++ )
263 glVertex2f( pts[p].x, pts[p].y );
264 glEnd();
265 }
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
266
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
267 void Block::Draw( Model* mod )
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
268 {
269 // draw filled color polygons
270 stg_color_t col = inherit_color ? mod->color : color;
271
272 mod->PushColor( col );
273 glEnable(GL_POLYGON_OFFSET_FILL);
274 glPolygonOffset(1.0, 1.0);
275 DrawSides();
276 DrawTop();
277 glDisable(GL_POLYGON_OFFSET_FILL);
278
279 // // draw the block outline in a darker version of the same color
280 double r,g,b,a;
281 stg_color_unpack( col, &r, &g, &b, &a );
282 mod->PushColor( stg_color_pack( r/2.0, g/2.0, b/2.0, a ));
283
284 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
285 glDepthMask(GL_FALSE);
286 DrawTop();
287 DrawSides();
288 glDepthMask(GL_TRUE);
289 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
290
291 mod->PopColor();
292 mod->PopColor();
293 }
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
294
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
295 void Block::DrawSolid()
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
296 {
297 DrawSides();
298 DrawTop();
299 }
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
300
301
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
302 //#define DEBUG 1
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
303
304
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
305 void Block::Load( Worldfile* wf, int entity )
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
306 {
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored Jan 5, 2009
307 //printf( "Block::Load entity %d\n", entity );
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
308
309 if( pts )
310 stg_points_destroy( pts );
311
312 pt_count = wf->ReadInt( entity, "points", 0);
313 pts = stg_points_create( pt_count );
314
315 //printf( "reading %d points\n",
316 // pt_count );
317
318 char key[128];
03e148e code tidying - eliminated all -Wall warnings
rtv authored Dec 5, 2008
319 for( unsigned int p=0; p<pt_count; p++ ) {
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
320 snprintf(key, sizeof(key), "point[%d]", p );
321
322 pts[p].x = wf->ReadTupleLength(entity, key, 0, 0);
323 pts[p].y = wf->ReadTupleLength(entity, key, 1, 0);
324 }
325
326 local_z.min = wf->ReadTupleLength( entity, "z", 0, 0.0 );
327 local_z.max = wf->ReadTupleLength( entity, "z", 1, 1.0 );
328
329 const char* colorstr = wf->ReadString( entity, "color", NULL );
330 if( colorstr )
331 {
332 color = stg_lookup_color( colorstr );
333 inherit_color = false;
334 }
d60514d geometry & graphics fixes. added normalization of blocks to fit insid…
rtv authored Dec 2, 2008
335 else
336 inherit_color = true;
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
337 }
338
89dcdaf major changes for 3.1
rtv authored Nov 15, 2008
339
340
Something went wrong with that request. Please try again.