mirror of
https://github.com/overte-org/overte.git
synced 2025-08-04 15:59:57 +02:00
dashed lines in GeometryCache
This commit is contained in:
parent
56a3f98b27
commit
0606861b66
7 changed files with 104 additions and 52 deletions
|
@ -186,34 +186,3 @@ bool Base3DOverlay::findRayIntersection(const glm::vec3& origin, const glm::vec3
|
|||
float& distance, BoxFace& face) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void Base3DOverlay::drawDashedLine(const glm::vec3& start, const glm::vec3& end) {
|
||||
|
||||
glBegin(GL_LINES);
|
||||
|
||||
// draw each line segment with appropriate gaps
|
||||
const float DASH_LENGTH = 0.05f;
|
||||
const float GAP_LENGTH = 0.025f;
|
||||
const float SEGMENT_LENGTH = DASH_LENGTH + GAP_LENGTH;
|
||||
float length = glm::distance(start, end);
|
||||
float segmentCount = length / SEGMENT_LENGTH;
|
||||
int segmentCountFloor = (int)glm::floor(segmentCount);
|
||||
|
||||
glm::vec3 segmentVector = (end - start) / segmentCount;
|
||||
glm::vec3 dashVector = segmentVector / SEGMENT_LENGTH * DASH_LENGTH;
|
||||
glm::vec3 gapVector = segmentVector / SEGMENT_LENGTH * GAP_LENGTH;
|
||||
|
||||
glm::vec3 point = start;
|
||||
glVertex3f(point.x, point.y, point.z);
|
||||
for (int i = 0; i < segmentCountFloor; i++) {
|
||||
point += dashVector;
|
||||
glVertex3f(point.x, point.y, point.z);
|
||||
|
||||
point += gapVector;
|
||||
glVertex3f(point.x, point.y, point.z);
|
||||
}
|
||||
glVertex3f(end.x, end.y, end.z);
|
||||
|
||||
glEnd();
|
||||
}
|
||||
|
|
|
@ -60,8 +60,6 @@ public:
|
|||
}
|
||||
|
||||
protected:
|
||||
void drawDashedLine(const glm::vec3& start, const glm::vec3& end);
|
||||
|
||||
glm::vec3 _position;
|
||||
float _lineWidth;
|
||||
glm::quat _rotation;
|
||||
|
|
|
@ -110,21 +110,23 @@ void Cube3DOverlay::render(RenderArgs* args) {
|
|||
glm::vec3 bottomRightFar(halfDimensions.x, -halfDimensions.y, halfDimensions.z);
|
||||
glm::vec3 topLeftFar(-halfDimensions.x, halfDimensions.y, halfDimensions.z);
|
||||
glm::vec3 topRightFar(halfDimensions.x, halfDimensions.y, halfDimensions.z);
|
||||
|
||||
GeometryCache::SharedPointer geometryCache = DependencyManager::get<GeometryCache>();
|
||||
|
||||
drawDashedLine(bottomLeftNear, bottomRightNear);
|
||||
drawDashedLine(bottomRightNear, bottomRightFar);
|
||||
drawDashedLine(bottomRightFar, bottomLeftFar);
|
||||
drawDashedLine(bottomLeftFar, bottomLeftNear);
|
||||
geometryCache->renderDashedLine(bottomLeftNear, bottomRightNear);
|
||||
geometryCache->renderDashedLine(bottomRightNear, bottomRightFar);
|
||||
geometryCache->renderDashedLine(bottomRightFar, bottomLeftFar);
|
||||
geometryCache->renderDashedLine(bottomLeftFar, bottomLeftNear);
|
||||
|
||||
drawDashedLine(topLeftNear, topRightNear);
|
||||
drawDashedLine(topRightNear, topRightFar);
|
||||
drawDashedLine(topRightFar, topLeftFar);
|
||||
drawDashedLine(topLeftFar, topLeftNear);
|
||||
geometryCache->renderDashedLine(topLeftNear, topRightNear);
|
||||
geometryCache->renderDashedLine(topRightNear, topRightFar);
|
||||
geometryCache->renderDashedLine(topRightFar, topLeftFar);
|
||||
geometryCache->renderDashedLine(topLeftFar, topLeftNear);
|
||||
|
||||
drawDashedLine(bottomLeftNear, topLeftNear);
|
||||
drawDashedLine(bottomRightNear, topRightNear);
|
||||
drawDashedLine(bottomLeftFar, topLeftFar);
|
||||
drawDashedLine(bottomRightFar, topRightFar);
|
||||
geometryCache->renderDashedLine(bottomLeftNear, topLeftNear);
|
||||
geometryCache->renderDashedLine(bottomRightNear, topRightNear);
|
||||
geometryCache->renderDashedLine(bottomLeftFar, topLeftFar);
|
||||
geometryCache->renderDashedLine(bottomRightFar, topRightFar);
|
||||
|
||||
} else {
|
||||
glScalef(dimensions.x, dimensions.y, dimensions.z);
|
||||
|
|
|
@ -59,7 +59,7 @@ void Line3DOverlay::render(RenderArgs* args) {
|
|||
glRotatef(glm::degrees(glm::angle(rotation)), axis.x, axis.y, axis.z);
|
||||
|
||||
if (getIsDashedLine()) {
|
||||
drawDashedLine(_position, _end);
|
||||
DependencyManager::get<GeometryCache>()->renderDashedLine(_position, _end, _geometryCacheID);
|
||||
} else {
|
||||
DependencyManager::get<GeometryCache>()->renderLine(_start, _end, _geometryCacheID);
|
||||
}
|
||||
|
|
|
@ -65,6 +65,8 @@ void Rectangle3DOverlay::render(RenderArgs* args) {
|
|||
//glScalef(dimensions.x, dimensions.y, 1.0f);
|
||||
|
||||
glLineWidth(_lineWidth);
|
||||
|
||||
GeometryCache::SharedPointer geometryCache = DependencyManager::get<GeometryCache>();
|
||||
|
||||
// for our overlay, is solid means we draw a solid "filled" rectangle otherwise we just draw a border line...
|
||||
if (getIsSolid()) {
|
||||
|
@ -79,14 +81,12 @@ void Rectangle3DOverlay::render(RenderArgs* args) {
|
|||
glm::vec3 point3(halfDimensions.x, 0.0f, halfDimensions.y);
|
||||
glm::vec3 point4(-halfDimensions.x, 0.0f, halfDimensions.y);
|
||||
|
||||
drawDashedLine(point1, point2);
|
||||
drawDashedLine(point2, point3);
|
||||
drawDashedLine(point3, point4);
|
||||
drawDashedLine(point4, point1);
|
||||
geometryCache->renderDashedLine(point1, point2);
|
||||
geometryCache->renderDashedLine(point2, point3);
|
||||
geometryCache->renderDashedLine(point3, point4);
|
||||
geometryCache->renderDashedLine(point4, point1);
|
||||
|
||||
} else {
|
||||
|
||||
GeometryCache::SharedPointer geometryCache = DependencyManager::get<GeometryCache>();
|
||||
|
||||
if (halfDimensions != _previousHalfDimensions) {
|
||||
QVector<glm::vec3> border;
|
||||
|
|
|
@ -1318,6 +1318,83 @@ void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottom
|
|||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
void GeometryCache::renderDashedLine(const glm::vec3& start, const glm::vec3& end, int id) {
|
||||
bool registered = (id != UNKNOWN_ID);
|
||||
Vec3Pair key(start, end);
|
||||
BufferDetails& details = registered ? _registeredDashedLines[id] : _dashedLines[key];
|
||||
|
||||
// if this is a registered , and we have buffers, then check to see if the geometry changed and rebuild if needed
|
||||
if (registered && details.buffer.isCreated()) {
|
||||
if (_lastRegisteredDashedLines[id] != key) {
|
||||
details.buffer.destroy();
|
||||
_lastRegisteredDashedLines[id] = key;
|
||||
#if 1// def WANT_DEBUG
|
||||
qDebug() << "renderDashedLine()... RELEASING REGISTERED";
|
||||
#endif // def WANT_DEBUG
|
||||
}
|
||||
}
|
||||
|
||||
if (!details.buffer.isCreated()) {
|
||||
|
||||
// draw each line segment with appropriate gaps
|
||||
const float DASH_LENGTH = 0.05f;
|
||||
const float GAP_LENGTH = 0.025f;
|
||||
const float SEGMENT_LENGTH = DASH_LENGTH + GAP_LENGTH;
|
||||
float length = glm::distance(start, end);
|
||||
float segmentCount = length / SEGMENT_LENGTH;
|
||||
int segmentCountFloor = (int)glm::floor(segmentCount);
|
||||
|
||||
glm::vec3 segmentVector = (end - start) / segmentCount;
|
||||
glm::vec3 dashVector = segmentVector / SEGMENT_LENGTH * DASH_LENGTH;
|
||||
glm::vec3 gapVector = segmentVector / SEGMENT_LENGTH * GAP_LENGTH;
|
||||
|
||||
const int FLOATS_PER_VERTEX = 3;
|
||||
details.vertices = (segmentCountFloor + 1) * 2;
|
||||
details.vertexSize = FLOATS_PER_VERTEX;
|
||||
|
||||
GLfloat* vertexData = new GLfloat[details.vertices * FLOATS_PER_VERTEX];
|
||||
GLfloat* vertex = vertexData;
|
||||
|
||||
glm::vec3 point = start;
|
||||
*(vertex++) = point.x;
|
||||
*(vertex++) = point.y;
|
||||
*(vertex++) = point.z;
|
||||
|
||||
for (int i = 0; i < segmentCountFloor; i++) {
|
||||
point += dashVector;
|
||||
*(vertex++) = point.x;
|
||||
*(vertex++) = point.y;
|
||||
*(vertex++) = point.z;
|
||||
|
||||
point += gapVector;
|
||||
*(vertex++) = point.x;
|
||||
*(vertex++) = point.y;
|
||||
*(vertex++) = point.z;
|
||||
}
|
||||
*(vertex++) = end.x;
|
||||
*(vertex++) = end.y;
|
||||
*(vertex++) = end.z;
|
||||
|
||||
details.buffer.create();
|
||||
details.buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
|
||||
details.buffer.bind();
|
||||
details.buffer.allocate(vertexData, details.vertices * FLOATS_PER_VERTEX * sizeof(GLfloat));
|
||||
delete[] vertexData;
|
||||
|
||||
#if 1 //def WANT_DEBUG
|
||||
qDebug() << "new registered linestrip buffer made -- _registeredLinestrips.size():" << _registeredLinestrips.size();
|
||||
#endif
|
||||
} else {
|
||||
details.buffer.bind();
|
||||
}
|
||||
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
glVertexPointer(details.vertexSize, GL_FLOAT, 0, 0);
|
||||
glDrawArrays(GL_LINES, 0, details.vertices);
|
||||
glDisableClientState(GL_VERTEX_ARRAY);
|
||||
details.buffer.release();
|
||||
}
|
||||
|
||||
void GeometryCache::renderLine(const glm::vec3& p1, const glm::vec3& p2, int id) {
|
||||
bool registeredLine = (id != UNKNOWN_ID);
|
||||
Vec3Pair key(p1, p2);
|
||||
|
|
|
@ -112,6 +112,7 @@ public:
|
|||
|
||||
|
||||
void renderLine(const glm::vec3& p1, const glm::vec3& p2, int id = UNKNOWN_ID);
|
||||
void renderDashedLine(const glm::vec3& start, const glm::vec3& end, int id = UNKNOWN_ID);
|
||||
void renderLine(const glm::vec2& p1, const glm::vec2& p2, int id = UNKNOWN_ID);
|
||||
|
||||
void updateLinestrip(int id, const QVector<glm::vec2>& points);
|
||||
|
@ -174,6 +175,11 @@ private:
|
|||
|
||||
QHash<int, BufferDetails> _registeredLinestrips;
|
||||
|
||||
QHash<int, Vec3Pair> _lastRegisteredDashedLines;
|
||||
QHash<Vec3Pair, BufferDetails> _dashedLines;
|
||||
QHash<int, BufferDetails> _registeredDashedLines;
|
||||
|
||||
|
||||
|
||||
|
||||
QHash<IntPair, QOpenGLBuffer> _gridBuffers;
|
||||
|
|
Loading…
Reference in a new issue