History

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).

ZappoMan committed 13 years ago

Commit 8ecc0d51 parent c9e16995 files · +99 −4Browse files

Showing the changes to interface/src/VoxelSystem.cpp (1 of 5 files). Show all

Unified · Split

interface/src/VoxelSystem.cpp+54−2
@@ -8,6 +8,8 @@ · 7 unmodified lines
88
99 #include <cstring>
1010 #include <cmath>
11+#include <iostream> // to load voxels from file
12+#include <fstream> // to load voxels from file
1113 #include <SharedUtil.h>
1214 #include <OctalCode.h>
1315 #include <AgentList.h>
@@ -46,6 +48,56 @@ VoxelSystem::~VoxelSystem() { · 32 unmodified lines
4648 delete tree;
4749 }
4850
51+//////////////////////////////////////////////////////////////////////////////////////////
52+// Method: VoxelSystem::loadVoxelsFile()
53+// Description: Loads HiFidelity encoded Voxels from a binary file. The current file
54+// format is a stream of single voxels with NO color data. Currently
55+// colors are set randomly
56+// Complaints: Brad :)
57+// To Do: Need to add color data to the file.
58+void VoxelSystem::loadVoxelsFile(char* fileName) {
59+
60+ std::ifstream file(fileName, std::ios::in|std::ios::binary);
61+
62+ char octets;
63+ unsigned int lengthInBytes;
64+
65+ int totalBytesRead = 0;
66+ if(file.is_open())
67+ {
68+ while (!file.eof()) {
69+ file.get(octets);
70+ totalBytesRead++;
71+ lengthInBytes = (octets*3/8)+1;
72+ unsigned char * voxelData = new unsigned char[lengthInBytes+1+3];
73+ voxelData[0]=octets;
74+ char byte;
75+
76+ for (size_t i = 0; i < lengthInBytes; i++) {
77+ file.get(byte);
78+ totalBytesRead++;
79+ voxelData[i+1] = byte;
80+ }
81+ // random color data
82+ voxelData[lengthInBytes+1] = randomColorValue(65);
83+ voxelData[lengthInBytes+2] = randomColorValue(65);
84+ voxelData[lengthInBytes+3] = randomColorValue(65);
85+
86+ tree->readCodeColorBufferToTree(voxelData);
87+ delete voxelData;
88+ }
89+ file.close();
90+ }
91+
92+ // reset the verticesEndPointer so we're writing to the beginning of the array
93+ verticesEndPointer = verticesArray;
94+ // call recursive function to populate in memory arrays
95+ // it will return the number of voxels added
96+ voxelsRendered = treeToArrays(tree->rootNode);
97+ // set the boolean if there are any voxels to be rendered so we re-fill the VBOs
98+ voxelsToRender = (voxelsRendered > 0);
99+}
100+
49101 void VoxelSystem::parseData(void *data, int size) {
50102 // output the bits received from the voxel server
51103 unsigned char *voxelData = (unsigned char *) data + 1;
@@ -68,7 +120,7 @@ void VoxelSystem::parseData(void *data, int size) { · 16 unmodified lines
68120
69121 int VoxelSystem::treeToArrays(VoxelNode *currentNode) {
70122 int voxelsAdded = 0;
71−
123+
72124 for (int i = 0; i < 8; i++) {
73125 // check if there is a child here
74126 if (currentNode->children[i] != NULL) {
@@ -95,7 +147,7 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode) { · 20 unmodified lines
95147
96148 delete [] startVertex;
97149 }
98−
150+
99151 return voxelsAdded;
100152 }
101153