1976 lines · 73.7 KB · C++
RawDownload
////  Cube.cpp//  interface////  Created by Philip on 12/31/12.//  Copyright (c) 2012 High Fidelity, Inc. All rights reserved.// #ifdef _WIN32#define _timeval_#define _USE_MATH_DEFINES#endif #include <cstring>#include <cmath>#include <iostream> // to load voxels from file#include <fstream> // to load voxels from file#include <pthread.h> #include <OctalCode.h>#include <PacketHeaders.h>#include <PerfStat.h>#include <SharedUtil.h>#include <NodeList.h>#include <NodeTypes.h> #include "Application.h"#include "CoverageMap.h"#include "CoverageMapV2.h"#include "InterfaceConfig.h"#include "Menu.h"#include "renderer/ProgramObject.h"#include "VoxelConstants.h"#include "VoxelSystem.h" float identityVertices[] = { 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1,                             0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1,                             0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1 }; GLfloat identityNormals[] = { 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1,                              0,0,+1, 0,0,+1, 0,0,+1, 0,0,+1,                              0,-1,0, 0,-1,0, 0,+1,0, 0,+1,0,                              0,-1,0, 0,-1,0, 0,+1,0, 0,+1,0,                              -1,0,0, +1,0,0, +1,0,0, -1,0,0,                              -1,0,0, +1,0,0, +1,0,0, -1,0,0 }; GLubyte identityIndices[] = { 0,2,1,    0,3,2,    // Z-                              8,9,13,   8,13,12,  // Y-                              16,23,19, 16,20,23, // X-                              17,18,22, 17,22,21, // X+                              10,11,15, 10,15,14, // Y+                              4,5,6,    4,6,7 };  // Z+ VoxelSystem::VoxelSystem(float treeScale, int maxVoxels)    : NodeData(NULL),      _treeScale(treeScale),      _maxVoxels(maxVoxels),      _initialized(false) {     _voxelsInReadArrays = _voxelsInWriteArrays = _voxelsUpdated = 0;    _writeRenderFullVBO = true;    _readRenderFullVBO = true;    _tree = new VoxelTree();    pthread_mutex_init(&_bufferWriteLock, NULL);    pthread_mutex_init(&_treeLock, NULL);     VoxelNode::addDeleteHook(this);    _abandonedVBOSlots = 0;    _falseColorizeBySource = false;    _dataSourceID = UNKNOWN_NODE_ID;    _voxelServerCount = 0;     _viewFrustum = Application::getInstance()->getViewFrustum();     connect(_tree, SIGNAL(importSize(float,float,float)), SIGNAL(importSize(float,float,float)));    connect(_tree, SIGNAL(importProgress(int)), SIGNAL(importProgress(int)));     _useVoxelShader = false;    _useByteNormals = false;        _writeVoxelShaderData = NULL;    _readVoxelShaderData = NULL;     _readVerticesArray = NULL;    _writeVerticesArray = NULL;    _readColorsArray = NULL;    _writeColorsArray = NULL;    _writeVoxelDirtyArray = NULL;    _readVoxelDirtyArray = NULL; } void VoxelSystem::nodeDeleted(VoxelNode* node) {    if (node->isKnownBufferIndex() && (node->getVoxelSystem() == this)) {        freeBufferIndex(node->getBufferIndex());    }} // returns an available index, starts by reusing a previously freed index, but if there isn't one available// it will use the end of the VBO array and grow our accounting of that array.// and makes the index available for some other node to useglBufferIndex VoxelSystem::getNextBufferIndex() {    glBufferIndex output = GLBUFFER_INDEX_UNKNOWN;    // if there's a free index, use it...    if (_freeIndexes.size() > 0) {        output = _freeIndexes.back();        _freeIndexes.pop_back();    } else {        output = _voxelsInWriteArrays;        _voxelsInWriteArrays++;    }    return output;} // Doesn't actually clean up the VBOs for the index, but does release responsibility of the index from the VoxelNode, // and makes the index available for some other node to usevoid VoxelSystem::freeBufferIndex(glBufferIndex index) {    _freeIndexes.push_back(index);} // This will run through the list of _freeIndexes and reset their VBO array values to be "invisible".void VoxelSystem::clearFreeBufferIndexes() {    for (int i = 0; i < _freeIndexes.size(); i++) {        glBufferIndex nodeIndex = _freeIndexes[i];        glm::vec3 startVertex(FLT_MAX, FLT_MAX, FLT_MAX);        float voxelScale = 0;        _writeVoxelDirtyArray[nodeIndex] = true;        nodeColor color = {0, 0, 0, 0};        updateNodeInArrays(nodeIndex, startVertex, voxelScale, color);        _abandonedVBOSlots++;    }    _freeIndexes.clear();} VoxelSystem::~VoxelSystem() {    cleanupVoxelMemory();    delete _tree;    pthread_mutex_destroy(&_bufferWriteLock);    pthread_mutex_destroy(&_treeLock);     VoxelNode::removeDeleteHook(this);} void VoxelSystem::setUseByteNormals(bool useByteNormals) {    pthread_mutex_lock(&_bufferWriteLock);    bool wasInitialized = _initialized;    if (wasInitialized) {        cleanupVoxelMemory();    }    _useByteNormals = useByteNormals;    if (wasInitialized) {        init();    }    pthread_mutex_unlock(&_bufferWriteLock);     if (wasInitialized) {        forceRedrawEntireTree();    }}  void VoxelSystem::setMaxVoxels(int maxVoxels) {    pthread_mutex_lock(&_bufferWriteLock);    bool wasInitialized = _initialized;    if (wasInitialized) {        cleanupVoxelMemory();    }    _maxVoxels = maxVoxels;    if (wasInitialized) {        init();    }    pthread_mutex_unlock(&_bufferWriteLock);     if (wasInitialized) {        forceRedrawEntireTree();    }} void VoxelSystem::setUseVoxelShader(bool useVoxelShader) {    pthread_mutex_lock(&_bufferWriteLock);    bool wasInitialized = _initialized;    if (wasInitialized) {        clearAllNodesBufferIndex();        cleanupVoxelMemory();    }    _useVoxelShader = useVoxelShader;    if (wasInitialized) {        init();    }    pthread_mutex_unlock(&_bufferWriteLock);     if (wasInitialized) {        forceRedrawEntireTree();    }} void VoxelSystem::cleanupVoxelMemory() {    if (_initialized) {        if (_useVoxelShader) {            // these are used when in VoxelShader mode.            glDeleteBuffers(1, &_vboVoxelsID);            glDeleteBuffers(1, &_vboVoxelsIndicesID);             delete[] _writeVoxelShaderData;            delete[] _readVoxelShaderData;        } else {            // Destroy  glBuffers            glDeleteBuffers(1, &_vboVerticesID);            glDeleteBuffers(1, &_vboNormalsID);            glDeleteBuffers(1, &_vboColorsID);            glDeleteBuffers(1, &_vboIndicesID);                    delete[] _readVerticesArray;            delete[] _writeVerticesArray;            delete[] _readColorsArray;            delete[] _writeColorsArray;        }        delete[] _writeVoxelDirtyArray;        delete[] _readVoxelDirtyArray;    }    _initialized = false; // no longer initialized} void VoxelSystem::initVoxelMemory() {    _initialMemoryUsageGPU = getFreeMemoryGPU();    _memoryUsageRAM = 0;    _memoryUsageVBO = 0; // our VBO allocations as we know them    if (_useVoxelShader) {        qDebug("Using Voxel Shader...\n");        GLuint* indicesArray = new GLuint[_maxVoxels];         // populate the indicesArray        // this will not change given new voxels, so we can set it all up now        for (int n = 0; n < _maxVoxels; n++) {            indicesArray[n] = n;        }         // bind the indices VBO to the actual indices array        glGenBuffers(1, &_vboVoxelsIndicesID);        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboVoxelsIndicesID);        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * _maxVoxels, indicesArray, GL_STATIC_DRAW);        _memoryUsageVBO += sizeof(GLuint) * _maxVoxels;         glGenBuffers(1, &_vboVoxelsID);        glBindBuffer(GL_ARRAY_BUFFER, _vboVoxelsID);        glBufferData(GL_ARRAY_BUFFER, _maxVoxels * sizeof(VoxelShaderVBOData), NULL, GL_DYNAMIC_DRAW);        _memoryUsageVBO += _maxVoxels * sizeof(VoxelShaderVBOData);            // delete the indices and normals arrays that are no longer needed        delete[] indicesArray;         // we will track individual dirty sections with these arrays of bools        _writeVoxelDirtyArray = new bool[_maxVoxels];        memset(_writeVoxelDirtyArray, false, _maxVoxels * sizeof(bool));        _memoryUsageRAM += (_maxVoxels * sizeof(bool));         _readVoxelDirtyArray = new bool[_maxVoxels];        memset(_readVoxelDirtyArray, false, _maxVoxels * sizeof(bool));        _memoryUsageRAM += (_maxVoxels * sizeof(bool));         // prep the data structures for incoming voxel data        _writeVoxelShaderData = new VoxelShaderVBOData[_maxVoxels];        _memoryUsageRAM += (sizeof(VoxelShaderVBOData) * _maxVoxels);         _readVoxelShaderData = new VoxelShaderVBOData[_maxVoxels];        _memoryUsageRAM += (sizeof(VoxelShaderVBOData) * _maxVoxels);    } else {        GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels];         // populate the indicesArray        // this will not change given new voxels, so we can set it all up now        for (int n = 0; n < _maxVoxels; n++) {            // fill the indices array            int voxelIndexOffset = n * INDICES_PER_VOXEL;            GLuint* currentIndicesPos = indicesArray + voxelIndexOffset;            int startIndex = (n * VERTICES_PER_VOXEL);             for (int i = 0; i < INDICES_PER_VOXEL; i++) {                // add indices for this side of the cube                currentIndicesPos[i] = startIndex + identityIndices[i];            }        }         if (_useByteNormals) {            qDebug("Using Byte Normals...\n");            GLbyte* normalsArray = new GLbyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];            GLbyte* normalsArrayEndPointer = normalsArray;             // populate the normalsArray            for (int n = 0; n < _maxVoxels; n++) {                for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) {                    *(normalsArrayEndPointer++) = (identityNormals[i] * CHAR_MAX);                }            }             // VBO for the normalsArray            glGenBuffers(1, &_vboNormalsID);            glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);            glBufferData(GL_ARRAY_BUFFER,                         VERTEX_POINTS_PER_VOXEL * sizeof(GLbyte) * _maxVoxels,                         normalsArray, GL_STATIC_DRAW);            _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLbyte) * _maxVoxels;             // delete the indices and normals arrays that are no longer needed            delete[] normalsArray;        } else {            qDebug("Using Float Normals...\n");            GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];            GLfloat* normalsArrayEndPointer = normalsArray;             // populate the normalsArray            for (int n = 0; n < _maxVoxels; n++) {                for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) {                    *(normalsArrayEndPointer++) = identityNormals[i];                }            }                        // VBO for the normalsArray            glGenBuffers(1, &_vboNormalsID);            glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);            glBufferData(GL_ARRAY_BUFFER,                         VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels,                         normalsArray, GL_STATIC_DRAW);            _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels;             // delete the indices and normals arrays that are no longer needed            delete[] normalsArray;        }          glGenBuffers(1, &_vboVerticesID);        glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);        glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);        _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels;         // VBO for colorsArray        glGenBuffers(1, &_vboColorsID);        glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);        glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);        _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * _maxVoxels;         // VBO for the indicesArray        glGenBuffers(1, &_vboIndicesID);        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);        glBufferData(GL_ELEMENT_ARRAY_BUFFER,                     INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels,                     indicesArray, GL_STATIC_DRAW);        _memoryUsageVBO += INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels;         // delete the indices and normals arrays that are no longer needed        delete[] indicesArray;          // we will track individual dirty sections with these arrays of bools        _writeVoxelDirtyArray = new bool[_maxVoxels];        memset(_writeVoxelDirtyArray, false, _maxVoxels * sizeof(bool));        _memoryUsageRAM += (sizeof(bool) * _maxVoxels);         _readVoxelDirtyArray = new bool[_maxVoxels];        memset(_readVoxelDirtyArray, false, _maxVoxels * sizeof(bool));        _memoryUsageRAM += (sizeof(bool) * _maxVoxels);         // prep the data structures for incoming voxel data        _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];        _memoryUsageRAM += (sizeof(GLfloat) * VERTEX_POINTS_PER_VOXEL * _maxVoxels);        _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];        _memoryUsageRAM += (sizeof(GLfloat) * VERTEX_POINTS_PER_VOXEL * _maxVoxels);         _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];        _memoryUsageRAM += (sizeof(GLubyte) * VERTEX_POINTS_PER_VOXEL * _maxVoxels);        _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];        _memoryUsageRAM += (sizeof(GLubyte) * VERTEX_POINTS_PER_VOXEL * _maxVoxels);          // create our simple fragment shader if we're the first system to init        if (!_perlinModulateProgram.isLinked()) {            switchToResourcesParentIfRequired();            _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/perlin_modulate.vert");            _perlinModulateProgram.addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/perlin_modulate.frag");            _perlinModulateProgram.link();             _perlinModulateProgram.bind();            _perlinModulateProgram.setUniformValue("permutationNormalTexture", 0);            _perlinModulateProgram.release();        }    }} void VoxelSystem::loadVoxelsFile(const char* fileName, bool wantColorRandomizer) {    _tree->loadVoxelsFile(fileName, wantColorRandomizer);    setupNewVoxelsForDrawing();} void VoxelSystem::writeToSVOFile(const char* filename, VoxelNode* node) const {    _tree->writeToSVOFile(filename, node);} bool VoxelSystem::readFromSVOFile(const char* filename) {    bool result = _tree->readFromSVOFile(filename);    if (result) {        setupNewVoxelsForDrawing();    }    return result;} bool VoxelSystem::readFromSquareARGB32Pixels(const char *filename) {    bool result = _tree->readFromSquareARGB32Pixels(filename);    if (result) {        setupNewVoxelsForDrawing();    }    return result;} bool VoxelSystem::readFromSchematicFile(const char* filename) {    bool result = _tree->readFromSchematicFile(filename);    if (result) {        setupNewVoxelsForDrawing();    }    return result;} long int VoxelSystem::getVoxelsCreated() {    return _tree->voxelsCreated;} float VoxelSystem::getVoxelsCreatedPerSecondAverage() {    return (1 / _tree->voxelsCreatedStats.getEventDeltaAverage());} long int VoxelSystem::getVoxelsColored() {    return _tree->voxelsColored;} float VoxelSystem::getVoxelsColoredPerSecondAverage() {    return (1 / _tree->voxelsColoredStats.getEventDeltaAverage());} long int VoxelSystem::getVoxelsBytesRead() {    return _tree->voxelsBytesRead;} float VoxelSystem::getVoxelsBytesReadPerSecondAverage() {    return _tree->voxelsBytesReadStats.getAverageSampleValuePerSecond();} int VoxelSystem::parseData(unsigned char* sourceBuffer, int numBytes) {     unsigned char command = *sourceBuffer;    int numBytesPacketHeader = numBytesForPacketHeader(sourceBuffer);    unsigned char* voxelData = sourceBuffer + numBytesPacketHeader;     pthread_mutex_lock(&_treeLock);     switch(command) {        case PACKET_TYPE_VOXEL_DATA: {            PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),                                    "readBitstreamToTree()");            // ask the VoxelTree to read the bitstream into the tree            ReadBitstreamToTreeParams args(WANT_COLOR, WANT_EXISTS_BITS, NULL, getDataSourceID());            _tree->readBitstreamToTree(voxelData, numBytes - numBytesPacketHeader, args);        }            break;        case PACKET_TYPE_VOXEL_DATA_MONOCHROME: {            PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),                                    "readBitstreamToTree()");            // ask the VoxelTree to read the MONOCHROME bitstream into the tree            ReadBitstreamToTreeParams args(NO_COLOR, WANT_EXISTS_BITS, NULL, getDataSourceID());            _tree->readBitstreamToTree(voxelData, numBytes - numBytesPacketHeader, args);        }            break;        case PACKET_TYPE_Z_COMMAND:             // the Z command is a special command that allows the sender to send high level semantic            // requests, like erase all, or add sphere scene, different receivers may handle these            // messages differently            char* packetData = (char *)sourceBuffer;            char* command = &packetData[numBytesPacketHeader]; // start of the command            int commandLength = strlen(command); // commands are null terminated strings            int totalLength = 1+commandLength+1;             qDebug("got Z message len(%d)= %s\n", numBytes, command);             while (totalLength <= numBytes) {                if (0==strcmp(command,(char*)"erase all")) {                    qDebug("got Z message == erase all\n");                    _tree->eraseAllVoxels();                    _voxelsInReadArrays = _voxelsInWriteArrays = 0; // better way to do this??                }                if (0==strcmp(command,(char*)"add scene")) {                    qDebug("got Z message == add scene - NOT SUPPORTED ON INTERFACE\n");                }                totalLength += commandLength+1;            }        break;    }     setupNewVoxelsForDrawing();        pthread_mutex_unlock(&_treeLock); 

Lines 1–500 of 1976.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t