Point Sprite shader (OpenGL only)

This tutorial applies to OpenGL renderer only.

This shader allows you to use Point Sprites, meaning points that are drawn with a texture always facing the viewer.

This allows to draw points with various shapes, simply providing the point coordinate and a texture.

 

First define the shader:

private class PointSpriteShader : GLShader
        {
            private static string vertexCode = @"
void main()
{
    gl_Position = ftransform();
}";

            private static string fragmentCode = @"
uniform sampler2D tex;

void main()
{
    vec4 finalColor = texture(tex, gl_PointCoord);

    if (finalColor.w == 0)
       discard;
    gl_FragColor = finalColor;
}
";

            public PointSpriteShader() : base(vertexCode, fragmentCode)
            {
            }

            public override void SetParameters(object shaderParams)
            {
                ShaderParametersBase shaderParameters = (ShaderParametersBase)shaderParams;

                Enable(shaderParameters.renderContext);

                gl.Uniform1i(GetUniformLocation("tex"), 0);

                Disable();
            }
        }

 

Then derive the Point class so that it properly sets the shader when it is drawn:

class MyPoint : devDept.Eyeshot.Entities.Point
        {
            public GLShader shader;
            public MyPoint(Point3D p) : base(p)
            {
            }

            public Texture texture;

            const int GL_POINT_SPRITE = 0x8861;
            const int GL_COORD_REPLACE = 0x8862;

            protected override void DrawForSelection(DrawParams data)
            {
                base.Draw(data);
            }

            protected override void Draw(DrawParams data)
            {
                shader.Enable(data.RenderContext);
                
                data.RenderContext.SetTexture(texture);
                data.RenderContext.PushBlendState();

                // If transparency in the texture is desired, enable the blending
                data.RenderContext.SetState(blendStateType.Blend);

                gl.Enable(GL_POINT_SPRITE);
                gl.TexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, gl.TRUE);

                base.Draw(data);

                gl.Disable(GL_POINT_SPRITE);

                data.RenderContext.CloseTexture();
                data.RenderContext.PopBlendState();

                // Restore the current shader 
                data.RenderContext.SetShader(data.RenderContext.CurrentShader, data.ShaderParams, true);
            }
        }

 

 Then create the shader, compile it, create the derived point, assign the shader and a texture:

GLShader myShader = new PointSpriteShader();
            bool success = myShader.Compile(viewportLayout1.renderContext);
            if (success)
            {
                var texture = new Texture();
                var bmp = new Bitmap(@"star.png");

                texture.Load(vp.renderContext, bmp, textureFilteringFunctionType.Linear);
                
var rand = new Random(123); for (int i = 0; i < 100; i++) { var pt = new MyPoint(new Point3D(rand.Next(255), rand.Next(255), rand.Next(255))); pt.texture = texture; pt.shader = myShader; pt.LineWeight = 15; pt.LineWeightMethod = colorMethodType.byEntity; vp.Entities.Add(pt); } myShader.SetParameters(new ShaderParameters(vp.renderContext)); }

 Result:

 

Was this article helpful?
1 out of 1 found this helpful
Have more questions? Submit a request

Comments

1 comment
  • Hi.
    Is it possible to make each point in the FastPointCloud displayed with an asterisk?

Please sign in to leave a comment.