2011-09-26 9 views
8

동적 점 구름 시각화 프로그램을 만들려고합니다. 포인트는 Kinect 센서로 매 프레임마다 업데이트됩니다. 프레임을 잡으려면 OpenCV와 GLUT를 사용하여 표시합니다. OpenCV API는 포인트 xyz 위치에 대해 640 x 480 (float *)을, rgb 색상 데이터에 대해 640 x 480 (int *)을 반환합니다. 최대 성능을 얻으려면 간단한 버텍스 배열 대신 스트림 모드에서 버텍스 버퍼 개체를 사용하려고합니다. Vertex Array로 렌더링 할 수는 있지만 VBO 구현에서는 아무 것도 렌더링되지 않습니다. 나는 선언문에 여러 가지 명령을 내렸지 만, 나는 실종 된 것을 발견 할 수 없다. 누군가가 나를 올바른 방향으로 향하게 할 수 있습니까? (기독교 라우에 의해 요청대로 I've 잘못된 버전을 rewrited, 그래서 너희들은 내 실수를 이해할 수)VBO (Vertex Buffer Object)가있는 Kinect 포인트 클라우드 렌더링

int main() 
{ 
    //Delaring variables, inittiating glut, setting camera and checking the compatibility as http://www.songho.ca/opengl/gl_vbo.html 
    glutDisplayFunc(displayCB); 
    glutIdleFunc(displayCB); 
    ... 
    //Inittiating the vertex buffers 
    if(vboSupported) 
    { 
     glGenBuffers(1, &vertex_buffer); 
     glBindBuffer(GL_ARRAY_BUFFER_ARB, buffer); 
     glBufferData(GL_ARRAY_BUFFER_ARB, (sizeof(GLfloat) * 640 * 480 * 3), 0, GL_STREAM_DRAW_ARB); 
     glBufferSubData(GL_ARRAY_BUFFER_ARB, 0, (sizeof(float) * 640 * 480 * 3), point_cloud.points_position); 

     glGenBuffers(2, &color_buffer); 
     glBindBuffer(GL_ARRAY_BUFFER_ARB, buffer); 
     glBufferData(GL_ARRAY_BUFFER_ARB, (sizeof(GLbyte) * 640 * 480 * 3), 0, GL_STREAM_DRAW_ARB); 
     glBufferSubData(GL_ARRAY_BUFFER_ARB, 0, (sizeof(char) * 640 * 480 * 3), point_cloud.points_color); 
    } 
    //glutMainLoop(), cleaning memory, ending main 
    .. 
} 

//Updating the screen 
void displayCB() 
{ 
    point_cloud.update(); 

    // clear buffer 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 

    // save the initial ModelView matrix before modifying ModelView matrix 
    glPushMatrix(); 

     glBindBuffer(GL_ARRAY_BUFFER_ARB, color_buffer); 
     glBufferSubData(GL_ARRAY_BUFFER_ARB, 0, (sizeof(char) * 640 * 480 * 3), point_cloud.points_color); 
     glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0); 

     glBindBuffer(GL_ARRAY_BUFFER_ARB, vertex_buffer); 
     glBufferSubData(GL_ARRAY_BUFFER_ARB, 0, (sizeof(float) * 640 * 480 * 3), point_cloud.points_position); 
     glVertexPointer(3, GL_FLOAT, 0, 0)); 

     // enable vertex arrays 
     glEnableClientState(GL_VERTEX_ARRAY); 
     glEnableClientState(GL_COLOR_ARRAY); 

     glDrawArrays(GL_POINT, 0, 640*480); 

     glDisableClientState(GL_VERTEX_ARRAY); // disable vertex arrays 
     glDisableClientState(GL_COLOR_ARRAY); 

     glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); 

    glPopMatrix(); 

    glutSwapBuffers(); 
} 
+1

아주 관련이없는 댓글 : 전체 버퍼를 업데이트 할 때 오히려 (대신 glBufferSubData''의)'glBufferData'를 사용 I've 세 가지 변경을 수행. 이것은 운전자가 물건을 최적화하는 데 도움이 될 수 있지만 지금은 자세하게 설명하지 않겠습니다. 적어도 메인 코드에서'glBufferData' 다음에 나오는'glBufferSubData'는 전혀 쓸모가 없습니다. –

