You are on page 1of 185

GPU Programming And Cg Language Primer 1rd Edition

GPU CG

kang_yu_zhi@yahoo.cn

2009 9

GPU CG

GPU Programming And Cg Language Primer 1rd Edition


kang_yu_zhi@yahoo.cn

2009 9

3

26

4
7

17

IT

5

5


GPU
Cg NVIDIA CgUsersManual

7
1 ............................................................................................................13
1.1 Programmable Graphics Processing Unit ....................................13
1.2 GPU VS CPU..................................................................................................15
1.3 ...........................................................................................18
1.4 ...................................................................................18
2 GPU .......................................................................................21
2.1 .......................................................................................................22
2.1.1 object space world space............................................................23
2.1.2 world space eye space................................................................24
2.1.3 eye space project and clip space ................................................25
2.2 Primitive Assembly && Triangle setup .........................................................26
2.3 ...................................................................................................27
2.3.1 Rasterization .........................................................................................27
2.3.2 Pixel Operation .....................................................................................28
2.4 .......................................................................................................30
2.4.1 GPU .....................................................................................30
2.4.2 Z Buffer Z ...................................................................................31
2.4.3 Stencil Buffer ........................................................................................33
2.4.4 Frame Buffer .........................................................................................34
2.5 ........................................................................................................35
3 Shader Language............................................................................................36
3.1 Shader Language ..................................................................................37
3.2 Vertex Shader Program.................................................................................39
3.3 Fragment Shader Program ............................................................................40
3.4 CG VS GLSL VS HLSL.................................................................................41
3.5 .......................................................................................................43
4 Cg ..................................................................................................44
4.1 Cg ................................................................................................44
4.2 CG ........................................................................................................45

8
4.3 CG .......................................................................................................45
4.3.1 CG ........................................................................................45
4.3.1 CGC .....................................................................................47
4.4 CG Profiles....................................................................................................50
5 CG ................................................................................................53
5.1 ...............................................................................................53
5.2 .......................................................................................................55
5.3 .......................................................................................................56
5.4 Interfaces...............................................................................58
5.5 .......................................................................................................58
6 CG ................................................................................60
6.1 Comparison Operators ......................................................60
6.2 Logical Operators..............................................................61
6.3 Math Operators .................................................................62
6.4 ...................................................................................................62
6.5 Swizzle ............................................................................................63
6.6 Conditional Operators........................................................64
6.7 ...........................................................................................65
6.8 Control Flow Statement .....................................................66
7 \..................................................................................67
7.1 Cg .....................................................................................................67
7.2 uniform..........................................................................................................68
7.3 const ..............................................................................................................69
7.4 \in\out\inout..............................................................69
7.5 SemanticBinding Semantics ..........................70
7.5.1 ...............................................................71
7.5.2 ...................................................................72
7.5.3 ...................................................................73
7.5.4 ...................................................................75

9
7.5.5 .......................................................................................75
8 ............................................................................................79
8.1 ...............................................................................................................79
8.1.1 ...............................................................................................80
8.2 ........................................................................................................81
8.3 .......................................................................................................82
8.4 CG ............................................................................................83
8.4.1 Mathematical Functions................................................84
8.4.2 Geometric Functions.....................................................87
8.4.3 Texture Map Functions .........................................88
8.4.4 Derivative Functions.....................................................91
8.4.5 Debugging Function......................................................94
8.5 ................................................................................94
...............................................................................................................96
9 illumination model .........................................................97
9.1 ...............................................................................................................97
9.2 Lambert ...............................................................................98
9.2.1 ..........................................................................................99
9.3 Phong .............................................................................102
9.3.1 phong ..................................................................................103
9.4 Blinn-Phong ...............................................................................107
9.5 Rendering Equation ........................................................109
9.6 .....................................................................................................110
10 ............................................................................................ 111
10.1 Cook-Torrance .........................................................................112
10.1.1 Cook-Torrance .................................................116
10.2 BRDF .......................................................................................118
10.2.1 BRDF ...............................................................118
10.2.2 ............................................................................119

10
10.3 Bank BRDF ................................................................................120
10.4 ...................................................................................................123
11 ........................................................................124
11.1 Snell Fresnel ........................................................................125
11.1.1 Snell ......................................................................125
11.1.2 ................................................................................................126
11.1.3 Fresnel ...................................................................................127
11.2 ....................................................................................................128
11.3 ...................................................................................132
11.4 ...........................................................136
12 Projective Texture Mapping.......................................137
12.1 ...............................................................................137
12.2 Homogeneous Texture Coordinates..........................139
12.3 ........................................................................................139
12.4 ...................................................................................................143
13 Shadow Map ..............................................................................................144
13.1 depth map.....................................................................................144
13.2 Shadow map shadow texture ..................................................145
13.3 Shadow map .................................................................146
14 Volume Rendering .........................................................152
14.1 ...............................................................................153
14.2 .......................................................................................153
14.3 ...................................................................................154
14.4 Volume Data .........................................................................155
14.4.2 Voxel ................................................................................156
14.4.1 Volume Texture ............................................................157
14.5 ...............................................................................................159
15 Ray Casting................................................................160
15.1 ...................................................................................160

11
15.1.1 ...........................................................................................161
15.2 ...................................................................161
15.2.1 ......................................................................161
15.2.2 ..................................................................................163
15.2.3 ..............................................................................164
15.2.2 ..........................................................166
15.3 ...................................................................................................168
15.4 ...................................................................................169
15.5 ...................................................................................................172
A ................................................................................................173
B.............................................................................175
CStencil Shadow ............................................................178
....................................................................................................................182

Alice
Alice

Alice

-------Lewis Carroll Alices adventures in Wonderland


12
1

------Henry David Thoreau

1.1 Programmable Graphics Processing Unit

Programmable Graphics Processing UnitGPU

GPU 20 70 80
monolithic GPU
20 80 90
digital signal processor chip GPU
1991
S3 Graphics 2D 1995 PC
2D
fixed-function Windows accelerators
PC

1998 NVIDIA modern GPU GPU


20 70 1998
pre-GPU 1998 GPU modern GPU pre-GPU
SGIEvans & Sutherland GPU GPU

modern GPU transistorsmicrochip


GPU CPUIntel 2.4GHz Pentium IV
5 5 55 million NVIDIA GeForce FX GPU
1 2 5 125 million NVIDDIA 7800 GXT

13
3 2 302 million

Modern GPU 1998 4 NVIDIA


1998 Modern GPU Modern GPU
Modern GPU NVIDIA TNT2ATI Rage 3Dfx Voodoo3
GPU CPU
GPU
GPU

1999 2000 modern GPU GPU


3D Object Transformation and Lighting,
T&L OpenGL DirectX7

workstation GPU
cube mapNVIDIA GeForce256
GeForce MAXATI Radeon 7500

2001 modern GPU GPU vertex


programmability GeForce 3GeForce 4TiATI 8500
GPU GPU
GPU
2009

Direct8 OpenGL
vertex programmability
GPU fragment programmability
modern GPU vertex programmability
fragment programmability GPU

modern GPU 2002 2003 NVIDIA GeForce


FX ATI Radeon 9700 GPU vertex

14
programmability fragment programmability DirectX OpenGL
API vertex programmability fragment programmability 2003
DirectX OpenGL
GPU GeForce
ATI
DirectX OpenGL

1. vertex programmability fragment programmability

2. IEEE32

3. 4 4

4.

5. >27.1GB/s

6. 1D2D3D

7. Render to TextureRTT

GPU Feng Liu Platform Independent


Real-time X3D Shaders and Their Applications in Bioinformatics Visualization

1.2 GPU VS CPU

GPU CPU GPUGPU


vertex programmability fragment programmability
2 6
Modern GPUs implement a number of graphics primitive operations in a way
that make running them much faster than drawing directly to the screen with the host
CPU. They are efficient at manipulating and displaying computer graphics, and their
highly parallel structure makes them more effective than typical CPUs for a range of

15
complex algorithms.
GPU highly parallel structure
GPU CPU 1 GPU VS
CPU GPU CPU CPU

GPU ALUArithmetic Logic Unit

CPU
OS GPU

Control ALU ALU

ALU ALU

Cache

DRAM DRAM

GPU CPU

1 GPU VS CPU

GPU

1
2 2D
CPU C++ GPU

16
2 CPU GPU

GPU CPU
GPU CPU GPU CPU
GPU CPU
GPU

GPU CPU

GPU CPUGPU
CPU
OpenGL fp2.0fp3.0
DirectX ps_4_0 profile
GPU
GPU
4-6
2012 GPU

GPU API
GPU
GPU
Shader language GPU

17
1.3

GPU Visualization in Scientific Computing

CT CPU
GPU
CPU
GPU GPU

GPUGPGPU
General-Purpose Computing on Graphics Processing Units,GPGP

GP 2 GPU

linear algebra
kruger and westermann 2003
physical simulation
Harris et al. 2003ray tracer
Purcell et
al.2002 Carr et al. 2002GPU
GPU29GPGPU
http://gpgpu.org/
GPU GPU
2004 BROOKGPU
C
BROOK GPU BROOKGPU AMD

30

1.4

GPU GPU
GPU bank BRDF
ray-casting GPU

18

GPU

GPU

GPU GPU CPU


GPU GPU shader language
3 GPU Shader
LanguageGPU GPU GPU
Shader Language vertex program
fragment program GPU

Cg
NVIDIA The Cg Tutorial The Definitive Guide to
Programmable Real-Time Graphics Cg ToolKit Users Manual
The Cg Tutorial The Definitive Guide to
Programmable Real-Time Graphics Cg _

Cg ToolKit
Users Manual


GPU
Cg BRDF
BRDF

19

GPU

20
2 GPU

GPU
GPU
GPU

GPU
GPU

CC++JAVA
CPU

b/s
Accelerated Graphic Port,AGP

387


234 GPU
10

Pixel

color buffer frame buffer

21

2.1


T&LT&L Transform & Lighting

Because, your application supplies the geometric data as a collection of vertices,


but the resulting image typically represents what an observer or camera would see
from a particular vantage point.
As the geometric data flows through the pipeline, the GPUs vertex processor
transforms the continuant vertices into one or more different coordinate system, each
of which serves a particular purpose. CG vertex programs provide a way for you to
program these transformations yourself.

GPU
GPU
GPU


Object spaceWorld spaceEye space
Clip and Project space 3 GPU

22
3 GPU

2.1.1 object space world space

When an artist creates a 3D model of an object, the artist selects a convenient


orientation and position with which to place the models continent vertices.

The object space for one object may have no relationship to the object space of
another object.3

object space object space coordinate


3DMAX
.max object space
coordinateobject space coordinate
object space coordinate world space coordinate

world space coordinate

23
world space coordinate object space coordinate
world space coordinate world
matrix

