7

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,...)
enter image description here

but I cannot draw (the screen is blank) when I use glDrawElements(GL_TRIANGLES,...) or glDrawElements(GL_TRIANGLE_STRIP,...).
enter image description here

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.

12
  • 5
    Try replacing "#" with "number of"
    – Laurel
    Commented Feb 5 at 19:59
  • 1
    Thanks. Tried that...but same error unfortunately. I can't understand it. The code seems properly formatted in THIS question. But when I try to submit this as a question I never get past the error to submit the question.
    – Craig
    Commented Feb 5 at 20:20
  • What if you surround the lines that start with # with 3 backticks just as if it were code? Commented Feb 5 at 20:24
  • I looked for a place to ask a question. I couldn't find another place. Don't know the difference between SO and SOM.
    – Craig
    Commented Feb 5 at 20:27
  • @HovercraftFullOfEels I've changed the '#' to 'nr. of'. Just to be sure I've also changed '*.ply' to 'PointCloudImage.ply'. The error persists. Appreciate the suggestions. Thanks.
    – Craig
    Commented Feb 5 at 20:30
  • 1
    Does the preview when posting on SO show the code formatted as code?
    – Jon Skeet
    Commented Feb 5 at 20:34
  • 6
    (As an aside, if you're using code fences, you don't need to add four spaces at the start of line as well - and doing so harms readability by indenting everything.)
    – Jon Skeet
    Commented Feb 5 at 20:35
  • 2
    Don't know the difference between SO and SOM. Meta sites (like this one) are for talking ABOUT the main site. Things like feature requests and bug reports are discussed here. The main site is for actual questions about programming.
    – Fastnlight
    Commented Feb 5 at 20:55
  • @JonSkeet Yes, the preview shows the code formatted as code.
    – Craig
    Commented Feb 5 at 21:01
  • 5
    Thanks all. It seems it was the '=' character near the top of the post. "vertices = 2,411,523" changed to "vertices: 2,411,523".
    – Craig
    Commented Feb 5 at 21:17
  • 8
    @Craig you should write that up as an answer below. Might help somebody else in the future
    – Phil
    Commented Feb 5 at 22:25
  • 1
    Though you didn't ask, if I saw this question go through I would likely have voted to close as "needs more focus" almost reflexively. I wrote an answer here that I hope illustrates why. Commented Feb 6 at 6:12

2 Answers 2

9

It seems it was the '=' character near the top of the post.

I changed "vertices = 2,411,523" to "vertices: 2,411,523" and that worked (was able to submit the question).

2
  • 10
    A thought: Wouldn't it be great if, when a format error is picked up the lines which are problematic are highlighted? How do I make this suggestion to the SO team?
    – Craig
    Commented Feb 6 at 7:52
  • 1
    You can create a question tagged feature-request on MSO to create a feature request.
    – cafce25
    Commented Feb 6 at 8:19
-5

#vertices = 2,411,523
#triangle faces = 4,821,969
#triangle indices = 14,465,907

Text like this looks an awful lot like code.

If your intent is to show a comment from the code, then it should be inside the corresponding code block, as a comment.

If your intent is to make claims about the point cloud data, please use complete English sentences to do so - and unless there is a clear reason why you expect the exact numeric values to be relevant to the problem, round them off in order to communicate more clearly: e.g. "There are about 2.4 million vertices, 4.8 million triangle faces and 14.5 million triangle indices described in the data".

Assuming that even that much precision is relevant. It's probably enough to say "I read in a file in .ply format that describes on the order of millions of triangles". Maybe the size of the data doesn't matter at all - are you asking a performance-related question, or something related to running out of memory? Did you try (as you should, in order to create a MRE to check if the problem happens with smaller input data? If it still goes wrong with the smaller data, then we aren't interested in the overall scale of your project - we're interested in the question you are asking. If the larger data is necessary to reproduce the problem, try to compare the input size to your system resources; try timing with different sizes to see if there's a threshold effect or unexpected worse-than-linear performance; etc.

Keep in mind that your question will be read by many more people than you or the people answering it - and this is even more true if it's judged to have any value to the site. While we're happy to edit questions to improve them, we do expect people to try to write clearly and effectively in the initial attempt. Think about how the complete page will look after all is said and done, and what you would want it to say if you were reading it out of a professionally written guide. When you post a question on Stack Overflow, you are effectively trying to write the introduction to such an article.

7
  • 1
    Hi. # is generally accepted as 'numer of'. I did not copy and paste that from code. I didn't think much of it. Most of the examples explaining use of glDrawElements() in OpenGL use very small, synthetics models e.g. 24 points for a cube. The context of using an actual point cloud from LIDAR scanning is worth noting.
    – Craig
    Commented Feb 6 at 7:02
  • 2
    The fact that it's understandable does not change the facts that a) it looks like code and probably has a lot to do with the automated detection you ran into; b) it does not meet our copyediting standards; c) the purpose of questions posted on Stack Overflow is not simply to get your code project to work, but to contribute to a high-quality, searchable reference Q&A library as described in the tour. Commented Feb 6 at 7:21
  • 2
    I get you point but remember that people will use common language/symbols to express themselves. That is what I did. If it becomes too onerous and "specialist" to post questions, then people won't. Better to share this feedback. I've seen many poorly worded questions that still conveyed the gist and I understood them. Good for them I say. Being pedantic does not help this site.
    – Craig
    Commented Feb 6 at 7:27
  • 2
    You seem to be under the apprehension that the site's success depends on many more questions being asked. Considering that the site's scope is "programming" versus "literally everything", but it already has more than three times as many questions as Wikipedia has articles, I'm inclined to disagree. Being strict (pedantic, as you put it) certainly does help the site, because quality is an explicit goal, and operating a help desk is not. Commented Feb 6 at 7:30
  • 6
    To a point, it does help. Beyond that it does not help. Spending hours finding one symbol is not sufficiently user friendly to me. Automation is a double edged sword. Reading up on the error I found numerous complaints about wasted time. Note: Not making any assumptions about quantity of questions.
    – Craig
    Commented Feb 6 at 7:31
  • 1
    @Craig Don't abuse symbols & don't write in sentence fragments. Use clear language.
    – philipxy
    Commented Feb 6 at 15:54
  • Thanks @philipxy. Will know to watch for certain things in future...but note I don't think that negates the value of the format checker "highlighting problematic lines".
    – Craig
    Commented Feb 6 at 17:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .