Showing the changes to interface/src/MetavoxelSystem.cpp (1 of 13 files). Show all
Unified · Split
| @@ -0,0 +1,202 @@ | |||
| 1 | // | ||
| 2 | // MetavoxelSystem.cpp | ||
| 3 | // interface | ||
| 4 | // | ||
| 5 | // Created by Andrzej Kapolka on 12/10/13. | ||
| 6 | // Copyright (c) 2013 High Fidelity, Inc. All rights reserved. | ||
| 7 | // | ||
| 8 | |||
| 9 | #include <QFile> | ||
| 10 | #include <QTextStream> | ||
| 11 | #include <QtDebug> | ||
| 12 | |||
| 13 | #include <SharedUtil.h> | ||
| 14 | |||
| 15 | #include <MetavoxelUtil.h> | ||
| 16 | |||
| 17 | #include "Application.h" | ||
| 18 | #include "MetavoxelSystem.h" | ||
| 19 | |||
| 20 | ProgramObject MetavoxelSystem::_program; | ||
| 21 | int MetavoxelSystem::_pointScaleLocation; | ||
| 22 | |||
| 23 | MetavoxelSystem::MetavoxelSystem() : | ||
| 24 | _pointVisitor(_points), | ||
| 25 | _buffer(QOpenGLBuffer::VertexBuffer) { | ||
| 26 | } | ||
| 27 | |||
| 28 | MetavoxelSystem::~MetavoxelSystem() { | ||
| 29 | NodeList::getInstance()->removeHook(this); | ||
| 30 | } | ||
| 31 | |||
| 32 | void MetavoxelSystem::init() { | ||
| 33 | if (!_program.isLinked()) { | ||
| 34 | switchToResourcesParentIfRequired(); | ||
| 35 | _program.addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/metavoxel_point.vert"); | ||
| 36 | _program.link(); | ||
| 37 | |||
| 38 | _pointScaleLocation = _program.uniformLocation("pointScale"); | ||
| 39 | } | ||
| 40 | |||
| 41 | NodeList::getInstance()->addHook(this); | ||
| 42 | |||
| 43 | AttributeRegistry::getInstance()->configureScriptEngine(&_scriptEngine); | ||
| 44 | |||
| 45 | QFile scriptFile("resources/scripts/sphere.js"); | ||
| 46 | scriptFile.open(QIODevice::ReadOnly); | ||
| 47 | QScriptValue guideFunction = _scriptEngine.evaluate(QTextStream(&scriptFile).readAll()); | ||
| 48 | _data.setAttributeValue(MetavoxelPath(), AttributeValue(AttributeRegistry::getInstance()->getGuideAttribute(), | ||
| 49 | encodeInline(PolymorphicDataPointer(new ScriptedMetavoxelGuide(guideFunction))))); | ||
| 50 | |||
| 51 | _buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw); | ||
| 52 | _buffer.create(); | ||
| 53 | } | ||
| 54 | |||
| 55 | void MetavoxelSystem::processData(const QByteArray& data, const HifiSockAddr& sender) { | ||
| 56 | QMetaObject::invokeMethod(this, "receivedData", Q_ARG(const QByteArray&, data), Q_ARG(const HifiSockAddr&, sender)); | ||
| 57 | } | ||
| 58 | |||
| 59 | void MetavoxelSystem::simulate(float deltaTime) { | ||
| 60 | _points.clear(); | ||
| 61 | _data.guide(_pointVisitor); | ||
| 62 | |||
| 63 | _buffer.bind(); | ||
| 64 | int bytes = _points.size() * sizeof(Point); | ||
| 65 | if (_buffer.size() < bytes) { | ||
| 66 | _buffer.allocate(_points.constData(), bytes); | ||
| 67 | } else { | ||
| 68 | _buffer.write(0, _points.constData(), bytes); | ||
| 69 | } | ||
| 70 | _buffer.release(); | ||
| 71 | } | ||
| 72 | |||
| 73 | void MetavoxelSystem::render() { | ||
| 74 | int viewport[4]; | ||
| 75 | glGetIntegerv(GL_VIEWPORT, viewport); | ||
| 76 | const int VIEWPORT_WIDTH_INDEX = 2; | ||
| 77 | const int VIEWPORT_HEIGHT_INDEX = 3; | ||
| 78 | float viewportWidth = viewport[VIEWPORT_WIDTH_INDEX]; | ||
| 79 | float viewportHeight = viewport[VIEWPORT_HEIGHT_INDEX]; | ||
| 80 | float viewportDiagonal = sqrtf(viewportWidth*viewportWidth + viewportHeight*viewportHeight); | ||
| 81 | float worldDiagonal = glm::distance(Application::getInstance()->getViewFrustum()->getNearBottomLeft(), | ||
| 82 | Application::getInstance()->getViewFrustum()->getNearTopRight()); | ||
| 83 | |||
| 84 | _program.bind(); | ||
| 85 | _program.setUniformValue(_pointScaleLocation, viewportDiagonal * | ||
| 86 | Application::getInstance()->getViewFrustum()->getNearClip() / worldDiagonal); | ||
| 87 | |||
| 88 | _buffer.bind(); | ||
| 89 | |||
| 90 | Point* pt = 0; | ||
| 91 | glVertexPointer(4, GL_FLOAT, sizeof(Point), &pt->vertex); | ||
| 92 | glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Point), &pt->color); | ||
| 93 | glNormalPointer(GL_BYTE, sizeof(Point), &pt->normal); | ||
| 94 | |||
| 95 | glEnableClientState(GL_VERTEX_ARRAY); | ||
| 96 | glEnableClientState(GL_COLOR_ARRAY); | ||
| 97 | glEnableClientState(GL_NORMAL_ARRAY); | ||
| 98 | |||
| 99 | glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB); | ||
| 100 | |||
| 101 | glDrawArrays(GL_POINTS, 0, _points.size()); | ||
| 102 | |||
| 103 | glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB); | ||
| 104 | |||
| 105 | glDisableClientState(GL_VERTEX_ARRAY); | ||
| 106 | glDisableClientState(GL_COLOR_ARRAY); | ||
| 107 | glDisableClientState(GL_NORMAL_ARRAY); | ||
| 108 | |||
| 109 | _buffer.release(); | ||
| 110 | |||
| 111 | _program.release(); | ||
| 112 | } | ||
| 113 | |||
| 114 | void MetavoxelSystem::nodeAdded(Node* node) { | ||
| 115 | if (node->getType() == NODE_TYPE_METAVOXEL_SERVER) { | ||
| 116 | QMetaObject::invokeMethod(this, "addClient", Q_ARG(const QUuid&, node->getUUID()), | ||
| 117 | Q_ARG(const HifiSockAddr&, node->getPublicSocket())); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | void MetavoxelSystem::nodeKilled(Node* node) { | ||
| 122 | if (node->getType() == NODE_TYPE_METAVOXEL_SERVER) { | ||
| 123 | QMetaObject::invokeMethod(this, "removeClient", Q_ARG(const QUuid&, node->getUUID())); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | void MetavoxelSystem::addClient(const QUuid& uuid, const HifiSockAddr& address) { | ||
| 128 | MetavoxelClient* client = new MetavoxelClient(address); | ||
| 129 | _clients.insert(uuid, client); | ||
| 130 | _clientsBySessionID.insert(client->getSessionID(), client); | ||
| 131 | } | ||
| 132 | |||
| 133 | void MetavoxelSystem::removeClient(const QUuid& uuid) { | ||
| 134 | MetavoxelClient* client = _clients.take(uuid); | ||
| 135 | _clientsBySessionID.remove(client->getSessionID()); | ||
| 136 | delete client; | ||
| 137 | } | ||
| 138 | |||
| 139 | void MetavoxelSystem::receivedData(const QByteArray& data, const HifiSockAddr& sender) { | ||
| 140 | int headerPlusIDSize; | ||
| 141 | QUuid sessionID = readSessionID(data, sender, headerPlusIDSize); | ||
| 142 | if (sessionID.isNull()) { | ||
| 143 | return; | ||
| 144 | } | ||
| 145 | MetavoxelClient* client = _clientsBySessionID.value(sessionID); | ||
| 146 | if (client) { | ||
| 147 | client->receivedData(data, sender); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | MetavoxelSystem::PointVisitor::PointVisitor(QVector<Point>& points) : | ||
| 152 | MetavoxelVisitor(QVector<AttributePointer>() << | ||
| 153 | AttributeRegistry::getInstance()->getColorAttribute() << | ||
| 154 | AttributeRegistry::getInstance()->getNormalAttribute()), | ||
| 155 | _points(points) { | ||
| 156 | } | ||
| 157 | |||
| 158 | bool MetavoxelSystem::PointVisitor::visit(const MetavoxelInfo& info) { | ||
| 159 | if (!info.isLeaf) { | ||
| 160 | return true; | ||
| 161 | } | ||
| 162 | QRgb color = info.attributeValues.at(0).getInlineValue<QRgb>(); | ||
| 163 | QRgb normal = info.attributeValues.at(1).getInlineValue<QRgb>(); | ||
| 164 | int alpha = qAlpha(color); | ||
| 165 | if (alpha > 0) { | ||
| 166 | Point point = { glm::vec4(info.minimum + glm::vec3(info.size, info.size, info.size) * 0.5f, info.size), | ||
| 167 | { qRed(color), qGreen(color), qBlue(color), alpha }, { qRed(normal), qGreen(normal), qBlue(normal) } }; | ||
| 168 | _points.append(point); | ||
| 169 | } | ||
| 170 | return false; | ||
| 171 | } | ||
| 172 | |||
| 173 | static QByteArray createDatagramHeader(const QUuid& sessionID) { | ||
| 174 | QByteArray header(MAX_PACKET_HEADER_BYTES, 0); | ||
| 175 | populateTypeAndVersion(reinterpret_cast<unsigned char*>(header.data()), PACKET_TYPE_METAVOXEL_DATA); | ||
| 176 | header += sessionID.toRfc4122(); | ||
| 177 | return header; | ||
| 178 | } | ||
| 179 | |||
| 180 | MetavoxelClient::MetavoxelClient(const HifiSockAddr& address) : | ||
| 181 | _address(address), | ||
| 182 | _sessionID(QUuid::createUuid()), | ||
| 183 | _sequencer(createDatagramHeader(_sessionID)) { | ||
| 184 | |||
| 185 | connect(&_sequencer, SIGNAL(readyToWrite(const QByteArray&)), SLOT(sendData(const QByteArray&))); | ||
| 186 | connect(&_sequencer, SIGNAL(readyToRead(Bitstream&)), SLOT(readPacket(Bitstream&))); | ||
| 187 | } | ||
| 188 | |||
| 189 | void MetavoxelClient::receivedData(const QByteArray& data, const HifiSockAddr& sender) { | ||
| 190 | // save the most recent sender | ||
| 191 | _address = sender; | ||
| 192 | |||
| 193 | // process through sequencer | ||
| 194 | _sequencer.receivedDatagram(data); | ||
| 195 | } | ||
| 196 | |||
| 197 | void MetavoxelClient::sendData(const QByteArray& data) { | ||
| 198 | NodeList::getInstance()->getNodeSocket().writeDatagram(data, _address.getAddress(), _address.getPort()); | ||
| 199 | } | ||
| 200 | |||
| 201 | void MetavoxelClient::readPacket(Bitstream& in) { | ||
| 202 | } | ||