2011-09-26 14 views
8

Intento hacer un visualizador de nube de puntos dinámico. Los puntos se actualizan en cada cuadro con Kinect Sensor. Para agarrar los marcos, estoy usando OpenCV y GLUT para visualizar. La API de OpenCV arroja un 640 x 480 (float *), para la posición de los puntos xyz, y un 640 x 480 (int *) para los datos de color rgb. Para obtener el máximo rendimiento, estoy tratando de usar Vertex Buffer Object en modo transmisión en lugar de una simple Vertex Array. Puedo renderizarlo con Vertex Array, pero no se está procesando nada con mi implementación de VBO. Intenté un montón de órdenes diferentes en las declaraciones, pero no puedo encontrar lo que me falta. ¿Alguien puede tratar de señalarme en la dirección correcta? Aquí está el código simpflified: (he reescrito la versión incorrecta, tal como solicitó por Christian Rau, así que ustedes pueden entender mis errores)Representación de Kinect Point Cloud con Vertex Buffer Object (VBO)

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

comentario sin relación alguna: en lugar usar 'glBufferData' (en lugar de' glBufferSubData') al actualizar el búfer entero. Esto podría ayudar al conductor a optimizar las cosas, pero no voy a entrar en detalles sobre eso ahora. Al menos en el código principal, el 'glBufferSubData' que sigue a' glBufferData' es completamente inútil. –

+1

Debería ** adjuntar ** nuevas versiones de su código a la pregunta, en lugar de cambiar completamente el código de la pregunta después de cada respuesta o comentario, ya que esto invalida otras respuestas y las personas que leen la pregunta y las respuestas se confunden por completo. –

+0

Lo hago por postint como una nueva respuesta? –

Respuesta

0

PS: El programa está en funcionamiento, pero no puedo ver cualquier mejora en el rendimiento promedio en comparación con la versión de Vertex Array. ¿Está eso bien? EL PROBLEMA FUE RESUELTO. He hecho tres alteraciones:

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. 

El código final:

//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(); 
    } 
Cuestiones relacionadas