Notes on Computer Graphics and OpenGL. Thomas Strathmann

Similar documents
CS 130 Final. Fall 2015

For each question, indicate whether the statement is true or false by circling T or F, respectively.

Module Contact: Dr Stephen Laycock, CMP Copyright of the University of East Anglia Version 1

CHAPTER 1 Graphics Systems and Models 3

CS 130 Exam I. Fall 2015

Interactive Computer Graphics A TOP-DOWN APPROACH WITH SHADER-BASED OPENGL

CS 4600 Fall Utah School of Computing

Advanced Lighting Techniques Due: Monday November 2 at 10pm

CS5620 Intro to Computer Graphics

CS 559 Computer Graphics Midterm Exam March 22, :30-3:45 pm

Introduction to Visualization and Computer Graphics

ECS 175 COMPUTER GRAPHICS. Ken Joy.! Winter 2014

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

CS 4620 Program 3: Pipeline

Game Architecture. 2/19/16: Rasterization

Orthogonal Projection Matrices. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

MET71 COMPUTER AIDED DESIGN

Mathematical Tools in Computer Graphics with C# Implementations Table of Contents

Computer Graphics I Lecture 11

Pipeline Operations. CS 4620 Lecture 14

Introduction Rasterization Z-buffering Shading. Graphics 2012/2013, 4th quarter. Lecture 09: graphics pipeline (rasterization and shading)

CS 130 Exam I. Fall 2015

Raytracing CS148 AS3. Due :59pm PDT

GLOBAL EDITION. Interactive Computer Graphics. A Top-Down Approach with WebGL SEVENTH EDITION. Edward Angel Dave Shreiner

Topics and things to know about them:

Lighting/Shading III. Week 7, Wed Mar 3

Shading 1: basics Christian Miller CS Fall 2011

Interactive Real-Time Raycasting

Open Game Engine Exchange Specification

Homework #2 and #3 Due Friday, October 12 th and Friday, October 19 th

Illumination Models & Shading

Objectives Shading in OpenGL. Front and Back Faces. OpenGL shading. Introduce the OpenGL shading methods. Discuss polygonal shading

Recollection. Models Pixels. Model transformation Viewport transformation Clipping Rasterization Texturing + Lights & shadows

Models and Architectures

Computer Graphics. Illumination and Shading

CPSC GLOBAL ILLUMINATION

Graphics Pipeline 2D Geometric Transformations

Institutionen för systemteknik

Lecture 15: Shading-I. CITS3003 Graphics & Animation

CSE 167: Introduction to Computer Graphics Lecture #6: Lights. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2016

Spring 2012 Final. CS184 - Foundations of Computer Graphics. University of California at Berkeley

Lets assume each object has a defined colour. Hence our illumination model is looks unrealistic.

COMP30019 Graphics and Interaction Transformation geometry and homogeneous coordinates

Computer Graphics Seminar

CPSC 314 LIGHTING AND SHADING

EF432. Introduction to spagetti and meatballs

2D/3D Geometric Transformations and Scene Graphs

Lecture 8 Ray tracing Part 1: Basic concept Yong-Jin Liu.

Deferred Rendering Due: Wednesday November 15 at 10pm

Topic 9: Lighting & Reflection models 9/10/2016. Spot the differences. Terminology. Two Components of Illumination. Ambient Light Source

Computergrafik. Matthias Zwicker. Herbst 2010

Topic 9: Lighting & Reflection models. Lighting & reflection The Phong reflection model diffuse component ambient component specular component

Pipeline Operations. CS 4620 Lecture 10

CS559 Computer Graphics Fall 2015

UNIT 4 GEOMETRIC OBJECTS AND TRANSFORMATIONS-1

Curves and Surfaces. CS475 / 675, Fall Siddhartha Chaudhuri

Homework #2. Shading, Projections, Texture Mapping, Ray Tracing, and Bezier Curves

Today. Global illumination. Shading. Interactive applications. Rendering pipeline. Computergrafik. Shading Introduction Local shading models

Graphics and Interaction Transformation geometry and homogeneous coordinates

Interpolation using scanline algorithm

