我正在尝试查看是否可以使用 oscP5 将处理中制作的草图链接到蚱蜢。该草图是该编码训练视频前半部分中概述的点球体:
https://www.youtube.com/watch?v=RkuBWEkBrZA
在我开始将其与 oscP5 链接之前的代码似乎工作正常:
import peasy.*;
PeasyCam cam;
PVector[][] globe;
int total = 20;
void setup() {
size(600, 600, P3D);
cam = new PeasyCam(this, 500);
globe = new PVector[total+1][total+1];
}
void draw() {
background(0);
fill(255);
lights();
float r = 200;
for (int i = 0; i < total+1; i++) {
float lat = map(i, 0, total, 0, PI);
for (int j = 0; j < total+1; j++) {
float lon = map(j, 0, total, 0, TWO_PI);
float x = r * sin(lat) * cos(lon);
float y = r * sin(lat) * sin(lon);
float z = r * cos(lat);
globe[i][j] = new PVector(x, y, z);
}
}
noFill();
for (int i = 0; i < total; i++) {
beginShape(TRIANGLE_STRIP);
for (int j = 0; j < total+1; j++) {
PVector v1 = globe[i][j];
stroke(255);
strokeWeight(2);
vertex(v1.x, v1.y, v1.z);
PVector v2 = globe[i+1][j];
vertex(v2.x, v2.y, v2.z);
}
endShape();
}
}
但是,当我尝试实现 oscP5 时,它并不能很好地工作。我的蚱蜢文件正在接收草图,但是点不正确,所以我必须对我发送的数据有问题,但我似乎无法弄清楚我应该发送什么。该代码采用指定的半径、经度和纬度的值并将其转换为 x、y、z 坐标,我试图让 oscP5 发送这些 x、y、z 坐标。
有没有人有任何想法?到目前为止的代码如下。
//import necessary libraries
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
//import camera
import peasy.*;
PeasyCam cam;
// message to send
String message;
PVector[][] sphere;
int total = 20;
float lat;
float lon;
void setup() {
size(600, 600, P3D);
cam = new PeasyCam(this, 500);
sphere = new PVector[total+1][total+1];
//send message from this port
oscP5 = new OscP5(this,12000);
//send message to this port
myRemoteLocation = new NetAddress("127.0.0.1",12001);
}
void draw() {
background(0);
fill(255);
lights();
float r = 200;
for (int i = 0; i < total+1; i++) {
float lat = map(i, 0, total, 0, PI);
for (int j = 0; j < total+1; j++) {
float lon = map(j, 0, total, 0, TWO_PI);
float x = r * sin(lat) * cos(lon);
float y = r * sin(lat) * sin(lon);
float z = r * cos(lat);
sphere[i][j] = new PVector(x, y, z);
}
}
noFill();
for (int i = 0; i < total; i++) {
beginShape(TRIANGLE_STRIP);
for (int j = 0; j < total+1; j++) {
PVector v1 = sphere[i][j];
stroke(255);
strokeWeight(2);
vertex(v1.x, v1.y, v1.z);
PVector v2 = sphere[i+1][j];
vertex(v2.x, v2.y, v2.z);
}
endShape();
}
// osc message
OscMessage myMessage = new OscMessage("/hello world");
for (int j = 0; j < total+1; j++) {
//message to send
message = "x = " + String.valueOf(r * sin(lat) * cos(lon)) + "; " +
"y = " + String.valueOf(r * sin(lat) * sin(lon))+ "; " +
"z = " + String.valueOf(r * cos(lat))+ "; " ;
myMessage.add(String.valueOf(r * sin(lat) * cos(lon)));
myMessage.add(String.valueOf(r * sin(lat) * sin(lon)));
myMessage.add(String.valueOf(r * cos(lat)));
}
//print message
println(message);
//send message
oscP5.send(myMessage, myRemoteLocation);
}
我相信错误出现在这部分代码中
for (int j = 0; j < total+1; j++) {
//message to send
message = "x = " + String.valueOf(r * sin(lat) * cos(lon)) + "; " +
"y = " + String.valueOf(r * sin(lat) * sin(lon))+ "; " +
"z = " + String.valueOf(r * cos(lat))+ "; " ;
myMessage.add(String.valueOf(r * sin(lat) * cos(lon)));
myMessage.add(String.valueOf(r * sin(lat) * sin(lon)));
myMessage.add(String.valueOf(r * cos(lat)));
}
任何帮助都会很棒。