User Tools

Site Tools


cs444rt:3dserverclient

3D Server and Client Code:

_3DServer.pde

import processing.net.*;
Server myServer;
int val = 0;
float x=0, y=0, z=0;
float[] clientXs = new float[2];
float[] clientYs = new float[2];
float[] clientZs = new float[2];
    
void setup() {
size(200, 200);
// Starts a myServer on port 5204
myServer = new Server(this, 5204); 
// Get the next available client
  
//int clientCount = 0;
String[] tokens;
println("Waiting for clients...");
int i=0;
while(myServer.clientCount < 2) //2
{
  //delay(1000);
  i++;
  if(i>=10000){ i=0;
  println("Currently "+myServer.clientCount+" clients... waiting.");}
}
Client thisClient = myServer.clients[0];
delay(1000);
String positions = null;
while(true)
{
  positions = thisClient.readString();
  if(positions != null)
  {
    tokens = positions.split(",");
    if(tokens.length >=3)
      break;
  }
}
println(positions);
clientXs[0] = Float.valueOf(tokens[0].trim()).floatValue();
clientYs[0] = Float.valueOf(tokens[1].trim()).floatValue();
clientZs[0] = Float.valueOf(tokens[2].trim()).floatValue();
  
thisClient = myServer.clients[1];
delay(1000);
positions = null;
while(true)
{
  positions = thisClient.readString();
  if(positions != null)
  {
   tokens = positions.split(",");
    if(tokens.length >=3)
      break;
  }
}
clientXs[1] = Float.valueOf(tokens[0].trim()).floatValue();
clientYs[1] = Float.valueOf(tokens[1].trim()).floatValue();
clientZs[1] = Float.valueOf(tokens[2].trim()).floatValue();
  
x=Float.valueOf(clientXs[1]);
y=0;//clientYs[1];
z=clientZs[1];
}
  
void draw() {
x+=5;
y--;
   
myServer.write(x+","+y+","+z+"-");
}

_3d_client

import processing.net.*;
Client myClient;
int val = 0;
float X, Y, Z;
float outX=100, outY=100, outZ=100;
float inX, inY, inZ;

void setup() {
  size(screen.width, screen.height, P3D);
  // Server conection:
  myClient = new Client(this, "152.97.100.115", 5204); //152.97.100.111
    colorMode(RGB, 1); 
  //int clientCount = 0;
  X = 0;
  Y = 12;
  Z = 30;
  myClient.write(""+X+","+Y+","+Z);
}

void draw() {
  delay(100);
  if (myClient.available() > 0) { 
    //background(0); 
    String positions = myClient.readString(); 
    String[] tokens = positions.split(",");
    try{
      inX = Float.valueOf(tokens[0].trim()).floatValue();
      inY = Float.valueOf(tokens[1].trim()).floatValue();
    //sometimes two strings are combined, so z would be something like 0.0267.0
    //split it up just in case this happenss
    String[] zs = tokens[2].trim().split("-");
//for(int i=0; i<zs.length; i++)
//  println("\t"+zs[i]);
//    if(zs.length >= 2)
      inZ = Float.valueOf(zs[0]).floatValue();
    }
    catch(Exception e)
    {}

//    else   
//      inZ = Float.valueOf(tokens[2].trim()).floatValue();
    //Transform from input to output... input is in inches, output is in pixels. Proper pixel/inch conversion???
    outX = (inX - X);
    outY = (inY - Y);
    outZ = (inZ - Z);
  }
  println(""+outX+","+outY+","+outZ);
pushMatrix();
//translate(outX, outY, outZ-30); 
//scale(90);
translate(width/2, height/2, -30); 
beginShape(QUADS);
background(0);
fill(0, 1, 1); vertex(-100+outX,  100+outY,  100+outZ);
fill(1, 1, 1); vertex( 100+outX,  100+outY,  100+outZ);
fill(1, 0, 1); vertex( 100+outX, -100+outY,  100+outZ);
fill(0, 0, 1); vertex(-100+outX, -100+outY,  100+outZ);

fill(1, 1, 1); vertex( 100+outX,  100+outY,  100+outZ);
fill(1, 1, 0); vertex( 100+outX,  100+outY, -100+outZ);
fill(1, 0, 0); vertex( 100+outX, -100+outY, -100+outZ);
fill(1, 0, 1); vertex( 100+outX, -100+outY,  100+outZ);

fill(1, 1, 0); vertex( 100+outX,  100+outY, -100+outZ);
fill(0, 1, 0); vertex(-100+outX,  100+outY, -100+outZ);
fill(0, 0, 0); vertex(-100+outX, -100+outY, -100+outZ);
fill(1, 0, 0); vertex( 100+outX, -100+outY, -100+outZ);

fill(0, 1, 0); vertex(-100+outX,  100+outY, -100+outZ);
fill(0, 1, 1); vertex(-100+outX,  100+outY,  100+outZ);
fill(0, 0, 1); vertex(-100+outX, -100+outY,  100+outZ);
fill(0, 0, 0); vertex(-100+outX, -100+outY, -100+outZ);

fill(0, 1, 0); vertex(-100+outX,  100+outY, -100+outZ);
fill(1, 1, 0); vertex( 100+outX,  100+outY, -100+outZ);
fill(1, 1, 1); vertex( 100+outX,  100+outY,  100+outZ);
fill(0, 1, 1); vertex(-100+outX,  100+outY,  100+outZ);

fill(0, 0, 0); vertex(-100+outX, -100+outY, -100+outZ);
fill(1, 0, 0); vertex( 100+outX, -100+outY, -100+outZ);
fill(1, 0, 1); vertex( 100+outX, -100+outY,  100+outZ);
fill(0, 0, 1); vertex(-100+outX, -100+outY,  100+outZ);

endShape();
  
popMatrix();
}
cs444rt/3dserverclient.txt · Last modified: 2010/05/03 11:16 by tomliar0