History

More work on point rendering.

Andrzej Kapolka committed 12 years ago

Commit dd7f3701 parent 4ff32226 files · +201 −212Browse files

Showing the changes to interface/src/MetavoxelSystem.cpp (1 of 6 files). Show all

Unified · Split

interface/src/MetavoxelSystem.cpp+110−116
@@ -29,11 +29,6 @@ REGISTER_META_OBJECT(StaticModelRenderer) · 28 unmodified lines
29ProgramObject MetavoxelSystem::_program;29ProgramObject MetavoxelSystem::_program;
30int MetavoxelSystem::_pointScaleLocation;30int MetavoxelSystem::_pointScaleLocation;
31 31
32MetavoxelSystem::MetavoxelSystem() :
33 _simulateVisitor(_points),
34 _buffer(QOpenGLBuffer::VertexBuffer) {
35}
36
37void MetavoxelSystem::init() {32void MetavoxelSystem::init() {
38 MetavoxelClientManager::init();33 MetavoxelClientManager::init();
39 34
@@ -41,10 +36,10 @@ void MetavoxelSystem::init() { · 1 unmodified line
41 _program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/metavoxel_point.vert");36 _program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/metavoxel_point.vert");
42 _program.link();37 _program.link();
43 38
39 _program.bind();
44 _pointScaleLocation = _program.uniformLocation("pointScale");40 _pointScaleLocation = _program.uniformLocation("pointScale");
41 _program.release();
45 }42 }
46 _buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
47 _buffer.create();
48 _pointBufferAttribute = AttributeRegistry::getInstance()->registerAttribute(new PointBufferAttribute());43 _pointBufferAttribute = AttributeRegistry::getInstance()->registerAttribute(new PointBufferAttribute());
49}44}
50 45
@@ -55,21 +50,85 @@ MetavoxelLOD MetavoxelSystem::getLOD() const { · 4 unmodified lines
55 BASE_LOD_THRESHOLD * Menu::getInstance()->getAvatarLODDistanceMultiplier());50 BASE_LOD_THRESHOLD * Menu::getInstance()->getAvatarLODDistanceMultiplier());
56}51}
57 52
53class SpannerSimulateVisitor : public SpannerVisitor {
54public:
55
56 SpannerSimulateVisitor(float deltaTime);
57
58 virtual bool visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize);
59
60private:
61
62 float _deltaTime;
63};
64
65SpannerSimulateVisitor::SpannerSimulateVisitor(float deltaTime) :
66 SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(),
67 QVector<AttributePointer>(), QVector<AttributePointer>(), QVector<AttributePointer>(),
68 Application::getInstance()->getMetavoxels()->getLOD()),
69 _deltaTime(deltaTime) {
70}
71
72bool SpannerSimulateVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) {
73 spanner->getRenderer()->simulate(_deltaTime);
74 return true;
75}
76
58void MetavoxelSystem::simulate(float deltaTime) {77void MetavoxelSystem::simulate(float deltaTime) {
59 // update the clients78 // update the clients
60 _points.clear();
61 _simulateVisitor.setDeltaTime(deltaTime);
62 _simulateVisitor.setOrder(-Application::getInstance()->getViewFrustum()->getDirection());
63 update();79 update();
64 80
65 _buffer.bind();81 SpannerSimulateVisitor spannerSimulateVisitor(deltaTime);
66 int bytes = _points.size() * sizeof(Point);82 guide(spannerSimulateVisitor);
67 if (_buffer.size() < bytes) {83}
68 _buffer.allocate(_points.constData(), bytes);84
69 } else {85class PointBufferRenderVisitor : public MetavoxelVisitor {
70 _buffer.write(0, _points.constData(), bytes);86public:
87
88 PointBufferRenderVisitor();
89
90 virtual int visit(MetavoxelInfo& info);
91
92private:
93
94 int _order;
95};
96
97PointBufferRenderVisitor::PointBufferRenderVisitor() :
98 MetavoxelVisitor(QVector<AttributePointer>() << Application::getInstance()->getMetavoxels()->getPointBufferAttribute(),
99 QVector<AttributePointer>(), Application::getInstance()->getMetavoxels()->getLOD()),
100 _order(encodeOrder(Application::getInstance()->getViewFrustum()->getDirection())) {
101}
102
103int PointBufferRenderVisitor::visit(MetavoxelInfo& info) {
104 PointBufferPointer buffer = info.inputValues.at(0).getInlineValue<PointBufferPointer>();
105 if (buffer) {
106 buffer->render(1000);
71 }107 }
72 _buffer.release();108 if (info.isLeaf) {
109 return STOP_RECURSION;
110 }
111 return _order;
112}
113
114class SpannerRenderVisitor : public SpannerVisitor {
115public:
116
117 SpannerRenderVisitor();
118
119 virtual bool visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize);
120};
121
122SpannerRenderVisitor::SpannerRenderVisitor() :
123 SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(),
124 QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannerMaskAttribute(),
125 QVector<AttributePointer>(), QVector<AttributePointer>(), Application::getInstance()->getMetavoxels()->getLOD(),
126 encodeOrder(Application::getInstance()->getViewFrustum()->getDirection())) {
127}
128
129bool SpannerRenderVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) {
130 spanner->getRenderer()->render(1.0f, SpannerRenderer::DEFAULT_MODE, clipMinimum, clipSize);
131 return true;
73}132}
74 133
75void MetavoxelSystem::render() {134void MetavoxelSystem::render() {
@@ -87,20 +146,14 @@ void MetavoxelSystem::render() { · 11 unmodified lines
87 _program.setUniformValue(_pointScaleLocation, viewportDiagonal *146 _program.setUniformValue(_pointScaleLocation, viewportDiagonal *
88 Application::getInstance()->getViewFrustum()->getNearClip() / worldDiagonal);147 Application::getInstance()->getViewFrustum()->getNearClip() / worldDiagonal);
89 148
90 _buffer.bind();
91
92 Point* pt = 0;
93 glVertexPointer(4, GL_FLOAT, sizeof(Point), &pt->vertex);
94 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Point), &pt->color);
95 glNormalPointer(GL_BYTE, sizeof(Point), &pt->normal);
96
97 glEnableClientState(GL_VERTEX_ARRAY);149 glEnableClientState(GL_VERTEX_ARRAY);
98 glEnableClientState(GL_COLOR_ARRAY);150 glEnableClientState(GL_COLOR_ARRAY);
99 glEnableClientState(GL_NORMAL_ARRAY);151 glEnableClientState(GL_NORMAL_ARRAY);
100 152
101 glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);153 glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
102 154
103 glDrawArrays(GL_POINTS, 0, _points.size());155 PointBufferRenderVisitor pointBufferRenderVisitor;
156 guide(pointBufferRenderVisitor);
104 157
105 glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);158 glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
106 159
@@ -108,19 +161,10 @@ void MetavoxelSystem::render() { · 1 unmodified line
108 glDisableClientState(GL_COLOR_ARRAY);161 glDisableClientState(GL_COLOR_ARRAY);
109 glDisableClientState(GL_NORMAL_ARRAY);162 glDisableClientState(GL_NORMAL_ARRAY);
110 163
111 _buffer.release();
112
113 _program.release();164 _program.release();
114 165
115 foreach (const SharedNodePointer& node, NodeList::getInstance()->getNodeHash()) {166 SpannerRenderVisitor spannerRenderVisitor;
116 if (node->getType() == NodeType::MetavoxelServer) {167 guide(spannerRenderVisitor);
117 QMutexLocker locker(&node->getMutex());
118 MetavoxelSystemClient* client = static_cast<MetavoxelSystemClient*>(node->getLinkedData());
119 if (client) {
120 client->guide(_renderVisitor);
121 }
122 }
123 }
124}168}
125 169
126MetavoxelClient* MetavoxelSystem::createClient(const SharedNodePointer& node) {170MetavoxelClient* MetavoxelSystem::createClient(const SharedNodePointer& node) {
@@ -129,74 +173,6 @@ MetavoxelClient* MetavoxelSystem::createClient(const SharedNodePointer& node) { · 2 unmodified lines
129 173
130void MetavoxelSystem::updateClient(MetavoxelClient* client) {174void MetavoxelSystem::updateClient(MetavoxelClient* client) {
131 MetavoxelClientManager::updateClient(client);175 MetavoxelClientManager::updateClient(client);
132 client->guide(_simulateVisitor);
133}
134
135MetavoxelSystem::SimulateVisitor::SimulateVisitor(QVector<Point>& points) :
136 SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(),
137 QVector<AttributePointer>(), QVector<AttributePointer>() << AttributeRegistry::getInstance()->getColorAttribute() <<
138 AttributeRegistry::getInstance()->getNormalAttribute() <<
139 AttributeRegistry::getInstance()->getSpannerColorAttribute() <<
140 AttributeRegistry::getInstance()->getSpannerNormalAttribute()),
141 _points(points) {
142}
143
144bool MetavoxelSystem::SimulateVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) {
145 spanner->getRenderer()->simulate(_deltaTime);
146 return true;
147}
148
149int MetavoxelSystem::SimulateVisitor::visit(MetavoxelInfo& info) {
150 SpannerVisitor::visit(info);
151
152 if (!info.isLeaf) {
153 return _order;
154 }
155 QRgb color = info.inputValues.at(0).getInlineValue<QRgb>();
156 QRgb normal = info.inputValues.at(1).getInlineValue<QRgb>();
157 quint8 alpha = qAlpha(color);
158 if (!info.isLODLeaf) {
159 if (alpha > 0) {
160 Point point = { glm::vec4(info.minimum + glm::vec3(info.size, info.size, info.size) * 0.5f, info.size),
161 { quint8(qRed(color)), quint8(qGreen(color)), quint8(qBlue(color)), alpha },
162 { quint8(qRed(normal)), quint8(qGreen(normal)), quint8(qBlue(normal)) } };
163 _points.append(point);
164 }
165 } else {
166 QRgb spannerColor = info.inputValues.at(2).getInlineValue<QRgb>();
167 QRgb spannerNormal = info.inputValues.at(3).getInlineValue<QRgb>();
168 quint8 spannerAlpha = qAlpha(spannerColor);
169 if (spannerAlpha > 0) {
170 if (alpha > 0) {
171 Point point = { glm::vec4(info.minimum + glm::vec3(info.size, info.size, info.size) * 0.5f, info.size),
172 { quint8(qRed(spannerColor)), quint8(qGreen(spannerColor)), quint8(qBlue(spannerColor)), spannerAlpha },
173 { quint8(qRed(spannerNormal)), quint8(qGreen(spannerNormal)), quint8(qBlue(spannerNormal)) } };
174 _points.append(point);
175
176 } else {
177 Point point = { glm::vec4(info.minimum + glm::vec3(info.size, info.size, info.size) * 0.5f, info.size),
178 { quint8(qRed(spannerColor)), quint8(qGreen(spannerColor)), quint8(qBlue(spannerColor)), spannerAlpha },
179 { quint8(qRed(spannerNormal)), quint8(qGreen(spannerNormal)), quint8(qBlue(spannerNormal)) } };
180 _points.append(point);
181 }
182 } else if (alpha > 0) {
183 Point point = { glm::vec4(info.minimum + glm::vec3(info.size, info.size, info.size) * 0.5f, info.size),
184 { quint8(qRed(color)), quint8(qGreen(color)), quint8(qBlue(color)), alpha },
185 { quint8(qRed(normal)), quint8(qGreen(normal)), quint8(qBlue(normal)) } };
186 _points.append(point);
187 }
188 }
189 return STOP_RECURSION;
190}
191
192MetavoxelSystem::RenderVisitor::RenderVisitor() :
193 SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(),
194 QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannerMaskAttribute()) {
195}
196
197bool MetavoxelSystem::RenderVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) {
198 spanner->getRenderer()->render(1.0f, SpannerRenderer::DEFAULT_MODE, clipMinimum, clipSize);
199 return true;
200}176}
201 177
202MetavoxelSystemClient::MetavoxelSystemClient(const SharedNodePointer& node, MetavoxelSystem* system) :178MetavoxelSystemClient::MetavoxelSystemClient(const SharedNodePointer& node, MetavoxelSystem* system) :
@@ -232,6 +208,9 @@ BufferBuilder::BufferBuilder(const MetavoxelLOD& lod) : · 29 unmodified lines
232 208
233const int ALPHA_RENDER_THRESHOLD = 0;209const int ALPHA_RENDER_THRESHOLD = 0;
234 210
211const int BUFFER_LEVELS = 5;
212const int LAST_BUFFER_LEVEL = BUFFER_LEVELS - 1;
213
235int BufferBuilder::visit(MetavoxelInfo& info) {214int BufferBuilder::visit(MetavoxelInfo& info) {
236 if (info.inputValues.at(2).getInlineValue<PointBufferPointer>()) {215 if (info.inputValues.at(2).getInlineValue<PointBufferPointer>()) {
237 info.outputValues[0] = AttributeValue(_outputs.at(0));216 info.outputValues[0] = AttributeValue(_outputs.at(0));
@@ -253,11 +232,9 @@ int BufferBuilder::visit(MetavoxelInfo& info) { · 15 unmodified lines
253 return STOP_RECURSION;232 return STOP_RECURSION;
254 }233 }
255 _depthPoints[_depth].second.append(point);234 _depthPoints[_depth].second.append(point);
256 return DEFAULT_ORDER;235 return DEFAULT_ORDER | ((_depth % BUFFER_LEVELS) == LAST_BUFFER_LEVEL ? 0 : ALL_NODES);
257}236}
258 237
259const int BUFFER_LEVELS = 5;
260
261bool BufferBuilder::postVisit(MetavoxelInfo& info) {238bool BufferBuilder::postVisit(MetavoxelInfo& info) {
262 if (_depth % BUFFER_LEVELS != 0) {239 if (_depth % BUFFER_LEVELS != 0) {
263 return false;240 return false;
@@ -305,7 +282,17 @@ bool BufferBuilder::postVisit(MetavoxelInfo& info) { · 41 unmodified lines
305 282
306void MetavoxelSystemClient::dataChanged(const MetavoxelData& oldData) {283void MetavoxelSystemClient::dataChanged(const MetavoxelData& oldData) {
307 BufferBuilder builder(_remoteDataLOD);284 BufferBuilder builder(_remoteDataLOD);
308 _data.guideToDifferent(oldData, builder);285 const AttributePointer& pointBufferAttribute = Application::getInstance()->getMetavoxels()->getPointBufferAttribute();
286 MetavoxelNode* oldRoot = oldData.getRoot(pointBufferAttribute);
287 if (oldRoot && oldData.getSize() == _data.getSize()) {
288 oldRoot->incrementReferenceCount();
289 _data.setRoot(pointBufferAttribute, oldRoot);
290 _data.guideToDifferent(oldData, builder);
291
292 } else {
293 _data.clear(pointBufferAttribute);
294 _data.guide(builder);
295 }
309}296}
310 297
311void MetavoxelSystemClient::sendDatagram(const QByteArray& data) {298void MetavoxelSystemClient::sendDatagram(const QByteArray& data) {
@@ -320,6 +307,19 @@ PointBuffer::PointBuffer(const QOpenGLBuffer& buffer, const QVector<int>& offset · 8 unmodified lines
320}307}
321 308
322void PointBuffer::render(int level) {309void PointBuffer::render(int level) {
310 int first, count;
311 int nextLevel = level + 1;
312 if (nextLevel >= _offsets.size()) {
313 first = _offsets.last() - _lastLeafCount;
314 count = _lastLeafCount;
315
316 } else {
317 first = _offsets.at(level);
318 count = _offsets.at(nextLevel) - first;
319 }
320 if (count == 0) {
321 return;
322 }
323 _buffer.bind();323 _buffer.bind();
324 324
325 BufferPoint* point = 0;325 BufferPoint* point = 0;
@@ -327,14 +327,8 @@ void PointBuffer::render(int level) { · 1 unmodified line
327 glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(BufferPoint), &point->color);327 glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(BufferPoint), &point->color);
328 glNormalPointer(GL_BYTE, sizeof(BufferPoint), &point->normal);328 glNormalPointer(GL_BYTE, sizeof(BufferPoint), &point->normal);
329 329
330 int nextLevel = level + 1;330 glDrawArrays(GL_POINTS, first, count);
331 if (nextLevel >= _offsets.size()) {331
332 glDrawArrays(GL_POINTS, _offsets.last() - _lastLeafCount, _lastLeafCount);
333
334 } else {
335 int first = _offsets.at(level);
336 glDrawArrays(GL_POINTS, first, _offsets.at(nextLevel) - first);
337 }
338 _buffer.release();332 _buffer.release();
339}333}
340 334