Computer Graphics Seminar

Similar documents
Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

Computer Graphics Geometric Transformations

Rendering Objects. Need to transform all geometry then

CSE 167. Discussion 03 ft. Glynn 10/16/2017

COMP371 COMPUTER GRAPHICS

GEOMETRIC TRANSFORMATIONS AND VIEWING

CS475/CS675 - Computer Graphics. OpenGL Drawing

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

Overview. By end of the week:

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

Ciril Bohak. - INTRODUCTION TO WEBGL

Computer graphics 2: Graduate seminar in computational aesthetics

Rendering Pipeline and Coordinates Transforms

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

Today. Rendering pipeline. Rendering pipeline. Object vs. Image order. Rendering engine Rendering engine (jtrt) Computergrafik. Rendering pipeline

CENG 477 Introduction to Computer Graphics. Graphics Hardware and OpenGL

CS 130 Final. Fall 2015

CHAPTER 1 Graphics Systems and Models 3

Lecture 6 Sections 4.3, 4.6, 4.7. Wed, Sep 9, 2009

The Graphics Pipeline and OpenGL I: Transformations!

Chapter 2 - Basic Mathematics for 3D Computer Graphics

CS 130 Exam I. Fall 2015

The Graphics Pipeline and OpenGL I: Transformations!

Rasterization Overview

Graphics Pipeline 2D Geometric Transformations

SUMMARY. CS380: Introduction to Computer Graphics Varying Variables Chapter 13. Min H. Kim KAIST School of Computing 18/04/26.

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

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

CPSC 436D Video Game Programming

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

We assume that you are familiar with the following:

CSE328 Fundamentals of Computer Graphics

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

API Background. Prof. George Wolberg Dept. of Computer Science City College of New York

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

Graphics pipeline and transformations. Composition of transformations

CS 432 Interactive Computer Graphics

Transformations. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

Homework 5: Transformations in geometry

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

window World space (Object space)

GEOMETRIC OBJECTS AND TRANSFORMATIONS I

Pipeline Operations. CS 4620 Lecture 14

2D transformations: An introduction to the maths behind computer graphics

Introduction to Computer Graphics with WebGL

Tutorial 04. Harshavardhan Kode. September 14, 2015

Game Architecture. 2/19/16: Rasterization

UNIT 4 GEOMETRIC OBJECTS AND TRANSFORMATIONS-1

E.Order of Operations

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

Notes on Assignment. Notes on Assignment. Notes on Assignment. Notes on Assignment

CS 4204 Computer Graphics

CSE528 Computer Graphics: Theory, Algorithms, and Applications

Building Models. Prof. George Wolberg Dept. of Computer Science City College of New York

COMP3421. Vector geometry, Clipping

Drawing in 3D (viewing, projection, and the rest of the pipeline)

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

Models and Architectures

8 Three-Dimensional Object Representations. Chapter 8. Three-Dimensional Object Representations. Department of Computer Science and Engineering 8-1

Affine Transformations Computer Graphics Scott D. Anderson

We will use WebGL 1.0. WebGL 2.0 is now being supported by most browsers but requires a better GPU so may not run on older computers or on most cell

Chapter 5. Projections and Rendering

Viewing with Computers (OpenGL)

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

Computer Graphics. Chapter 7 2D Geometric Transformations

CS452/552; EE465/505. Geometry Transformations

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

CSE 690: GPGPU. Lecture 2: Understanding the Fabric - Intro to Graphics. Klaus Mueller Stony Brook University Computer Science Department

Computer Viewing. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

Shaders. Introduction. OpenGL Grows via Extensions. OpenGL Extensions. OpenGL 2.0 Added Shaders. Shaders Enable Many New Effects

DRAFT: Mathematical Background for Three-Dimensional Computer Graphics. Jonathan R. Senning Gordon College

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

Computer Viewing. Prof. George Wolberg Dept. of Computer Science City College of New York