world coordinate space


eye coordinate space

object space GPU


world space
object space world space
object space world space world matrix

3D

11

2.1.2 world space eye space

GPU
world space eye space

eye space camera


viewing frustum
4

Frustum Culling

24
4

2.1.3 eye space project and clip space

Once positions are in eye space, the next step is to determine what positions are
actually viewable in the image you eventually intend trend.3

eye space
viewing frustum
Frustum Culling
clip

viewing frustum

(-1,-1,-1)(1,1,1)Canonical view volume,


CVV 9 CVV
XY 00Z

CVV
screen coordinate space

25
1. CVV

2. CVV

3.

viewing frustum
CVV

Twinsen
12 3


CVV

12
5

1 Twinsen

2 GPU C\C++
CPU
GPU
GPU
GPU C++ OGRE
Object-Oriented Graphics Rendering Engine

2.2 Primitive Assembly && Triangle setup

Primitive Assembly primitive,

26

Back-face Culling
OpenGL
enable disable

2.2 2.3

View Frustum Culling


Back-Face Culling
Occlusing Culling

Triangle Setup
Rasterizing

2.3

2.3.1 Rasterization

Rasterization is the process of


determining the set of pixels covered by a geometric primitive
Screen coordinate

27


2 3


10.4820.511021
52


DDA Bresenham
3

(vertex)
(pixel)

2.3.2 Pixel Operation

Pixel operation Raster Operation2 Raster


Operation

Pixel operation

2Texture operation

3Blending

28
Alpha
alpha
RGB Z
alpha
alpha 1.0 0

RGBA over

cd = aica + (1 a )cs over

a alpha ca cs

cd Over

compositingOGRE
compositor

z buffer
4.5 59

4Filtering Filtering

5 21.2.3

29
5 OpenGL Direct3D Raster Operations

2.4

GPU
z bufferstencil buffer
frame buffer color buffer

2.4.1 GPU

cpu gpu
cpu gpu
Shader
GPU glsl hlsl
GPU

30
6 GPU

2.4.2 Z Buffer Z

Z buffer depth buffer


Z
Z 01
Z 0.0 Z
1.0 z buffer
depth-buffer method Z
Z

Z Euclidean distance

steve Baker Learning to love your


Z-buffer GPU Z

az +b
z _ buffer _ value = (1 << N )
z
f
a=
f n

31
f n
b=
n f

f n

z N Z

z buffer z
Z

3D

7 AE
1 5 234
BCD BCD

Z Z
Z


1000

32
7 Z

16 Z buffer 24 Z buffer
32 32 Z buffer Z
Z-precision

Z Z

flimmeringZ-fighting

2.4.3 Stencil Buffer

A stencil buffer is an extra buffer, in addition to the color buffer and depth buffer
found on modern computer graphics hardware. The buffer is per pixel, and works on
integer values, usually with a depth of one byte per pixel. The depth buffer and stencil
buffer often share the same area in the RAM of the graphics hardware.

Stencil buffer, buffer


z buffer 15 z buffer 1 stencil buffer( 2 )
24 z buffer 8 stencil buffer 4

33
stencil buffer( Z buffer) Z buffer stencil buffer
Stencil buffer
buffer stencil buffer 1
shadow volume

2.4.4 Frame Buffer

A framebuffer is a video output device that drives a video display from a


memory buffer containing a complete frame of data. The information in the buffer
typically consists of color values for every pixel on the screen. Color values are
commonly stored in 1-bit monochrome, 4-bit palettized, 8-bit palettized, 16-bit
highcolor and 24-bit truecolor formats. An additional alpha channel is sometimes used
to retain information about pixel transparency. The total amount of the memory
required to drive the framebuffer depends on the resolution of the output signal, and
on the color depth and palette size.

Frame buffer buffer


Frame buffer color buffer
z buffer 12 frame buffer
webMediaBrands frame buffer
frame buffer
general main memory

Frame buffer: The portion of memory reserved for holding the complete
bit-mapped image that is sent to the monitor. Typically the frame buffer is stored in
the memory chips on the video adapter. In some instances, however, the video chipset
is integrated into the motherboard design, and the frame buffer is stored in general
main memory.

34
2.5

GPU
GPU

openGPU
2 3 Blinn A Trip
Down the Graphics Pineline

35
3 Shader Language
In the last year I have never had to write a single HLSL/GLSL shader. Bottom
line, I cant think of any reason NOT to use CG.

shader languageshade
Wikipedia shader language The job of a surface shading procedure is
to choose a color for each pixel on a surface, incorporating any variations in color of
the surface itself and the effects of lights that shine on the surface(Marc Olano)
shader language

GPU shader
language
shader language

shader language GLSL High Level Shading


LanguageCg C for Graphic shader language
C
shader language shader language GPU
shader language
GPU shader language

shader language C++\JAVA

Shader language
3 OpenGL GLSL Direct3D HLSL NVIDIA
Cg

shader language
Programmable Vertex Processor

36
Programmable Fragment Processor
vertex program fragment program
GLSLHLSL Cg

3.1 Shader Language

shader language shader program


vertex shader program fragment shader program

GPU Programmable Vertex Processor


Programmable Fragment Processor
[2] 1.2.4

The vertex and Fragment processing broken out into programmable units. The
Programmable vertex processor is the hardware unit that runs your Cg Vertex
programs, whereas the programmable fragment processor is the unit that runs your Cg
fragment programs.


GPU 8

37
8

3 GPU

Vertex program Fragment program

9 \

buffer

38
9 \

3.2 Vertex Shader Program

Vertex shader program Fragment shader program


Programmable Vertex Processor
Programmable Fragment Processo

GPU

10

vertex buffer
texture buffer

39

phong
Gourand
phong
phong Gourand

GPU shader

10

3.3 Fragment Shader Program

40

GPU

3.4 CG VS GLSL VS HLSL

Shader language 3 OpenGL GLSLOpenGL


Shading Language GLslang Direct3D HLSLHigh Level Shading
Language
NVIDIA Cg C for Graphic

GLSL HLSL OpenGL Direct3D


OpenGL Direct3D
OpenGL Direct3D

ATI OpenGL OSG


Open Scene
Graphic OpenGL 3D
GLSL
HLSL Cg HLSL
Cg OpenGL API
OpenGL
GLSL GLSL
OpenGL unix

41

HLSL windows

HLSL HLSL
Shader language HLSL


NVIDIA AMD ATI
GPU GeForce
Cg NVIDIA
GPU
GPU Cg
NVIDIA Cg
Cg

Cg OpenGL Direct3D
Cg OpenGLDirectX OpenGL DirectX
Cg OpenGL DirectX

Cg Microsoft NVIDIA
[1] 1.3.1 Microsoft and NVIDIAs
Collaboration to Develop Cg and HLSLHLSL Cg
Cg _ 29
HLSL Cg cg
Microsoft NVIDIA cg
GPU CG

Microsoft NVIDIA Cg

42
GLSL

Cg C for graphics C
C
C++ Java C cg C
CC++Java Cg
Cg C
Cg cg

NVIDIA Cg
CgGLSLHLSL Cg
2007 12 OSG CgGLSLHLSL
Cg GLSL
3D Cg
2009 Cg

OGRE Cg GLSL

In the last year I have never had to write a single HLSL/GLSL shader. Bottom
line, I cant think of any reason NOT to use CG.

3.5


CgGLSLHLSL
Cg GLSL

43
4 Cg

CgC for Graphcis NVIDIA Microsoft

Cg C Cg
*. Cg

4.1 Cg

NVIDIA http://developer.nvidia.com/object/cg_toolkit.html Cg
Toolkit 2009 10 Cg 2.2
bin cgc.exe NVIDIA Cg

Cg NVIDIA Cg

IDE
.cg NShader
http://nshader.codeplex.com/ Visual Studio2008
11

44
11 NShader

4.2 CG

Cg OpenGL Direct3D API


Cg Cg
OpenGL Direct3D

4.3 CG

4.3.1 CG

01

C C++
.exe GPU
static
coompilation
dynamic compilation

Cg Cg ( Cg

45
Runtime library) Cg Cg
OGRE
[2] 1.4.2 Cg
Cg

Cg Cg APIOpenGL Direct3D
OpenGL Direct3D Cg
OpenGL Direct3D
NVIDIA Cg cgc.exe

Cg
Cg
GPU Cg
AIP Cg
Cg Profiles profile profile
profile

Cg Profiles Cg Cg
Cg Profile

46
4.3.1 CGC

12 cgc -h

Cg Toolkit NVIDIA Corporation\Cg\bin


cgc.exe cgc h
12

Cg

cgc [options] file

[options]file Cg
profile

47

cgc profile glslv entry main_v test.cg

-profile profile glslv profile


-entry
main_v
test.cg
main\
main_v ,main_f

1. profile profile
profile
profile
profile profile fp20

cgc profile fp20 glslv entry main_v test.cg


profile

2. profile
profile
tex2D(sampler2D tex , float3 sz ,float2 dsdx , float2 dsdy ) fp20

cgc profile fp20 entry main_f test.cg

error C3004: function tex2D not supported in this profile

48
fp30

cgc profile fp30 entry main_f test.cg

for,whilevs_2_x, vp30, vp40,fp40


profilesCgUsersManualIn other profiles, for and

while loops may only be used if the compiler can fully unroll them (that is, if the
compiler can determine the iteration count at compile time)
profilesforwhile
profilesfor,while

error c6003instruction limit of exceeded

profiles

3. .cg

cgc profile glslv entry main_v

fatal error C9999: Cant open file:test

4. cgc Cg
GLSL HLSL
test.cg main_v
glsl direct.glsl

cgc profile glslv o direct.glsl entry main_v test.cg

5.


49


GPU

C++\JAVA

4.4 CG Profiles

Profile [1]A Cg profile


defines a subset of the full Cg language that is supported on a particular hardware
platform or APICgUsersManual 21 Cg profile
API Cg
shader language
:Profile
Profile Profile profile profile OpenGL DirectX
OpenGL DirectX
Cg

Cg compiler profiles
z OpenGL ARB vertex programs
Runtime profile: CG_PROFILE_ARBVP1
Compiler option: _profile arbvp1
z OpenGL ARB fragment programs
Runtime profile: CG_PROFILE_ARBFP1
Compiler option: _profile arbfp1

