"矢量迭代器不可增量"与OpenGL,LeapMotion应用程序
"vector iterator not incrementable" with OpenGL, LeapMotion application
我已经成功地编写了代码,通过Leap Motion控制器(跟踪手指和手部运动的红外扫描设备)测量我一根手指的(x,y)位置,并使用OpenGL绘制具有类似(x,y)坐标的点。基本上,你移动你的手指,你移动屏幕上显示的点。
我现在想做的是显示记录的最后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;
}
如果这是线程化的,那么SampleListener::onFrame()
中的collection.erase()
将使display()
中的迭代器无效。你需要一个互斥锁。见鬼,push_back()
可能会使迭代器无效。看起来你想要一个带有头/尾指针的固定长度结构,而不是这个动态向量。
相关文章:
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 了解 GLM- openGL 中的相机转换
- 如何在全屏模式下(在OpenGL中)使背景透明
- 为什么我不能使用 EGL 创建无头 OpenGl 上下文?
- OpenGL大的3D纹理(>2GB)非常慢
- OpenGl glm rotate
- C++atioglxx.pdb未加载错误glBufferData OpenGL
- OpenGL在启用深度测试时不会丢弃我的碎片
- OpenGL相机和相机空间转型的困惑
- OpenGL将纹理四边形渲染为(0,0)
- OpenGL 和 GLM 矩阵无法正确扩展,总是按比例缩小
- 发布旋转矩阵(openGL/glm)
- 如何在Visual Basic中使用矩形函数OpenGL绘制矩形
- 无法使用VAO和EBO(openGL)绘制多个对象
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- OpenGL 16 位模板缓冲区?
- 将QOpenGLWidget子类转换为使用Metal而不是OpenGL的子类是否可行?
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 在 openGL 中多次绑定缓冲区
- "矢量迭代器不可增量"与OpenGL,LeapMotion应用程序