CGT520 Lighting. Lighting. T-vertices. Normal vector. Color of an object can be specified 1) Explicitly as a color buffer

Topic 0. Introduction: What Is Computer Graphics? CSC 418/2504: Computer Graphics EF432. Today s Topics. What is Computer Graphics?

Homework 3: Programmable Shaders

GEOMETRIC TRANSFORMATIONS AND VIEWING

Motivation. Parametric Curves (later Surfaces) Outline. Tangents, Normals, Binormals. Arclength. Advanced Computer Graphics (Fall 2010)

Final Exam CS 184: Foundations of Computer Graphics! page 1 of 12!

Midterm Exam Fundamentals of Computer Graphics (COMP 557) Thurs. Feb. 19, 2015 Professor Michael Langer

S U N G - E U I YO O N, K A I S T R E N D E R I N G F R E E LY A VA I L A B L E O N T H E I N T E R N E T

GEOMETRIC OBJECTS AND TRANSFORMATIONS I

Blue colour text questions Black colour text sample answers Red colour text further explanation or references for the sample answers

Illumination & Shading: Part 1

Computer Graphics Curves and Surfaces. Matthias Teschner

Computer Graphics and GPGPU Programming

Geometric Transformations

Texture Mapping. Texture (images) lecture 16. Texture mapping Aliasing (and anti-aliasing) Adding texture improves realism.

lecture 16 Texture mapping Aliasing (and anti-aliasing)

Today. Global illumination. Shading. Interactive applications. Rendering pipeline. Computergrafik. Shading Introduction Local shading models

The University of Calgary

CSE 167: Lecture #7: Color and Shading. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2011

Surface Graphics. 200 polys 1,000 polys 15,000 polys. an empty foot. - a mesh of spline patches:

Lighting and Shading II. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Computer Graphics (CS 543) Lecture 7b: Intro to lighting, Shading and Materials + Phong Lighting Model

CMSC427: Computer Graphics Lecture Notes Last update: November 21, 2014

CMSC427 Final Practice v2 Fall 2017

TSBK03 Screen-Space Ambient Occlusion

Computer Graphics (CS 4731) Lecture 16: Lighting, Shading and Materials (Part 1)

Computer Graphics. Illumination Models and Surface-Rendering Methods. Somsak Walairacht, Computer Engineering, KMITL

2D rendering takes a photo of the 2D scene with a virtual camera that selects an axis aligned rectangle from the scene. The photograph is placed into

Topic 12: Texture Mapping. Motivation Sources of texture Texture coordinates Bump mapping, mip-mapping & env mapping

The exam begins at 2:40pm and ends at 4:00pm. You must turn your exam in when time is announced or risk not having it accepted.

3D Graphics for Game Programming (J. Han) Chapter II Vertex Processing

Topic 11: Texture Mapping 11/13/2017. Texture sources: Solid textures. Texture sources: Synthesized

GEOMETRIC TOOLS FOR COMPUTER GRAPHICS

CS 418: Interactive Computer Graphics. Basic Shading in WebGL. Eric Shaffer

Lighting and Shading

Objectives. Introduce the OpenGL shading Methods 1) Light and material functions on MV.js 2) per vertex vs per fragment shading 3) Where to carry out

Graphics for VEs. Ruth Aylett

Mach band effect. The Mach band effect increases the visual unpleasant representation of curved surface using flat shading.

EF432. Introduction to spagetti and meatballs

Transcription:

Notes on Computer Graphics and OpenGL Thomas Strathmann February 2, 2011

Preface The purpose of this document is to server as a sort of logbook in which I put down all the interesting math and assorted trivia I encounter while occasionally dabbling in computer graphics. Thus, the contents are not in any meaningful sense of the word exhaustive or suitable as an introductionary text. Only rasterization techniques and related topics (such as interpolation or procedural geometry/textures) are discussed. I hope that others will find this information not only useful, but will be compelled to extend the contents of this little "book". Because, in order to really understand any subject matter one not only has to absorb the knowledge of other people, but rather think it through and try to explain it to others. This could be the start of a beautiful project... Thomas Strathmann (December 2008) i