50
z OpenGL NV40 vertex programs
Runtime profile: CG_PROFILE_VP40
Compiler option: _profile vp40
z OpenGL NV40 fragment programs
Runtime profile: CG_PROFILE_FP40
Compiler option: _profile fp40
z OpenGL NV30 vertex programs
Runtime profile: CG_PROFILE_VP30
Compiler option: _profile vp30
z OpenGL NV30 fragment programs
Runtime profile: CG_PROFILE_FP30
Compiler option: _profile fp30
z OpenGL NV2X vertex programs
Runtime profile: CG_PROFILE_VP20
Compiler option: _profile vp20
z OpenGL NV2X fragment programs
Runtime profile: CG_PROFILE_FP20
Compiler option: _profile fp20
z DirectX 9 vertex shaders
Runtime profiles: CG_PROFILE_VS_2_X
CG_PROFILE_VS_2_0
Compiler options:-profile vs_2_x
-profile vs_2_0
z DirectX 9 pixel shaders
Runtime profiles: CG_PROFILE_PS_2_X
CG_PROFILE_PS_2_0
Compiler options: -profile ps_2_x
-profile ps_2_0
z DirectX 8 vertex shaders
Runtime profiles: CG_PROFILE_VS_1_1
51
Compiler options:-profile vs_1_1
z DirectX 8 pixel shaders
Runtime profiles: CG_PROFILE_PS_1_3
CG_PROFILE_PS_1_2
CG_PROFILE_PS_1_1
Compiler options: -profile ps_1_3
-profile ps_1_2
-profile ps_1_2
-profile ps_1_1

2009 10 profile
DirectX 11 profile

52
5 CG

Cgbuilt inprimitive

5.1

Cg 7
1. float32 profile
DirectX8 pixel profiles

2. half16

3. int32 profile int float

4. fixed12 fragment profiles

5. bool, if ?:
profiles

6. sampler*,the handle to a texture object


6
sampler, sampler1D, sampler2D, sampler3D, samplerCUBE, samplerRECT
DirectX profiles samplerRECT pixel
profiles NV40 vertex program profile CgUsersManual 30

7. string profile
Cg Cg runtime API
Cg

53
6 Wikipedia Cg
6 string Cg
(built-in vector data types)
float4 float 4 bool4 bool
4

4 Cg float1float2float3
float4 4

float5 array;//

float4 array = float4(1.0, 2.0, 3.0, 4.0);

float2 a = float2(1.0, 1.0);


float4 b = float4(a, 0.0, 0.0);

Cg 4*4

float1x1 matrix1;// float matirx1; x


float2x3 matrix2;// 2*3 6 float
float4x2 matrix3;// 4*2 8 float
float4x4 matrix4;// 4*4

float2x3 matrix5 = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};

54
Cg

C\C++ C\C++
Cg

5.2

General-purpose arrays can only be used as uniform parameters to a vertex


program. The intent is to allow an application to pass arrays of skinning matrices and
arrays of light parameters to a vertex program(3 Array )


Cg

Cg

Cg C

float a[10];// 10 float


float4 b[10];// 10 float4

float a[4] = {1.0, 2.0, 3.0, 4.0}; //

.length

float a[10]; //
int length = a.length;//

55
float b[2][3] = {{0.0, 0.0, 0.0},{1.0, 1.0, 1.0}};

int length1 = b.length; // length1 2


int length2 = b[0].length; // length2 3

4*4
float M[4][4];4 float4x4 M 16
float 4 float4x4 M[4]
4 4


profiles

Unsized arrays may only be declared as function parameters-they may not be


declared as variables. Furthermore, in all current profiles, the actual array length and
address calculations implied by array indexing must be known at compile time

3

8 8.1

5.3

Cg structure Cg C++

Cg
C++
public private shader
Cg

56

struct

myAdd

struct myAdd
{
float val;
float add(float x)
{
return val + x;
}
};
myAdd s;

float a = s.value;

float b = s.add(a);

profile

profile [3] structures and Member functions

,Cg
C
( Binding Semantics)

57
Cg POSTION )COLOR()NORMAL(
)Texcoord()

5.4 Interfaces

Cg
Cg
GPU
3
Interfaces

5.5

Cg C C

Cg

float a = 1.0;
half b = 2.0;
float c = a+b; // float c = a + (float)b;

float a = 1.0;
float b = a + 2.0; //2.0 float

Cg

58
float a = 1.0;
float b = a + 2.0h; //2.0h half

type suffix 3
z f float;
z h: half;
z x: fixed

59
6 CG

Cg7
Cg
operatoroperand

CgCC

CgSwizzle
CCg
>Cg
C

6.1 Comparison Operators


Cg6
1, bool

< expr < expr

<= expr <= expr

!= expr != expr

== expr == expr

>= expr >= expr

> expr > expr


1

60
Cgbool
boolean operator

Cgboolean operator

bool

float3 a = float4(0.5, 0.0, 1.0);


float3 b = float4(0.6, -0.1, 0.9);
bool3 c = a<b;

cfloat3(true, false, true);

6.2 Logical Operators

Cg3(boolean Operators) 2
bool

&& expr && expr

|| expr || expr

! !expr
2


bool

Cg&&||C
short-circuitingbool
bool

61
6.3 Math Operators

CgCg*
/-+%++*=/=+=-=

23.3Math Expressions
++
Cg

%int
error C1021: operands to % must be integral.

void function()
{
float2 a = float2(1.0, 1.0);

float b = 2.0;

f *= d;
f *= 2.0;
}

6.4

CgC
int

62
int2 a = int2(0.0,0.0);
int2 b = a>>1;

error C1021:operands to shr


must be integral.

float2 a = int2(0.0,0.0);
float2 b = a>>1;

6.5 Swizzle

Cgswizzle.
swizzleGPUswizzlexyzw
swizzle
rgba
swizzlergba

float4(a, b, c, d).xyz float3(a, b, c)


float4(a, b, c, d).xyy float3(a, b, b)
float4(a, b, c, d).wzyx float4(d, c, b, a)
float4(a, b, c, d).w float d

Cgfloat a float1 a
floatboolhalfswizzle

float a = 1.0;
float4 b = a.xxxx;

swizzle
swizzleerror C1010: expression left of .x is

63
not a struct or arrayarrayvector

[]

float a[3] = {1.0,1.0,0.0};


float b = a[0]; //
float c = a.x; //

6.6 Conditional Operators

expr1 ? expr2 : expr3;

expr1trueflasetrue,expr2expr3

if-else

if(a < 0){b = a}


else{c = a}

a < 0?(b = a) :( c = a);

Cgexpr1
boolexpr2expr3expr1

64
float3 h = float3(-1.0,1.0,1.0);
float3 i = float3(1.0,0.0,0.0);
float3 g = float3(1.0,1.0,0.0);
float3 k;

k = (h < float3(0.0,0.0,0.0))?(i):(g);

hfloat3(0.0, 0.0, 0.0)true, false, false,


iKgk
K(1.0, 1.0, 0.0)

6.7

Cg 3
Cg_
3.3.1

() [] -> .


! ~ ++ - + - * &
(type) sizeof


*/%
+-
<< >>
< >= > >=
== !=
&
^
|
&&
||
?:
= += -= *= /= %=
&= ^= != <<= >>=
,

65
3

6.8 Control Flow Statement

statement

Cifif-else
switchwhiledo-whileforCg
Cifif-elsewhileforbreak
for

Cgbool
CC01

vs_2_x, vp30vp40profilebranch
instruction
forwhileprofile3

In other profiles, for and while loops may only be used if the compiler can
fully unroll them (that is, if the compiler can determine the iteration count at compile
time)

profilesforwhile
fp40ps_3_0
profilesfor, whileerror c6003instruction
limit of exceededprofiles

returnrecursion
CgSwitch casedefaultCg
profile

66
7 \

GPU

Cg

1. GPUGPU
GPU

2. GPU

3.

7.1 Cg

67
intfloat structCg
C\C++ Cg

Semanticsbinding semantics

Cg inoutinout
\
in out

Cg uniformconst
C\C++


Cg

7.2 uniform

Cg [3]Program inputs and Outputs

1. Varying inputs,
GPU
Cg

2. Uniform inputs

Uniform

68
uniform float brightness,
uniform float4x4 modleWorldProject

float 4

OpenGL DirectX

Uniform

uniform Cg
uniform
uniform

Error C5056:uniformnot allowed on local variable

7.3 const

Cg const C\C++ const


const

const float a = 1.0;


a = 2.0; //
float b = a++; //

error C1026: assignment to const variable

const uniform
const uniform

7.4 \in\out\inout

C\C++

69
pass-by-value
pass-by-reference

C\C++

Cg
GPU Cg

1. in:

2. out:

3. inout:

void myFunction(out float x); // x

void myFunction(inout float x); // x

void myFunction(in float x); // x

void myFunction(float x); / in float x C\C++

return out \

7.5 SemanticBinding Semantics

70
Varying inputs
binding semantics

7.5.1

GPU

C/C++
Cg
Cg
binding semantics\
OpenGL Cg profiles
DirectX-based Cg profiles

\
POSITION

Semantics attached to parameters to non-main


functions are ignored(261 )


Shader POSITION

71
POSITION
POSITION
POSITION

OpenGL Cg profiles \
DirectX Cg profiles 3
25
In the OpenGL Cg profiles, binding semantics implicitly specify the mapping of varying
inputs to particular hardware registers. However, in DirectX based Cg profiles there is no
such implied mapping.

3 260
Often, these predefined names correspond to the names of hardware registers or API
resources

7.5.2

13 Cg vertex profile ,
profile

POSITION BLENDWEIGHT

NORMAL TANGENT

BINORMAL PSIZE

BLENDINDICES TEXCOORD0---TEXCOORD7

13

POSITION0 POSITION

in float4 modelPos: POSITION


POSITION OpenGL

72

in float4 modelNormal: NORMAL


NORMAL OpenGL

A
1
0

7.5.3


POSITION

Cg vertex profilesCg fragment


profilesPOSITIONPSIZEFOG,COLOR0-COLOR1,
TEXCOORD0-TEXCOORD7

POSITION
POSITION

73
void main_v(float4 position: POSITION,
//out float4 oposition : POSITION,
uniform float4x4 modelViewProj)
{
//oposition = mul(modelViewProj,position);
}

vp20vp30error C6014: Required output


HPOS not writtenvs_2_0vs_3_0error C6014
Required output POSITION not written.


struct

struct VertexIn
{
float4 position : POSITION;
float4 normal : NORMAL;

};

struct VertexScreen
{
float4 oPosition : POSITION;
float4 objectPos : TEXCOORD0;
float4 objectNormal : TEXCOORD1;
};

struct
POSITION


TEXCOORD
TEXCOORD

74
struct
vertex programPOSITION
fragment program

7.5.4

COLOR
GPU out
COLOR

void main_f(out float4 color : COLOR)


{

color.xyz = float3(1.0,1.0,1.0);
color.w = 1.0;
}

fragment profileDEPTH
multiple
render targets , MRTs

COLOR

7.5.5

\ 4 3 260

75
1.

[const] [in | out | inout]<type><identifier> [ :


<binding-semantic>][=<initializer>]