+1

각 답변 또는 코멘트 다음에 질문 코드를 완전히 변경하지 말고 질문 **에 ** 새 버전의 코드를 ** 추가하는 것이 좋습니다. 그러면 다른 답변이 무효화되고 질문과 답변을 읽는 사람들이 완전히 혼란 스럽습니다. –

+0

새로운 답변으로 postint에서이 작업을 수행합니까? –

답변

0

PS :이 프로그램은 및 실행,하지만 난 볼 수 없습니다 여기 simpflified 코드는 Vertex Array 버전과 비교했을 때 평균 성능이 향상되었습니다. 괜찮습니까? 문제가 해결되었습니다.

1 - I though that glGenBuffers first parameter was the number that 
would be associated to the buffer, instead, its the number of 
buffers that would be allocated. This doesn't matter anyway, because 
now I'm using a single buffer and adding all the data to it. Didn't 
solved the problem, but was going in the right way. 

2 - There are two ways of being able to use OpenGL VBO functions. 
First, you can bind the ARB version functions by yourself OR you 
can add glew.h and use glewInit() after starting an OpenGL context. 
The second option is a LOT cleaner and I changed my code so I don't 
use the ARB versions anymore. Still didn't solved the problem. 

3 - Christian Rau asked me for glErrors in the code, so, I wrote it 
after each OpenGL operation in displayCB. After glDrawArrays I got 
an 0x00500 error, that means there is an invalid enum, so I noticed 
I was using GL_POINT as parameter to glDrawArrays, instead of 
GL_POINTS, so silly that makes me want to kill myself. 

마지막 코드 :

//EDIT: Added glew.h as a header and I´m not using the ARB version anymore 
#include <glew.h> 

    int main() 
    { 
     //Declaring variables, initiating glut, setting camera and checking the compatibility as http://www.songho.ca/opengl/gl_vbo.html 
     glutDisplayFunc(displayCB); 
     glutIdleFunc(displayCB); 
     ... 

     //EDIT: IS VERY IMPORTANT TO ADD IT AS SOON AS YOU START AN OPENGL CONTEXT. 
     initGlew(); 

     //Inittiating the vertex buffers 
     if(vboSupported) 
     { 
      //EDIT: I was using two buffers, one for color and another for the vertex. I changed the code so I use a single buffer now. 
      //EDIT: Notice that I'm not using the ARB version of the functions anymore. 
      glGenBuffers(1, &buffer); 
      glBindBuffer(GL_ARRAY_BUFFER_ARB, buffer); 
      glBufferData(GL_ARRAY_BUFFER_ARB, (sizeof(GLfloat) * 640 * 480 * 3) + (sizeof(GLbyte) * 640 * 480 * 3), 0, GL_STREAM_DRAW_ARB); 
     } 
     //glutMainLoop(), cleaning memory, ending main 
     .. 
    } 

    //Updating the screen 
    void displayCB() 
    { 
     point_cloud.update(); 

     // clear buffer 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 

     // save the initial ModelView matrix before modifying ModelView matrix 
     glPushMatrix(); 

      glBindBuffer(GL_ARRAY_BUFFER_ARB, buffer); 
      glBufferSubData(GL_ARRAY_BUFFER_ARB, 0, (sizeof(char) * 640 * 480 * 3), point_cloud.points_color); 
      glBufferSubData(GL_ARRAY_BUFFER_ARB, (sizeof(char) * 640 * 480 * 3), (sizeof(float) * 640 * 480 * 3), point_cloud.points_position); 

      // enable vertex arrays 
      glEnableClientState(GL_VERTEX_ARRAY); 
      glEnableClientState(GL_COLOR_ARRAY); 

      glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0); 
      //EDIT: Added the right offset at the vertex pointer position 
      glVertexPointer(3, GL_FLOAT, 0, (void*)(sizeof(char) * 640 * 480 * 3)); 

      //EDIT: Was using GL_POINT instead of GL_POINTS 
      glDrawArrays(GL_POINTS, 0, 640*480); 

      glDisableClientState(GL_VERTEX_ARRAY); // disable vertex arrays 
      glDisableClientState(GL_COLOR_ARRAY); 

      glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); 

     glPopMatrix(); 

     glutSwapBuffers(); 
    }