我已经成功编写了代码,该代码采用我的 Leap Motion 控制器(跟踪手指和手部运动的红外扫描设备)测量的我的一个手指的 (x,y) 位置,并绘制一个具有相似 (x,y) 坐标的点使用 OpenGL。基本上,您移动手指,移动屏幕上显示的点。
我现在想做的是显示最近记录的 100 个位置 - 因此,您不会看到一个点移动,而是会看到一个蛇形的点集合,这些点会随着您的指尖移动而蜿蜒而行。
我尝试使用两个向量来做到这一点——一个用于在给定帧期间保持 x 和 y 位置,一个用于保持一堆前面提到的向量。通过在向量的大小超过 100 后弹出向量中的第一个元素,我想我可以遍历所有这些点并在我的显示方法中绘制它们。
它编译得很好,但是在画了几个点之后,我得到一个运行时错误,说“向量迭代器不可递增”。不知道如何处理。
代码:
#include <iostream>
#include <fstream>
#include "Leap.h"
#include <math.h>
#include "GL/glut.h"
#include <deque>
using namespace Leap;
using namespace std;
bool one = true;
double x = 10, y = 100;
double screenWidth = 480, screenHeight = 480;
double time, timeDisplayed = 5.0 * (pow(10.0, 9.0));
vector < double > entry, *temp;
vector < vector< double > > collection;
class SampleListener : public Listener {
public:
int firstFrame, firstTime;
bool first;
virtual void onInit(const Controller&);
virtual void onConnect(const Controller&);
virtual void onExit(const Controller&);
virtual void onFrame(const Controller&);
};
void SampleListener::onInit(const Controller& controller){
cout << "Initialized.\n";
first = true;
}
void SampleListener::onConnect(const Controller& controller){
cout << "Connected.\n";
controller.enableGesture(Gesture::TYPE_SCREEN_TAP);
}
void SampleListener::onExit(const Controller& controller){
cout << "Exited.\n";
}
void SampleListener::onFrame(const Controller& controller) {
const Frame frame = controller.frame();
//some initial frame processing/file writing
if (first){
first = !first;
firstFrame = frame.id();
firstTime = frame.timestamp();
}
else {
//record hand information
if (!frame.hands().isEmpty()){
const Hand hand = frame.hands().frontmost();
const Finger track = hand.fingers().frontmost();
x = track.tipPosition().x;
y = track.tipPosition().y;
time = frame.timestamp() - firstTime;
entry.push_back(x);
entry.push_back(y);
entry.push_back(time);
collection.push_back(entry);
entry.clear();
while (collection.size() > 100 && !collection.empty()){
collection.erase(collection.begin());
}
}
}
}
// Create a sample listener and controller
SampleListener listener;
Controller controller;
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POINTS);
for (auto i = collection.begin(); i != collection.end(); *i++){
cout << collection.size() << endl;
glVertex2f(i->at(0), i->at(1));
}
glEnd();
glFlush();
glutSwapBuffers();
}
void init(){
cout << "in init" << endl;
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-screenWidth/2, screenWidth/2, 0, screenHeight);
glViewport(0, 0, screenWidth, screenHeight);
glPointSize(3.0f);
cout << "leaving init" << endl;
}
void idle(void){
glutPostRedisplay();
}
//<<<<<<<<<<<<<<<<<<<<<<< myKeys >>>>>>>>>>>>>>>>>>>>>>>>
void myKeys(unsigned char key, int x, int y)
{
switch(key)
{
case 'q': // Quit
// Remove the sample listener when done
controller.removeListener(listener);
exit(0);
}
}
int main (int argc, char** argv) {
listener.first = true;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(800, 100);
glutCreateWindow("test");
init();
glutDisplayFunc(display);
glutKeyboardFunc(myKeys);
glutIdleFunc(idle);
controller.addListener(listener);
glutMainLoop();
return 0;
}