0

I have a very large set of points (~300k) derived from an rgb & depth image and for the sake of obtaining geometric information I have calculated the normal vector for each of those points. However, I can't proceed to any further calculations until I'm certain the normal vectors are correct, so I decided to display each vector with a line:

glm::vec3 lvec = point + normal;
normals[index + 0] = point.x;
normals[index + 1] = point.y;
normals[index + 2] = point.z;
normals[index + 3] = lvec.x;
normals[index + 4] = lvec.y;
normals[index + 5] = lvec.z;

Here point is a vector containing each point's coordinates and normal is its normal vector. I store both ends of the line contiguously in an array and after buffering, I use GL_LINES to draw the data.

glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);

glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, height * width * 6 * sizeof(GLfloat),
    normals, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

draw function:

glDisable(GL_PROGRAM_POINT_SIZE);
glLineWidth(3.0);

//binding shader program and setting uniform variables
glUseProgram(shader);
glUniformMatrix4fv(modelLocation, 1, GL_FALSE, &model[0][0]);
glUniformMatrix4fv(viewLocation, 1, GL_FALSE, &view[0][0]);
glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, &projection[0][0]);

//binding vertex array object and drawing
glBindVertexArray(VAO);
glDrawArrays(GL_LINES, 0, width * height * 2);

vertex shader:

#version 330 core

layout(location = 0) in vec3 position;

uniform mat4 model, view, projection;

void main() {

    gl_Position = projection * view * model * vec4(position, 1.0);

}

fragment shader:

#version 330 core

out vec4 colour;

void main() {

    colour = vec4(1.0, 0.0, 0.0, 1.0);

}

enter image description here

Naturally, there are 300k lines being drawn and the result is too dense for me to understand whether they are correct or not. Is there a way I could randomly discard some of them? There is a single draw call that does the drawing so I can't think of any way to use uniform variables to select which ones get drawn. Thanks in advance.

VlassisFo
  • 650
  • 1
  • 8
  • 26
  • 2
    I would do this on CPU side ... why not create a copy of your geometry but use only every n-th line ? this can be also done without the actual copy just by creating indices where only some percentage of lines are used ... I would try 1-10% not more ... so just adding single VBO to your project where just the indexes of your used points are ... Indices example can be found here: [complete GL+GLSL+VAO/VBO C++ example](https://stackoverflow.com/a/31913542/2521214) look for `#ifdef vao_indices` – Spektre Jun 25 '19 at 09:23
  • @Spektre You're probably right. I was hoping for a shader solution to save memory and processing time but the drawbacks aren't very bad either. Thanks for your reply :) – VlassisFo Jun 25 '19 at 09:26
  • with shaders this would be doable in Geometry shader where you would emit the normal line instead of creating VBO for it ... so either emit line only if `random()<=0.01` or set the size of lines above threshold to zero ... but beware geometry shaders are slow at least from mine experience but I had not used them for a while so implementations might change since then for the better – Spektre Jun 25 '19 at 09:28
  • @Spektre So geometry shader has an implementation of `random()`? I might try it! Submit an answer if you like so I can accept it – VlassisFo Jun 25 '19 at 09:32
  • have no clue if yes or not ... beware some operations outside fragment shader do not work correctly even if compiled/linked correctly ... havent used random in geometry yet ... – Spektre Jun 25 '19 at 09:33
  • btw this might be also doable in tesselation where you could compute normal from the connected points and also set line size to something or zero based on the same random if geometry will not work ... – Spektre Jun 25 '19 at 09:35
  • 1
    @VlassisFo *"So geometry shader has an implementation of `random()`"* - see [Random / noise functions for GLSL](https://stackoverflow.com/questions/4200224/random-noise-functions-for-glsl) – Rabbid76 Jun 25 '19 at 09:35
  • heh those are all based on Fragment coordinates that will not work in geometry nor teselation but can be modified to use vertex position instead ... – Spektre Jun 25 '19 at 09:38
  • @Spektre unfortunately I am using opengl 3 and i think tesselation shader was introduced in 4. I should migrate soon.. – VlassisFo Jun 25 '19 at 09:40
  • 1
    @VlassisFo heh don't worry I am still on GL 1.0 + VBO majority of the time as the new stuff is not working reliably outside nVidia gfx cards and Windows platform so I am forced to use the old stuff all the time. Sadly even that is no more what it was especially for AMD and Intel... – Spektre Jun 25 '19 at 09:47
  • @Spektre Thankfully, I haven't had to deal with compatibility issues. Its a university project and as long as it works on my laptop everything's fine :p – VlassisFo Jun 25 '19 at 09:53

0 Answers0