Interaction. CSCI 420 Computer Graphics Lecture 3

Software Engineering. ò Look up Design Patterns. ò Code Refactoring. ò Code Documentation? ò One of the lessons to learn for good design:

Basic Elements. Geometry is the study of the relationships among objects in an n-dimensional space

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Lecture 4. Viewing, Projection and Viewport Transformations

CS 130 Exam I. Fall 2015

CS770/870 Spring 2017 Transformations

Homework 5: Transformations in geometry

PROFESSIONAL. WebGL Programming DEVELOPING 3D GRAPHICS FOR THE WEB. Andreas Anyuru WILEY. John Wiley & Sons, Ltd.

Notes on Computer Graphics and OpenGL. Thomas Strathmann

Rendering. Converting a 3D scene to a 2D image. Camera. Light. Rendering. View Plane

Best practices for effective OpenGL programming. Dan Omachi OpenGL Development Engineer

Computação Gráfica. Computer Graphics Engenharia Informática (11569) 3º ano, 2º semestre. Chap. 4 Windows and Viewports

Graphics and Interaction Transformation geometry and homogeneous coordinates

Core Graphics and OpenGL ES. Dr. Sarah Abraham

COMP30019 Graphics and Interaction Transformation geometry and homogeneous coordinates

Specifying Complex Scenes

CMSC427 Transformations II: Viewing. Credit: some slides from Dr. Zwicker

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

Review of Thursday. xa + yb xc + yd

MATRIX REVIEW PROBLEMS: Our matrix test will be on Friday May 23rd. Here are some problems to help you review.

GRAFIKA KOMPUTER. ~ M. Ali Fauzi

Pipeline Operations. CS 4620 Lecture 10

Spring 2009 Prof. Hyesoon Kim

3D GRAPHICS. design. animate. render

Interactive Computer Graphics. Hearn & Baker, chapter D transforms Hearn & Baker, chapter 5. Aliasing and Anti-Aliasing

Transcription:

Computer Graphics Seminar MTAT.03.305 Spring 2018 Raimond Tunnel

Computer Graphics Graphical illusion via the computer Displaying something meaningful (incl art)

Math Computers are good at... computing. To do computer graphics, we need math for the computer to compute. Geometry, algebra, calculus.

Math For actually creating and manipulating objects in 3D we need: Analytic geometry math about coordinate systems Linear algebra math about vectors and spaces

Skills for Computer Graphics Mathematical understanding Geometrical (spatial) thinking Programming ( )( ) ( a b x ax+by = c d y cx+dy GLuint vaohandle; glgenvertexarrays(1, &vaohandle); glbindvertexarray(vaohandle); Visual creativity & aesthetics )

Point Simplest geometry primitive In homogeneous coordinates: (x, y, z, w), w 0 Represents a point (x/w, y/w, z/w) Usually you can put w = 1 for points Actual division will be done by GPU later (x, y, z, 1)

Line (segment) Consists of: 2 endpoints Infinite number of points between (x1, y1, z1, 1) Defined by the endpoints Interpolated and rasterized in the GPU (x2, y2, z2, 1)

Line (segment) Consists of: 2 endpoints Infinite number of points between (x1, y1, z1, 1) Defined by the endpoints Interpolated and rasterized in the GPU (x2, y2, z2, 1)

Line (segment) Consists of: 2 endpoints Infinite number of points between (x1, y1, z1, 1) Defined by the endpoints Interpolated and rasterized in the GPU (x2, y2, z2, 1)

Triangle Consists of: (x3, y3, z3, 1) 3 points called vertices (x2, y2, z2, 1) 3 lines called edges 1 face Front face (x1, y1, z1, 1) Defined by 3 vertices Face interpolated and rasterized in the GPU Counter-clockwise order defines front face

Why triangles? They are in many ways the simplest polygons 3 different points always form a plane Easy to rasterize (fill the face with pixels) Every other polygon can be converted to triangles

