32
CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2013

CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

CSE 167:

Introduction to Computer Graphics

Lecture #8: GLSL

Jürgen P. Schulze, Ph.D.

University of California, San Diego

Fall Quarter 2013

Page 2: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Announcements

� Limited office hours this Thursday: only 3:30-5:30pm

Midterm Exam #1

� This Thursday, Oct 24th

� Location: Center Hall 119

� Time: 2:00pm – 3:20pm

� To bring:

� Pen/pencil, eraser

� Ruler

� Scratch paper

� Photo ID: put on your table until you got checked off

� Not allowed:

� Books, written or printed notes, cell phones, other electronic devices

2

Page 3: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Lecture Overview

� Programmable Shaders

� Vertex Programs

� Fragment Programs

� GLSL

3

Page 4: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Vertex Programs

Vertex

program

Vertex Attributes

From Application

To Rasterizer

Output Variables

Uniform Parameters

4

Page 5: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Vertex Attributes

� Declared using the attribute storage classifier

� Different for each execution of the vertex program

� Can be modified by the vertex program

� Two types:

� Pre-defined OpenGL attributes. Examples:attribute vec4 gl_Vertex;

attribute vec3 gl_Normal;

attribute vec4 gl_Color;

� User-defined attributes. Example:attribute float myAttrib;

5

Page 6: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Uniform Parameters

� Declared by uniform storage classifier

� Normally the same for all vertices

� Read-only

� Two types:

� Pre-defined OpenGL state variables

� User-defined parameters

6

Page 7: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Uniform Parameters: Pre-Defined

� Provide access to the OpenGL state

� Examples for pre-defined variables:uniform mat4 gl_ModelViewMatrix;

uniform mat4 gl_ModelViewProjectionMatrix;

uniform mat4 gl_ProjectionMatrix;

uniform gl_LightSourceParameters

gl_LightSource[gl_MaxLights];

7

Page 8: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Uniform Parameters: User-Defined

� Parameters that are set by the application

� Should not be changed frequently

� Especially not on a per-vertex basis!

� To access, use glGetUniformLocation, glUniform*

in application

� Example:

� In shader declareuniform float a;

� Set value of a in application:GLuint p;

int I = glGetUniformLocation(p,”a”);

glUniform1f(i, 1.0f);

8

Page 9: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Vertex Programs: Output Variables

� Required output: homogeneous vertex coordinatesvec4 gl_Position

� varying output variables

� Mechanism to send data to the fragment shader

� Will be interpolated during rasterization

� Fragment shader gets interpolated data

� Pre-defined varying output variables, for example:varying vec4 gl_FrontColor;

varying vec4 gl_TexCoord[];

Any pre-defined output variable that you do not overwrite will have the value of the OpenGL state.

� User-defined varying output variables, e.g.:

varying vec4 vertex_color;

9

Page 10: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Lecture Overview

� Programmable Shaders

� Vertex Programs

� Fragment Programs

� GLSL

10

Page 11: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Fragment Programs

Fragment

program

Fragment Data

From Rasterizer

To Frame Buffer

Output Variables

Uniform Parameters

11

Page 12: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Fragment Data

� Changes for each execution of the fragment program

� Fragment data includes:

� Interpolated standard OpenGL variables for fragment shader, as generated by vertex shader, for example:varying vec4 gl_Color;

varying vec4 gl_TexCoord[];

� Interpolated varying variables from vertex shader

� Allows data to be passed from vertex to fragment shader

12

Page 13: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Uniform Parameters

� Same as in vertex programs

13

Page 14: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Output Variables

� Pre-defined output variables:� gl_FragColor

� gl_FragDepth

� OpenGL writes these to the frame buffer

� Result is undefined if you do not set these variables!

14

Page 15: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Lecture Overview

� Programmable Shaders

� Vertex Programs

� Fragment Programs

� GLSL

15

Page 16: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

GLSL Main Features

� Similar to C language

� attribute, uniform, varying storage classifiers

� Set of predefined variables

� Access to per-vertex, per-fragment data

� Access OpenGL state

� Built-in vector data types, vector operations

� No pointers

� No direct access to data or variables in your C++ code

16

Page 17: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Example: Treat normals as colors// Vertex Shader

varying vec4 color;

void main()

{

// Treat the normal (x, y, z) values as (r, g, b) color

components.

color = vec4(clamp(abs((gl_Normal + 1.0) * 0.5), 0.0, 1.0),

1.0);

gl_Position = ftransform();

}