ii

Contents 1 Models and Geometry 1 1.1 Coordinate Systems......................... 1 1.1.1 OpenGL............................ 2 1.1.2 Homogeneous Matrices.................... 2 1.1.3 Orientation.......................... 3 1.2 Calculating Normals......................... 4 1.2.1 Calculating Normals for Triangles............. 4 1.2.2 Calculating Normals for Subdivision Surfaces....... 4 1.2.3 Calculating Normals for Analytical Curves........ 5 2 Interpolation 7 2.1 Linear Interpolation......................... 7 2.1.1 Bilinear Interpolation.................... 7 2.2 Cosine Interpolation......................... 8 2.3 Catmull-Rom Splines......................... 8 2.4 Smooth Step............................. 8 3 Illumination 9 3.1 Attenuation.............................. 9 3.1.1 Point Light.......................... 9 3.1.2 Spot Light.......................... 9 3.1.3 Directional Light....................... 9 3.2 Phong Shading............................ 9 A Notation 11 iii

iv CONTENTS

Chapter 1 Models and Geometry 1.1 Coordinate Systems In 3D graphics all scenes usually live in the three dimensional vector space R 3, i. e. coordinates are assumed to be real numbers. In practise however these coordinates are only approximations of real numbers with finite precision (e..g. float or double data types). All vectors v R 3 can be expressed as linear combinations of a finite collection of basis vectors. This set is usually (for convenience) an orthonormal basis which means that all basis vectors have length 1 (unit length) and the vectors are pairwise orthogonal. The most important aspect of coordinate systems for computer graphics is how we interpret these basis vectors: Which of the basis vectors points up, down, right, and so on? That is, we have to decide how to relate a mathematical vector v = (0 2 1) to a point in the space we see with our eyes. There are two (classes of) conventions, the left-handed and the right-handed coordinate system depicted in figure 1.1. In addition to fixing the correspondence between components of a vector and physical dimensions the drawings give the direction of rotation around the axis. In left-handed coordinate systems a positive value rotates clockwise (CW) whereas in right-handed coordinate systems a positive angle means a counter-clockwise (CCW) rotation. z z x y (a) Left-handed orientation y x (b) Right-handed orientation Figure 1.1: Coordinate system conventions 1

2 CHAPTER 1. MODELS AND GEOMETRY 1.1.1 OpenGL In OpenGL the y-axis points upwards and the x-axis points towards the right. The most peculiar part of the convention is that the positive z-axis points towards the viewer, i. e. the z-component of any vector becomes negative as it moves farther away from the camera. y x z Figure 1.2: OpenGL texture coordinates Texture coordinates are another matter as figure 1.1.1 depicts. (0, 1) (1, 1) (0, 0) (1, 0) Figure 1.3: OpenGL 2D texture coordinates Thus, to draw a textured Quad in the xy-plane the following OpenGL code may be used. Note, that the winding convention for front facing polygons mandates that the vertices be drawn in counter-clockwise order. glbegin (GL_QUADS) glnormal3i ( 0, 0, 1 ) ; gltexcoord2i ( 0, 0 ) ; g l V e r t e x 3 i ( 1, 1, 0 ) ; gltexcoord2i ( 1, 0 ) ; g l V e r t e x 3 i ( 1, 1, 0 ) ; gltexcoord2i ( 1, 1 ) ; g l V e r t e x 3 i ( 1, 1, 0 ) ; gltexcoord2i ( 0, 1 ) ; g l V e r t e x 3 i ( 1, 1, 0 ) ; glend ( ) ; 1.1.2 Homogeneous Matrices Linear transformations in n dimensions can be described by a n n matrix, but affine transformations can only be described by n + 1 n + 1 matrices. We use so-called homogeneous 4 4 matrices to describe affine and other kinds of transformations in 3D space. Such a Matrix can be understood as transforming one coordinate system A into another coordinate system B. This matrix is then denoted A T B and consists of a linear part (the rotational component) and an