Why triangles? They are in many ways the simplest polygons 3 different points always form a plane Easy to rasterize (fill the face with pixels) Every other polygon can be converted to triangles OpenGL used to support other polygons too Must have been: Simple No edges intersect each other Convex All points between any two points are inner points

Examples of polygons C I D H J B G K F L A D A B A F C B A C C D E E B

OpenGL < 3.1 primitives OpenGL Programming Guide 7th edition, p49

After OpenGL 3.1 OpenGL Programming Guide 8th edition, p89-90

In the beginning there were points We can now define our geometric objects!

In the beginning there were points We can now define our geometric objects! We want to move our objects! World's (0, 0, 0)

Transformations Homogeneous coordinates allow easy: Linear transformations Act u hom ally th e oge neo se we c us c oord ould do inat w es... ithout Affine transformations Scaling, reflection Rotation Shearing Translation (moving / shifting) Projection transformations Perspective Orthographic This to o...

Transformations Every transformation is a function As you remember from Algebra, linear functions can be represented as matrices ( ) ( )( ) 2 x 2 0 0 x f (v)= y = 0 1 0 y z 0 0 1 z Linear function, which increases the first coordinate two times. Same function as a matrix v R 3 () x v= y z Column-major format

Transformations GPU-s are built for doing transformations with matrices on points (vertices). M v 0 M v 1 M v 2 Shader code Control Cache... Control Cache... Control Cache... Control Cache... Control Cache... Control Cache... Control Cache... Control Cache... DRAM

Transformations GPU-s are built for doing transformations with matrices on points (vertices). Linear transformations satisfy: f (a 1 x 1+...+ a n x n )=a1 f ( x 1 )+...+ a n f ( x n ) We don't use homogeneous coordinates at the moment, but they will be back...

Linear Transformation Scale

Scaling Multiplies the coordinates by a scalar factor. ( )( ) 2 0 x 0 1 y ( )( ) ( ) 2 0 1.5 3 = 0 1 1.5 1.5

Scaling Multiplies the coordinates by a scalar factor. Scales the standard basis vectors / axes. ( )( ) ( ) 2 0 1 2 = =e 0 0 1 0 0 ( )( ) ( ) 2 0 0 0 = =e 1 0 1 1 1

Scaling In general we could scale each axis ( ax 0 0 0 ay 0 0 0 az ) ax x-axis scale factor ay y-axis scale factor ax z-axis scale factor If some factor is negative, this matrix will reflect the points from that axis. Thus we get reflection. What happens to out triangles when an odd number of factors are negative?

Linear Transformation Shear

Shearing Not much used by itself, but remember it for translations later. Tilts only one axis. Squares become parallelograms. ( )( ) ( ) ( )( ) ( ) 1 0 0 0 = 1 1 2 2 1 0 1 1 = 1 1 2 3 ( )( ) 1 0 x 1 1 y

Shearing Shear-y, we tilt parallel to y axis by angle φ counter-clockwise ( )( ) ( 1 0 x x = tan (ϕ) 1 y y+ tan (ϕ) x ) Shear-x, we tilt parallel to x axis by angle φ clockwise ( )( ) ( 1 tan(ϕ) x x+ tan(ϕ) y = y 0 1 y )

Linear Transformation Rotation

Rotation Shearing moved only one axis Also changed the size of the basis vector Can we do better? Did you notice that the columns of the transformation matrix show the coordinates of the new basis vectors?

Rotation e ' 0=( a, b )=(cos (α), sin (α)) a a cos (α)= = = a e ' 0 1 e ' 1=( a ', b ' )=( sin(α), cos(α))

Rotation So if we rotate by α in counter-clockwise order in 2D, the transformation matrix is: e'0 ( e'1 ) cos(α) sin(α) sin(α) cos(α) In 3D we can do rotations in each plane (xy, xz, yz), so there can be 3 different matrices.

