旋转时 P3D 中的重叠问题

Overlapping issue in P3D when rotating

我想在 P3D 中旋转一个立方体,我的代码使用实时传感器数据来执行此操作。 问题是立方体之前的方向始终可见且重叠(如您在此图像中所见:http://i.stack.imgur.com/txXw6.jpg),这是我不想要的。我已经尝试了 "hint(DISABLE_OPTIMIZED_STROKE)" 和 "hint(ENABLE_DEPTH_TEST)" 函数,但它们什么也没做。除了提示功能外,我在类似问题上一无所获。

如何只渲染当前方向?

import processing.serial.*;
import toxi.geom.*;

Serial myPort;

float qW;
float qX;
float qY;
float qZ;

float[] axis = new float[4];
Quaternion quat = new Quaternion(1, 0, 0, 0);

void setup()
{
  size(600, 400, P3D);

  myPort = new Serial(this, "COM3", 9600);                 

  background(0);
  lights();
}

void draw()
{
  serialEvent();

  quat.set(qW, qX, qY, qZ);
  axis = quat.toAxisAngle();

  pushMatrix(); 
  translate(width/2, height/2, -100);
  rotate(axis[0], axis[1], axis[2], axis[3]);
  noFill();
  stroke(255);
  box(330, 200, 40);
  popMatrix();
}

void serialEvent()
{
  int newLine = 13; // new line character in ASCII
  String message;

  do
  {
    message = myPort.readStringUntil(newLine); // read from port until new line

    if (message != null)
    {
      String[] list = split(trim(message), " ");
      if (list.length >= 4)
      {
        qW  = float(list[0]);
        qX  = float(list[1]);
        qY  = float(list[2]);
        qZ  = float(list[3]);
      }
    }
  } while (message != null);

}

您似乎没有清除帧缓冲区。尝试添加 background(0); 作为 draw();:

中的第一行
void draw()
{
  //clear background
  background(0);

  serialEvent();

  quat.set(qW, qX, qY, qZ);
  axis = quat.toAxisAngle();

  pushMatrix(); 
  translate(width/2, height/2, -100);
  rotate(axis[0], axis[1], axis[2], axis[3]);
  noFill();
  stroke(255);
  box(330, 200, 40);
  popMatrix();
}

题外话,可能值得一试 serialEvent()。 你可以在 setup()

中做这样的事情
myPort = new Serial(this, "COM3", 9600);     
myPort.bufferUntil('\n');

您不需要在 draw() 中调用 serialEvent(),串行库会这样做,因为它正在缓冲。

然后在 serialEvent() 希望你可以逃脱:

String message = myPort.readString();
if(message !=null){
   String[] list = split(trim(message), " ");
   if (list.length >= 4)
      {
        qW  = float(list[0]);
        qX  = float(list[1]);
        qY  = float(list[2]);
        qZ  = float(list[3]);
      }
}