Cg ShadingTutorial (Open GL)

parakeetincurableSoftware and s/w Development

Dec 13, 2013 (4 years and 6 months ago)


Cg ShadingTutorial (Open GL)

CIS 700/010

GPU Programming and Architecture

Instructor: Dr. Suresh Venkatasubramanian

TA: Paul Kanyuk


What is Cg?

How to Install/Run Cg programs

Anatomy of a Cg Shader.

Cg Examples


What is Cg?

Cg stands for “
C for Graphics

Cg compiles to code that can be executed by a GPU’s
programmable fragment or vertex processor

Cg is “theoretically” cross
platform, API independent
and future
proof. In reality, porting Cg code from
system to system is a pain. For this class, we will
use Cg and

Visual Studio


What is Cg used for?

What does Cg look like?

Where do I get Cg?

Download the latest
Cg Toolkit
, which contains:

Cg Compiler

Cg Runtime

Cg User’s Manual

Cg Browser

Sample Cg Shaders

*Be sure to let the installer setup the appropriate
environment variables (PATH, CG_LIB_PATH,

Will Cg work on my Machine?

How old is your graphics card? Anything over a year old is
starting to push it.

Try running the Cg Browser, and see how many of the effects run.
If none run, you’re in trouble. If some of the fancy ones don’t,
have no fear, that could be Nvidia’s way of scaring ATI users.

A Better Test

Try building and running
Program Files

in Visual Studio.

If you get errors about vertex or fragment programs being
unsupported, your gpu is probably too old. (be careful that the
PATH includes the Cg bin directory, if not, these errors can
come up as well).

If all else fails, Emulate.

If your hardware is just too old, you can always run Cg
programs via software emulation. This technique, however, is
very very

How can I use Cg?

Your application must call the
Cg Runtime

to invoke the Cg
programs and pass the appropriate parameters.

Cg Runtime

The Cg Runtime can be more challenging than Cg

For complete documentation on the Cg Runtime, see
Cg User’s Manual

The best way to learn is by hacking and poking
Program Files

in Visual Studio.

Cg Runtime Example

Anatomy of a Cg Vertex Program

Anatomy of a Cg Fragment Program

Cg Examples

The following examples demonstrate, in building
complexity, how to use interesting Cg programs with
OpenGL and the Cg runtime.

Each example is derived from the Nvidia Cg OpenGL
test scene.

Green Sphere

Uses a simple vertex
program to color a
sphere solid green.

The fragment
program just sets its
color output to its
color input (does
nothing but pass the
data along).

Color Sphere

Same as green
sphere, but the Cg
Runtime is used to
pass color in as a
uniform parameter.

float myColor[4] = { 1, 0, 0,1 };


Normal Vertex Sphere

Uses a vertex
program to shade a
sphere with rgb
components equal
to the normal

The normals are
passed via a varying
vertex parameter.

Normal Fragment Sphere

Uses a fragment program to
set the sphere color to the
normalized interpolated
vertex normal
(demonstrating the benefit
of fragment programs over
vertex programs).

The normals are passed to
the vertex program via a
varying parameters, and are
passed to the fragment
program via texture

Texture Fragment Sphere

Uses a fragment program to
map a texture of the world
to a sphere.

The texture coordinates are
passed to the vertex
program via a varying

The texture is passed
directly to the fragment
program as a "sampler".

“Plastic” Per
Vertex Shading

Uses a vertex program
to calculate standard
"fixed function" lighting
(aka plastic).

Note the “faceted”
nature of the shading,
this will be addressed in
the next example.

“Plastic” Per
Fragment Shading

Uses a fragment
program to perform
standard "fixed
function" lighting (aka

Note that the per
fragment calculations
perform a much more
accurate rendering (But
Slower, most shading
uses a hybrid

Color Shaping using “lerp”

Uses a fragment program to
perform color shaping on the
diffuse and specular
components of the standard
"fixed function" lighting.

Color shaping is achieved by
lerp'ing between two colors
based on the diffuse and
specular float values.

float3 specular =
lerp(Ks0,Ks1,specularLight) *
lightColor * specularLight;

Normal Mapping

Uses a fragment program to
read normal and color
information from texture
maps and perform standard
"fixed function" plastic


C++ class for rendering the output of a
fragment program to a texture (facilitates
pass rendering).

You can download RenderTexture from

You will also need to install glew (openGL
Extension Wrangler):

RenderTexture Example

A rotating glutTorus is
rendered into a
texture, and then read
into a Cg fragment
program bound to a
rotating plane.

Note, depth as well as
color can be stored
with RenderTexture
(at variable bit

That’s all for now, be sure to Practice!

One of the best ways to learn Cg is by
hacking existing examples: The Nvidia
OpenGL CgSphere Demo, and the GPGPU
RenderTexture example.

HW1 will ask you to do just that. This
presentation as well as the slides will be
available on the course website.