Rotation To do a rotation around an arbitrary axis, we can: Rotate that axis to be the x-axis Rotate around the new x-axis Invert the first rotations (move the old x-axis back) ( 1 0 0 0 cos(α) sin (α) 0 sin (α) cos(α) 0 0 0 OpenGL provides a command for rotating around a given axis. Often quaternions are used for rotations. Quaternions are elements of a number system that extend the complex numbers... 0 0 0 1 )

Do we have everything now? We can scale, share and rotate our geometry around the origin... What if we have an object not centered in the origin?

Affine Transformation Translation

Translation Imagine that our 1D world is located at y=1 line in 2D space. Objects Our world Notice that all the points are in the form: (x, 1)

Translation What happens if we do shear-x(45 ) operation on the 2D world? Everything in our world has moved magically one x-coordinate to the right... tan(45 ) = 1

Translation What if we do shear-x(63.4 )? tan(63.4 ) = 2 Everything has now moved 2 x-coordinates to the right from the original position We can do translation (movement)!

Translation When we represent our points in one dimension higher space, where the extra coordinate is 1, we get to the homogeneous space. ( )( ) ( ) 1 0 xt x x+ x t = 1 1 1 ( )( ) ( ) 1 0 0 1 0 0 xt x x+ x t y t y = y+ y t 1 1 1 ( )( ) ( ) 1 0 0 0 0 1 0 0 0 0 1 0 xt x x+ x t yt y y+ y t = zt z z+ z t 1 1 1

Transformations This together gives us a very good toolset to transform our geometry as we wish. Translation column Linear transformations on i t e ma n i f Af snfor tra ( )( ) ( a d g 0 b e h 0 c f i 0 xt x ax+ by+ cz+ x t yt y dx+ ey+ fz+ y t = zt z gx+ hy+ iz+ z t 1 1 1 Used for perspective projection... )

Multiple transformations Everything starts from the origin! To apply multiple transformations, just multiply matrices.

Multiple transformations Our initial geometry defined by vertices: (-1, -1), (1, -1), (1, 1), (-1, 1)

Multiple transformations ( cos (45)) ( cos (45 ) sin (45 ) 0 sin (45 ) cos(45 ) 0 0 0 1 )

Multiple transformations ( ) 1 0 4 0 1 0 0 0 1

Multiple transformations We can combine the transformations to a single matrix. ( )( )( 1 0 4 cos(45 ) sin(45 ) 0 cos(45 ) sin (45 ) 4 0 1 0 sin (45 ) cos(45 ) 0 = sin(45 ) cos(45 ) 0 0 0 1 0 0 1 0 0 1 ) This works for combining different affine transformations, but the result is hard to read... Order of transformations / matrices is important! http://cgdemos.tume-maailm.pri.ee

This in Practice?

OpenGL GPU API / middleware Set of commands that a program can give to GPU Supported in many languages Send vertex data Send transformations Draw!

WebGL GPU API in JavaScript Supported by major browsers THREE.js Higher level library to ease your coding: http://threejs.org/docs/ Send vertex data Send transformations Draw!

OpenGL and WebGL https://www.slideshare.net/khronos_group/open-standardsforgaming-nov13

The Setup Sending vertex data to the GPU

Vertex Array Object (VAO) An object to contain data arrays for vertices GLuint vaohandle; glgenvertexarrays(1, &vaohandle); glbindvertexarray(vaohandle); // Generate and bind vertex buffers - VBO-s // set data for position, color etc glbindvertexarray(0);

Vertex Buffer Object (VBO) Buffer for hold one vertex data array GLuint vaohandle; glgenvertexarrays(1, &vaohandle); glbindvertexarray(vaohandle); GLuint vbohandle; glgenbuffers(1, &vbohandle); glbindbuffer(gl_array_buffer, vbohandle); glbufferdata(gl_array_buffer, sizeof(glfloat) * vertexcount, vertexdataarray, GL_STATIC_DRAW);...

