COMPUTER GRAPHICS PRACTICAL LESSON 10 Shaders and CG

  • Slides: 12
Download presentation
COMPUTER GRAPHICS PRACTICAL LESSON 10 Shaders and CG

COMPUTER GRAPHICS PRACTICAL LESSON 10 Shaders and CG

What is shader? Shaders are programs that allow program the GPU pipeline and enable

What is shader? Shaders are programs that allow program the GPU pipeline and enable flexibility in the graphic pipeline. They can be programmed using language Open. GL Shading Language, or GLSL. We will focus on language of NVIDIA – CG Please download and install NVIDIA CG TOOLKIT ver 2. 0 or above.

Shaders Types Vertex Shaders – process vertices, position, texture but not creating vertices. The

Shaders Types Vertex Shaders – process vertices, position, texture but not creating vertices. The output goes to the next stage in the pipeline. Geometry Shaders – add or remove vertices from the mesh. Add volume to existing meshes. Pixel Shaders (a. k. a. fragment shader ) – process pixels, lighting and related effects.

CG Each shader code is written in separate file with extension CG. It is

CG Each shader code is written in separate file with extension CG. It is compiled during runtime. Each shaper runs on different part of the pipeline, which enables high degree of flexibility. With this technology, programmers started performing computations on the GPU , which is the base for CUDA.

CG Objects CG contains special objects to work with this external code: CGprogram –

CG Objects CG contains special objects to work with this external code: CGprogram – contains runtime code of a shader. CGprofile – detect hardware profile, allow detecting which commands are valid. CGparameter – parameter for shader.

Loading Shader Program To load shader program we call cg. Create. Program. From. File

Loading Shader Program To load shader program we call cg. Create. Program. From. File To prepare it for running: cg. GLLoad. Program Now we need to init its parameters using cg. Get. Named. Parameter To use shader we call the function:

Using shaders To enable usage of shaders: CGprofile = cg. GLGet. Latest. Profile(CG_GL_VERTEX); cg.

Using shaders To enable usage of shaders: CGprofile = cg. GLGet. Latest. Profile(CG_GL_VERTEX); cg. GLEnable. Profile(profile); CGprofile = cg. GLGet. Latest. Profile(CG_GL_FRAGMENT); cg. GLEnable. Profile(profile); To run shder itself: cg. GLBind. Program

CG special types In addition to standard types of C – float and int,

CG special types In addition to standard types of C – float and int, there are special types in CG to store color, normal and other graphic structs: float 3 – store normal and position. float 4 – store position ( we need 4 values to be able to multiple matrix with them ). float 4 x 4 – to store matrices like model matrix and ect’

CG special types(cont’) The fields of structs can be accessed by x, y, z,

CG special types(cont’) The fields of structs can be accessed by x, y, z, w Alternatively – r, g, b, a We can copy several fields of source field: float 4(0. 0 f, i. Color. gba);

Shaders input and output We define 2 structs for shaders, in vertices shader Vert.

Shaders input and output We define 2 structs for shaders, in vertices shader Vert. In for input and Vert. Out for output. struct Vert. In{ float 4 i. Position float 3 i. Normal float 4 i. Color } : POSITION; : NORMAL; : COLOR 0; The label after the ‘: ’ mark define data source.

Shader main function This function is called for every vertex that reaches vertex shader,

Shader main function This function is called for every vertex that reaches vertex shader, and every pixel on fragment shader. Input struct contains information of specific object that current instance run on. It always returns instance of output struct. The output of a shader in an early stage of the pipeline is the input of the shader of a later stage.

Passing parameters Some parameters are created automatically, from input arrays received by vertex arrays:

Passing parameters Some parameters are created automatically, from input arrays received by vertex arrays: POSITION, NORMAL We can define parameters of ourselves using commands: cg. GLSet. Parameter 4 f cg. GLSet. State. Matrix. Parameter