作者 主题: beginshap();endshape();不能连续使用?  (阅读 3349 次)

Tealeaf

  • Newbie
  • *
  • 帖子: 18
beginshap();endshape();不能连续使用?
« 于: 七月 22, 2012, 02:20:00 下午 »
在simpleopenni的一个example(Hands3D)上尝试改写一个用手画彩虹的效果,想多用几次begin shape();Endshape();函数来相邻的线并且分别填充色彩来实现,结果只能现实第一个设定,有人能告诉我该怎么实现吗?代码如下

import processing.opengl.*;

/* --------------------------------------------------------------------------
 * SimpleOpenNI Hands3d Test
 * --------------------------------------------------------------------------
 * Processing Wrapper for the OpenNI/Kinect library
 * http://code.google.com/p/simple-openni
 * --------------------------------------------------------------------------
 * prog:  Max Rheiner / Interaction Design / zhdk / http://iad.zhdk.ch/
 * date:  02/27/2011 (m/d/y)
 * ----------------------------------------------------------------------------
 * This demos shows how to use the gesture/hand generator.
 * It's not the most reliable yet, a two hands example will follow
 * ----------------------------------------------------------------------------
 */
 
import SimpleOpenNI.*;
//import processing.opengl.*;

SimpleOpenNI context;
float        zoomF =0.3f;
float        rotX = radians(180);  // by default rotate the hole scene 180deg around the x-axis,
                                   // the data from openni comes upside down
float        rotY = radians(0);
boolean      handsTrackFlag = false;
PVector      handVec = new PVector();
ArrayList    handVecList = new ArrayList();
int          handVecListSize = 30;
String       lastGesture = "";

void setup()
{
  //size(1024,768,P3D);  // strange, get drawing error in the cameraFrustum if i use P3D, in opengl there is no problem
  size(1024,768,OPENGL);

  context = new SimpleOpenNI(this);
   
  // enable mirror
  context.setMirror(true);

  // enable depthMap generation
  if(context.enableDepth() == false)
  {
     println("Can't open the depthMap, maybe the camera is not connected!");
     exit();
     return;
  }

  // enable hands + gesture generation
  context.enableGesture();
  context.enableHands();
  context.enableDepth();
  // add focus gestures  / here i do have some problems on the mac, i only recognize raiseHand ? Maybe cpu performance ?
  context.addGesture("Wave");
  context.addGesture("Click");
  context.addGesture("RaiseHand");

  // set how smooth the hand capturing should be
  context.setSmoothingHands(0.5);
 
  //stroke(255,255,255);
  smooth();
 
  perspective(radians(45),
              float(width)/float(height),
              10.0f,150000.0f);
 }

