I'm following this tutorial to write an OpenGL application. I have a dataset of 3D objects, where each object is described by:
- position (a 3D vector)
- bounding-box (lower and upper 3D vectors)
- point cloud (in my case, a pcd file)
So far, I managed to draw a point cloud loaded from a pcd file (here you can find the code). In the render loop I wrote:
//3) bind VAO
glBindVertexArray(_VAO);
//4) draw
glDrawArrays(GL_POINTS,0,_num_vertices);
where in the GL_BUFFER_ARRAY
I stored the vertices of the cloud.
Now, my question is the following:
For this object I want also to draw a bounding-box (as a wireframe rectangle) and a set of 3d axis centered in its position (as a set of three cylinders). What is the best way to do that?
@MichaelIV
Thanks for your reply. I'm following your advice, but I get a weird behavior.
To populate the buffer I wrote the following code:
Eigen::Vector3f min(1.7921,-0.2782,0.0);
Eigen::Vector3f max(2.1521,0.1994,0.9915);
insertVertex(_box_vertices,min.x(),min.y(),min.z(),0.0,0.0,1.0);
insertVertex(_box_vertices,max.x(),min.y(),min.z(),0.0,0.0,1.0);
insertVertex(_box_vertices,max.x(),max.y(),min.z(),0.0,0.0,1.0);
insertVertex(_box_vertices,min.x(),max.y(),min.z(),0.0,0.0,1.0);
_num_box_vertices = _box_vertices.size()/6.0f;
std::cerr << "Box vertices:" << std::endl;
for(size_t i=0; i<_num_box_vertices; ++i){
std::cerr << _box_vertices[6*i] << " " << _box_vertices[6*i+1] << " " << _box_vertices[6*i+2] << " ";
std::cerr << _box_vertices[6*i+3] << " " << _box_vertices[6*i+4] << " " << _box_vertices[6*i+5] << std::endl;
}
where _box_vertices
is a std::vector<float>
array and insertVertex
is just an utility function. If I print the array this is what I get:
dede@srrg-02:~/source/develop/opengl_viewer/bin$ ./opengl_viewer
Box vertices:
1.7921 -0.2782 0 0 0 1
2.1521 -0.2782 0 0 0 1
2.1521 0.1994 0 0 0 1
1.7921 0.1994 0 0 0 1
So, it should be correct. Then I use this code (OpenGL 3.3) to allocate gl objects:
//[OPENGL] create box buffers
glGenBuffers(1, &_bVBO);
glBindBuffer(GL_ARRAY_BUFFER, _bVBO);
glBufferData(GL_ARRAY_BUFFER, _box_vertices.size()*sizeof(float), &_box_vertices[0], GL_STATIC_DRAW);
glGenVertexArrays(1, &_bVAO);
glBindVertexArray(_bVAO);
//[OPENGL] link box vertex attributes
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3* sizeof(float)));
glEnableVertexAttribArray(1);
and this to render them:
//3) bind box VAO
glBindVertexArray(_bVAO);
//4) draw box
glDrawArrays(GL_LINE_LOOP,0,_num_box_vertices);
but the result is not what I would expect:
It looks like each line starts from (0,0,0) instead of starting from my desired point.
Can you please explain me how to fix that?
Thanks.