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).
This commit is contained in:
ZappoMan 2013-03-23 21:59:27 -07:00
parent c9e16994f4
commit 8ecc0d53ad
5 changed files with 99 additions and 4 deletions

View file

@ -8,6 +8,8 @@
#include <cstring>
#include <cmath>
#include <iostream> // to load voxels from file
#include <fstream> // to load voxels from file
#include <SharedUtil.h>
#include <OctalCode.h>
#include <AgentList.h>
@ -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;
}

View file

@ -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;

View file

@ -53,6 +53,7 @@
#include "Oscilloscope.h"
#include "UDPSocket.h"
#include "SerialInterface.h"
#include <SharedUtil.h>
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);

View file

@ -73,4 +73,34 @@ void switchToResourcesIfRequired() {
chdir(path);
#endif
}
}
//////////////////////////////////////////////////////////////////////////////////////////
// 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;
}

View file

@ -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__) */