void draw()
{
  // update the cam
  context.update();

  background(0,0,0);
 
  // set the scene pos
  translate(width/2, height/2, 0);
  rotateX(rotX);
  rotateY(rotY);
  scale(zoomF);
 
  // draw the 3d point depth map
  int[]   depthMap = context.depthMap();
  int     steps   = 3;  // to speed up the drawing, draw every third point
  int     index;
  PVector realWorldPoint;
 
  translate(0,0,-1000);  // set the rotation center of the scene 1000 infront of the camera


    stroke(#3458E0); //blue point of the scene
 //draw the point view based on depth map
  for(int y=0;y < context.depthHeight();y+=steps)
  {
    for(int x=0;x < context.depthWidth();x+=steps)
    {
      index = x + y * context.depthWidth();
      if(depthMap[index] > 0)
      {
        // draw the projected point
        realWorldPoint = context.depthMapRealWorld()[index];
        point(realWorldPoint.x,realWorldPoint.y,realWorldPoint.z);
      }
    }
  }

  //image(context.rgbImage(),0,0);
  // draw the tracked hand
  if(handsTrackFlag) 
  {
    pushStyle();
      stroke(#FF2D0D);//red for line
      strokeWeight(2);
      Iterator itr = handVecList.iterator();
      beginShape(TRIANGLES);
        while( itr.hasNext() )
        {
          PVector p = (PVector) itr.next();
          vertex(p.x,p.y,p.z);
        }
      endShape();   
     

     
      stroke(#FFF40D);//yellow for handflag
      //strokeWeight(4);
      lights();
      translate(handVec.x,handVec.y,handVec.z);
      sphere(28);
     
     
    popStyle();   
  }
   
  // draw the kinect cam
  //context.drawCamFrustum();
  //image(context.irImage(), 0, 0);
}


// -----------------------------------------------------------------
// hand events

void onCreateHands(int handId,PVector pos,float time)
{
  println("onCreateHands - handId: " + handId + ", pos: " + pos + ", time:" + time);
 
  handsTrackFlag = true;
  handVec = pos;
 
  handVecList.clear();
  handVecList.add(pos);
}

void onUpdateHands(int handId,PVector pos,float time)
{
  //println("onUpdateHandsCb - handId: " + handId + ", pos: " + pos + ", time:" + time);
  handVec = pos;
 
  handVecList.add(0,pos);
  if(handVecList.size() >= handVecListSize)
  { // remove the last point
    handVecList.remove(handVecList.size()-1);
  }
}

void onDestroyHands(int handId,float time)
{
  println("onDestroyHandsCb - handId: " + handId + ", time:" + time);
 
  handsTrackFlag = false;
  context.addGesture(lastGesture);
}

// -----------------------------------------------------------------
// gesture events

void onRecognizeGesture(String strGesture, PVector idPosition, PVector endPosition)
{
  println("onRecognizeGesture - strGesture: " + strGesture + ", idPosition: " + idPosition + ", endPosition:" + endPosition);
 
  lastGesture = strGesture;
  context.removeGesture(strGesture);
  context.startTrackingHands(endPosition);
 
}

void onProgressGesture(String strGesture, PVector position,float progress)
{
  //println("onProgressGesture - strGesture: " + strGesture + ", position: " + position + ", progress:" + progress);
}

// -----------------------------------------------------------------
// Keyboard event
void keyPressed()
{
  switch(key)
  {
  case ' ':
    context.setMirror(!context.mirror());
    break;
  }
   
  switch(keyCode)
  {
    case LEFT:
      rotY += 0.1f;
      break;
    case RIGHT:
      rotY -= 0.1f;
      break;
    case UP:
      if(keyEvent.isShiftDown())
        zoomF += 0.01f;
      else
        rotX += 0.1f;
      break;
    case DOWN:
      if(keyEvent.isShiftDown())
      {
        zoomF -= 0.01f;
        if(zoomF < 0.01)
          zoomF = 0.01;
      }
      else
        rotX -= 0.1f;
      break;
  }
}


出错部分
程序代码
void draw()
{
  // update the cam
  context.update();

  background(0,0,0);
 
  // set the scene pos
  translate(width/2, height/2, 0);
  rotateX(rotX);
  rotateY(rotY);
  scale(zoomF);
 
  // draw the 3d point depth map
  int[]   depthMap = context.depthMap();
  int     steps   = 3;  // to speed up the drawing, draw every third point
  int     index;
  PVector realWorldPoint;
 
  translate(0,0,-1000);  // set the rotation center of the scene 1000 infront of the camera


    stroke(#3458E0); //blue point of the scene
 //draw the point view based on depth map
  for(int y=0;y < context.depthHeight();y+=steps)
  {
    for(int x=0;x < context.depthWidth();x+=steps)
    {
      index = x + y * context.depthWidth();
      if(depthMap[index] > 0)
      {
        // draw the projected point
        realWorldPoint = context.depthMapRealWorld()[index];
        point(realWorldPoint.x,realWorldPoint.y,realWorldPoint.z);
      }
    }
  }

  //image(context.rgbImage(),0,0);
  // draw the tracked hand
  if(handsTrackFlag) 
  {
    pushStyle();
      stroke(#FF2D0D);//red for line
      strokeWeight(4);
      Iterator itr = handVecList.iterator();
      beginShape(LINES);
        while( itr.hasNext() )
        {
          PVector p = (PVector) itr.next();
          vertex(p.x,p.y,p.z);
        }
      endShape(); 
     
       stroke(#81FFA7);//red for line
      strokeWeight(4);
            beginShape(LINES);
        while( itr.hasNext() )
        {
          PVector t = (PVector) itr.next();
          vertex(t.x-10,t.y,t.z);
        }
      endShape();
     


      stroke(#FFF40D);//yellow for handflag
      //strokeWeight(4);
      lights();
      translate(handVec.x,handVec.y,handVec.z);
      sphere(28);
     
     
    popStyle();   
  }

错误就是只有第一个beginshape();Endshape();执行了。
« 最后编辑时间: 七月 25, 2012, 12:56:46 下午 作者 Tealeaf »

vinjn

  • SuperManager
  • Hero Member
  • *****
  • 帖子: 586
Re: beginshap();endshap();不能连续使用?
« 回复 #1 于: 七月 24, 2012, 03:38:46 下午 »
既然给出了代码,你要把出错的部分代码隔离出来
给出一个我们可以直接运行的版本,并且附上截图
现在这情况,我们不知道错在哪里,应该是怎样的情况

Tealeaf

  • Newbie
  • *
  • 帖子: 18
Re: beginshap();endshape();不能连续使用?
« 回复 #2 于: 七月 25, 2012, 12:57:20 下午 »
已经重新编辑

Tags: