Using custom shader in Rendered display mode

This tutorial applies to OpenGL renderer only.

In Eyeshot 5 we added support for shaders for graphics cards with OpenGL greater than 3.3 to create a variety of effects in the Rendered display mode (shadows, Phong lighting, fade out of planar reflections...).

The user can create a custom shader and use it to affect the way entities are drawn.

Beware that this will override the shaders used by Eyeshot to draw that entity so you will lose all Eyeshot's effects.

Here's how to do it.

 

1. Define a class derived from Eyeshot GLShader class and a class to store the shader parameters:

        class MyShaderParameters : ShaderParametersBase 
        { 
            public int MyParam1;

            public MyShaderParameters(RenderContextBase context, int myParam1) : base(context) 
            { 
                MyParam1 = myParam1; 
            } 
        } 

        private class MyShader : GLShader
        { 
            public MyShader(string vertexCode, string fragmentCode)
                : base(vertexCode, fragmentCode) 
            { 
            } 

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

                Enable(shaderParameters.renderContext); 

                gl.Uniform1i(GetUniformLocation("MyParam1"), shaderParameters.MyParam1); 

                Disable(); 
            } 
        } 

 

2. Define a derived entity class that stores the custom shader and enables it in the SetShader() method:

        class MyMesh : Mesh
        {
            public GLShader shader;

            protected override void SetShader(DrawParams data)
            {
                // enable the custom shader for this entity                  
                shader.Enable(data.RenderContext);                
            }

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

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

            protected override void Render(RenderParams data)
            {            
                base.Render(data);                                

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

 

3. After the Form has been loaded (for instance in the Form.OnLoad() method) create the shader, compile it, assing the parameters and assign the shader to the custom entity. Finally add the entity to the ViewportLayout:

            // Define the strings with the shader programs source code  
            string vertexShaderSource = "/*Vertex Shader Program goes here.*/" + 
                                        @"void main(void)  
                                        {                                                                         
                                            gl_Position = ftransform(); 
                                        }"; 
            string fragmentShaderSource = "/*Fragment Shader Program goes here.*/" + 
                @" 
                void main(void) 
                {                 
                    gl_FragColor = vec4(1,0,1,1);                            
                }"; 

            // Create the shader and compile it  
            GLShader myShader = new MyShader(vertexShaderSource, fragmentShaderSource);
            bool success = myShader.Compile(viewportLayout1.renderContext);

            if (success) 
            { 
                // Create the parameters and set them to the shader 
                int myParam1 = 3;
                MyShaderParameters shaderParams = new MyShaderParameters(viewportLayout1.renderContext, myParam1);
                myShader.SetParameters(shaderParams); 

                // Create the custom entity and assign the shader to it 
                MyMesh myMesh = Mesh.CreateSphere<MyMesh>(10, 10, 10);
                myMesh.shader = myShader; 

                // add the custom entity to the ViewportLayout             
                viewportLayout1.Entities.Add(myMesh); 
            }
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.