Skip to content

Commit 1a48d65

Browse files
committed
removed async rendering for ranger visualization that introduced race condition bugs on some platforms
1 parent 1350f47 commit 1a48d65

File tree

1 file changed

+87
-93
lines changed

1 file changed

+87
-93
lines changed

libstage/model_ranger.cc

Lines changed: 87 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -263,119 +263,109 @@ std::string ModelRanger::Sensor::String() const
263263
return (std::string(buf));
264264
}
265265

266+
typedef struct { GLfloat x; GLfloat y; } glpoint_t;
267+
266268
void ModelRanger::Sensor::Visualize(ModelRanger::Vis *vis, ModelRanger *rgr) const
267269
{
268270
// glTranslatef( 0,0, ranger->GetGeom().size.z/2.0 ); // shoot the ranger beam
269271
// out at the right height
270272

271-
size_t alloc_count;
272-
if (sample_count == 1) {
273-
alloc_count = 6; // to fake some data, see below
274-
} else {
275-
alloc_count = (2 * sample_count + 3) + 1;
276-
}
277-
278-
// Unfortunately, it was not possible (=> strange crashes during the
279-
// raytracing that have / should
280-
// have obviously nothing to do with this here) to have the following buffer
281-
// as a class member and
282-
// update it only when this->sample_count changes (in ctor and Load()). So
283-
// we'll do it this way:
284-
static struct VertexBufferData {
285-
VertexBufferData() : size(0), buf(NULL) {}
286-
~VertexBufferData()
287-
{
288-
delete[] buf;
289-
buf = NULL;
290-
}
291-
size_t size;
292-
GLfloat *buf;
293-
} data;
294-
if (!data.buf || data.size != alloc_count) { // never allocated or buffer size changed?
295-
// This should happen rarely.
296-
delete[] data.buf;
297-
data.buf = NULL;
298-
data.buf = new GLfloat[alloc_count];
299-
data.size = alloc_count;
300-
data.buf[0] = 0.0f;
301-
data.buf[1] = 0.0f;
302-
}
303-
304-
// Pack the ranger hit points into a vertex array for fast rendering:
305-
glVertexPointer(2, GL_FLOAT, 0, data.buf);
306-
307-
glDepthMask(GL_FALSE);
308-
glPointSize(2);
309-
310273
glPushMatrix();
311274

312275
Gl::pose_shift(pose);
313276

277+
const double sample_fov = fov / sample_count;
278+
314279
if (vis->showTransducers) {
315280
// draw the sensor body as a rectangle in a darker version of the body color
316-
// Color col( rgr->color );
281+
//Color col( rgr->color );
317282
// col.r /= 3.0;
318283
// col.g /= 3.0;
319284
// col.b /= 3.0;
320285
rgr->PushColor(color);
321-
286+
322287
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
323288
glRectf(-size.x / 2.0, -size.y / 2.0, size.x / 2.0, size.y / 2.0);
324289
rgr->PopColor();
325290
}
326-
291+
327292
if (vis->showFov) {
328-
for (size_t s(0); s < sample_count; s++) {
329-
double ray_angle((s * (fov / (sample_count - 1))) - fov / 2.0);
330-
data.buf[2 * s + 2] = (float)(range.max * cos(ray_angle));
331-
data.buf[2 * s + 3] = (float)(range.max * sin(ray_angle));
332-
}
333293

334294
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
335-
336295
Color c = color;
337296
c.a = 0.5;
338297
rgr->PushColor(c);
339-
glDrawArrays(GL_POLYGON, 0, sample_count + 1);
340-
rgr->PopColor();
341-
}
298+
glBegin( GL_POLYGON );
342299

343-
if (ranges.size()) { // if we have some data
344-
rgr->PushColor(color);
345-
glPolygonMode(GL_FRONT, GL_FILL);
346-
347-
if (sample_count == 1) {
348-
// only one sample, so we fake up some beam width for beauty
349-
const double sidelen = ranges[0];
350-
const double da = fov / 2.0;
351-
352-
data.buf[2] = sidelen * cos(-da);
353-
data.buf[3] = sidelen * sin(-da);
354-
data.buf[4] = sidelen * cos(+da);
355-
data.buf[5] = sidelen * sin(+da);
356-
} else {
357-
for (size_t s(0); s < sample_count + 1; s++) {
358-
double ray_angle = (s * (fov / (sample_count - 1))) - fov / 2.0;
359-
data.buf[2 * s + 2] = (float)(ranges[s] * cos(ray_angle));
360-
data.buf[2 * s + 3] = (float)(ranges[s] * sin(ray_angle));
361-
}
300+
glVertex2f( 0,0 );
301+
302+
for (size_t s(0); s < sample_count; s++) {
303+
const double ray_angle = s * sample_fov - fov / 2.0;
304+
const GLfloat x = range.max * cos(ray_angle);
305+
const GLfloat y = range.max * sin(ray_angle);
306+
307+
glVertex2f( x, y );
362308
}
363309

364-
if (vis->showArea) {
365-
if (sample_count > 1) {
366-
// draw the filled polygon in transparent blue
367-
glDrawArrays(GL_POLYGON, 0, sample_count);
368-
}
369-
}
310+
glVertex2f( 0,0 );
370311

312+
glEnd();
313+
314+
rgr->PopColor();
315+
}
316+
317+
std::vector<glpoint_t> verts( sample_count );
318+
319+
if (sample_count == 1) {
320+
// only one sample, so we fake up some beam width for beauty
321+
const double sidelen = ranges[0];
322+
const double da = fov / 2.0;
323+
324+
verts.resize(3);
325+
verts[0].x = 0;
326+
verts[0].y = 0;
327+
verts[1].x = sidelen * cos(-da);
328+
verts[1].y = sidelen * sin(-da);
329+
verts[2].x = sidelen * cos(+da);
330+
verts[2].y = sidelen * sin(+da);
331+
} else {
332+
for (size_t s(0); s < sample_count; s++) {
333+
const double ray_angle = s * sample_fov - fov / 2.0;
334+
verts[s].x = (float)(ranges[s] * cos(ray_angle));
335+
verts[s].y = (float)(ranges[s] * sin(ray_angle));
336+
}
337+
}
338+
339+
if (vis->showArea) {
340+
// draw the filled polygon in transparent blue
341+
glEnable( GL_BLEND );
342+
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
343+
344+
glDepthMask(GL_FALSE);
345+
glPolygonMode(GL_FRONT, GL_FILL);
346+
Color c = color;
347+
c.a = 0.35; // some alpha
348+
rgr->PushColor(c);
349+
350+
glBegin( GL_POLYGON );
351+
glVertex2f( 0,0 );
352+
353+
FOR_EACH( it, verts )
354+
glVertex2f( it->x, it->y );
355+
356+
glVertex2f( 0,0 );
357+
glEnd();
358+
359+
rgr->PopColor();
371360
glDepthMask(GL_TRUE);
372-
373-
if (vis->showStrikes) {
374-
// TODO - paint the stike point in a color based on intensity
375-
// // if the sample is unusually bright, draw a
376-
// little blob
377-
// if( intensities[s] > 0.0 )
378-
// {
361+
}
362+
363+
if (vis->showStrikes) {
364+
// TODO - paint the stike point in a color based on intensity
365+
// // if the sample is unusually bright, draw a
366+
// little blob
367+
// if( intensities[s] > 0.0 )
368+
// {
379369
// // this happens rarely so we can
380370
// do
381371
// it in immediate mode
@@ -388,12 +378,18 @@ void ModelRanger::Sensor::Visualize(ModelRanger::Vis *vis, ModelRanger *rgr) con
388378

389379
// draw the beam strike points
390380
// c.a = 0.8;
391-
rgr->PushColor(Color::blue);
392-
glDrawArrays(GL_POINTS, 0, sample_count + 1);
393-
rgr->PopColor();
394-
}
395-
396-
// if( vis->showBeams ) {
381+
rgr->PushColor(Color::blue); // solid color
382+
glPointSize(2);
383+
384+
glBegin( GL_POINTS );
385+
FOR_EACH( it, verts )
386+
glVertex2f( it->x, it->y );
387+
glEnd();
388+
389+
rgr->PopColor();
390+
}
391+
392+
// if( vis->showBeams ) {
397393
// Color c = color;
398394

399395
// // darker version of the same color
@@ -416,10 +412,8 @@ void ModelRanger::Sensor::Visualize(ModelRanger::Vis *vis, ModelRanger *rgr) con
416412

417413
// rgr->PopColor();
418414
// }
419-
420-
rgr->PopColor();
421-
}
422-
415+
416+
//rgr->PopColor();
423417
glPopMatrix();
424418
}
425419

0 commit comments

Comments
 (0)