Hello,
I am new in Processing.
I would like to show on two windows a 3D object. The orientation of the object in each windows is given by roll pitch yaw comming from serial port.
I succeed to launch a new window using secondApplet but don't know how to render and rotate the 3D object on this second window.
Here's the code I am using:
`` import processing.serial.*; import java.awt.datatransfer.*; import java.awt.Toolkit; import processing.opengl.*; import saito.objloader.*; import g4p_controls.*; import javax.swing.*; import javax.swing.JFrame;
float xx, yy, zz;
float xx2, yy2, zz2;
// import the Processing serial library
Serial myPort; // The serial port
float bgcolor; // Background color
float fgcolor; // Fill color
float xpos, ypos; // Starting position of the ball
OBJModel model;
OBJModel model2;
// UI controls.
GPanel configPanel;
GDropList serialList;
GLabel serialLabel;
GLabel calLabel;
GCheckbox printSerialCheckbox;
PFrame f;
secondApplet s;
public class PFrame extends JFrame {
public PFrame() {
setBounds(0, 0, 640, 480);
s = new secondApplet();
add(s);
s.init();
show();
}
}
public class secondApplet extends PApplet {
public void setup() {
size(640, 480, OPENGL);
fill(0);
//framerate(30);
// List all the available serial ports
//println(Serial.list());
}
public void draw() {
background(0);
noStroke();
translate(width/3, height/2);
pushMatrix();
// Simple 3 point lighting for dramatic effect.
// Slightly red light in upper right, slightly blue light in upper left, and white light from behind.
pointLight(255, 200, 200, 400, 400, 500);
pointLight(200, 200, 255, -400, 400, 500);
pointLight(255, 255, 255, 0, 0, -500);
rotateX(-yy);//pitch
rotateY(zz);//yaw
rotateZ(-xx);//roll
model2.draw();
translate(2*width/3,height/2);
popMatrix();
}
}
void setup() {
size(640, 480, OPENGL);
fill(0);
//framerate(30);
model= new OBJModel(this,"Vagabond.obj");
model2= new OBJModel(this,"Vagabond.obj");
model.scale(40);
model2.scale(40);
PFrame p = new PFrame();
myPort = new Serial(this, Serial.list()[0], 115200);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');
// smooth();
}
void draw() {
background(0);
noStroke();
translate(width/3, height/2);
pushMatrix();
// Simple 3 point lighting for dramatic effect.
// Slightly red light in upper right, slightly blue light in upper left, and white light from behind.
pointLight(255, 200, 200, 400, 400, 500);
pointLight(200, 200, 255, -400, 400, 500);
pointLight(255, 255, 255, 0, 0, -500);
rotateX(-yy);//pitch
rotateY(zz);//yaw
rotateZ(-xx);//roll
model.draw();
translate(2*width/3,height/2);
popMatrix();
}
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
float sensors[] = float(split(myString, '\t'));
println("roll: " + sensors[0] + " pitch: " + sensors[1] + " yaw: " + sensors[2] + "\t" + "roll: " + sensors[5] + " pitch: " + sensors[4] + " yaw: " + sensors[3] + "\t");
// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
//print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
if (sensorNum == 0) {
xx = sensors[0];
xx2= sensors[3];
}
if (sensorNum == 1) {
yy = sensors[1];
yy2 = sensors[4];
}
if (sensorNum == 2) {
zz = sensors[2];
zz2=sensors[5];
}
}
}``
Any help would be appreciate .