Skip to content

Lesson 2 Edit the Shaders

Steve Baer edited this page Jan 5, 2019 · 5 revisions

Let's draw some fancier geometry with a custom set of OpenGL shaders.

1 - Set up a canvas to send curve data to GL Shader component.

For this lesson, we will be sending points along a curve to a GL Shader to draw the curve a different way. Create a NURBS curve and divide it. Then feed the points along the curve to the GL Shader along with an input color.

Here is a link to the GH definition for this stage in the lesson if you don't want to start from scratch.

2 Display the points with the shader

Disable the preview for the NURBS curve component and the Divide Curve component to see what the shader is drawing. You should see something like the following.

We are seeing points because that is how we are telling OpenGL we want to interpret the input. Change the mode to GL_LINE_STRIP using the context menu for the GL Shader component.

The points are what the shader uses to draw geometry. If you adjust the number of points that you divide the curve by, you should see chunkier or smoother display of the curve based on the number of input points.

3 Add a new input to GL Shader

Zoom in on the GL Shader component's input area until the +/- symbols for parameter addition/removal are displayed. Click on a + symbol to add a new input parameter and then rename the parameter to "tangent". Wire the tangent output of the Divide Curve component to this new input as shown below.

Adding this input parameter allows for a new variable to be used in the GL Shader program.

Here is a link to the GH definition for this stage in the lesson.

4 Edit the shader code

Double click on the GL shader component to show the code editor for the associated GLSL shaders. A shader program is composed of several "sub-programs" that operate during different stages of data processing. For now, we will only focus on the vertex, geometry, and fragment shader "sub programs" that when combined form a single GL shader. Each tab on the code editor represents these different shaders and the GL Shader component starts with some default code to get things started (the geometry shader stage is empty which is fine.)

Make sure the vertex tab is selected as we are going to start by editing the vertex shader.


Shader references

https://www.khronos.org/opengl/wiki/Vertex_Shader

https://www.khronos.org/opengl/wiki/Geometry_Shader

https://www.khronos.org/opengl/wiki/Fragment_Shader

Clone this wiki locally