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
2929 ProgramObject MetavoxelSystem::_program;
3030 int MetavoxelSystem::_pointScaleLocation;
3131
32−MetavoxelSystem::MetavoxelSystem() :
33− _simulateVisitor(_points),
34− _buffer(QOpenGLBuffer::VertexBuffer) {
35−}
36−
3732 void MetavoxelSystem::init() {
3833 MetavoxelClientManager::init();
3934
@@ -41,10 +36,10 @@ void MetavoxelSystem::init() { · 1 unmodified line
4136 _program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/metavoxel_point.vert");
4237 _program.link();
4338
39+ _program.bind();
4440 _pointScaleLocation = _program.uniformLocation("pointScale");
41+ _program.release();
4542 }
46− _buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
47− _buffer.create();
4843 _pointBufferAttribute = AttributeRegistry::getInstance()->registerAttribute(new PointBufferAttribute());
4944 }
5045
@@ -55,21 +50,85 @@ MetavoxelLOD MetavoxelSystem::getLOD() const { · 4 unmodified lines
5550 BASE_LOD_THRESHOLD * Menu::getInstance()->getAvatarLODDistanceMultiplier());
5651 }
5752
53+class SpannerSimulateVisitor : public SpannerVisitor {
54+public:
55+
56+ SpannerSimulateVisitor(float deltaTime);
57+
58+ virtual bool visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize);
59+
60+private:
61+
62+ float _deltaTime;
63+};
64+
65+SpannerSimulateVisitor::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+
72+bool SpannerSimulateVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) {
73+ spanner->getRenderer()->simulate(_deltaTime);
74+ return true;
75+}
76+
5877 void MetavoxelSystem::simulate(float deltaTime) {
5978 // update the clients
60− _points.clear();
61− _simulateVisitor.setDeltaTime(deltaTime);
62− _simulateVisitor.setOrder(-Application::getInstance()->getViewFrustum()->getDirection());
6379 update();
6480
65− _buffer.bind();
66− int bytes = _points.size() * sizeof(Point);
67− if (_buffer.size() < bytes) {
68− _buffer.allocate(_points.constData(), bytes);
69− } else {
70− _buffer.write(0, _points.constData(), bytes);
81+ SpannerSimulateVisitor spannerSimulateVisitor(deltaTime);
82+ guide(spannerSimulateVisitor);
83+}
84+
85+class PointBufferRenderVisitor : public MetavoxelVisitor {
86+public:
87+
88+ PointBufferRenderVisitor();
89+
90+ virtual int visit(MetavoxelInfo& info);
91+
92+private:
93+
94+ int _order;
95+};
96+
97+PointBufferRenderVisitor::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+
103+int PointBufferRenderVisitor::visit(MetavoxelInfo& info) {
104+ PointBufferPointer buffer = info.inputValues.at(0).getInlineValue<PointBufferPointer>();
105+ if (buffer) {
106+ buffer->render(1000);
71107 }
72− _buffer.release();
108+ if (info.isLeaf) {
109+ return STOP_RECURSION;
110+ }
111+ return _order;
112+}
113+
114+class SpannerRenderVisitor : public SpannerVisitor {
115+public:
116+
117+ SpannerRenderVisitor();
118+
119+ virtual bool visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize);
120+};
121+
122+SpannerRenderVisitor::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+
129+bool SpannerRenderVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) {
130+ spanner->getRenderer()->render(1.0f, SpannerRenderer::DEFAULT_MODE, clipMinimum, clipSize);
131+ return true;
73132 }
74133
75134 void MetavoxelSystem::render() {
@@ -87,20 +146,14 @@ void MetavoxelSystem::render() { · 11 unmodified lines
87146 _program.setUniformValue(_pointScaleLocation, viewportDiagonal *
88147 Application::getInstance()->getViewFrustum()->getNearClip() / worldDiagonal);
89148
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−
97149 glEnableClientState(GL_VERTEX_ARRAY);
98150 glEnableClientState(GL_COLOR_ARRAY);
99151 glEnableClientState(GL_NORMAL_ARRAY);
100152
101153 glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
102154
103− glDrawArrays(GL_POINTS, 0, _points.size());
155+ PointBufferRenderVisitor pointBufferRenderVisitor;
156+ guide(pointBufferRenderVisitor);
104157
105158 glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
106159
@@ -108,19 +161,10 @@ void MetavoxelSystem::render() { · 1 unmodified line
108161 glDisableClientState(GL_COLOR_ARRAY);
109162 glDisableClientState(GL_NORMAL_ARRAY);
110163
111− _buffer.release();
112−
113164 _program.release();
114165
115− foreach (const SharedNodePointer& node, NodeList::getInstance()->getNodeHash()) {
116− if (node->getType() == NodeType::MetavoxelServer) {
117− QMutexLocker locker(&node->getMutex());
118− MetavoxelSystemClient* client = static_cast<MetavoxelSystemClient*>(node->getLinkedData());
119− if (client) {
120− client->guide(_renderVisitor);
121− }
122− }
123− }
166+ SpannerRenderVisitor spannerRenderVisitor;
167+ guide(spannerRenderVisitor);
124168 }
125169
126170 MetavoxelClient* MetavoxelSystem::createClient(const SharedNodePointer& node) {
@@ -129,74 +173,6 @@ MetavoxelClient* MetavoxelSystem::createClient(const SharedNodePointer& node) { · 2 unmodified lines
129173
130174 void MetavoxelSystem::updateClient(MetavoxelClient* client) {
131175 MetavoxelClientManager::updateClient(client);
132− client->guide(_simulateVisitor);
133−}
134−
135−MetavoxelSystem::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−
144−bool MetavoxelSystem::SimulateVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) {
145− spanner->getRenderer()->simulate(_deltaTime);
146− return true;
147−}
148−
149−int 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−
192−MetavoxelSystem::RenderVisitor::RenderVisitor() :
193− SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(),
194− QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannerMaskAttribute()) {
195−}
196−
197−bool 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;
200176 }
201177
202178 MetavoxelSystemClient::MetavoxelSystemClient(const SharedNodePointer& node, MetavoxelSystem* system) :
@@ -232,6 +208,9 @@ BufferBuilder::BufferBuilder(const MetavoxelLOD& lod) : · 29 unmodified lines
232208
233209 const int ALPHA_RENDER_THRESHOLD = 0;
234210
211+const int BUFFER_LEVELS = 5;
212+const int LAST_BUFFER_LEVEL = BUFFER_LEVELS - 1;
213+
235214 int BufferBuilder::visit(MetavoxelInfo& info) {
236215 if (info.inputValues.at(2).getInlineValue<PointBufferPointer>()) {
237216 info.outputValues[0] = AttributeValue(_outputs.at(0));
@@ -253,11 +232,9 @@ int BufferBuilder::visit(MetavoxelInfo& info) { · 15 unmodified lines
253232 return STOP_RECURSION;
254233 }
255234 _depthPoints[_depth].second.append(point);
256− return DEFAULT_ORDER;
235+ return DEFAULT_ORDER | ((_depth % BUFFER_LEVELS) == LAST_BUFFER_LEVEL ? 0 : ALL_NODES);
257236 }
258237
259−const int BUFFER_LEVELS = 5;
260−
261238 bool BufferBuilder::postVisit(MetavoxelInfo& info) {
262239 if (_depth % BUFFER_LEVELS != 0) {
263240 return false;
@@ -305,7 +282,17 @@ bool BufferBuilder::postVisit(MetavoxelInfo& info) { · 41 unmodified lines
305282
306283 void MetavoxelSystemClient::dataChanged(const MetavoxelData& oldData) {
307284 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+ }
309296 }
310297
311298 void MetavoxelSystemClient::sendDatagram(const QByteArray& data) {
@@ -320,6 +307,19 @@ PointBuffer::PointBuffer(const QOpenGLBuffer& buffer, const QVector<int>& offset · 8 unmodified lines
320307 }
321308
322309 void 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+ }
323323 _buffer.bind();
324324
325325 BufferPoint* point = 0;
@@ -327,14 +327,8 @@ void PointBuffer::render(int level) { · 1 unmodified line
327327 glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(BufferPoint), &point->color);
328328 glNormalPointer(GL_BYTE, sizeof(BufferPoint), &point->normal);
329329
330− int nextLevel = level + 1;
331− if (nextLevel >= _offsets.size()) {
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− }
330+ glDrawArrays(GL_POINTS, first, count);
331+
338332 _buffer.release();
339333 }
340334