const inoutinout
type identifier


3

void mian_v(float4 position_obj : POSITION,


float3 normal_obj : NORMAL,

out float4 oPosition : POSITION,


out float4 oColor : COLOR,

uniform float4x4 modelViewProj)


{

}

2. struct

struct <struct-tag>
{
<type><identifier> [:<binding-semantic >];
};
C2E1v_Outpu 2 POSITION
COLOR C2E1v_green C2E1v_Outpu

76
struct C2E1v_Output {
float4 position : POSITION;
float3 color : COLOR;
};

C2E1v_Output C2E1v_green(float2 position : POSITION)


{
C2E1v_Output OUT;
OUT.position = float4(position,0,1);
OUT.color = float3(0,1,0);

return OUT;
}

3.

<type> <identifier> (<parameter-list>) [:<binding-semantic]


{
<body>
}
COLOR
float4 return

float4 main_v(float4 position: POSITION,


out float4 oposition : POSITION,
uniform float4x4 modelViewProj):COLOR
{
oposition = mul(modelViewProj,position);

float4 ocolor = float4(1.0,0,0,0);

return ocolor;
}

4.

77
<type> <identifier> [:<binding-semantic>][=<initializer>];

78
8

5 7 5
6 \ 7
Cg
\ C\C++ main() Cg

8.1

Cg
C\C++return typeparameter list

void

void myFunc(inout float val ) float myFunc(float vals[])


{ {
float sum = 0.0;
..
val += 10.0; return sum;
} }

void

