57
GLSL I

GLSL I. Fixed vs. Programmable HW fixed function pipeline ▪ Faster ▪ Limited New programmable hardware ▪ Many effects become possible. ▪ Global

Embed Size (px)

Citation preview

Page 1: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

GLSL I

Page 2: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

Fixed vs. Programmable HW fixed function pipeline

▪ Faster▪ Limited

New programmable hardware▪ Many effects become possible.▪ Global illumination effects▪ Ray tracing.▪ …

Multiple Normal Map

Refraction/ reflection

Subsurface scattering composing

Make the Graphics Effects

Possible & Real-time !!

Page 3: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

Vertex Shader Skinning Mesh

Tessellation Terrain Generation

Geometry Shader Surface details

Fragment Shader Advance shading

Page 4: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

Shader language The language itself.

Runtime environment How to co-work with OpenGL/D3D

Architecture The role of the shaders.

Page 5: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global
Page 6: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

2000 Asambly, (nVidia)

2002 CG 1.0 (nVidia)

▪ C for graphcis Vertex/fragment extension (OpenGL ) HLSL (High Level Shading Language, M$)

2003 GLSL (OpenGL)

2004 GPGPU Applications Uniform GPU architecture for both VS and FS,

(ATI) 2008

Direct Computing, CUDA, 2009

OpenCL

Page 7: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

Currently 3 major shading languages - Cg (Nvidia) - HLSL (Microsoft)

▪ Derived from Cg

- GLSL (OpenGL)

Main influences are - C language - Pre-existing Shading languages

developed in university and industry.

Page 8: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

1.1+ Vertex/Fragment Shader

1.5+ Geometry Shader

4.0 Tessellation Shader

Page 9: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

Rast

eri

zer

Screen Vertices3D model Color Pixels

Transform &

Lighting

Transform &

Lighting

Transform &

Lighting

Transform &

Lighting

Transform &

Lighting

Transform &

Lighting

Pixel Op.Pixel

Op.Pixel Op.Pixel

Op.Pixel Op.

Pixels

Scene Setting

Page 10: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

10Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Vertex Shader Applications

•Moving vertices Morphing

Wave motion

Fractals

•Lighting More realistic models

Cartoon shaders

Page 11: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

11Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Fragment Shader Applications

Per fragment lighting calculations

per vertex lighting per fragment lighting

Page 12: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

12Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

GLSL

•OpenGL Shading Language•Part of OpenGL 2.0•High level C-like language•New data types

Matrices Vectors Samplers

•OpenGL state available through built-in variables

Page 13: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

13Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Simple Vertex Shader

const vec4 red = vec4(1.0, 0.0, 0.0, 1.0);

void main(void)

{

gl_Position = gl_ProjectionMatrix

*gl_ModelViewMatrix*gl_Vertex;

gl_FrontColor = red;

}

Page 14: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

14Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Execution Model

Page 15: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

15Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Simple Fragment Program

void main(void)

{

gl_FragColor = gl_FrontColor;

}

Page 16: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

16Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Execution Model

Page 17: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

17Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Data Types

•C types: int, float, bool•Vectors:

float vec2, vec 3, vec4 Also int (ivec) and boolean (bvec)

•Matrices: mat2, mat3, mat4 Stored by columns Standard referencing m[row][column]

•C++ style constructors vec3 a =vec3(1.0, 2.0, 3.0) vec2 b = vec2(a)

Page 18: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

18Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Pointers

•There are no pointers in GLSL•We can use C structs which

can be copied back from functions•Because matrices and vectors are basic types they can be passed into and output from GLSL functions, e.g.

matrix3 func(matrix3 a)

Page 19: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

19Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Qualifiers

• GLSL has many of the same qualifiers such as const as C/C++

• Need others due to the nature of the execution model

• Variables can change Once per primitive Once per vertex Once per fragment At any time in the application

• Vertex attributes are interpolated by the rasterizer into fragment attributes

Page 20: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

20Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Attribute Qualifier

•Attribute-qualified variables can change at most once per vertex

Cannot be used in fragment shaders

•Built in (OpenGL state variables)­gl_Color­gl_ModelViewMatrix

•User defined (in application program)­attribute float temperature­attribute vec3 velocity

Page 21: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

21Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Uniform Qualified

•Variables that are constant for an entire primitive

•Can be changed in application outside scope of glBegin and glEnd

•Cannot be changed in shader•Used to pass information to shader such as the bounding box of a primitive

Page 22: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

22Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Varying Qualified

•Variables that are passed from vertex shader to fragment shader

•Automatically interpolated by the rasterizer

•Built in Vertex colors

Texture coordinates

•User defined Requires a user defined fragment shader

Page 23: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

23Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Example: Vertex Shader

