The OpenGL Shading Language Rahul Arora The Fixed Functionality - - PowerPoint PPT Presentation

the opengl shading language
SMART_READER_LITE
LIVE PREVIEW

The OpenGL Shading Language Rahul Arora The Fixed Functionality - - PowerPoint PPT Presentation

The OpenGL Shading Language Rahul Arora The Fixed Functionality Rendering Pipeline Object space View space Clip space Screen space Vertex and Transformation Primitive Fragment Depth test, Rasterization index lists and lighting


slide-1
SLIDE 1

The OpenGL Shading Language

Rahul Arora

slide-2
SLIDE 2

Vertex and index lists Transformation and lighting Primitive assembly Rasterization Fragment

  • perations

Depth test, Blending, etc.

The Fixed Functionality Rendering Pipeline

Object space View space Clip space Screen space

slide-3
SLIDE 3

Vertex and index lists Transformation and lighting Primitive assembly Rasterization Fragment

  • perations

Depth test, Blending, etc.

The Fixed Functionality Rendering Pipeline

How would you perform Phong shading?

slide-4
SLIDE 4

Problems?

  • Only Phong illumination model supported.
  • Only a few pre-programmed shading models supported.
  • Flat shading
  • Gouraud shading
  • No per-fragment lighting.
  • No screen space shading.
slide-5
SLIDE 5

The Programmable Pipeline

Vertex and index lists Vertex shader Primitive assembly Rasterization Fragment shader Depth test, Blending, etc.

Object space Clip space Screen space

slide-6
SLIDE 6
slide-7
SLIDE 7
slide-8
SLIDE 8
slide-9
SLIDE 9

Or Use an Entirely Different Illumination Model!

slide-10
SLIDE 10

GLSL: The OpenGL Shading Language

  • C-like programming language.
  • Both vertex and fragment shaders are written in GLSL.
  • OpenGL requires certain outputs from the shaders.
  • But you can add additional ones for doing cool things.
slide-11
SLIDE 11

GLSL Qualifiers

  • uniform
  • Remains the same throughout the execution of the shader
  • attribute
  • Per-vertex data
  • varying
  • Per-fragment data
  • Automatically interpolated by fixed stages of the pipeline

Vertex and index lists Vertex shader Primitive assembly Rasterization Fragment shader Depth test, Blending, etc.

slide-12
SLIDE 12

GLSL: Data types

  • Scalars

float, int, bool

  • Vectors

Float: vec2, vec3, vec4 Integer: ivec[2|3|4] Boolean: bvec[2|3|4] Accessing data: vert[0], vert.x, vert.r, vert.xyz, vert.rgba

  • Matrices

Floating point: mat2, mat3, mat4

  • Textures

sampler1D, sampler2D, sampler3D Accessing data: texture(u, v)

slide-13
SLIDE 13

GLSL: Built-in Functions

  • Trigonometric

cos, sin, tan, etc.

  • Exponentiation

exp, log, sqrt, etc.

  • Common floating-point

abs, floor, min, clamp, etc.

  • Geometric

length, dot, cross, normalize, reflect, etc.

slide-14
SLIDE 14

Built-in Inputs and Outputs

Vertex Shader

Input: _________ Output: gl_Position (vec4)

Fragment Shader

Input: gl_FragCoord (vec4) Output: gl_FragColor (vec4)

slide-15
SLIDE 15

Recall the Phong Illumination Model

Ambient term Diffuse term Specular term

shininess

slide-16
SLIDE 16

Example

// Vertex Shader attribute highp vec4 vertex; uniform mediump mat4 modelview; uniform mediump mat4 projection; void main() { gl_Position = projection * modelview * vertex; } // Fragment Shader uniform vec3 ambientColor; void main() { gl_FragColor = vec4(ambientColor, 1); }

slide-17
SLIDE 17
slide-18
SLIDE 18

Example

// Vertex Shader attribute highp vec4 vertex; uniform mediump mat4 modelview; uniform mediump mat4 projection; varying vec3 normalInterp; void main() { gl_Position = projection * modelview * vertex; normalInterp = <your_code>; } // Fragment Shader uniform vec3 ambientColor; uniform vec3 diffuseColor; varying vec3 normalInterp; uniform vec3 lightPos; void main() { // normalize normalInterp first vec3 N = <your_code>; // get light direction vec3 L = <your_code>; float lambertian = <your_code>; gl_FragColor = vec4(ambientColor + lambertian * diffuseColor, 1); }

slide-19
SLIDE 19
slide-20
SLIDE 20

Example

// Vertex Shader attribute highp vec4 vertex; uniform mediump mat4 modelview; uniform mediump mat4 projection; varying vec3 normalInterp; void main() { gl_Position = projection * modelview * vertex; normalInterp = <your_code>; } // Fragment Shader ... uniform vec3 specularColor; uniform float shininess; void main() { // find N, L, lambertian ... // use lambertian and shininess to find // specular intensity float specular = <your_code>; gl_FragColor = vec4(ambientColor + lambertian * diffuseColor + specular * specularColor, 1); }

slide-21
SLIDE 21
slide-22
SLIDE 22

Questions?