Vertex Attribute Variable in the shader, which points to the data GLuint vbohandle; glgenbuffers(1, &vbohandle); glbindbuffer(gl_array_buffer, vbohandle); glbufferdata(gl_array_buffer, sizeof(glfloat) * vertexcount, vertexdataarray, GL_STATIC_DRAW); GLuint loc = glgetattriblocation(shaderprogram, name); glenablevertexattribarray(loc); glvertexattribpointer(loc, elementspervertex, GL_FLOAT, GL_FALSE, 0, 0);... Each vertex gets their own values for the same attribute variable.

VBO: Element Array Buffer Buffer for indices to map the vertices faces GLuint loc = glgetattriblocation(shaderprogram, name); glenablevertexattribarray(loc); glvertexattribpointer(loc, 3, GL_FLOAT, GL_FALSE, 0, 0); glgenbuffers(1, &vbohandle); glbindbuffer(gl_element_array_buffer, vbohandle); glbufferdata(gl_element_array_buffer, sizeof(glfloat)*indexcount, indices, GL_STATIC_DRAW); glbindbuffer(gl_array_buffer, 0); glbindvertexarray(0);

Example trianglevao positionvbo: [-1, 0, 0, 1, 0, 0, 0, 1, 0] indicesvbo: [0, 1, 2] 2: (0, 1, 0) 0: (-1, 0, 0) 1: (1, 0, 0)

Example squarevao positionvbo: [-1, -1, 0, 1, -1, 0, -1, 1, 0, 1, 1, 0] indicesvbo: [0, 1, 2, 1, 3, 2] 2: (-1, 1, 0) 3: (1, 1, 0) 0: (-1, -1, 0) 1: (1, -1, 0)

Drawing Each frame we tell the GPU to draw std::stack<glm::mat4> ms; ms.push(glm::mat4(1.0)); ms.push(ms.top()); ms.top() = glm::rotate(ms.top(),...); ms.top() = glm::translate(ms.top(),...); GLint loc = glgetuniformlocation(prog, matrixname); gluniformmatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(ms.top())); glbindvertexarray(vaohandle); gldrawelements(gl_triangles, vertexcount, GL_UNSIGNED_BYTE, 0); ms.pop();

The Scene Useful to think of the scene as a tree M RightHand Scene LeftHand M*A... Fingers M*A*B M*A*B M*A Finger1 M*A*B*C1 M*A*B*C3... Finger5 M*A*B*C5

Use of the Matrix Stack More complex geometry for a single object std::stack<glm::mat4> ms; ms.push(glm::mat4(1.0)); MV ms.push(ms.top()); MV*A DrawPalm... (transform top, send matrix, draw) Saved matrices here ms.push(ms.top()); MV*A MV*A*B DrawFingers... MV (transform top, send matrix, draw) MV*A ms.pop(); MV ms.pop();

Old and new OpenGL? It used to be different before OpenGL 3. Everything old still works in compatibility mode. Prior to OpenGL 3 OpenGL 3+ glbegin(...) Vertex Array Object (VAO) glvertex(...) Vertex Buffer Object (VBO) glend(...) gltranslate(...) Use other Matrix library (eg GLM) glrotate(...) Send your matrices to shaders glscale(...) glmaterial(...) Vertex Buffer Object (VBO) Good tutorials: http://antongerdelan.net/opengl/

Now You Know Data (1, 0, 0), (0, 1, 0), (0, 0, 1), this is a triangle, please draw Vertex transformations Vertex shader, P V M v Perspective division, Viewport transformation Culling & Clipping Culling remember the face directions? Clipping some parts are out of view Rasterization BLACK MAGIC Fragment shading Fragment shader Visibility tests & Blending vs

Next time... The graphics pipeline in more detail How to define color for our geometry? Vertex and fragment shaders or Col??