4.1 Processing and OSC

You can download processing here: https://processing.org/download

The example on this page is the basic framework for reading OSC data from Vicon Tracker. To read OSC you will need the OscP5 library installed. Below is a quick guide to install a contributed library using the Processing Contribution Manager.

Select Sketch> Import Library>Manage Libraries.

The Contribution Manager will pop up in another window. Search for OscP5 and then press install in the lower right of the window.

import oscP5.*;
import netP5.*;

ArrayList <PVector> trace = new ArrayList <PVector> ();
  
OscP5 oscP5;
NetAddress myRemoteLocation;

void setup() {
  size(400,400);
  myRemoteLocation = new NetAddress("127.0.0.1",12000);
}


void draw() {
  background(0);  
  for(int i = 0; i < trace.size()-1; i++){
    PVector p1 = trace.get(i);
    PVector p2 = trace.get(i+1);
    line(p1.x,p1.y,p2.x,p2.y);
  }
}



/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage theOscMessage) {
  /* print the address pattern and the typetag of the received OscMessage */
  print("### received an osc message.");
  print(" addrpattern: "+theOscMessage.addrPattern());
  println(" typetag: "+theOscMessage.typetag());
}

import oscP5.*;
import netP5.*;

OscP5 oscP5;

//declare and initialize a list of vectors called pts
ArrayList  <PVector> pts = new ArrayList<PVector>();

float xcor = 0;
float ycor = 0;

///camera varibles
int oldx = mouseX;
int oldy = mouseY;
float rotx = 0;
float roty = 0;
float zcam = 680;

void setup() {
  size(800,800,P3D);

  /* start oscP5, listening for incoming messages at port 12000 */
  oscP5 = new OscP5(this,28000);
  
}


void draw() {
  background(0);  
  cam();
   for(int i = 0; i < pts.size(); i++){
    PVector p1 = pts.get(i);
    stroke(255);
    strokeWeight(5);
    point(p1.x,p1.y,p1.z);
  }
}

void cam() {
  int newx = mouseX;
  int newy = mouseY;
  translate(width/2, height/2,zcam);
  rotateY(rotx);
  rotateX(roty);
  //rotateZ(PI);
  if ((mousePressed == true)) {
    rotx = rotx + (oldx-newx)/50.0;
    roty = roty + (oldy-newy)/50.0;
  }
  oldx = newx;
  oldy = newy;
}

void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  zcam = zcam - e*5;
}

/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage theOscMessage) {
  /* print the address pattern and the typetag of the received OscMessage */
  print("### received an osc message.");
  print(" addrpattern: "+theOscMessage.addrPattern());
  println(" typetag: "+theOscMessage.typetag());
  String datatype = theOscMessage.typetag();
  pts = new ArrayList();

  for(int i = 0; i < datatype.length()/3; i++){
    float xx = float(theOscMessage.get(i*3).stringValue());
    float yy = float(theOscMessage.get(i*3+1).stringValue())*-1;
    float zz = float(theOscMessage.get(i*3+2).stringValue());
    pts.add(new PVector(xx,yy,zz));
  }
}