fix to LOD bug caused by flip-flopped X and Z, add color randomization to inserted voxels on server, added head position display to display_stats()

This commit is contained in:
ZappoMan 2013-04-04 20:50:51 -07:00
parent b017f9a454
commit 57829785eb
4 changed files with 66 additions and 14 deletions

View file

@ -133,12 +133,35 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode, float nodePosition[3]) {
float halfUnitForVoxel = powf(0.5, *currentNode->octalCode) * (0.5 * TREE_SCALE);
glm::vec3 viewerPosition = viewerHead->getPos();
// XXXBHG - Note: It appears as if the X and Z coordinates of Head or Agent are flip-flopped relative to the
// coords of the voxel space. This flip flop causes LOD behavior to be extremely odd. This is my temporary hack
// to fix this behavior. To disable this swap, set swapXandZ to false.
bool swapXandZ=true;
float viewerX = swapXandZ ? viewerPosition[2] : viewerPosition[0];
float viewerZ = swapXandZ ? viewerPosition[0] : viewerPosition[2];
// debugging code.
//printf("treeToArrays() halfUnitForVoxel=%f\n",halfUnitForVoxel);
//printf("treeToArrays() viewerPosition {x,y,z or [0],[1],[2]} ={%f,%f,%f}\n",
// viewerPosition[0],viewerPosition[1],viewerPosition[2]);
//printf("treeToArrays() nodePosition {x,y,z or [0],[1],[2]} = {%f,%f,%f}\n",
// nodePosition[0],nodePosition[1],nodePosition[2]);
//float* vertices = firstVertexForCode(currentNode->octalCode);
//printf("treeToArrays() firstVerticesForCode(currentNode->octalCode)={x,y,z or [0],[1],[2]} = {%f,%f,%f}\n",
// vertices[0],vertices[1],vertices[2]);
//delete []vertices;
float distanceToVoxelCenter = sqrtf(powf(viewerPosition[0] - nodePosition[0] - halfUnitForVoxel, 2) +
float distanceToVoxelCenter = sqrtf(powf(viewerX - nodePosition[0] - halfUnitForVoxel, 2) +
powf(viewerPosition[1] - nodePosition[1] - halfUnitForVoxel, 2) +
powf(viewerPosition[2] - nodePosition[2] - halfUnitForVoxel, 2));
if (distanceToVoxelCenter < boundaryDistanceForRenderLevel(*currentNode->octalCode + 1)) {
powf(viewerZ - nodePosition[2] - halfUnitForVoxel, 2));
int boundaryPosition = boundaryDistanceForRenderLevel(*currentNode->octalCode + 1);
//printf("treeToArrays() distanceToVoxelCenter=%f boundaryPosition=%d\n",distanceToVoxelCenter,boundaryPosition);
bool alwaysDraw = false; // XXXBHG - temporary debug code. Flip this to true to disable LOD blurring
if (alwaysDraw || distanceToVoxelCenter < boundaryPosition) {
for (int i = 0; i < 8; i++) {
// check if there is a child here
if (currentNode->children[i] != NULL) {
@ -155,8 +178,6 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode, float nodePosition[3]) {
childNodePosition[j] -= (powf(0.5, *currentNode->children[i]->octalCode) * TREE_SCALE);
}
}
voxelsAdded += treeToArrays(currentNode->children[i], childNodePosition);
}
}

View file

@ -98,6 +98,7 @@ Head myHead; // The rendered head of oneself
char starFile[] = "https://s3-us-west-1.amazonaws.com/highfidelity/stars.txt";
FieldOfView fov;
Stars stars;
#ifdef STARFIELD_KEYS
int starsTiles = 20;
@ -255,10 +256,12 @@ void display_stats(void)
char legend2[] = "* - toggle stars, & - toggle paint mode";
drawtext(10, 32, 0.10f, 0, 1.0, 0, legend2);
glm::vec3 headPos = myHead.getPos();
char stats[200];
sprintf(stats, "FPS = %3.0f Pkts/s = %d Bytes/s = %d ",
FPS, packets_per_second, bytes_per_second);
sprintf(stats, "FPS = %3.0f Pkts/s = %d Bytes/s = %d Head(x,y,z)=( %f , %f , %f )",
FPS, packets_per_second, bytes_per_second, headPos.x,headPos.y,headPos.z);
drawtext(10, 49, 0.10f, 0, 1.0, 0, stats);
if (serialPort.active) {
sprintf(stats, "ADC samples = %d, LED = %d",
@ -490,7 +493,7 @@ void simulateHead(float frametime)
char broadcast_string[MAX_BROADCAST_STRING];
int broadcast_bytes = myHead.getBroadcastData(broadcast_string);
agentList.broadcastToAgents(broadcast_string, broadcast_bytes,AgentList::AGENTS_OF_TYPE_VOXEL_AND_INTERFACE);
// If I'm in paint mode, send a voxel out to VOXEL server agents.
if (::paintOn) {

View file

@ -255,10 +255,17 @@ unsigned char * VoxelTree::loadBitstreamBuffer(unsigned char *& bitstreamBuffer,
unsigned char * childMaskPointer = NULL;
float halfUnitForVoxel = powf(0.5, *currentVoxelNode->octalCode) * (0.5 * TREE_SCALE);
// XXXBHG - Note: It appears as if the X and Z coordinates of Head or Agent are flip-flopped relative to the
// coords of the voxel space. This flip flop causes LOD behavior to be extremely odd. This is my temporary hack
// to fix this behavior. To disable this swap, set swapXandZ to false.
bool swapXandZ=true;
float agentX = swapXandZ ? agentPosition[2] : agentPosition[0];
float agentZ = swapXandZ ? agentPosition[0] : agentPosition[2];
float distanceToVoxelCenter = sqrtf(powf(agentPosition[0] - thisNodePosition[0] - halfUnitForVoxel, 2) +
float distanceToVoxelCenter = sqrtf(powf(agentX - thisNodePosition[0] - halfUnitForVoxel, 2) +
powf(agentPosition[1] - thisNodePosition[1] - halfUnitForVoxel, 2) +
powf(agentPosition[2] - thisNodePosition[2] - halfUnitForVoxel, 2));
powf(agentZ - thisNodePosition[2] - halfUnitForVoxel, 2));
// if the distance to this voxel's center is less than the threshold
// distance for its children, we should send the children

View file

@ -47,6 +47,8 @@ const int MAX_VOXEL_TREE_DEPTH_LEVELS = 4;
AgentList agentList('V', VOXEL_LISTEN_PORT);
VoxelTree randomTree;
bool wantColorRandomizer = false;
void addSphere(VoxelTree * tree,bool random, bool wantColorRandomizer) {
float r = random ? randFloatInRange(0.05,0.1) : 0.25;
float xc = random ? randFloatInRange(r,(1-r)) : 0.5;
@ -229,7 +231,7 @@ int main(int argc, const char * argv[])
// Voxel File. If so, load it now.
const char* WANT_COLOR_RANDOMIZER="--WantColorRandomizer";
const char* INPUT_FILE="-i";
bool wantColorRandomizer = cmdOptionExists(argc, argv, WANT_COLOR_RANDOMIZER);
::wantColorRandomizer = cmdOptionExists(argc, argv, WANT_COLOR_RANDOMIZER);
printf("wantColorRandomizer=%s\n",(wantColorRandomizer?"yes":"no"));
const char* voxelsFilename = getCmdOption(argc, argv, INPUT_FILE);
@ -273,19 +275,38 @@ int main(int argc, const char * argv[])
// XXXBHG: Hacked in support for 'I' insert command
if (packetData[0] == 'I') {
unsigned short int itemNumber = (*((unsigned short int*)&packetData[1]));
printf("got I - insert voxels - command from client receivedBytes=%ld itemNumber=%d\n",receivedBytes,itemNumber);
printf("got I - insert voxels - command from client receivedBytes=%ld itemNumber=%d\n",
receivedBytes,itemNumber);
int atByte = 3;
unsigned char* pVoxelData = (unsigned char*)&packetData[3];
while (atByte < receivedBytes) {
unsigned char octets = (unsigned char)*pVoxelData;
int voxelDataSize = bytesRequiredForCodeLength(octets)+3; // 3 for color!
int voxelCodeSize = bytesRequiredForCodeLength(octets);
// color randomization on insert
int colorRandomizer = ::wantColorRandomizer ? randIntInRange (-50, 50) : 0;
int red = pVoxelData[voxelCodeSize+0];
int green = pVoxelData[voxelCodeSize+1];
int blue = pVoxelData[voxelCodeSize+2];
printf("insert voxels - wantColorRandomizer=%s old r=%d,g=%d,b=%d \n",
(::wantColorRandomizer?"yes":"no"),red,green,blue);
red = std::max(0,std::min(255,red + colorRandomizer));
green = std::max(0,std::min(255,green + colorRandomizer));
blue = std::max(0,std::min(255,blue + colorRandomizer));
printf("insert voxels - wantColorRandomizer=%s NEW r=%d,g=%d,b=%d \n",
(::wantColorRandomizer?"yes":"no"),red,green,blue);
pVoxelData[voxelCodeSize+0]=red;
pVoxelData[voxelCodeSize+1]=green;
pVoxelData[voxelCodeSize+2]=blue;
float* vertices = firstVertexForCode(pVoxelData);
printf("inserting voxel at: %f,%f,%f\n",vertices[0],vertices[1],vertices[2]);
delete []vertices;
randomTree.readCodeColorBufferToTree(pVoxelData);
//printf("readCodeColorBufferToTree() of size=%d atByte=%d receivedBytes=%ld\n",voxelDataSize,atByte,receivedBytes);
//printf("readCodeColorBufferToTree() of size=%d atByte=%d receivedBytes=%ld\n",
// voxelDataSize,atByte,receivedBytes);
// skip to next
pVoxelData+=voxelDataSize;
atByte+=voxelDataSize;