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
8 8
9#include <cstring>9#include <cstring>
10#include <cmath>10#include <cmath>
11#include <iostream> // to load voxels from file
12#include <fstream> // to load voxels from file
11#include <SharedUtil.h>13#include <SharedUtil.h>
12#include <OctalCode.h>14#include <OctalCode.h>
13#include <AgentList.h>15#include <AgentList.h>
@@ -46,6 +48,56 @@ VoxelSystem::~VoxelSystem() { · 32 unmodified lines
46 delete tree;48 delete tree;
47}49}
48 50
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.
58void 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
49void VoxelSystem::parseData(void *data, int size) {101void VoxelSystem::parseData(void *data, int size) {
50 // output the bits received from the voxel server102 // output the bits received from the voxel server
51 unsigned char *voxelData = (unsigned char *) data + 1;103 unsigned char *voxelData = (unsigned char *) data + 1;
@@ -68,7 +120,7 @@ void VoxelSystem::parseData(void *data, int size) { · 16 unmodified lines
68 120
69int VoxelSystem::treeToArrays(VoxelNode *currentNode) {121int VoxelSystem::treeToArrays(VoxelNode *currentNode) {
70 int voxelsAdded = 0;122 int voxelsAdded = 0;
71 123
72 for (int i = 0; i < 8; i++) {124 for (int i = 0; i < 8; i++) {
73 // check if there is a child here125 // check if there is a child here
74 if (currentNode->children[i] != NULL) {126 if (currentNode->children[i] != NULL) {
@@ -95,7 +147,7 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode) { · 20 unmodified lines
95 147
96 delete [] startVertex;148 delete [] startVertex;
97 }149 }
98 150
99 return voxelsAdded;151 return voxelsAdded;
100}152}
101 153