// Fragment Shader

varying vec4 color;

void main()

{

gl_FragColor = color;

}

17

Page 18: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Creating Shaders in OpenGL

Source: Gabriel Zachmann, Clausthal University

18

Page 19: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Video

� OpenGL and GLSL Demo 2

� http://www.youtube.com/watch?v=cQ8P16X0Op8

19

Page 20: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Tutorials and Documentation

� OpenGL and GLSL specifications

� http://www.opengl.org/documentation/specs/

� GLSL tutorials

� http://www.lighthouse3d.com/opengl/glsl/

� http://www.clockworkcoders.com/oglsl/tutorials.html

� OpenGL Programming Guide (Red Book)

� OpenGL Shading Language (Orange Book)

� OpenGL 4.4 API Reference Card

� http://www.khronos.org/files/opengl44-quick-reference-card.pdf

20

Page 21: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Lecture Overview

� Texture Mapping

� Overview

� Wrapping

� Texture coordinates

� Anti-aliasing

21

Page 22: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Large Triangles

Pros:

� Often sufficient for simple geometry

� Fast to render

Cons:

� Per vertex colors look boring and computer-generated

22

Page 23: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Texture Mapping

� Map textures (images) onto surface polygons

� Same triangle count, much more realistic appearance

23

Page 24: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Texture Mapping

� Goal: map locations in texture to locations on 3D geometry

� Texture coordinate space

� Texture pixels (texels) have texture coordinates (s,t)

� Convention

� Bottom left corner of texture is at(s,t) = (0,0)

� Top right corner is at (s,t) = (1,1)

(1,1)

(0,0)s

t

Texture coordinates

24

Page 25: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Texture Mapping

� Store 2D texture coordinates s,t with each triangle vertex

(0.4,0.45)

(0.6,0.4)

(1,1)

(0,0)s

t

Texture coordinates

(0.65,0.75)

v1

(s,t) = (0.65,0.75)

Triangle in any space before projection

v0

(s,t) = (0.6,0.4)

v2

(s,t) = (0.4,0.45)

25

Page 26: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Texture Mapping

� Each point on triangle has barycentric coordinates α, β, γ

� Barycentric coordinates interpolate texture coordinates

� Done automatically on GPU

(0.4,0.45)

(0.6,0.4)

(1,1)

(0,0)s

t

(0.65,0.75)

v1

(s,t) = (0.65,0.75)

v0

(s,t) = (0.6,0.4)

v2

(s,t) = (0.4,0.45)

Texture coordinates

Triangle in any space before projection

26

Page 27: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Texture Mapping

� Each point on triangle gets color from its corresponding point in texture

(0.4,0.45)

(0.6,0.4)

(1,1)

(0,0)s

t

(0.65,0.75)

v1

(s,t) = (0.65,0.75)

v0

(s,t) = (0.6,0.4)

v2

(s,t) = (0.4,0.45)

Texture coordinatesTriangle in any space before projection

27

Page 28: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Texture Mapping

Includes texture mapping

Frame-buffer access

(z-buffering)

Modeling and viewing

transformation

Shading

Projection

Rasterization

Primitives

Image

Fragment processing

28

Page 29: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Texture Look-Up

� Given interpolated texture coordinates (s, t) at current pixel

� Closest four texels in texture space are at

(s0,t0), (s1,t0), (s0,t1), (s1,t1)

� How to compute pixel color?

29

t1

t

t0

s0 s s1

Page 30: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Nearest-Neighbor Interpolation

� Use color of closest texel

� Simple, but low quality and aliasing

30

t1

t

t0

s0 s s1

Page 31: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

Bilinear Interpolation

1. Linear interpolation horizontally:

Ratio in s direction rs:

ctop = tex(s0,t1) (1-rs) + tex(s1,t1) rscbot = tex(s0, t0) (1-rs) + tex(s1,t0) rs

31

01

0

ss

ssr

s

−=

t1

t

t0

s0 s s1

ctop

cbot

Page 32: CSE 167: Introduction to Computer Graphics Lecture #8: GLSLivl.calit2.net/wiki/images/9/95/08_GLSL_F13.pdf · CSE 167: Introduction to Computer Graphics Lecture #8: GLSL Jürgen P

2. Linear interpolation vertically

Ratio in t direction rt:

c = cbot (1-rt) + ctop rt

Bilinear Interpolation

32

01

0

tt

ttrt

−=

t1

t

t0

s0 s s1

ctop

cbot

c