2

I have to support some legacy code which draws point clouds using the following code:

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, (float*)cloudGlobal.data());

glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, (float*)normals.data());

glDrawArrays(GL_POINTS, 0, (int)cloudGlobal.size());
glFinish();

This code renders all vertices regardless of the angle between normal and the "line of sight". What I need is draw only vertices whose normals are directed towards us.

For faces this would be called "culling", but I don't know how to enable this option for mere vertices. Please suggest.

Aleksei Petrenko
  • 6,698
  • 10
  • 53
  • 87
  • 1
    discard in the vertex shader – ratchet freak May 15 '14 at 10:23
  • I do not have a vertex shader, as you can see this is an ugly OpenGL 1.x code and I do not want to rewrite it using shaders. I will consider this only if this is the only way. – Aleksei Petrenko May 15 '14 at 10:30
  • IMHO `discard` is only in a fragment shader and can be implemented using tesselation / geometry shaders. In vertex shader, you would need to send the vertex off to infinity (by setting the homogenous `w` coordinate to 0). – the swine May 15 '14 at 12:48
  • 1
    The only way to effectively "`discard`" in a vertex shader would be to set the `w` coordinate of `gl_Position` to **0.0**. That will clip the vertex, but it might do some really funky things for primitive types other than `GL_POINTS`. – Andon M. Coleman May 15 '14 at 12:52
  • @theswine lol, you wrote yor comment at exactly the same time as I did. So you do not get the wrong idea, that was in response to ratchetfreak, not trying to correct anything you said. – Andon M. Coleman May 15 '14 at 12:55
  • @AndonM.Coleman don't worry, no hurt feelings :) – the swine May 15 '14 at 13:09

1 Answers1

4

You could try to use the lighting system (unless you already need it for shading). Set ambient color alpha to zero, and then simply use alpha test to discard the points with zero alpha. You will probably need to set quite high alpha in diffuse color in order to avoid half-transparent points, in case alpha blending is required to antialiass the points (to render discs instead of squares).

This assumes that the vertices have normals (but since you are talking about "facing away", I assume they do).

EDIT:

As correctly pointed out by @derhass, this will not work.

If you have cube-map textures, perhaps you can copy normal to texcoord and perform lookup of alpha from a cube-map (also in combination with the texture matrix to take camera and point cloud transformations into account).

Actually in case your normals are normalized, you can scale them using the texture matrix to [-0.49, +0.49] and then use a simple 1D (or 2D) bar texture (half white, half black - incl. alpha). Note that counterintuitively, this requires texture wrap mode to be left as default GL_REPEAT (not clamp).

If your point clouds have shape of some closed objects, you can still get similar behavior even without cube-map textures by drawing a dummy mesh with glColorMask(0, 0, 0, 0) (will only write depth) that will "cover" the points that are facing away. You can generate this mesh also as a group of quads that are placed behind the points in the opposite direction of their normal, and are only visible from the other side than the points are supposed to be visible, thus covering them.

Note that this will only lead to visual improvement (it will look like the points are culled), not performance improvement.

Just out of curiosity - what's your application and why do you need to avoid shaders?

the swine
  • 10,713
  • 7
  • 58
  • 100
  • Unfortunately, this is not going to work. The fixed-function lighting of the GL will never calculate the alpha value depending on the normal (which also wouldn't make much sense when you think about it), but simply uses the alpha value of the diffuse color material as the resulting alpha: "The value of A produced by lighting is the alpha value associated with `d_cm`." (From the [GL2.1 spec](http://www.opengl.org/registry/doc/glspec21.20061201.pdf), section 2.14.1) – derhass May 15 '14 at 18:48
  • @derhass You're right. My bad, I'm a bit rusty with the fixed pipeline. Well then, there is probably no way to cull the points. – the swine May 16 '14 at 13:59
  • Requirements change quickly and finally we decided to draw simple mesh instead of point cloud, so the problem has gone. Thank you for the answer. – Aleksei Petrenko May 20 '14 at 09:02