const vec4 red = vec4(1.0, 0.0, 0.0, 1.0);

varying vec3 color_out;

void main(void)

{

gl_Position =

gl_ModelViewProjectionMatrix*gl_Vertex;

color_out = red;

}

Page 24: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

24Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Required Fragment Shader

varying vec3 color_out;

void main(void)

{

gl_FragColor = color_out;

}

Page 25: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

25

A Quick Review

•What are the qualifier (attribute, uniform, varying) for the following cases?

• If variables change… Once per primitive (uniform? attribute?...)

Once per vertex ?

Once per fragment ?

•Vertex attributes are interpolated by the rasterizer into fragment attributes

Page 26: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

26Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Passing values

• call by value-return•Variables are copied in•Returned values are copied back•Three possibilities

in

out

inout

Page 27: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

27Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Operators and Functions

•Standard C functions Trigonometric

Arithmetic

Normalize, reflect, length

•Overloading of vector and matrix typesmat4 a;

vec4 b, c, d;

c = b*a; // a column vector stored as a 1d array

d = a*b; // a row vector stored as a 1d array

Page 28: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

28Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Swizzling and Selection

•Can refer to array elements by element using [] or selection (.) operator with

x, y, z, w r, g, b, a s, t, p, q­a[2], a.b, a.z, a.p are the same

•Swizzling operator lets us manipulate componentsvec4 a;a.yz = vec2(1.0, 2.0);

Page 29: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

29

For More Information…

•Tutorials available online (see the course webpage, Reference section)

At www.Lighthouse3D.com

At www.OpenGL.org

Page 30: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

GLSL II

(Adapted from

Ed Angel’s lecture slides)

Page 31: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

31Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Objectives

•Coupling GLSL to Applications•Example applications

Page 32: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

32Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Linking Shaders to OpenGL

•OpenGL Extensions ARB_shader_objects

ARB_vertex_shader

ARB_fragment_shader

•OpenGL 2.0 Almost identical to using extensions

Avoids extension suffixes on function names

Page 33: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

33Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Program Object

•Container for shaders Can contain multiple shaders

Other GLSL functions

GLuint myProgObj;myProgObj = glCreateProgram(); /* define shader objects here */glUseProgram(myProgObj);glLinkProgram(myProgObj);

Page 34: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

34Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Reading a Shader

•Shader are added to the program object and compiled

•Usual method of passing a shader is as a null-terminated string using the function glShaderSource

• If the shader is in a file, we can write a reader to convert the file to a string

Page 35: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

35Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Shader Reader

char* readShaderSource(const char* shaderFile){ struct stat statBuf; FILE* fp = fopen(shaderFile, "r"); char* buf;

stat(shaderFile, &statBuf); buf = (char*) malloc(statBuf.st_size + 1 * sizeof(char)); fread(buf, 1, statBuf.st_size, fp); buf[statBuf.st_size] = '\0'; fclose(fp); return buf;}

Page 36: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

36Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Adding a Vertex Shader

GLint vShader;GLunit myVertexObj;GLchar vShaderfile[] = “my_vertex_shader”;GLchar* vSource = readShaderSource(vShaderFile);glShaderSource(myVertexObj, 1, &vSource, NULL);myVertexObj = glCreateShader(GL_VERTEX_SHADER);glCompileShader(myVertexObj);glAttachObject(myProgObj, myVertexObj);

Page 37: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

37Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Vertex Attributes

•Vertex attributes are named in the shaders

•Linker forms a table •Application can get index from table and tie it to an application variable

•Similar process for uniform variables

Page 38: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

38Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Vertex Attribute Example

GLint colorAttr;colorAttr = glGetAttribLocation(myProgObj, "myColor");/* myColor is name in shader */

GLfloat color[4];glVertexAttrib4fv(colorAttr, color);/* color is variable in application */

In C/C++ application

Page 39: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

39Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Uniform Variable Example

GLint angleParam;angleParam = glGetUniformLocation(myProgObj, "angle");/* angle defined in shader */

/* my_angle set in application */GLfloat my_angle;my_angle = 5.0 /* or some other value */

glUniform1f(angleParam, my_angle);

In C/C++ application

Page 40: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

40Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Vertex Shader Applications

•Moving vertices Morphing

Wave motion

Fractals

•Lighting More realistic models

Cartoon shaders

Page 41: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

41Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Wave Motion Vertex Shader

uniform float time;uniform float xs, zs;void main(){float s;s = 1.0 + 0.1*sin(xs*time)*sin(zs*time);gl_Vertex.y = s*gl_Vertex.y;gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;}

In GLSL shader

Page 42: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

42Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Particle System

