Merge remote-tracking branch 'origin/master'

This commit is contained in:
Stephen Birarda 2013-03-28 14:39:59 -07:00
commit 1b3c15a701
9 changed files with 112 additions and 189 deletions

View file

@ -37,6 +37,7 @@
#include <arpa/inet.h>
#endif
const int DOMAIN_LISTEN_PORT = 40102;
unsigned char packetData[MAX_PACKET_SIZE];
@ -60,6 +61,21 @@ unsigned char * addAgentToBroadcastPacket(unsigned char *currentPosition, Agent
int main(int argc, const char * argv[])
{
// If user asks to run in "local" mode then we do NOT replace the IP
// with the EC2 IP. Otherwise, we will replace the IP like we used to
// this allows developers to run a local domain without recompiling the
// domain server
bool useLocal = cmdOptionExists(argc, argv, "--local");
if (useLocal) {
printf("NOTE: Running in Local Mode!\n");
} else {
printf("--------------------------------------------------\n");
printf("NOTE: Running in EC2 Mode. \n");
printf("If you're a developer testing a local system, you\n");
printf("probably want to include --local on command line.\n");
printf("--------------------------------------------------\n");
}
setvbuf(stdout, NULL, _IOLBF, 0);
ssize_t receivedBytes = 0;
@ -90,7 +106,11 @@ int main(int argc, const char * argv[])
// if it matches our local address we're on the same box
// so hardcode the EC2 public address for now
if (agentPublicAddress.sin_addr.s_addr == serverLocalAddress) {
agentPublicAddress.sin_addr.s_addr = 895283510;
// If we're not running "local" then we do replace the IP
// with the EC2 IP. Otherwise, we use our normal public IP
if (!useLocal) {
agentPublicAddress.sin_addr.s_addr = 895283510; // local IP in this format...
}
}
if (agentList.addOrUpdateAgent((sockaddr *)&agentPublicAddress,

View file

@ -14,7 +14,7 @@
#include <OctalCode.h>
#include "VoxelSystem.h"
const int MAX_VOXELS_PER_SYSTEM = 250000;
const int MAX_VOXELS_PER_SYSTEM = 1500000; //250000;
const int VERTICES_PER_VOXEL = 8;
const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL;
@ -54,57 +54,9 @@ VoxelSystem::~VoxelSystem() {
// colors are set randomly
// Complaints: Brad :)
// To Do: Need to add color data to the file.
void VoxelSystem::loadVoxelsFile(char* fileName) {
int vCount = 0;
std::ifstream file(fileName, std::ios::in|std::ios::binary);
char octets;
unsigned int lengthInBytes;
void VoxelSystem::loadVoxelsFile(const char* fileName, bool wantColorRandomizer) {
int totalBytesRead = 0;
if(file.is_open())
{
bool bail = false;
while (!file.eof() && !bail) {
file.get(octets);
totalBytesRead++;
lengthInBytes = bytesRequiredForCodeLength(octets)-1; //(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;
}
// read color data
char red,green,blue;
file.get(red);
file.get(green);
file.get(blue);
//printf("red:%d\n",red);
//printf("green:%d\n",green);
//printf("blue:%d\n",blue);
vCount++;
//printf("vCount:%d\n",vCount);
//randomColorValue(65);
float rf = randFloatInRange(.5,1); // add a little bit of variance to colors so we can see the voxels
voxelData[lengthInBytes+1] = red * rf;
voxelData[lengthInBytes+2] = green * rf;
voxelData[lengthInBytes+3] = blue * rf;
//printVoxelCode(voxelData);
tree->readCodeColorBufferToTree(voxelData);
delete voxelData;
}
file.close();
}
tree->pruneTree(tree->rootNode);
tree->loadVoxelsFile(fileName,wantColorRandomizer);
// reset the verticesEndPointer so we're writing to the beginning of the array
verticesEndPointer = verticesArray;
@ -122,99 +74,9 @@ void VoxelSystem::loadVoxelsFile(char* fileName) {
// mechanism to tell the system to redraw it's arrays after voxels are done
// being added. This is a concept mostly only understood by VoxelSystem.
// Complaints: Brad :)
void VoxelSystem::createSphere(float r,float xc, float yc, float zc, float s, bool solid)
{
// About the color of the sphere... we're going to make this sphere be a gradient
// between two RGB colors. We will do the gradient along the phi spectrum
unsigned char r1 = randomColorValue(165);
unsigned char g1 = randomColorValue(165);
unsigned char b1 = randomColorValue(165);
unsigned char r2 = randomColorValue(65);
unsigned char g2 = randomColorValue(65);
unsigned char b2 = randomColorValue(65);
// we don't want them to match!!
if (r1==r2 && g1==g2 && b1==b2)
{
r2=r1/2;
g2=g1/2;
b2=b1/2;
}
void VoxelSystem::createSphere(float r,float xc, float yc, float zc, float s, bool solid) {
/**
std::cout << "creatSphere COLORS ";
std::cout << " r1=" << (int)r1;
std::cout << " g1=" << (int)g1;
std::cout << " b1=" << (int)b1;
std::cout << " r2=" << (int)r2;
std::cout << " g2=" << (int)g2;
std::cout << " b2=" << (int)b2;
std::cout << std::endl;
**/
// Psuedocode for creating a sphere:
//
// for (theta from 0 to 2pi):
// for (phi from 0 to pi):
// x = xc+r*cos(theta)*sin(phi)
// y = yc+r*sin(theta)*sin(phi)
// z = zc+r*cos(phi)
int t=0; // total points
// We want to make sure that as we "sweep" through our angles
// we use a delta angle that's small enough to not skip any voxels
// we can calculate theta from our desired arc length
//
// lenArc = ndeg/360deg * 2pi*R
// lenArc = theta/2pi * 2pi*R
// lenArc = theta*R
// theta = lenArc/R
// theta = g/r
float angleDelta = (s/r);
// assume solid for now
float ri = 0.0;
if (!solid)
{
ri=r; // just the outer surface
}
// If you also iterate form the interior of the sphere to the radius, makeing
// larger and larger sphere's you'd end up with a solid sphere. And lots of voxels!
for (; ri <= r; ri+=s)
{
for (float theta=0.0; theta <= 2*M_PI; theta += angleDelta)
{
for (float phi=0.0; phi <= M_PI; phi += angleDelta)
{
t++; // total voxels
float x = xc+r*cos(theta)*sin(phi);
float y = yc+r*sin(theta)*sin(phi);
float z = zc+r*cos(phi);
/*
std::cout << " r=" << r;
std::cout << " theta=" << theta;
std::cout << " phi=" << phi;
std::cout << " x=" << x;
std::cout << " y=" << y;
std::cout << " z=" << z;
std::cout << " t=" << t;
std::cout << std::endl;
*/
// gradient color data
float gradient = (phi/M_PI);
unsigned char red = r1+((r2-r1)*gradient);
unsigned char green = g1+((g2-g1)*gradient);
unsigned char blue = b1+((b2-b1)*gradient);
unsigned char* voxelData = pointToVoxel(x,y,z,s,red,green,blue);
tree->readCodeColorBufferToTree(voxelData);
delete voxelData;
}
}
}
tree->createSphere(r,xc,yc,zc,s,solid);
// reset the verticesEndPointer so we're writing to the beginning of the array
verticesEndPointer = verticesArray;

View file

@ -33,7 +33,7 @@ public:
void render();
void setVoxelsRendered(int v) {voxelsRendered = v;};
int getVoxelsRendered() {return voxelsRendered;};
void loadVoxelsFile(char* fileName);
void loadVoxelsFile(const char* fileName,bool wantColorRandomizer);
void createSphere(float r,float xc, float yc, float zc, float s, bool solid);
private:
int voxelsRendered;

View file

@ -476,9 +476,9 @@ void display(void)
GLfloat light_position0[] = { 1.0, 1.0, 0.0, 0.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
GLfloat ambient_color[] = { 0.125, 0.305, 0.5 };
GLfloat ambient_color[] = { 0.7, 0.7, 0.8 }; //{ 0.125, 0.305, 0.5 };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color);
GLfloat diffuse_color[] = { 0.5, 0.42, 0.33 };
GLfloat diffuse_color[] = { 0.8, 0.7, 0.7 }; //{ 0.5, 0.42, 0.33 };
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_color);
GLfloat specular_color[] = { 1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_SPECULAR, specular_color);
@ -895,8 +895,16 @@ void audioMixerUpdate(in_addr_t newMixerAddress, in_port_t newMixerPort) {
}
#endif
int main(int argc, char** argv)
int main(int argc, const char * argv[])
{
// Handle Local Domain testing with the --local command line
bool wantLocalDomain = cmdOptionExists(argc, argv, "--local");
if (wantLocalDomain) {
printf("Local Domain MODE!\n");
int ip = getLocalAddress();
sprintf(DOMAIN_IP,"%d.%d.%d.%d", (ip & 0xFF), ((ip >> 8) & 0xFF),((ip >> 16) & 0xFF), ((ip >> 24) & 0xFF));
}
// the callback for our instance of AgentList is attachNewHeadToAgent
agentList.linkedDataCreateCallback = &attachNewHeadToAgent;
@ -913,7 +921,7 @@ int main(int argc, char** argv)
int wsaresult = WSAStartup( MAKEWORD(2,2), &WsaData );
#endif
glutInit(&argc, argv);
glutInit(&argc, (char**)argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Interface");
@ -941,12 +949,17 @@ int main(int argc, char** argv)
init();
bool wantColorRandomizer = true;
// Check to see if the user passed in a command line option for randomizing colors
if (cmdOptionExists(argc, argv, "--NoColorRandomizer")) {
wantColorRandomizer = false;
}
// 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(argc, argv, "-i");
if (voxelsFilename)
{
voxels.loadVoxelsFile(voxelsFilename);
const char* voxelsFilename = getCmdOption(argc, argv, "-i");
if (voxelsFilename) {
voxels.loadVoxelsFile(voxelsFilename,wantColorRandomizer);
}
// create thread for receipt of data via UDP

View file

@ -29,6 +29,10 @@ float randFloat () {
return (rand() % 10000)/10000.f;
}
int randIntInRange (int min, int max) {
return min + (rand() % (max - min));
}
float randFloatInRange (float min,float max) {
return min + ((rand() % 10000)/10000.f * (max-min));
}
@ -89,14 +93,11 @@ void switchToResourcesIfRequired() {
// then you're using the "-i" flag to set the input file name.
// Usage: char * inputFilename = getCmdOption(argc, argv, "-i");
// Complaints: Brad :)
char* getCmdOption(int argc, char** argv,char* option)
{
const char* getCmdOption(int argc, const char * argv[],char* option) {
// check each arg
for (int i=0; i < argc; i++)
{
for (int i=0; i < argc; i++) {
// if the arg matches the desired option
if (strcmp(option,argv[i])==0 && i+1 < argc)
{
if (strcmp(option,argv[i])==0 && i+1 < argc) {
// then return the next option
return argv[i+1];
}
@ -110,14 +111,12 @@ char* getCmdOption(int argc, char** argv,char* option)
// included while launching the application. Returns bool true/false
// Usage: bool wantDump = cmdOptionExists(argc, argv, "-d");
// Complaints: Brad :)
bool cmdOptionExists(int argc, char** argv,char* option)
{
bool cmdOptionExists(int argc, const char * argv[],char* option) {
// check each arg
for (int i=0; i < argc; i++)
{
for (int i=0; i < argc; i++) {
// if the arg matches the desired option
if (strcmp(option,argv[i])==0)
{
if (strcmp(option,argv[i])==0) {
// then return the next option
return true;
}
@ -172,13 +171,11 @@ unsigned char* pointToVoxel(float x, float y, float z, float s, unsigned char r,
// Now we actually fill out the voxel code
while (octetsDone < voxelSizeInOctets) {
if (x > xTest) {
//<write 1 bit>
byte = (byte << 1) | true;
xTest += sTest/2.0;
}
else {
} else {
//<write 0 bit;>
byte = (byte << 1) | false;
xTest -= sTest/2.0;
@ -186,8 +183,7 @@ unsigned char* pointToVoxel(float x, float y, float z, float s, unsigned char r,
bitInByteNDX++;
// If we've reached the last bit of the byte, then we want to copy this byte
// into our buffer. And get ready to start on a new byte
if (bitInByteNDX > 7)
{
if (bitInByteNDX > 7) {
voxelOut[byteNDX]=byte;
byteNDX++;
bitInByteNDX=0;
@ -198,8 +194,7 @@ unsigned char* pointToVoxel(float x, float y, float z, float s, unsigned char r,
//<write 1 bit>
byte = (byte << 1) | true;
yTest += sTest/2.0;
}
else {
} else {
//<write 0 bit;>
byte = (byte << 1) | false;
yTest -= sTest/2.0;
@ -207,8 +202,7 @@ unsigned char* pointToVoxel(float x, float y, float z, float s, unsigned char r,
bitInByteNDX++;
// If we've reached the last bit of the byte, then we want to copy this byte
// into our buffer. And get ready to start on a new byte
if (bitInByteNDX > 7)
{
if (bitInByteNDX > 7) {
voxelOut[byteNDX]=byte;
byteNDX++;
bitInByteNDX=0;
@ -219,8 +213,7 @@ unsigned char* pointToVoxel(float x, float y, float z, float s, unsigned char r,
//<write 1 bit>
byte = (byte << 1) | true;
zTest += sTest/2.0;
}
else {
} else {
//<write 0 bit;>
byte = (byte << 1) | false;
zTest -= sTest/2.0;
@ -228,8 +221,7 @@ unsigned char* pointToVoxel(float x, float y, float z, float s, unsigned char r,
bitInByteNDX++;
// If we've reached the last bit of the byte, then we want to copy this byte
// into our buffer. And get ready to start on a new byte
if (bitInByteNDX > 7)
{
if (bitInByteNDX > 7) {
voxelOut[byteNDX]=byte;
byteNDX++;
bitInByteNDX=0;
@ -242,11 +234,9 @@ unsigned char* pointToVoxel(float x, float y, float z, float s, unsigned char r,
// If we've got here, and we didn't fill the last byte, we need to zero pad this
// byte before we copy it into our buffer.
if (bitInByteNDX > 0 && bitInByteNDX < 7)
{
if (bitInByteNDX > 0 && bitInByteNDX < 7) {
// Pad the last byte
while (bitInByteNDX <= 7)
{
while (bitInByteNDX <= 7) {
byte = (byte << 1) | false;
bitInByteNDX++;
}
@ -263,8 +253,7 @@ unsigned char* pointToVoxel(float x, float y, float z, float s, unsigned char r,
return voxelOut;
}
void printVoxelCode(unsigned char* voxelCode)
{
void printVoxelCode(unsigned char* voxelCode) {
unsigned char octets = voxelCode[0];
unsigned int voxelSizeInBits = octets*3;
unsigned int voxelSizeInBytes = (voxelSizeInBits/8)+1;
@ -277,8 +266,7 @@ void printVoxelCode(unsigned char* voxelCode)
printf("voxelSizeInOctets=%d\n",voxelSizeInOctets);
printf("voxelBufferSize=%d\n",voxelBufferSize);
for(int i=0;i<voxelBufferSize;i++)
{
for(int i=0;i<voxelBufferSize;i++) {
printf("i=%d ",i);
outputBits(voxelCode[i]);
}

View file

@ -22,6 +22,7 @@ double usecTimestamp(timeval *time);
double usecTimestampNow();
float randFloat();
int randIntInRange (int min, int max);
float randFloatInRange (float min,float max);
unsigned char randomColorValue(int minimum);
bool randomBoolean();
@ -33,8 +34,8 @@ bool oneAtBit(unsigned char byte, int bitIndex);
void switchToResourcesIfRequired();
char* getCmdOption(int argc, char** argv,char* option);
bool cmdOptionExists(int argc, char** argv,char* option);
const char* getCmdOption(int argc, const char * argv[],char* option);
bool cmdOptionExists(int argc, const char * argv[],char* option);
unsigned char* pointToVoxel(float x, float y, float z, float s, unsigned char r, unsigned char g, unsigned char b );
#endif /* defined(__hifi__SharedUtil__) */

View file

@ -11,6 +11,8 @@
#include "SharedUtil.h"
#include "OctalCode.h"
#include "VoxelTree.h"
#include <iostream> // to load voxels from file
#include <fstream> // to load voxels from file
const int TREE_SCALE = 10;
@ -89,6 +91,7 @@ VoxelNode * VoxelTree::createMissingNode(VoxelNode *lastParentNode, unsigned cha
// if node has color & <= 4 children then prune children
void VoxelTree::pruneTree(VoxelNode* pruneAt) {
int childCount = 0;
int childMask = 0;
// determine how many children we have
for (int i = 0; i < 8; i++) {
@ -114,6 +117,14 @@ void VoxelTree::pruneTree(VoxelNode* pruneAt) {
}
}
}
// repair our child mask
// determine how many children we have
pruneAt->childMask = 0;
for (int i = 0; i < 8; i++) {
if (pruneAt->children[i]) {
pruneAt->childMask += (1 << (7 - i));
}
}
}
int VoxelTree::readNodeData(VoxelNode *destinationNode,
@ -410,4 +421,4 @@ void VoxelTree::printTreeForDebugging(VoxelNode *startNode) {
}
}
}
}
}

View file

@ -38,6 +38,8 @@ public:
unsigned char * octalCode = NULL);
void pruneTree(VoxelNode* pruneAt);
void loadVoxelsFile(const char* fileName, bool wantColorRandomizer);
void createSphere(float r,float xc, float yc, float zc, float s, bool solid);
};
#endif /* defined(__hifi__VoxelTree__) */

View file

@ -180,6 +180,14 @@ int main(int argc, const char * argv[])
{
setvbuf(stdout, NULL, _IOLBF, 0);
// Handle Local Domain testing with the --local command line
bool wantLocalDomain = cmdOptionExists(argc, argv, "--local");
if (wantLocalDomain) {
printf("Local Domain MODE!\n");
int ip = getLocalAddress();
sprintf(DOMAIN_IP,"%d.%d.%d.%d", (ip & 0xFF), ((ip >> 8) & 0xFF),((ip >> 16) & 0xFF), ((ip >> 24) & 0xFF));
}
agentList.linkedDataCreateCallback = &attachVoxelAgentDataToAgent;
agentList.startSilentAgentRemovalThread();
agentList.startDomainServerCheckInThread();
@ -201,6 +209,24 @@ int main(int argc, const char * argv[])
// loop to send to agents requesting data
while (true) {
if (agentList.getAgentSocket().receive(&agentPublicAddress, packetData, &receivedBytes)) {
// XXXBHG: Hacked in support for 'I' insert command
if (packetData[0] == 'I') {
unsigned short int itemNumber = (*((unsigned short int*)&packetData[1]));
printf("got I 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!
randomTree.readCodeColorBufferToTree(pVoxelData);
//printf("readCodeColorBufferToTree() of size=%d atByte=%d receivedBytes=%ld\n",voxelDataSize,atByte,receivedBytes);
// skip to next
pVoxelData+=voxelDataSize;
atByte+=voxelDataSize;
}
//printf("about to call pruneTree()\n");
//randomTree.pruneTree(randomTree.rootNode); // hack
}
if (packetData[0] == 'H') {
if (agentList.addOrUpdateAgent(&agentPublicAddress,
&agentPublicAddress,