Download presentation
Presentation is loading. Please wait.
1
Computer Graphics Practical Lesson 10
Shaders and CG
2
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 OpenGL Shading Language, or GLSL. We will focus on language of NVIDIA – CG Please download and install NVIDIA CG TOOLKIT ver 2.0 or above.
3
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.
4
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.
5
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.
6
Loading Shader Program
To load shader program we call cgCreateProgramFromFile To prepare it for running: cgGLLoadProgram Now we need to init its parameters using cgGetNamedParameter To use shader we call the function:
7
Using shaders To enable usage of shaders: cgGLEnableProfile(profile);
CGprofile profile = cgGLGetLatestProfile(CG_GL_VERTEX); cgGLEnableProfile(profile); CGprofile profile = cgGLGetLatestProfile(CG_GL_FRAGMENT); To run shder itself: cgGLBindProgram
8
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: float3 – store normal and position. float4 – store position ( we need 4 values to be able to multiple matrix with them ). float4x4 – to store matrices like model matrix and ect’
9
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: float4(0.0f,iColor.gba);
10
Shaders input and output
We define 2 structs for shaders, in vertices shader VertIn for input and VertOut for output. struct VertIn{ float4 iPosition : POSITION; float3 iNormal : NORMAL; float4 iColor : COLOR0; { The label after the ‘:’ mark define data source.
11
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.
12
Passing parameters Some parameters are created automatically, from input arrays received by vertex arrays: POSITION, NORMAL We can define parameters of ourselves using commands: cgGLSetParameter4f cgGLSetStateMatrixParameter
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.