History

dashed lines in GeometryCache

ZappoMan committed 11 years ago

Commit 06068611 parent 56a3f987 files · +106 −54Browse files

Showing the changes to libraries/render-utils/src/GeometryCache.cpp (1 of 7 files). Show all

Unified · Split

libraries/render-utils/src/GeometryCache.cpp+77−0
@@ -1318,6 +1318,83 @@ void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottom · 1317 unmodified lines
1318 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);1318 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1319}1319}
1320 1320
1321void GeometryCache::renderDashedLine(const glm::vec3& start, const glm::vec3& end, int id) {
1322 bool registered = (id != UNKNOWN_ID);
1323 Vec3Pair key(start, end);
1324 BufferDetails& details = registered ? _registeredDashedLines[id] : _dashedLines[key];
1325
1326 // if this is a registered , and we have buffers, then check to see if the geometry changed and rebuild if needed
1327 if (registered && details.buffer.isCreated()) {
1328 if (_lastRegisteredDashedLines[id] != key) {
1329 details.buffer.destroy();
1330 _lastRegisteredDashedLines[id] = key;
1331 #if 1// def WANT_DEBUG
1332 qDebug() << "renderDashedLine()... RELEASING REGISTERED";
1333 #endif // def WANT_DEBUG
1334 }
1335 }
1336
1337 if (!details.buffer.isCreated()) {
1338
1339 // draw each line segment with appropriate gaps
1340 const float DASH_LENGTH = 0.05f;
1341 const float GAP_LENGTH = 0.025f;
1342 const float SEGMENT_LENGTH = DASH_LENGTH + GAP_LENGTH;
1343 float length = glm::distance(start, end);
1344 float segmentCount = length / SEGMENT_LENGTH;
1345 int segmentCountFloor = (int)glm::floor(segmentCount);
1346
1347 glm::vec3 segmentVector = (end - start) / segmentCount;
1348 glm::vec3 dashVector = segmentVector / SEGMENT_LENGTH * DASH_LENGTH;
1349 glm::vec3 gapVector = segmentVector / SEGMENT_LENGTH * GAP_LENGTH;
1350
1351 const int FLOATS_PER_VERTEX = 3;
1352 details.vertices = (segmentCountFloor + 1) * 2;
1353 details.vertexSize = FLOATS_PER_VERTEX;
1354
1355 GLfloat* vertexData = new GLfloat[details.vertices * FLOATS_PER_VERTEX];
1356 GLfloat* vertex = vertexData;
1357
1358 glm::vec3 point = start;
1359 *(vertex++) = point.x;
1360 *(vertex++) = point.y;
1361 *(vertex++) = point.z;
1362
1363 for (int i = 0; i < segmentCountFloor; i++) {
1364 point += dashVector;
1365 *(vertex++) = point.x;
1366 *(vertex++) = point.y;
1367 *(vertex++) = point.z;
1368
1369 point += gapVector;
1370 *(vertex++) = point.x;
1371 *(vertex++) = point.y;
1372 *(vertex++) = point.z;
1373 }
1374 *(vertex++) = end.x;
1375 *(vertex++) = end.y;
1376 *(vertex++) = end.z;
1377
1378 details.buffer.create();
1379 details.buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
1380 details.buffer.bind();
1381 details.buffer.allocate(vertexData, details.vertices * FLOATS_PER_VERTEX * sizeof(GLfloat));
1382 delete[] vertexData;
1383
1384 #if 1 //def WANT_DEBUG
1385 qDebug() << "new registered linestrip buffer made -- _registeredLinestrips.size():" << _registeredLinestrips.size();
1386 #endif
1387 } else {
1388 details.buffer.bind();
1389 }
1390
1391 glEnableClientState(GL_VERTEX_ARRAY);
1392 glVertexPointer(details.vertexSize, GL_FLOAT, 0, 0);
1393 glDrawArrays(GL_LINES, 0, details.vertices);
1394 glDisableClientState(GL_VERTEX_ARRAY);
1395 details.buffer.release();
1396}
1397
1321void GeometryCache::renderLine(const glm::vec3& p1, const glm::vec3& p2, int id) {1398void GeometryCache::renderLine(const glm::vec3& p1, const glm::vec3& p2, int id) {
1322 bool registeredLine = (id != UNKNOWN_ID);1399 bool registeredLine = (id != UNKNOWN_ID);
1323 Vec3Pair key(p1, p2);1400 Vec3Pair key(p1, p2);