1.1. COORDINATE SYSTEMS 3 affine part (the translation component). x 1 y 1 z 2 p 1 A T B = x 2 y 2 z 2 p 2 x 3 y 3 z 3 p 3 0 0 0 1 The upper left 3 3 submatrix is the linear part of the transformations. Its constituent column vectors x, y, and z are the three basis vectors of the system B. Thus, they need to be linearly independent (or pairwise orthogonal). The remaining column vector p is exactly the affine portion of the matrix. The last row is invariantly the same for physically motivated transformations. Inverting a Homogeneous Matrix The inverse of a 4 4 homogenous matrix A T B is given by x 1 x 2 x 3 p, x ( A T B ) 1 = B T A = y 1 y 2 y 3 p, y z 1 z 2 z 3 p, z (1.1.1) 0 0 0 1 provided that the upper left 3 3 matrix is orthogonal. A matrix M is called orthogonal iff its column vectors are pairwise orthonormal, i. e. each column vector has norm 1 in addition to being orthogonal to all other column vectors. 1.1.3 Orientation The orientation of an object is determined by the linear component of the transformation matrix from inertial space to the world space (GL_MODELVIEWMATRIX). A number of different approaches beside transformation matrices exist to describe the orientation of an object. Rotation Matrices 1 0 0 R x = 0 cos(α) sin(α) (1.1.2) 0 sin(α) cos(α) cos(α) 0 sin(α) R y = 0 1 0 (1.1.3) sin(α) 0 cos(α) cos(α) sin(α) 0 R z = sin(α) cos(α) 0 (1.1.4) 0 0 1

4 CHAPTER 1. MODELS AND GEOMETRY Euler Angles TODO Definition, different conventions and names Converting to homogenous matrices and back Problem of gimbal lock (yields motiviation for use of quaternions) Quaternions TODO 1.2 Calculating Normals 1.2.1 Calculating Normals for Triangles Given a triangle consisting of three vertices v 1, v 2, v 3 oriented in counter-clockwise fashion the surface normal of the triangle mesh is N = (v 2 v 1 ) (v 3 v 1 ) (v 2 v 1 ) (v 3 v 1 ) (1.2.1) The normal at a vertex v in the triangulated mesh is then calculated by averaging the surface normals of the k triangles that share that vertex: N v = 1 k i=1 N i k N i (1.2.2) Instead of using formula (1.2.1) for the computation of the surface normals, one could use the following formula instead i=1 N = (v 2 v 1 ) (v 3 v 1 ) (1.2.3) which takes into account the area of the involved triangles. 1.2.2 Calculating Normals for Subdivision Surfaces e.g. a grid of QUADS or a TRIANGLE_STRIP for ( int z =0; z<s i z e z 2; z++) { for ( int x=0; x<s i z e x 2; x++) { f loat tx = ( float ) x/ s i z e x ; f loat tz = ( float ) z / s i z e z ; f loat dx = 1.0/ s i z e x ; f loat dz = 1.0/ s i z e z ; gltexcoord2f ( tx, tz ) ; glnormal3f ( h eight [ z ] [ x+1] height [ z ] [ x ], 1. 0, height [ z +1][ x ] height [ z ] [ x ] ) ; g l V e r t e x 3 f ( tx, h eight [ z ] [ x ], tz ) ;

1.2. CALCULATING NORMALS 5 gltexcoord2f ( tx+dx, tz ) ; glnormal3f ( h e i g h t [ z ] [ x+2] height [ z ] [ x +1], 1. 0, h e i g h t [ z +1][ x+1] height [ z ] [ x + 1 ] ) ; g l V e r t e x 3 f ( tx+dx, h e ight [ z ] [ x +1], tz ) ; gltexcoord2f ( tx+dx, t z+dz ) ; glnormal3f ( h e i g h t [ z +1][ x+2] height [ z +1][ x +1], 1. 0, h e i g h t [ z +2][ x+1] height [ z +1][ x + 1 ] ) ; g l V e r t e x 3 f ( tx+dx, h e ight [ z +1][ x +1], tz+dz ) ; } } gltexcoord2f ( tx, tz+dz ) ; glnormal3f ( h e i g h t [ z +1][ x+1] height [ z +1][ x ], 1. 0, h e i g h t [ z +2][ x ] height [ z +1][ x ] ) ; g l V e r t e x 3 f ( tx, h e i g ht [ z +1][ x ], tz+dz ) ; 1.2.3 Calculating Normals for Analytical Curves Given a differentiable parametric curve p : R R 3 with x(t) p(t) = y(t), z(t) calculate the Frenet frame as follows v tangent = ( x t ( x t y t y t v normal = v tangent ) z T t z t ( 2 x 2 t ( 2 x 2 t ) T (1.2.4) 2 y 2 t 2 y 2 t ) T 2 z 2 t 2 z 2 t ) T (1.2.5) v binormal = v tangent v normal (1.2.6) These three vectors form an orthonormal basis which is aligned to p at time t. The problem of odd behaviour at inflection points of p (where p (t) = 0 and p (t) = 0) can be mitigated by calculating the normal vector as the cross product of the tangent with a vector which is guaranteed to never be parallel to the normal vector as follows: v normal = v tangent ( 0 1 0 ) T (1.2.7) In the numerical case the tangent vector v tangent can be approximated from two computed points p 1 and p 2 by using either the forward, backward, or central difference method using the equation v tangent = p 2 p 1 p 2 p 1 (1.2.8)