uniform vec3 init_vel;uniform float g, m, t;void main(){vec3 object_pos;object_pos.x = gl_Vertex.x + vel.x*t;object_pos.y = gl_Vertex.y + vel.y*t + g/(2.0*m)*t*t;object_pos.z = gl_Vertex.z + vel.z*t;gl_Position = gl_ModelViewProjectionMatrix* vec4(object_pos,1);}

In GLSL shader

Page 43: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

43Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Modified Phong Vertex Shader I

void main(void)/* modified Phong vertex shader (without distance term) */{ float f; /* compute normalized normal, light vector, view vector, half-angle vector in eye cordinates */ vec3 norm = normalize(gl_NormalMatrix*gl_Normal); vec3 lightv = normalize(gl_LightSource[0].position -gl_ModelViewMatrix*gl_Vertex); vec3 viewv = -normalize(gl_ModelViewMatrix*gl_Vertex); vec3 halfv = normalize(lightv + norm); if(dot(lightv, norm) > 0.0) f = 1.0; else f = 0.0;

In GLSL shader

Page 44: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

44Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Modified Phong Vertex Shader II

/* compute diffuse, ambient, and specular contributions */

vec4 diffuse = max(0, dot(lightv, norm))*gl_FrontMaterial.diffuse *LightSource[0].diffuse; vec4 ambient = gl_FrontMaterial.ambient*LightSource[0].ambient; vec4 specular = f*gl_FrontMaterial.specular* gl_LightSource[0].specular) *pow(max(0, dot( norm, halfv)), gl_FrontMaterial.shininess); vec3 color = vec3(ambient + diffuse + specular) gl_FrontColor = vec4(color, 1); gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;}

In GLSL shader

Page 45: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

45Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Pass Through Fragment Shader

/* pass-through fragment shader */void main(void){ gl_FragColor = gl_FrontColor;}

In GLSL shader

Page 46: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

46Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Vertex Shader for per Fragment Lighting

/* vertex shader for per-fragment Phong shading */varying vec3 normale;varying vec4 positione;void main(){ normale = gl_NormalMatrixMatrix*gl_Normal; positione = gl_ModelViewMatrix*gl_Vertex; gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;}

In GLSL shader

Page 47: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

47Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Fragment Shader for Modified Phong Lighting I

In GLSL shader

Page 48: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

48Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Fragment Shader for Modified Phong Lighting II

int f;if(dot(lightv, viewv)> 0.0) f =1.0);else f = 0.0;vec3 specular = f*pow(max(0, dot(norm, halfv), gl_FrontMaterial.shininess) *gl_FrontMaterial.specular*gl_LightSource[0].specular);vec3 color = vec3(ambient + diffuse + specular);gl_FragColor = vec4(color, 1.0);}

In GLSL shader

Page 49: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

49Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Vertex vs Fragment Shader

per vertex lighting per fragment lighting

Page 50: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

50Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Samplers

• Provides access to a texture object• Defined for 1, 2, and 3 dimensional textures and

for cube maps• In shader: uniform sampler2D myTexture;vec2 texcoord;vec4 texcolor = texture2D(mytexture, texcoord);

• In application: texMapLocation = glGetUniformLocation(myProg,“myTexture”);

glUniform1i(texMapLocation, 0);/* assigns to texture unit 0 */

Page 51: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

Fragment Shader Applications

Texture mapping

51Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

smooth shading environment mapping

bump mapping

Page 52: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

52Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Cube Maps

•We can form a cube map texture by defining six 2D texture maps that correspond to the sides of a box

•Supported by OpenGL•Also supported in GLSL through cubemap sampler

vec4 texColor = textureCube(mycube, texcoord);

•Texture coordinates must be 3D

Page 53: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

53Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Environment Map

Use reflection vector to locate texture in cube map

Page 54: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

54Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Environment Maps with Shaders

•Environment map usually computed in world coordinates which can differ from object coordinates because of modeling matrix

May have to keep track of modeling matrix and pass it shader as a uniform variable

•Can also use reflection map or refraction map (for example to simulate water)

Page 55: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

55Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Environment Map Vertex Shader

Varying vec3 reflectw;uniform mat4 modelMat;uniform mat3 invModelMat;uniform vec4 eyew;void main(void){ vec4 positionw = modelMat*gl_Vertex; vec3 normw = normalize(gl_Normal*invModelMat.xyz); vec3 lightw = normalize(eyew.xyz-positionw.xyz); eyew = reflect(normw, eyew); gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;}

Page 56: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

56Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Environment Map Fragment Shader

/* fragment shader for reflection map */varying vec3 reflectw;uniform samplerCube MyMap;void main(void){ gl_FragColor = textureCube(myMap, reflectw);}

Page 57: GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global

57Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Bump Mapping

•Perturb normal for each fragment•Store perturbation as textures