error C0000: syntax error, unexpected(at token (


error C0501: type name expected at token (
error C1110: function main_v has no return statement

7.4 \

79
C\C++ Cg

8.1.1

C\C++
stephen C.Dewhurst C++
Cg
Cg [3]Array

The most important difference from C is that arrays are first-class types. That
means array assignments actually copy the entire array, and arrays that are passed as
parameters are passed by value, rather than by reference.

Cg first-class types

first-class

first-class Matthieu Sozeau and


Nicolas Oury First-Class Type Classes


Unsized Array

float myFunc( float vals[])


{
float sum = 0.0;
for(int i = 0; i< vals.length; i++)
{
sum += vals[i];
}
return sum;
}

80
myFunc
float

error C1102: incompatible


type for parameter

float myFunc( float vals[3])


{
float sum = 0.0;
for(int i = 0; i< vals.length; i++)
{
sum += vals[i];
}
return sum;
}

void main()
{
float a[2] = {0.0, 1.0};
float b[3] = {0.0, 1.0, 2.0};
myFunc(a); //
myFunc(b); //
}

8.2

Cg Functon Overlaoding C++

81
bool function(float a, float b) {return ( a == b);}
bool function(boo a, bool b) {return ( a == b);}

Cg

8.3

C\C++ main()


4.4 CG main
Cg

7.5

C2E1v_green

return CgNVIDIA
Corporation\Cg\examples\OpenGL\basic\ 01_vertex_program\C2E1v_green.cg

82

struct C2E1v_Output {
float4 position : POSITION;
float3 color : COLOR;
};

C2E1v_Output C2E1v_green(float2 position : POSITION)


{
C2E1v_Output OUT;

OUT.position = float4(position,0,1);
OUT.color = float3(0,1,0);

return OUT;
}

8.4 CG

C Cg

reflect
refract mul

GPU

Cg GPU
GPU

Cg

1. Mathematical Functions;
2. (Geometric Functions)
3. (Texture Map Functions)
4. (Derivative Functions)

83
5. (Debugging Function)

8.4.1 Mathematical Functions

4 Cg


abs(x)
acos(x) [-1,1]
[ 0, ]

all(x) 0 ture
flase&&
any(x) 0
true||
asin(x)
, [ 1,1]


,
2 2
atan(x)

,
2 2
atan2(y,x) y/x atan(x)
atan(x) =
atan2(x, float(1))
ceil(x) ceil(float(1.3))
2.0
clamp(x,a,b) x a a x b
b x
cos(x)
x [ 1,1]

cosh(x) hyperbolic cosine x



cross(A,B) (cross product)

degrees(x) (radians)
(degrees)

84
determinant(m)
dot(A,B) A B (dot product)
A B


exp(x)
e x e= 2.71828182845904523536
exp2(x)
2 x
floor(x) floor(float(1.3))
1.0 floor(float(-1.3))
-2.0 ceil(x)
fmod(x,y) x/y y 0
frac(x) Returns the fractional portion of a scalar or
each vector component
frexp(x, out x
exp) x = m* 2^exp m exp
x 0 0
isfinite(x)
true false;
(not-a-number NaN)
isinf(x)
true false;
isnan(x)
(not-a-number NaN) true
false;
ldexp(x, n)
x 2n
lerp(a, b, f)
(1 f ) a + b f a + f ( b a )

a b f
a b f

lit(NdotL, N L H
NdotH, m) m

4
X 1.0
Y N L < 0
0 N L
Z N L < 0
N H < 0 0 ( N H ) m
W 1.0

85
log(x)
ln ( x ) x 0

log2(x)
log (2x ) x 0

log10(x) ( x)
log10 x 0

max(a, b)
min(a,b)
modf(x, out ip) Cg Reference Manual
mul(M, N) M AxB
N BxC AxC

mul(M, v)
mul(v, M)
noise( x) 01


pow(x, y) y
x
radians(x)
round(x) Round-to-nearest closest integer to x

rsqrt(x) X x 0
saturate(x) x 0 0 x 1
1 x
sign(x) x 0 1 x 0
01 0
sin(x)
[ 1,1]

sincos(float x, x sin cos


out s, out c) s=sin(x)c=cos(x)
sin cos
!
sinh(x) hyperbolic sine
smoothstep(min, x minmax x=min
max, x) 0 x=max 1 x

x min 3 x min 2
2*( ) + 3*( )
max min max min
step(a, x) x<a 0 1
sqrt(x)
x x x 0

86
tan(x)
tanh(x)
transpose(M) M
4 Cg

8.4.2 Geometric Functions

5
Cg
3 normalize reflect
refract

1.

2. reflect refract


distance( pt1, pt2) Euclidean
distance
faceforward(N,I,Ng)
Ng I < 0 N-N

length(v) sqrt(dot(v,v))
normalize( v)
reflect(I, N) I
N I N

I

refract(I,N,eta) I N
eta I N
I N
000
I

87

5 Cg

8.4.3 Texture Map Functions

Cg ps_2_0ps_2_x
arbfp1fp30 fp40 profiles fully supported


tex1D(sampler1D tex, float s)

tex1D(sampler1D tex, float s, float dsdx, float dsdy)
derivatives
Tex1D(sampler1D tex, float2 sz)

Tex1D(sampler1D tex, float2 sz, float dsdx,float dsdy)
derivatives
Tex1Dproj(sampler1D tex, float2 sq)

Tex1Dproj(sampler1D tex, float3 szq)

Tex2D(sampler2D tex, float2 s)

Tex2D(sampler2D tex, float2 s, float2 dsdx, float2 dsdy)
derivatives
Tex2D(sampler2D tex, float3 sz)

Tex2D(sampler2D tex, float3 sz, float2 dsdx,float2 dsdy)
derivatives
Tex2Dproj(sampler2D tex, float3 sq)

Tex2Dproj(sampler2D tex, float4 szq)

texRECT(samplerRECT tex, float2 s)

texRECT (samplerRECT tex, float2 s, float2 dsdx, float2 dsdy)

texRECT (samplerRECT tex, float3 sz)


texRECT (samplerRECT tex, float3 sz, float2 dsdx,float2 dsdy)

88
texRECT proj(samplerRECT tex, float3 sq)
texRECT proj(samplerRECT tex, float3 szq)
Tex3D(sampler3D tex, float s)

Tex3D(sampler3D tex, float3 s, float3 dsdx, float3 dsdy)
derivatives
Tex3Dproj(sampler3D tex, float4 szq)

texCUBE(samplerCUBE tex, float3 s)

texCUBE (samplerCUBE tex, float3 s, float3 dsdx, float3 dsdy)
derivatives
texCUBEproj (samplerCUBE tex, float4 sq)

6 Cg

s z depth comparison
q perspective value,
S
0 1

1D
2D 3D TexREC

3D Volume Texture
14

15 16

89

14

proj

float4
uvproj

tex2Dproj(texture,uvproj);

float4 uvproj = uvproj/uvproj.q;


tex2D(texture,uvproj);

90
8.4.4 Derivative Functions


ddx(a) a
X
ddy(a) a
X
7 Cg


ddx x
ddy y
Cg Refernece
Manual ddx (ddy )returns
approximate partial derivative with respect to window-space X
\

1. ddx ddy
2. ddx ddy
3. ddx ddy

If you evaluate ddx (myVar), the GPU will give you the difference between the
value of myVar at the current pixel and its value at the pixel next door. It's a straight
linear difference, made efficient by the nature of GPU SIMD architectures
(neighboring pixels will be calculated simultaneously).The derivative of any uniform
value will always be zero.Because these derivatives are always linear, the second
derivativesfor example, ddx(ddx(myVar))will always be zero.

ddx myVar

p ( i, j ) ddx(myVar) p ( i + 1, j ) myVar

91
ddy(myVar) p ( i, j + 1) myVar ddx ddy

ddx\ddy myVar ddx(myVar)

p ( i + 1, j ) myVar

1. ddx ddy
2. ddx ddy
3. ddx ddy

GPU Gems: Programming Techniques, Tips and Tricks


for Real-Time Graphics 24.2. Understanding GPU Derivatives by Randima
Fernando.

Complex filtering depends on knowing just how much of the texture (or shading)
we need to filter. Modern GPUs such as the GeForce FX provide partial derivative
functions to help us. For any value used in shading, we can ask the GPU: "How much
does this value change from pixel to pixel, in either the screen-x or the screen-y
direction?"
These functions are ddx() and ddy(). Although they are little used, they can be
very helpful for filtering and antialiasing operations.

GPU partial derivative


DDX DDYCg ddx()
ddy()(glsl hlsl ) x y
TXD

92

ddx X ddy Y

ddx ddy ddx ddy

ddx ddy
A B
A B

GeForce GPU

15

15

tex2D(sampler2D tex , float3 sz ,float2 dsdx , float2 dsdy )


x,y0.010.010.020.020.0
0.015

93
16

8.4.5 Debugging Function


void debug(float4 x) DEBUG
x
COLOR

8 Cg

profiles shader language


8 debug
Cg
C++

8.5

94


IDE


GPU

95

913
1415

illumination model

ward BRDF

IMopenGPU

96
9 illumination model


---

CG
CG

9.1

(Ambient Light):
389

97

9.2 Lambert


diffuse reflection
Lambert

(9-1)

I ambdiff = kd I a (9-1)

I a kd 0< kd <1

I ambdiff

Lambert

98
Lambert Lambert

I ldiff = kd I l cos (9-2)

Il

(0 90) I ldiff

90

N L
cos N L (9-2)
(9-3)

I ldiff = kd I l ( N L) (9-3)

Lambert

I diff = I ambdiff + I ldiff = kd I a + kd I l ( N L) (9-4)

9.2.1

Hello Cg World

Cg

99
1

void main_v(float4 position : POSITION,


float4 normal : NORMAL,

out float4 oPosition : POSITION,


out float4 color : COLOR,

uniform float4x4 modelViewProj,


uniform float4x4 worldMatrix,
uniform float4x4 worldMatrix_IT,
uniform float3 globalAmbient,
uniform float3 lightPosition,
uniform float3 lightColor,
uniform float3 Kd)
{
oPosition = mul(modelViewProj, position);

float3 worldPos = mul(worldMatrix, position).xyz;


float3 N = mul(worldMatrix_IT, normal).xyz;
N = normalize(N);

//
float3 L = lightPosition - worldPos;
L = normalize(L);

//
float3 diffuseColor = Kd*lightColor*max(dot(N, L), 0);

//
float3 ambientColor = Kd*globalAmbient;

color.xyz = diffuseColor+ambientColor;
color.w = 1;
}

17

17

100

2

struct VertexIn
{
float4 position : POSITION;
float4 normal : NORMAL;
};
struct VertexScreen
{
float4 oPosition : POSITION;
float4 color : COLOR;
};
void main_v(VertexIn posIn,
out VertexScreen posOut,

uniform float4x4 modelViewProj,


uniform float4x4 worldMatrix,
uniform float4x4 worldMatrix_IT,
uniform float3 globalAmbient,
uniform float3 lightPosition,
uniform float3 lightColor,
uniform float3 Kd)
{
posOut.oPosition = mul(modelViewProj, posIn.position);

float3 worldPos = mul(worldMatrix, posIn.position).xyz;


float3 N = mul(worldMatrix_IT, posIn.normal).xyz;
N = normalize(N);

//
float3 L = lightPosition - worldPos;
L = normalize(L);

//
float3 diffuseColor = Kd*lightColor*max(dot(N, L), 0);

//
float3 ambientColor = Kd*globalAmbient;

posOut.color.xyz = diffuseColor+ambientColor;
posOut.color.w = 1;
}

101

9.3 Phong

Lambert

Phong Bui Tuong phong

(9-5)

I spec = k s I l (V R ) ns (9-5)

k s ns V

ns

ns

R L N

R + L = (2 N L) N (9-6)

102
R = (2 N L) N L (9-7)

Cg 8.3.2

VS

9.3.1 phong

Phong 3 18
phong

18 phong

18 phong
18


phong

103
3 phong

struct VertexIn
{
float4 position : POSITION; // Vertex in object-space
float4 normal : NORMAL;

};
struct VertexScreen
{
float4 oPosition : POSITION;
float4 color : COLOR;

};
void main_v( VertexIn posIn,
out VertexScreen posOut,
uniform float4x4 modelViewProj,
uniform float4x4 worldMatrix,
uniform float4x4 worldMatrix_IT,
uniform float3 globalAmbient,
uniform float3 eyePosition,
uniform float3 lightPosition,
uniform float3 lightColor,
uniform float3 Kd,
uniform float3 Ks,
uniform float shininess)
{
posOut.oPosition = mul(modelViewProj, posIn.position);

float3 worldPos = mul(worldMatrix, posIn.position).xyz;


float3 N = mul(worldMatrix_IT, posIn.normal).xyz;
N = normalize(N);

//
float3 L = normalize(lightPosition - worldPos);
float3 V = normalize(eyePosition - worldPos);
float3 R = 2*max(dot(N, L), 0)*N-L;
R = normalize(R);

//
float3 diffuseColor = Kd * globalAmbient+Kd*lightColor*max(dot(N, L), 0);
//
float3 specularColor = Ks * lightColor*pow(max(dot(V, R), 0), shininess);

posOut.color.xyz = diffuseColor + specularColor;


posOut.color.w = 1;
}

104
phong

4 3 VertexScreen
TEXCOORD objectPos objectNormal

4 phong

struct VertexIn
{
float4 position : POSITION;
float4 normal : NORMAL;
};

struct VertexScreen
{
float4 oPosition : POSITION;
float4 objectPos : TEXCOORD0;
float4 objectNormal : TEXCOORD1;
};

TEXCOORD

5 phong

void main_v(VertexIn posIn,


out VertexScreen posOut,
uniform float4x4 modelViewProj)
{
posOut.oPosition = mul(modelViewProj, posIn.position);
posOut.objectPos = posIn.position;
posOut.objectNormal = posIn.normal;
}

105
6 phong

6 phong

void main_f( VertexScreen posIn,


out float4 color : COLOR,

uniform float4x4 worldMatrix,


uniform float4x4 worldMatrix_IT,
uniform float3 globalAmbient,
uniform float3 eyePosition,
uniform float3 lightPosition,
uniform float3 lightColor,
uniform float3 Kd,
uniform float3 Ks,
uniform float shininess)
{
float3 worldPos = mul(worldMatrix, posIn.objectPos).xyz;
float3 N = mul(worldMatrix_IT, posIn.objectNormal).xyz;
N = normalize(N);

//
float3 L = normalize(lightPosition - worldPos);
float3 V = normalize(eyePosition - worldPos);
float3 R = 2*max(dot(N, L), 0)*N-L;
R = normalize(R);

//
float3 diffuseColor = Kd * globalAmbient+Kd*lightColor*max(dot(N, L), 0);

//
float3 specularColor = Ks * lightColor*pow(max(dot(V, R), 0), shininess);

color.xyz = diffuseColor + specularColor;


color.w = 1;
}

19 phong

106
19 phong

9.4 Blinn-Phong

Blinn-Phong Blinn-phong BlinnPhong reflection


model phong modified Phong reflection model Jim Blinn
1977 Models of light reflection for computer synthesized pictures
phong phong
Blinn-phong Lambert
Phong CG

OpenGL Direct3D Blinn-Phong

phong V R R
V Blinn-phong N H
V R Blinn-phong

I spec = ks I l ( N H ) ns
9-8

N H L V

107
L +V
H= 9-9
| L +V |

Blinn-phong phong
phong blinn-phong
Blinn-phong Blinn-phong
Blinn-phong
phong
Blinn-phong

7 Blinn-phong

void main_f(VertexScreen posIn,


out float4 color : COLOR,
uniform float4x4 worldMatrix,
uniform float4x4 worldMatrix_IT,
uniform float3 globalAmbient,
uniform float3 eyePosition,
uniform float3 lightPosition,
uniform float3 lightColor,
uniform float3 Kd,
uniform float3 Ks,
uniform float shininess)
{
float3 worldPos = mul(worldMatrix, posIn.objectPos).xyz;
float3 N = mul(worldMatrix_IT, posIn.objectNormal).xyz;
N = normalize(N);

//\\
float3 L = normalize(lightPosition - worldPos);
float3 V = normalize(eyePosition - worldPos);
float3 H = normalize(L + V);

//
float3 diffuseColor = Kd * globalAmbient+Kd*lightColor*max(dot(N, L), 0);

//
float3 specularColor = Ks * lightColor*pow(max(dot(N, H), 0), shininess);

color.xyz = diffuseColor + specularColor;


color.w = 1;
}

108
Blinn-phong

20 Blinn-phong

19 20
phong blinn-phong
phong blinn-phong

9.5 Rendering Equation

Kajia 1986 rendering equation

Lo ( x, wo ) = Le ( x, wo ) + f r ( x, wi , wo ) Li ( x, wi )(n wi )dwi 9-10


x Lo ( x, wo ) x wo

Le ( x, wo ) x wo

f r ( x, wi , wo ) wi x wo

BRDF Li ( x, wi ) wi x n x


phong BRDF

109

Lo ( x, wo ) = f r ( x, wi , wo ) Li ( x, wi )(n wi ) 9-11


BRDF
112

Lo ( x, wo ) = I diff + f rs ( x, wi , wo ) Li ( x, wi )(n wi ) 9-12

I diff f rs ( x, wi , wo ) BRDF

phong rendering equation

Phong

ks (nih) ns
f rs ( x, wi , wo ) = 9-13
ni wi

10 BRDF f rs ( x, wi , wo )

9.6

GPU

110
10

21

21

BRDFBidirectional Reflectance Distribution Function

BRDF

111
Cook-TorranceGPU
BRDFBRDFBidirectional Reflectance Distribution
FunctionBRDF

10.1 Cook-Torrance

phong blinn-phong

1981 Robert L. Cook Kenneth E. Torrance A Reflectance


Model For Computer Graphics Cook-Torrance

Cook-Torrance rough surface

Cook-Torrance
10-1

I c t = I diff + I spec = I diff + k s I l Rs 10-1

I diff k s I l Rs

cook-Torrance phong

blinn-phong Rs cook-Torrancephong

blinn-phong Rs

Rs specular term

Cook-Torrance Rs Wikipedia

112
specular highlight 10-2

F D G
Rs = 10-2
V N
3D 117 D3DBook (Lighting)
Cook-Torrance 10-3

F D G
Rs = 10-3
( N iV ) ( N i L )
A Reflectance Model For Computer Graphics
10-3

F Fresnel Fresnel reflect term


D Beckmann distribution factor

G Geometric attenuation

term N V L

schlick Fresnel 11 1%
10-4

F = f 0 + (1 f 0 )(1 V i H )
5
10-4

f 0 0 Fresnel V

H
Backmann

tan 2
1
D= 2 e m2
10-5
m cos 4
m m m

113
N H

1 cos 2
tan 2
cos 2
cos 2 1 ( N i H )2 1
= = 2 = 10-6
m2 m2 m cos 2 m 2 ( N i H ) 2

Backmann 10-7

( N i H ) 2 1 ( N i H ) 2 1
1 2 2 1 2 2
D= 2 e m ( N i H ) = 2 e m ( N i H ) 10-7
m cos
4
m (N iH ) 4

G = min(1, G1 , G2 ) 10-8

2( N i H )( N i L)
G1 = 10-9
V iH
2( N i H )( N iV )
G2 = 10-10
V iH

Cook-Torrance specular term

Ict = Idiff + Ispec = kd Il ( NiL) +ksIl Rs


(NiH)2 1

= kd Il ( NiL) +ksIl
( f0 +(1 f0 )(1ViH)
5
) 1
m cos
2 4
em2(NiH)2
min(1,
2(NiH)(NiL) 2(NiH)(NiV)
ViH
,
ViH
)
ViN
10-11
119 beckmann

Backmann beckmann 10-12

114
( N i H ) 2 1
1 2 2
D= e m ( N i H ) 10-12
4 m ( N i H )
2 4

A Reflectance Model For Computer Graphics


D 10-7



Cook-Torrance

beckmann

1. Beckmann, Petr and Spizzichino, Andre, The Scattering of Electromagnetic


Waves from Rpugh Surfaces, MacMillan, pp.1-33, 70-98, 1963;

115
2. Computer graphics: principles and practice, James D. Foley - 1995 -
Computers - 1175

10.1.1 Cook-Torrance

Cook-Torrance 22

22 Cook-Torrance

Cook-Torrance

8 Cook-Torrance

void main_v( float4 position : POSITION,


float4 normal : NORMAL,

out float4 oPosition : POSITION,


out float3 worldPos : TEXCOORD0,
out float3 oNormal : TEXCOORD1,

uniform float4x4 worldMatrix,


uniform float4x4 worldMatrix_IT,
uniform float4x4 worldViewProj)
{
oPosition = mul(worldViewProj, position);
worldPos = mul(worldMatrix, position).xyz;
oNormal = mul(worldMatrix_IT,normal).xyz;
oNormal = normalize(oNormal);
}

116
9 Cook-Torrance

void main_f(float3 position : TEXCOORD0,


float3 normal : TEXCOORD1,
out float4 color : COLOR,
uniform float3 globalAmbient,
uniform float3 lightColor,
uniform float3 lightPosition,
uniform float3 eyePosition,
uniform float3 Ka,
uniform float3 Kd,
uniform float3 Ks,
uniform float f,
uniform float m)
{
float3 P = position.xyz;
float3 N = normalize(normal);

float3 ambient = Ka * globalAmbient; //

float3 L = normalize(lightPosition - P);


float nl = max(dot(L, N), 0);
float3 diffuse = Kd * lightColor * nl; //

float3 V = normalize(eyePosition - P);


float3 H = normalize(L + V);
float3 specular = float3(0.0,0.0,0.0);

float nv = dot(N,V);
bool back = (nv>0) && (nl>0);
if(back)
{
float nh = dot(N,H);
float temp = (nh*nh-1)/(m*m*nh*nh);
float roughness = (exp(temp))/(pow(m,2)*pow(nh,4.0)); // beckmann

float vh = dot(V,H);
float a = (2*nh*nv)/vh;
float b = (2*nh*nl)/vh;
float geometric = min(a,b);
geometric = min(1,geometric); //

float fresnelCoe=f+(1- f)*pow(1- vh,5.0); //fresnel


float rs = (fresnelCoe*geometric*roughness)/(nv*nl);
specular = rs * lightColor * nl*Ks; //
}
color.xyz = ambient + diffuse + specular;
color.w = 1;
}

117

10.2 BRDF

10.2.1 BRDF

1965 Nicodemus, Fred Directional reflectance and emissivity of an


opaque surface BRDF BRDF Bidirectional Reflectance
Distribution Function

BRDF

111 23 wi wo

BRDF wi

wo

polynomial texture map

23

BRDF

dLr ( wo ) dLr ( wo )
f r ( wi , wo ) = = 10-13
dEi ( wi ) Li ( wi ) cos i dwi

118
Lr ( wo ) Ei ( wi )
wo Radiance

wi Irradiance

d dA
BRDF

BRDF rendering equation


9.4

10.2.2

(anisotropy)

anisotropic surface

24

24

119

25

1.
2.
3.

25

10.3 Bank BRDF

Bank BRDF
Bank BRDF
10-14

f = k s ( 1 ( L T ) 2 1 (V T ) 2 ( L T )(V T )) ns 10-14

ks ns L V

Bank BRDF 26 27 27

120
26 Bank BRDF 1

27 Bank BRDF 2

Bank BRDF

10 Bank BRDF

void main_v(float4 position : POSITION,


float4 normal : NORMAL,

out float4 oPosition : POSITION,


out float3 worldPos : TEXCOORD0,
out float3 worldNormal : TEXCOORD1,

uniform float4x4 modelViewProj,


uniform float4x4 worldMatrix,
uniform float4x4 worldMatrix_IT)
{
oPosition = mul(modelViewProj, position);
worldPos = mul(worldMatrix, position).xyz;
worldNormal = mul(worldMatrix_IT, normal).xyz;
}

121
11 Bank BRDF

void main_f(float4 position : TEXCOORD0,


float3 normal : TEXCOORD1,

out float4 color : COLOR,

uniform float3 globalAmbient,


uniform float3 lightColor,
uniform float3 lightPosition,
uniform float3 eyePosition,
uniform float3 Ka,
uniform float3 Kd,
uniform float3 Ks,
uniform float shininess)
{
float3 P = position.xyz;
float3 N = normalize(normal);

float3 ambient = Ka * globalAmbient; //

float3 L = normalize(lightPosition - P);


float ln = max(dot(L, N), 0);
float3 diffuse = Kd * lightColor *ln; //

//
float3 V = normalize(eyePosition - P);
float3 H = normalize(L + V);
float3 specular = float3(0.0,0.0,0.0);
bool back = (dot(V,N)>0) && (dot(L,N));
if(back)
{
float3 T = normalize(cross(N,V)); //
float a = dot(L,T);
float b = dot(V,T);
float c = sqrt(1-pow(a,2.0))* sqrt(1-pow(b,2.0)) - a*b; // Bank BRDF
float brdf = Ks* pow(c, shininess);

specular = brdf * lightColor *ln;


}
color.xyz = ambient + diffuse + specular;
color.w = 1;
}

122
10.4

BRDF HTSG BRDF


BRDF Ward BRDF
BRDF

123
11

CPUGPU


CPU
GPU
GPU
GPU

SnellFresnel

GPUGPU
3D

124
11.1 Snell Fresnel

11.1.1 Snell

c v 11-1
1
1

c
n= 11-1
v


\ 1.0\ 1.0003
1.333
1.5-1.7
2.417
1.309

Snell

28 i

ni t nt

sin i ni = sin t nt 11-2

snell

125


i

28 Snell

fresnel
fresnel

11.1.2


1672

29
1936

29

126

11.1.3 Fresnel

Fresnel

Fresnel Fresnel fresnel


fresnel fresnel fresnel
139 fresnel fresnel
0.7 70% 30%

kr fresnel kt

fresnel

fresnel
3D
117 Fresnel

schlick Fresnel 1%

F = f 0 + (1 f 0 )(1 V i H )
5
11-3

f 0 0 Fresnel V

H 11-3
90 1

fresnel equations-Wikepedia 0 fresnel

127
(n n )
2

f0 = i t 2 11-4
( ni + nt )

fresnel 1
0 fresnel

4n1n2
ft 0 = 1 f 0 = 11-5
( ni + nt )
2

11-311-4
fresnel

(n n )
2
4n1n2
F i t 2+ (1 V H )5 11-6
( ni + nt ) ( ni + nt )
2

11-6 0
fresnel 11-7
fresnel
11-7

F (1 V H ) 4 11-7

4%

11.2

Environment Mapping, EMReflection Mapping


Blinn Newell 1976
10

Greece 1986 11

128
6
6

DirectX
.dds 6

29 OGRE 2

30 OGRE 2

2 5.7.4

31

129
31

1.
2.
3.

Cg smaplerCUBE
texCUBE

12

130
12

void main_v( float4 position: POSITION,


float4 normal: NORMAL,

out float4 oposition : POSITION,


out float3 R : TEXCOORD1,

uniform float3 eyePosition,


uniform float4x4 modelViewProj,
uniform float4x4 modelToWorld)
{
oposition = mul( modelViewProj, position);

//
float3 positionW = mul( modelToWorld, position).xyz;
float3 normalW = mul( modelToWorld_IT, normal).xyz;
normalW = normalize( normalW);
float3 I = positionW - eyePosition;

// R.
R = reflect( I, normalW );
}

void main_f( float3 R : TEXCOORD1,


out float4 color : COLOR,
uniform samplerCUBE environmentMap)
{
color = texCUBE( environmentMap, R);
}

32

131
32

11.3

33
t A A

ia , ib A

i = (1 t ) ib + t ia 11-8

132
ib

ia

33

ia ib

ib

34

34

133

11-8
Cg lerp
8.3.1

11.2

13

struct VertexIn
{
float4 position : POSITION;
float4 normal : NORMAL;
};

struct VertexScreen
{
float4 oPosition : POSITION;
float4 objectPos : TEXCOORD0;
float4 objectNormal : TEXCOORD1;
};

14

void main_v(VertexIn posIn,


out VertexScreen posOut,
uniform float4x4 modelViewProj)
{
posOut.oPosition = mul(modelViewProj, posIn.position);
posOut.objectPos = posIn.position;
posOut.objectNormal = posIn.normal;
}

134
15

void main_f( VertexScreen posIn,


out float4 color : COLOR,

uniform float4x4 worldMatrix,


uniform float4x4 worldMatrix_IT,
uniform float3 globalAmbient,
uniform float3 eyePosition,
uniform float3 lightPosition,
uniform float3 lightColor,
uniform float3 Kd,
uniform float3 Ks,
uniform float shininess,
uniform float etaRatio, //
uniform float transmittance, //
uniform samplerCUBE environmentMap //
)
{
float3 worldPos = mul(worldMatrix, posIn.objectPos).xyz;
float3 N = mul(worldMatrix_IT, posIn.objectNormal).xyz;
N = normalize(N);

//\\
float3 L = normalize(lightPosition - worldPos);
float3 V = normalize(eyePosition - worldPos);
float3 H = normalize(L + V);

//
float3 diffuseColor = Kd * globalAmbient+Kd*lightColor*max(dot(N, L), 0);
float3 specularColor = Ks * lightColor*pow(max(dot(N, H), 0), shininess);
float3 reflectColor = diffuseColor+specularColor;

//, refract
float3 I = normalize(worldPos - eyePosition);
float3 T = refract(I, N, etaRatio);

//
float3 refractedColor = texCUBE(environmentMap, T).xyz;

color.xyz = lerp(reflectColor, refractedColor, transmittance);


color.w = 1;
}

135
11.4

2005 Wyman ACM SIGGRAPH


GPU Interactive
image-space refraction of nearby geometry

GPU 16

136
12 Projective Texture Mapping

Projective Texture Mapping Segal Fast shadows


and lighting effects using texture maaping

(coordinates in projective space)


3D


35

35 (12)

12.1

137

13
15

36

gradient

36

138
37


(a) (b)

37

12.2 Homogeneous Texture Coordinates

homogeneous texture coordinates

s,t,r,q,(x, y, z,
w) s (s, t) r
q w 1

12.3

139

3D

NDC

POSTION

Projective texture mapping


38

38

38

140

01 normalMatrix ,
1/21/2

0.5 0 0 0.5
0 0.5 0 0.5
normalMatrix = 12-1
0 0 0.5 0.5

0 0 0 1

texView Pr ojMatrix = biasMatrix projectionMatrix viewMatrix worldMatrix 12-2

texView Pr ojCoordinate = texViw Pr ojMatrix mod elCoordinate 12-3

Cg

tex2DProj(sampler2D tex, float4 szq)

tex2DProjtex2D
q

141

API

16

void main_v(
float4 position : POSITION,
float4 normal : NORMAL,

out float4 outPos : POSITION,


out float4 outShadowUV : TEXCOORD0,

uniform float4x4 worldMatrix,


uniform float4x4 worldViewProj,
uniform float4x4 texViewProj //
)
{
outPos = mul(worldViewProj, position);

//
float4 worldPos = mul(worldMatrix, position);
outShadowUV = mul(texViewProj, worldPos);
}

142
17

void main_f(
float4 shadowUV : TEXCOORD0,
out float4 result : COLOR,
uniform sampler2D projectiveMap //
)
{
shadowUV = shadowUV / shadowUV.w;

float4 mapColor ;

// 0-1
shadowUV.x = (shadowUV.x +float(1.0))/float(2.0);
shadowUV.y = (shadowUV.y +float(1.0))/float(2.0);
mapColor = tex2D(projectiveMap, shadowUV.xy);

result = mapColor;
}

12.4


Shadow Map 16

143
13 Shadow Map

Shadows are created by testing whether a pixel is visible from the light source,
by comparing it to a z-buffer or depth image of the light source's view, stored in the
form of a texture.

Shadow Map depth map Lance


Williams 1978 Casting curved shadows on curved surfaces

l enth1 l enth2 l enth1 l enth2

13.1 depth map

2D

Depth map l enth1

v l enth2 l enth1 l enth2

l enth2 > l enth1 v depth texure

v v v

depth map shdaow texture


2

144
Shadow texture Depth texture
39
wikipid shadow map depth map
shadow texture

39depth map shadow texture

Shadow texture
texture
projective

13.2 Shadow map shadow texture

Shadow map
Shadow map shadow
texture

map shadow map shadow


map shadow map
153 6.12.4 shadow map

shadow texture Shadow map depth


map

145
shadow texture

13.3 Shadow map

Shadow Map depth map()


depth map

depth map

1.

2. worldViewProjMatrix
Z Z
shadow
01Cg
0-1

depth map
depth map Z Buffer Z

Euclidean distanceZ
2.4.2 GPU
depth map
01
depth map

146

3. frame buffer depth


map depth map
depth map depth
map depth map

18 depth map

void main_v(float4 position : POSITION,


out float4 oPosition : POSITION,
out float2 depth : TEXCOORD0,
uniform float4x4 worldViewProj )
{
oPosition = mul(worldViewProj, position);

//
depth.x = oPosition.z;
depth.y = oPosition.w;
}

19 depth map

void main_f(float2 depth : TEXCOORD0,


out float4 result : COLOR,
uniform float pNear ,
uniform float pFar,
uniform float depthOffset )
{
float depthNum = 0.0;

// 0-1
depthNum = (depth.x - pNear) / (pFar - pNear);

depthNum += depthOffset;

result.xyz = depthNum.xxx;
result.w = 1.0;
}

147
19 depthOffset

0.01

depth map
1.
worldViewProjMatrix
13

l enth2


2. depth map

l enth1

3. l enth2 l enth1

20 depth map

void main_v(float4 position : POSITION,


float4 normal : NORMAL,
float2 tex : TEXCOORD,

out float4 outPos : POSITION,


out float4 outShadowUV : TEXCOORD0,

uniform float4x4 worldMatrix,


uniform float4x4 worldViewProj,
uniform float4x4 texViewProj)
{

outPos = mul(worldViewProj, position);


float4 worldPos = mul(worldMatrix, position);

//
outShadowUV = mul(texViewProj, worldPos);
}

148
21 depth map

void main_f(float4 position : POSITION,


float4 shadowUV : TEXCOORD0,
out float4 result : COLOR

uniform sampler2D shadowMap ,


uniform float pNear ,
uniform float pFar,
uniform float depthOffset,
uniform int pixelOffset)
{
//
float lightDistance = (shadowUV.z - pNear) / (pFar - pNear);
lightDistance = lightDistance - depthOffset;

shadowUV.xy = shadowUV.xy/ shadowUV.w;

//
float4 depths = float4(
tex2D(shadowMap, shadowUV.xy + float2(-pixelOffset, 0)).x,
tex2D(shadowMap, shadowUV.xy + float2(pixelOffset, 0)).x,
tex2D(shadowMap, shadowUV.xy + float2(0, -pixelOffset)).x,
tex2D(shadowMap, shadowUV.xy + float2(0, pixelOffset)).x);
float centerdepth = tex2D(shadowMap, shadowUV.xy).x;

//
float l_Lit = (lightDistance >= centerdepth? 0 : 1);
l_Lit += (lightDistance >= depths.x? 0 : 1);
l_Lit += (lightDistance >= depths.y? 0 : 1);
l_Lit += (lightDistance >= depths.z? 0 : 1);
l_Lit += (lightDistance >= depths.w? 0 : 1);
l_Lit *= 0.2f;

result = float4(l_Lit, l_Lit, l_Lit, 1.0);


}

40 shadow map

149
40 shadow map

Shadow map

Shadow map

depth map

01
z-fighting

Self-shadow Aliasing,

150
Spot light
Point light
cube textureDirectional light
, worldViewProjMatrix

stencil

shadow map C

151
14 Volume Rendering

1982 2

----1.1

20 80 Visualization in Scientific Computing

M.Levoy
Display of surfaces from volume data(14)volume rendering
describes a wide range of techniques for generating images from three-dimensional
scalar data

CT
,
CT CT

152

80
GPU

openGPU

server the people

14.1

14.2

153

server the people,
CT
CT
B

41

41

14.3


,
phong

154
cook-torrance


ray-cast

14.4 Volume Data

155

C
H20
XYZ

1.
2. CT MRI

VoxelVolume Texture

14.4.2 Voxel

Wikipediavoxel

A voxel (a portmanteau of the words volumetric and pixel) is a volume element,


representing a value on a regular grid in three dimensional space. This is analogous to
a pixel, which represents 2D image data in a bitmap

156

42

n*m*t
XYZnmt
256*256*256
256

mm
0.412mm0.412

42

14.4.1 Volume Texture

volume texture


2d texture3d texturevolume texture
3d texture
23
2d texture3d texture

157
3d texturevolume texture22

3D texture (Three Dimensional Texture), also known as "volume texture," is a


logical extension of the traditional (and better known) 2D texture. In this context, a
texture is simply a bitmap image that is used to provide surface coloring for a 3D
model. A 3D texture can be thought of as a number of thin pieces of texture used to
generate a three dimensional image map. 3D textures are typically represented by 3
coordinates.

2D

2D

1.
2.


x,y

XYvolume
texture

CT
DirectX.dds.raw
.raw
.raw.raw

158
.raw 43
University of TbingenGermanyViatronix Inc.(USA)Walter Reed Army Medical
Center (USA)

43

256 x 320 x 128 \0.66, 0.66, 0.66512 x 512


x 174\0.8398, 0.8398, 3.2512 x 512 x 463\0.625, 0.625, 1.0

http://wwwvis.informatik.uni-stuttgart.de/~engel/pre-integrated/data.html
http://www9.informatik.uni-erlangen.de/External/vollib/
http://www.volren.org/

14.5

Ray-casting-
Shear-warp Frequency Domain
Splatting

GPU

15

159
15 Ray Casting

15.1


CPU
GPU

16.1.2

160

15.1.1

Optical Models
Optical Model

15

1. Absorption only

2. Emission only
3. Absorption plus emission

4. Scattering and Shading/shadowing

5. Multiple Scattering

Absorption plus emission

15.2

15.2.1

161

OpenGL Direct3D

44

162
44
OpenGL 8
GPU 6 GPU

11

15.2.2

alpha
alpha over Alpha

c0 = as cs + (1 as )cd 15-1

as cs cd

c0

163

Ci = (1 Ai1 )Ci + Ci1


15-2
Ai = (1 Ai1 ) Ai + Ai1

Ci Ai

Ci Ai

Ci = (1 Ai )Ci+1 + Ci
15-3
Ai = (1 Ai ) Ai+1 + Ai

15.2.3

45

45 F L

164
m F nn<m

t = tstart + d * delta 15-4

tstart d delta

n t
n>m 1

OpenGL DirectX
profile
Cg OpenGL DirectX

165
15.2.2

m
n n>=m
n

CPU m
GPU

GPU

GPU

OpenGL Direct

1. frontDepth
14 frontDepth

2. backDepthbackDepth

166

3. m
14 shadow Map





\
46

46

frontDepth backDepth
rgba
a

01

167
15.3

Front face generation

Back face generation


front face

back face

47

47

168

48

48

15.4

169
22

struct VertexIn
{
float4 position : POSITION;
float4 texCoord: TEXCOORD;
};

struct VertexScreen
{
float4 position : POSITION;
float4 worldPos : TEXCOORD0;
float4 projPos : TEXCOORD1;
float4 texCoord : TEXCOORD2;
};

23

VertexScreen main_v(VertexIn posIn,


uniform float4x4 world,
uniform float4x4 worldViewProj,
uniform float4x4 texViewProj)
{
VertexScreen posOut;

posOut.position = mul(worldViewProj, posIn.position);


posOut.worldPos = mul(world,posIn.position);
posOut.projPos = mul(texViewProj, posOut.worldPos);
posOut.texCoord = posIn.texCoord;

return posOut;
}

170
24

void main_f(VertexScreen posIn,


uniform float3 eyePosition,
uniform sampler3D volumeTex: register(s0),
uniform sampler2D frontDepthTex: register(s1) ,
uniform sampler2D backDepthTex: register(s2) ,
out float4 result : COLOR)
{
//
float3 dir = posIn.worldPos.xyz-eyePosition;
dir = normalize(dir);
float3 deltaDir = float3(0.0, 0.0, 0.0);

//
float3 tex = posIn.texCoord.xyz;
float2 uvDelta;
uvDelta.x = 0.0;//ddx( tex ).x;
uvDelta.y = 0.0;//ddy( tex ).y;

//,
float2 uv= posIn.projPos.xy/posIn.projPos.w;
float frontDis = tex2D(frontDepthTex,uv).x;
float backDis = tex2D(backDepthTex,uv).x;
float len = backDis-frontDis;

//
float3 norm_dir = normalize(dir);
float stepsize = 0.01;
float delta = stepsize;
float3 delta_dir = norm_dir * delta;
float delta_dir_len = length(delta_dir);
float3 vec = posIn.texCoord.xyz;
float4 col_acc = float4(0,0,0,0);
float alpha_acc = 0;
float length_acc = 0;
float4 color_sample;
float alpha_sample;

for(int i = 0; i < 800; i++){


color_sample = tex3D(volumeTex,vec);
alpha_sample = color_sample.a * stepsize;
col_acc += (1.0 - alpha_acc) * color_sample * alpha_sample * 3;
alpha_acc += alpha_sample;
vec += delta_dir;
length_acc += delta_dir_len;
if(length_acc >= len || alpha_acc > 1.0) break; //
}
result.xyz = col_acc.xyz*2.0+float3(0.2,0.2,0.2);
result.w = col_acc.w;
}

171
15.5

1415


Object-Order Empty Space
Skipping

Markus Hadwiger
Advanced Illumination Techniques for GPU-Based Volume
Raycasting SIGGRAPH ASIA2008
166

172
A

----F.S.Hill,JR

( a, b, c )

x ' cos sin 0 0 x


'
y = sin cos 0 0 y
. A-1
z' 0 0 1 0 z

1 0 0 0 1 1

Z 317
1
0


v v1 v 2 v3
A-2

v = a v1 + b v 2 + c v3 A-2

p p1 p 2 p3 po

A-3

173
p = a p1 + b p 2 + c p3 + po A-3

A-2A-3 2
A-4A-5

v = ( a b c 0 ) ( v1 v 2 v3 p0 ) A-4

p = ( a b c 1) ( p1 p 2 p3 p0 ) A-5


1 0 1

(100 100 100 1)

100 (100 100 100 0 )

p ( x p yp z p ) ( t x ty t z )

(x '
p y' p z' p ) = ( xp yp z p ) + (tx ty tz ) A-6

x ' p 1 0 0 tx x p
'
y p = 0 1 0 t y y p
A-7
z ' p 0 0 1 tz z p

1 0 0 0 1 1

174
B


1CT

X
XX
X
1963X

CT1967

XCT

19719
104
X
X

19724
CTCT
X
1979CT

CTXX
1895XX
X70

175
30CT
CT

CTXX
XX
x

CTXCTX
CT
X XCT2000
X20

2CT

CTX

CTXX

CT
X

176
CT
1~10

CT
CT

CT

3CT

CTX
X/
analog/digital converter
voxel
X
digital matrix/
digital/analog converter
pixelCTCT
X

I0 X , d X
X I

177
I = I 0 e ud B-1


()
, X
CT

CT X
X
123 d

I = I 0e (u1 +...+un ) d B-2

I ,


CT CT
CT

u uwater
CT = *1000 B-3
uwater

CStencil Shadow

Stencil Shadowstencil
buffer

shadow map

178


OGRE

shadow volume
stencil buffer
2.4.3

Frank Crow 1977 19


49, ABC
DEFABC DEF
shadow volume

B
A
C

D
E

49

1
2

179
3D

50

50
501 502
503

Z Pass Z Fail Z Pass

Z Pass stencil
stencil stencil
stencil
stencil

stencil stencil
2.4.3

180
Z Pass z buffer
stencil buffer

1Z Enable z buffer

2Z Disable z buffer
stencil

stencil

3
stencil
4
stencil stencil buffer

Z Pass stencil
shadow volume

Z Fail Z Pass z pass


Z Fail

1
2
stencil
3

181
stencil
4 stencil
stencil

Z Fail

51


Doom3
object
Outlining

1 Feng Liu, Platform Independent Real-time X3D Shaders and Their Applications
in Bioinformatics Visualization, Georgia State University, 2005, ISBN:
0-542-56173-5, Order Number: AAI3207182.

182
2 Randima Fernando, Mark J. Kilgard, The Cg Tutorial The Definitive Guide to
Programmable Real-Time Graphics, Addison-Wdsley Professional, March 8, 2003,
ISBN-10: 0321194969.
3 Cg Toolkit Users Manual A Developers Guide to Programmable Graphics.
Release 1.4, September 2005.
4 Kr uger, J., and Westermann, R. 2003. Linear algebra operators for GPU
implementation of numerical algorithms. ACM Trans. Graph. 22, 3, 908-916.
5 Harris, M. J., Baxter, W. V., Scheuermann, T., and Lastra, A. 2003. Simulation of
cloud dynamics on graphics hardware. In Proceedings of Graphics hardware,
Eurographics Association, 92-101.
6 Purcell, T. J., Buck, I., Mark, W. R., and Hanrahan, P. 2002. Ray tracing on
programmable graphics hardware. ACM Trans. Graph., 703-712.
7 Alexander J.Faaborg, A Psychophysical Study of BRDF-Based Lighting, May 10,
2001.
8 Wynn, C. (2000). An Introduction to BRDF-Based Lighting. nVIDIA
Corporation.
9 Michael Ashikhmin, Peter Shirley, An Anisotropic Phong BRDF Model. August
13, 2000.
10 Blinn, J.F., and M.E. Newell, Texture and reflection in computer generated
images, Communications of the ACM, vol.19, no. 10, pp.542-547, October 1976.
11 Greene, Ned, Environment Mapping and Other Applications of World
Projections, IEEE Computer Graphics and Application, vol, 6, no, 11, pp21-29,
November 1986.
12 Cass Everitt, Projective Texture Mapping, 04/24/2001
13 Lance Williams, Casting curved shadows on curved surfaces, 1978,
ISSN:0097-8930, page: 270-274.
14 Levoy,M. Display of surfaces from volume data. May, 1988, Computer Graphics
and Applications, IEEE, Volume: 8, Issue: 3.
15 Nelson Max. Optical models for direct volume rendering. IEEE

Transactions on Visualization and Computer Graphics, 1(2):99108, June 1995.


16 Matthieu Sozeau , Nicolas Qury, First-Class Type Classes.
17 steve Baker, Learning to love your Z-buffer.
18 Markus Hadwiger, Patric Ljung, Christof Rezk Salama, Timo Ropinski. Advanced
Illumination Techniques for GPU-Based Volume Raycasting. SIGGRAPH
ASIA2008.

183
19 Crow,Franklin C:Shadow Algorithms for Computer Graphics, Computer
Graphics (SIGGRAPH 77 Proceedings), vol. 11, no. 2, 242-248.
20 http://nshader.codeplex.com/
21 http://www.opengpu.org/
22 www.tech-faq.com3d-texture.shtml#
23 http://wwwvis.informatik.uni-stuttgart.de/~engel/pre-integrated/data.html
24 http://www9.informatik.uni-erlangen.de/External/vollib/
25 http://www.volren.org/
26 Randima Fernando, Mark J.Kilgard, Cg
_ 2004 9 ISBN:
711512430
27 Tomes Akenine-Moller, Eric-Haines,
2004-8-18 7301071051
28 Stephen C.DewhurstC++2005 12 16
7115141010
29 GPU 2006 4

30 2004 2
31 Frangois X.Sillion, James R.Arvo, A Global Illumination Solution for General
Reflectance Distributions. ACM O-89791-436-8/91/007/0187, 1991.
32 Robert L. Cook, A Reflectance Model For Computer Graphics, ACM
O-8971-045-I/81-0800-0370. 1981.
33 Xin Sun, Kun Zhou, Yanyun Chen, Interactive Relighting with Dynamic BRDFs.
34 Wolfgang Heidrich , Hans-Peter Seidel. Efficient Rendering of Anisotropic
Surfaces Using Computer Graphics Hardware.
35 David C.Banks. Illumination In Diverse Codimensions.
36 Zhao Dong, Jan Kautz, Christian Theobalt. Interactive Global Illumination Using
Implicit Visibility.
37 Daniel Reiter Horn, Jeremy Sugerman, Mike Houston. Interactive k-D Tree GPU
Raytracing.
38 Benthin,C., Wald,I., Scherbaum, M., And Friedrich,H. 2006. Ray Tracing on the
Cell Processor. In Proceedings of the 2006 IEEE Symposium on Interactive Ray
Tracing.
39 Buck, I., Foley, T., Horn, D., Sugerman, J., Fatahalian, K., Houston, M., And
Hanrahan, P. 2004. Brook for GPUs: Stream computing on graphics hardware. In

184
Proceedings of ACM SIGGRAPH 2004.
40 Xin Sun, Kun Zhou, Eric Stollnitz. Interactive Relighting of Dynamic Refractive
Objects.
41 Timothy J.Purcell, Ian Buck, William R.Mark. Ray Tracing on Programmable
Graphics Hardware.
42 Ziyad S.Hakura, John M.Snyder. Realistic Reflections and Refractions on
Graphics Hardware With Hybrid Rendering and Layered Environment Maps.
43 Turner Whitted, An Improved Illumination Model for Shaded Display. 1980 ACM
0001-0782/80/0600-0343.
44 Mark Segal, carl Korobkin, Rolf van Widenfelt. Fast Shadows and Lighting
Effects Using Texture Mapping. 1992 ACM-0-89791-479-1/92/007/0249.
45 Mark J.Kilgard. Improving Shadows and Reflections via the Stencil Buffer.
NVIDIA Corporation.
46 Randima Fernando. Percentage-Closer Soft Shadows. NVIDIA Corporation.
47 Fan Zhang, Hanqiu Sun, Leilei Xu. Parallel-Split Shadow Maps for Large-scale
Virtual Environments.
48 Uwe Behrens, Ralf Rastering. Adding shadows to a texture-based volume renderer.
In VVS98: Proceedings of the 1998 IEEE symposium on Volume visualization,
pages 39-46. ACM Press, 1998.
49 Johanna Beyer, Markus Hadwiger. Smooth Mixed-Resolution GPU Volume
Rendering. In IEEE/EG International Symposium on Volume and Point-Based
Graphics, pages 163-170, 2008.
50 Praveen Bhaniramka, Yves Demange. OpenGL Volumizer: A Toolkit for High
Quality Volume Rendering of Large Data Sets. In Proceedings IEEE Visualization
2002, pages 45-53, 2002.
51 Imma Boada, Isabel Navazo, Roberto Scopigno. Multiresolution volume
visualization with a texture-based octree. The Visual Computer, 17:185-197, 2001.
52 N. Carr, J.Hall, J.Hart. GPU Algorithms for Radiosity and Subsurface Scattering.
In proc. Graphics Hardware, 2003.
53 Nathan A. Carr, Jesse D.hall, and John C.Hart. GPU algorithms for radiosity and
subsurface scattering. In HWWS03: Proceedings of the conference on Graphics
Hardware 03, pages 51-59. Eurographics Association, 2003.
54 R.A.Drebin, L.Carpenter, and P. Hanrahan. Volume rendering. In Proceedings of
SIGGRAPH 88, pages 65-74, 1988.

185

You might also like