From 8ecc0d53adba5e0c0c9035b3607b790ec723b336 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Sat, 23 Mar 2013 21:59:27 -0700 Subject: [PATCH] Added a utility to VoxelSystem for loading voxels from a local file Also added a couple shared utility functions for reading command line options (since that's how you can load a local file at this point). --- interface/src/VoxelSystem.cpp | 56 +++++++++++++++++++++++++++++++++-- interface/src/VoxelSystem.h | 2 ++ interface/src/main.cpp | 10 ++++++- shared/src/SharedUtil.cpp | 32 +++++++++++++++++++- shared/src/SharedUtil.h | 3 ++ 5 files changed, 99 insertions(+), 4 deletions(-) diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index ebb29a8e5f..4aaee09ed7 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -8,6 +8,8 @@ #include #include +#include // to load voxels from file +#include // to load voxels from file #include #include #include @@ -46,6 +48,56 @@ VoxelSystem::~VoxelSystem() { delete tree; } +////////////////////////////////////////////////////////////////////////////////////////// +// Method: VoxelSystem::loadVoxelsFile() +// Description: Loads HiFidelity encoded Voxels from a binary file. The current file +// format is a stream of single voxels with NO color data. Currently +// colors are set randomly +// Complaints: Brad :) +// To Do: Need to add color data to the file. +void VoxelSystem::loadVoxelsFile(char* fileName) { + + std::ifstream file(fileName, std::ios::in|std::ios::binary); + + char octets; + unsigned int lengthInBytes; + + int totalBytesRead = 0; + if(file.is_open()) + { + while (!file.eof()) { + file.get(octets); + totalBytesRead++; + lengthInBytes = (octets*3/8)+1; + unsigned char * voxelData = new unsigned char[lengthInBytes+1+3]; + voxelData[0]=octets; + char byte; + + for (size_t i = 0; i < lengthInBytes; i++) { + file.get(byte); + totalBytesRead++; + voxelData[i+1] = byte; + } + // random color data + voxelData[lengthInBytes+1] = randomColorValue(65); + voxelData[lengthInBytes+2] = randomColorValue(65); + voxelData[lengthInBytes+3] = randomColorValue(65); + + tree->readCodeColorBufferToTree(voxelData); + delete voxelData; + } + file.close(); + } + + // reset the verticesEndPointer so we're writing to the beginning of the array + verticesEndPointer = verticesArray; + // call recursive function to populate in memory arrays + // it will return the number of voxels added + voxelsRendered = treeToArrays(tree->rootNode); + // set the boolean if there are any voxels to be rendered so we re-fill the VBOs + voxelsToRender = (voxelsRendered > 0); +} + void VoxelSystem::parseData(void *data, int size) { // output the bits received from the voxel server unsigned char *voxelData = (unsigned char *) data + 1; @@ -68,7 +120,7 @@ void VoxelSystem::parseData(void *data, int size) { int VoxelSystem::treeToArrays(VoxelNode *currentNode) { int voxelsAdded = 0; - + for (int i = 0; i < 8; i++) { // check if there is a child here if (currentNode->children[i] != NULL) { @@ -95,7 +147,7 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode) { delete [] startVertex; } - + return voxelsAdded; } diff --git a/interface/src/VoxelSystem.h b/interface/src/VoxelSystem.h index e13ca27b3e..adb73808de 100644 --- a/interface/src/VoxelSystem.h +++ b/interface/src/VoxelSystem.h @@ -33,6 +33,8 @@ public: void render(); void setVoxelsRendered(int v) {voxelsRendered = v;}; int getVoxelsRendered() {return voxelsRendered;}; + void loadVoxelsFile(char* fileName); + private: int voxelsRendered; VoxelTree *tree; diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 5759aa3c8a..e933169e1d 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -53,6 +53,7 @@ #include "Oscilloscope.h" #include "UDPSocket.h" #include "SerialInterface.h" +#include using namespace std; @@ -324,7 +325,6 @@ void initDisplay(void) void init(void) { voxels.init(); - myHead.setRenderYaw(start_yaw); head_mouse_x = WIDTH/2; @@ -969,6 +969,14 @@ int main(int argc, char** argv) printf( "Initialized Display.\n" ); init(); + + // Check to see if the user passed in a command line option for loading a local + // Voxel File. If so, load it now. + char* voxelsFilename = getCmdOption(argv, argv + argc, "-i"); + if (voxelsFilename) + { + voxels.loadVoxelsFile(voxelsFilename); + } // create thread for receipt of data via UDP pthread_create(&networkReceiveThread, NULL, networkReceive, NULL); diff --git a/shared/src/SharedUtil.cpp b/shared/src/SharedUtil.cpp index 740e6f6cfb..3340241fdd 100644 --- a/shared/src/SharedUtil.cpp +++ b/shared/src/SharedUtil.cpp @@ -73,4 +73,34 @@ void switchToResourcesIfRequired() { chdir(path); #endif -} \ No newline at end of file +} + +////////////////////////////////////////////////////////////////////////////////////////// +// Function: getCmdOption() +// Description: Handy little function to tell you if a command line flag and option was +// included while launching the application, and to get the option value +// immediately following the flag. For example if you ran: +// ./app -i filename.txt +// then you're using the "-i" flag to set the input file name. +// Usage: char * inputFilename = getCmdOption(argv, argv + argc, "-i"); +// Complaints: Brad :) +char* getCmdOption(char ** begin, char ** end, const std::string & option) +{ + char ** itr = std::find(begin, end, option); + if (itr != end && ++itr != end) + { + return *itr; + } + return 0; +} + +////////////////////////////////////////////////////////////////////////////////////////// +// Function: getCmdOption() +// Description: Handy little function to tell you if a command line option flag was +// included while launching the application. Returns bool true/false +// Usage: bool wantDump = cmdOptionExists(argv, argv+argc, "-d"); +// Complaints: Brad :) +bool cmdOptionExists(char** begin, char** end, const std::string& option) +{ + return std::find(begin, end, option) != end; +} diff --git a/shared/src/SharedUtil.h b/shared/src/SharedUtil.h index 3c53721deb..1424fa77a5 100644 --- a/shared/src/SharedUtil.h +++ b/shared/src/SharedUtil.h @@ -31,4 +31,7 @@ bool oneAtBit(unsigned char byte, int bitIndex); void switchToResourcesIfRequired(); +char* getCmdOption(char ** begin, char ** end, const std::string& option); +bool cmdOptionExists(char** begin, char** end, const std::string& option); + #endif /* defined(__hifi__SharedUtil__) */