6 CHAPTER 1. MODELS AND GEOMETRY Let δ be the step size of the approximation and t the current time. Then p 1 and p 2 are given by p 1 = p(t) p 2 = p(t + δ) for forward diffence, (1.2.9) p 1 = p(t δ) p 2 = p(t) for backward diffence, (1.2.10) p 1 = p(t + δ 2 ) p 2 = p(t + δ ) for central diffence. (1.2.11) 2

Chapter 2 Interpolation 2.1 Linear Interpolation Given two points p 0 and p 1 and an index t linear interpolation between p 0 and p 1 is accomplished by the formula 2.1.1 Bilinear Interpolation (1 t) p 0 + t p 1 (2.1.1) Often one needs to calculate the value of a function f of two variables on a two dimensional grid. The most commonly used procedure to achieve this is called bilinear interpolation. It is used to interpolate 2D textures, for instance. Let the values of f at four points wich form a rectangular region in which the point p = (x, y) lies be known as q 00 = (x 0, y 0 ), q 01 = (x 0, y 1 ), q 10 = (x 1, y 0 ), q 11 = (x 1, y 1 ). First, we interpolate in the x direction: f((x, y 0 )) x 1 x x 1 x 0 f(q 00 ) + x x 0 x 1 x 0 f(q 10 ) (2.1.2) f((x, y 1 )) x 1 x x 1 x 0 f(q 01 ) + x x 0 x 1 x 0 f(q 11 ) (2.1.3) Then, we interpolate in the y direction by using the previously computed values: f(p) = f(x, y) y 1 y y 1 y 0 f((x, y 0 )) + y y 0 y 1 y 0 f((x, y 1 )) (2.1.4) Combining the two steps into one yields the following complete formula for the bilinear estimate of f(x, y): f(x, y) f(q 00 ) (x 1 x 0 )(y 1 y 0 ) (x 1 x)(y 1 y) + f(q 10 ) (x 1 x 0 )(y 1 y 0 ) (x x 0)(y 1 y) + f(q 01 ) (x 1 x 0 )(y 1 y 0 ) (x 1 x)(y y 0 ) + f(q 11 ) (x 1 x 0 )(y 1 y 0 ) (x x 0)(y y 0 ) (2.1.5) 7

