How do I fix the below format? I keep getting the error: "Your post appears to contain code that is not properly formatted as code." My code is indented 4 spaces and is between '''codeblock fences'''.
My question:
I am drawing a 3D point cloud in OpenGL4.6.
I read in a file with the point cloud data in *.ply format.
#vertices = 2,411,523
#triangle faces = 4,821,969
#triangle indices = 14,465,907
I am using glDrawElements() to draw the point cloud. The problem is that I can successfully draw the object with glDrawElements(GL_POINTS,...)
but I cannot draw (the screen is blank) when I use glDrawElements(GL_TRIANGLES,...) or glDrawElements(GL_TRIANGLE_STRIP,...).
I define one Vertex Array Object (VAO) and Vertex Buffer Object (VBO) which I set up with position(xyzw) data first, then color(rgba) data, then point size.
Here is the code to set up the VAO and VBO.
#define VERTEX_SIZE 4
//Create as global vars
std::vector<glm::vec4> vertex_buffer_data1; //x,y,z,w
std::vector<glm::vec4> vertex_buffer_data2; //x,y,z,w
std::vector<glm::vec4> color_buffer_data; //r,g,b,a
std::vector<glm::vec4> color_buffer_data_bkup; //r,g,b,a
std::vector<float> pointsize_buffer_data;
std::vector<unsigned int> indices_buffer_data;
GLuint VAO, VBO, EBO;
void initialize(void)
{
// create a Vertex Array Object and set it as the current one.
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glNamedBufferStorage(VBO,
(vertex_buffer_data1.size() * sizeof(decltype(vertex_buffer_data1)::value_type))
+ (color_buffer_data.size()* sizeof(decltype(color_buffer_data)::value_type))
+ (pointsize_buffer_data.size() * sizeof(decltype(pointsize_buffer_data)::value_type)),
nullptr,
GL_DYNAMIC_STORAGE_BIT);
// Transfer the {vertex) positions:
glNamedBufferSubData(VBO,
0, //offset in GL_ARRAY_BUFFER, which VBO is bound to.
vertex_buffer_data1.size() * sizeof(decltype(vertex_buffer_data1)::value_type),
&vertex_buffer_data1[0]);
// Transfer the (vertex) colors:
glNamedBufferSubData(VBO,
vertex_buffer_data1.size() * sizeof(decltype(vertex_buffer_data1)::value_type),
color_buffer_data.size() * sizeof(decltype(color_buffer_data)::value_type),
&color_buffer_data[0]);
// Transfer the (vertex) point sizes:
glNamedBufferSubData(VBO,
(vertex_buffer_data1.size() * sizeof(decltype(vertex_buffer_data1)::value_type))
+ (color_buffer_data.size()) * sizeof(decltype(color_buffer_data)::value_type),
pointsize_buffer_data.size() * sizeof(decltype(pointsize_buffer_data)::value_type),
&pointsize_buffer_data[0]);
// Create an Element Buffer Object that will store the indices array:
glGenBuffers(1, &EBO);
// Transfer the data from indices to EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
indices_buffer_data.size() * sizeof(unsigned int),
&indices_buffer_data[0],
GL_DYNAMIC_DRAW);
// Attribute binding
// Get the vertex position attribute in the vertex shader
GLint position_attribute = glGetAttribLocation(shader, "vertexPosition_modelspace");
// Specify how the data for position can be accessed
glVertexAttribPointer(position_attribute, // attribute - must match the shader.
VERTEX_SIZE, // =4 (size = 3 for xyz, = 4 for xyzw)
GL_FLOAT, // vertex data type
GL_FALSE, // normalized?
0, // stride
(void *)0); // array buffer offset
// Enable the attribute
glEnableVertexAttribArray(position_attribute);
// Get the color attribute in the vertex shader
GLint color_attribute = glGetAttribLocation(shader, "vertColor");
glVertexAttribPointer(color_attribute, // attribute - must match the shader.
4, // = 4 (3 for rgb, = 4 for rgba)
GL_FLOAT, // vertex data type
GL_FALSE, // normalized?
0, // stride
(void *)(vertex_buffer_data1.size() * sizeof(decltype(vertex_buffer_data1)::value_type)));
// Enable the attribute
glEnableVertexAttribArray(color_attribute);
// Get the color attribute in the vertex shader
GLint pointSize_attribute = glGetAttribLocation(shader, "vertexPointSize");
// Specify how the data for position can be accessed
glVertexAttribPointer(pointSize_attribute, // attribute - must match the shader.
1, // float
GL_FLOAT, // vertex data type
GL_FALSE, // normalized?
0, // stride
(void *)((vertex_buffer_data1.size() * sizeof(decltype(vertex_buffer_data1)::value_type))
+ (color_buffer_data.size() * sizeof(decltype(color_buffer_data)::value_type))));
// Enable the attribute
glEnableVertexAttribArray(pointSize_attribute);
}
The draw part of the code looks like this:
main()
{
some_code_to_load_the_point_cloud_and_populate_the_buffer_data_structures();
// Loop until the user closes the window
while (!glfwWindowShouldClose(window1))
{
//Note the Vertex Array Object (VAO) is bound in the initialise() function and never unbound so
//I don't bind it again in this while loop. Could that be a source of the error? Am I missing
//something?
some_code_to_set_viewport_to_left_side_of_screen();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen
glDrawElements(GL_POINTS, (GLsizei)indices_buffer_data.size(), GL_UNSIGNED_INT, NULL);
some_code_to_set_viewport_to_right_side_of_screen();
some_code_to_update_color_buffer_data(); //do some processing and update
//'std::vector<glm::vec4> color_buffer_data'
//which holds r,g,b,a values per vertex
// Update the (vertex) color buffer on the GPU.
glNamedBufferSubData(VBO,
vertex_buffer_data1.size() * sizeof(decltype(vertex_buffer_data1)::value_type),
color_buffer_data.size() * sizeof(decltype(color_buffer_data)::value_type),
&color_buffer_data[0]);
//do some processing and update 'std::vector<float> pointsize_buffer_data'
//which holds a float per vertex.
some_code_to_update_point_size_buffer_data();
// Update the (vertex) point sizes on the GPU.
glNamedBufferSubData(VBO,
(vertex_buffer_data1.size() * sizeof(decltype(vertex_buffer_data1)::value_type))
+ (color_buffer_data.size()) * sizeof(decltype(color_buffer_data)::value_type),
pointsize_buffer_data.size() * sizeof(decltype(pointsize_buffer_data)::value_type),
&pointsize_buffer_data[0]);
//do some processing and update 'std::vector<glm::vec4> vertex_buffer_data1'
//which holds x,y,z,w values
some_code_to_update_position_vertex_buffer_data();
// Update the (vertex) positions on the GPU.
glNamedBufferSubData(VBO,
0,
vertex_buffer_data1.size() * sizeof(decltype(vertex_buffer_data1)::value_type),
&vertex_buffer_data1[0]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen
if(drawTriangleFaces == false) //a flag which the user can toggle from keyboard input
//this draw call works for GL_POINTS but not for GL_TRIANGLES
glDrawElements(GL_POINTS, (GLsizei)indices_buffer_data.size(),
GL_UNSIGNED_INT, nullptr);
else
{//*************this doesn't draw anything - screen is blank*************
//this draw call works for GL_POINTS but not for GL_TRIANGLES nor GL_TRIANGLE_STRIP
glDrawElements(GL_TRIANGLES, (GLsizei)indices_buffer_data.size(),
GL_UNSIGNED_INT, nullptr);
}
}
}
I need to draw the point cloud both with and without the mesh (triangle faces).
Any suggestions please?
Note: The triangle faces are read in from file. I have checked that they are read in correctly. I can view the triangle faces (mesh) if I load the point cloud into an app like Meshlab.
#
with 3 backticks just as if it were code?