我试图查看是否可以使用oscP5将在处理中生成的草图链接到蚱grass。草图是此编码培训视频的上半部分概述的点范围:
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时,效果并不理想。我的蚱file文件接收到了正确的分数,但是要点并不正确,因此我必须与发送的数据有关的问题,但是我似乎无法弄清楚应该发送的数据。该代码采用指定的半径,经度和纬度的值,并将其转换为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)));
}
任何帮助都会很棒。
答案 0 :(得分:2)
永远不要设置float lat;
全局变量的值,因为在嵌套的float lon;
循环中,声明了两个新的局部变量(在循环块作用域中):>
for
请注意,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 lat = ...
是对现有变量lat = ...
的赋值。
但是使用全局变量而不是设置新变量不会改变任何内容,因为在发送消息时,变量lat
和lat
将具有最后一次迭代的值。嵌套循环。
您必须实现2个新循环,并且必须重新计算lon
和lat
的值。循环后的print语句没有任何意义,因为它将仅打印最后一个坐标。
代码应如下所示:
lon