8 CHAPTER 2. INTERPOLATION 2.2 Cosine Interpolation A variation on linear interpolation is cosine interpolation where the index t in formula 2.1.1 is substituted by t. 2.3 Catmull-Rom Splines t = 1 (1 cos(πt)) (2.2.1) 2 Catmull-Rom splines are a special case of cubic splines, thus requiring four points for interpolation. Given four points p 0,..., p 3 wherep 1 and p 2 are the points we want to interpolate between and p 0, p 3 are additional points, Catmull- Rom interpolation takes an index t and is computed as follows: 1 2 (2p 1 + ( p 0 + p 2 )t + (2p 0 5p 1 + 4p 2 p 3 )t 2 +( p 0 + 3p 1 3p 2 + p 3 )t 3 ) (2.3.1) Originally conceived to interpolate camera motion from a set of key points Catmull-Rom splines yield extremely pleasing results provided that the first and second derivative of the curve defined by the keypoints are bounded. 2.4 Smooth Step The smooth step function is useful in blending two values a and b given an index variable x: 0 for x < a smoothstep(a, b, x) := 1 for x b ( ) (2.4.1) 2 x a b a (3 2 x a b a ) otherwise

Chapter 3 Illumination 3.1 Attenuation 3.1.1 Point Light The intensity I P of a point light source P at distance d from its position is given by I P = 1 k c + k l d + k q d 2 C P (3.1.1) where C P is the color of the light source and k c, k l, and k q are the constant, linear, and qudaratic attenuation factors respectively. 3.1.2 Spot Light The intensity I S of a spot light source S at distance d from its position is given by I S = max( R, L, 0)p k c + k l d + k q d 2 C S (3.1.2) where C S is the color of the light, k c, k l, and k q are as before, R is the direction in which the spot is pointing, and L is the unit vectzor between the point under consideration and the light source (cf. section 3.2). 3.1.3 Directional Light Assumed to be infinitely far away so as to have light rays which are perfectly parallel, the intensity of the light emitted by a directional light source is constant. 3.2 Phong Shading The Phong lighting equation is due to Bui Tuong Phong, who published in 1973 as part of his Ph.D. thesis. The color of every pixel (fragment) I f is given by 9

10 CHAPTER 3. ILLUMINATION the equation I f = I a + I d + I s (3.2.1) where I a is a four-dimensional RGBA vector which denotes the ambient term, I d denotes the diffuse term, and I s the specular term. The ambient term is defined as I a = (A l A m ) + (A s A m ) (3.2.2) where A l is the ambient color of the light source, A m the ambient color of the material, and A s the scene color (gl_frontlightmodelproduct.scenecolor in GLSL). The diffuse component of the lighting equation is given by the Lambertian reflection term weighted by the diffuse color of light source D l and material D m I d = D l D m max( N, L, 0) (3.2.3) where N denotes the normal vector of the pixel. The vector L is the normalized vector between the position of the light source and the pixel under consideration. Given the vertex position v in eye coordinates (i.e. the vertex multiplied by the ModelView matrix) and the light source position l it is defined as follows: L = l v l v The specular term I s is calculated by the equation { S m S l max( R, V, 0) f if N, L > 0 I s = 0 otherwise (3.2.4) (3.2.5) where S m and S l again denote the specular color of the material and light source respectively. R is the reflected light vector given by R = 2 N, L N L (3.2.6) which is equal to reflect(l, N) in GLSL. V is the view vector given by V = v v (3.2.7) where v again is the current vertex in eye coordinates. Finally, f denotes the specular factor (shininess). Alternatively the specular component can be expressed in a more computationally efficient manner as follows: { S m S l max( N, H, 0) f if N, L > 0 I s = (3.2.8) 0 otherwise where the halfway vector H between the direction to light vector L and the viewer vector V is given by H = L + V L + V (3.2.9)

Appendix A Notation v 1, v 2 Dot (inner) product of vectors v 1 and v 2 v 1 v 2 Cross product of v 1 and v 2 v magnitude (length) of vector v x expectation value (avergage) of x x component-wise ( ) derivatives of x, i.e. for an n-dimensional vector x x = x x 1 x x n 11

Bibliography [Bou99] Paul Bourke. Interpolation methods. http://local.wasp.uwa.edu.au/ pbourke/miscellaneous/interpolation/, December 1999. [Len04] Eric Lengyel. Mathematics for 3D Game Programming and Computer Graphics. Charles River Media, Inc., 2. edition, 2004. [SWND06] Dave Shreiner, Mason Woo, Jackie Neider, and Tom Davis. OpenGL Programming Guide. Charles River Media, Inc., 5. edition, 2006. 13