Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Similar documents
Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Computer Graphics CS 543 Lecture 5 (Part 2) Implementing Transformations

Order of Transformations

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

Objectives. transformation. General Transformations. Affine Transformations. Notation. Pipeline Implementation. Introduce standard transformations

CSC 470 Computer Graphics

CS D Transformation. Junqiao Zhao 赵君峤

Modeling Transform. Chapter 4 Geometric Transformations. Overview. Instancing. Specify transformation for objects 李同益

Introduction to Computer Graphics with WebGL

CS452/552; EE465/505. Transformations

Computer Graphics (4731) Lecture 4: 2D Graphics Systems (Drawing Polylines, tiling, & Aspect Ratio)

Computer Graphics CS 543 Lecture 4 (Part 2) Building 3D Models (Part 2)

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

Interaction. CSCI 480 Computer Graphics Lecture 3

Building Models. 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

Interaction. CSCI 420 Computer Graphics Lecture 3

CS 4204 Computer Graphics

C OMPUTER G RAPHICS Thursday

Interaction Computer Graphics I Lecture 3

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

Recall: Indexing into Cube Map

CS 543 Lecture 1 (Part 3) Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Computer Graphics (CS 543) Lecture 6a: Hierarchical 3D Models. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

CS380: Computer Graphics 2D Imaging and Transformation. Sung-Eui Yoon ( 윤성의 ) Course URL:

Vertex Buffer Objects and Transformations Week 4

Computer Graphics (CS 543) Lecture 2b: 2D Graphics Systems (Drawing Polylines, tiling, & Aspect Ratio)

Modeling Objects by Polygonal Approximations. Linear and Affine Transformations (Maps)

Computer Graphics Geometric Transformations

Computer graphics MN1

Computer Graphics (CS 543) Lecture 1 (Part 2): Introduction to OpenGL/GLUT (Part 1)

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

Books, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, PointClouds, and G3D

Books, OpenGL, GLUT, CUDA, OpenCL, OpenCV, PointClouds, G3D, and Qt

Introduction to Computer Graphics with WebGL

Transformations. Overview. Standard Transformations. David Carr Fundamentals of Computer Graphics Spring 2004 Based on Slides by E.

Transformations II. Arbitrary 3D Rotation. What is its inverse? What is its transpose? Can we constructively elucidate this relationship?

gvirtualxray Tutorial 01: Creating a Window and an OpenGL Context Using GLUT

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

Computer Graphics (CS 543) Lecture 4a: Linear Algebra for Graphics (Points, Scalars, Vectors)

OpenGL/GLUT Intro. Week 1, Fri Jan 12

Precept 2 Aleksey Boyko February 18, 2011

Computer Graphics (CS 4731) OpenGL/GLUT (Part 2)

Computer Graphics (CS 4731) OpenGL/GLUT(Part 1)

Lecture 5: Viewing. CSE Computer Graphics (Fall 2010)

Computer Graphics. Transformations. CSC 470 Computer Graphics 1

Introduction to Computer Graphics. Hardware Acceleration Review

Computer Graphics (CS 4731) & 2D Graphics Systems

Computer Graphics. Bing-Yu Chen National Taiwan University

Review of Thursday. xa + yb xc + yd

CS Computer Graphics: Transformations & The Synthetic Camera

Lecture 4: Transformations and Matrices. CSE Computer Graphics (Fall 2010)

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Spring 2013, CS 112 Programming Assignment 2 Submission Due: April 26, 2013

Recall: Function Calls to Create Transform Matrices

CS452/552; EE465/505. Models & Viewing

Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

To Do. Computer Graphics (Fall 2008) Course Outline. Course Outline. Methodology for Lecture. Demo: Surreal (HW 3)

Using OpenGL with CUDA

Computer Graphics (CS 543) Lecture 8a: Per-Vertex lighting, Shading and Per-Fragment lighting

CS 432 Interactive Computer Graphics

FAKULTI TEKNOLOGI MAKLUMAT DAN KOMUNIKASI BITM INTERACTIVE COMPUTER GRAPHICS LAB SESSION 4. C++ - OpenGL

Objectives. Programming with OpenGL Part 5: More GLSL. Program Object. Reading a Shader. Shader Reader. Linking Shaders with Application

Lecture 3. Understanding of OPenGL programming

Computer Graphics (CS 543) Lecture 8c: Environment Mapping (Reflections and Refractions)

CS 432 Interactive Computer Graphics

Computer graphics MN1

Computer Graphics. Chapter 7 2D Geometric Transformations

CGT520 Transformations

Lecture 4 of 41. Lab 1a: OpenGL Basics

Programming with OpenGL Part 3: Three Dimensions

Transformations. Standard Transformations. David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel

// double buffering and RGB glutinitdisplaymode(glut_double GLUT_RGBA); // your own initializations

Lecture 3 Advanced Computer Graphics (CS & SE )

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

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico


Project Sketchpad. Ivan Sutherland (MIT 1963) established the basic interactive paradigm that characterizes interactive computer graphics:

Introduction to OpenGL

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Introduction to OpenGL

C++ is Fun Part 13 at Turbine/Warner Bros.! Russell Hanson

QUESTION 1 [10] 2 COS340-A October/November 2009

Intro to OpenGL III. Don Fussell Computer Science Department The University of Texas at Austin

Erik Anchondo cse 520 lab 4

Transformation, Input and Interaction. Hanyang University

SOURCES AND URLS BIBLIOGRAPHY AND REFERENCES

Tutorial 04. Harshavardhan Kode. September 14, 2015

UNIT 4 GEOMETRIC OBJECTS AND TRANSFORMATIONS-1

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Computer Graphics (CS 543) Lecture 6a: Viewing & Camera Control. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

OpenGL refresher. Advanced Computer Graphics 2012

The Projection Matrix

CS 432 Interactive Computer Graphics

CS559: Computer Graphics. Lecture 12: OpenGL Li Zhang Spring 2008

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

compatibility mode core mode

Lecture 2 CISC440/640 Spring Department of Computer and Information Science

Transcription:

Computer Graphics (CS 47) Lecture : Implementing Transformations Prof Emmanuel Agu Computer Science Dept. Worcester Polytechnic Institute (WPI)

Objectives Learn how to implement transformations in OpenGL Rotation Translation Scaling Introduce mat.h and vec.h header files for transformations Model-view Projection

Affine Transformations Translate, Scale, Rotate, Shearing, are affine transforms Rigid body transformations: rotation, translation, scaling, shear Line preserving: important in graphics since we can. Transform endpoints of line segments 2. Draw line segment between the transformed endpoints v Vertices v u Straight line Affine Transform Straight line u Transformed vertices

Previously: Transformations in OpenGL Pre. OpenGL had a set of transformation functions gltranslate glrotate( ) glscale( ) Previously, OpenGL would Receive transform commands (gltranslate, glrotate, glscale) Multiply tranform matrices together and maintain transform matrix stack known as modelview matrix

Previously: Modelview Matrix Formed? glmatrixmode(gl_modelview) glloadidentity(); glscale(,2,); gltranslate(,6,4); gltranslate Matrix 2 4 6 2 2 2 Modelview Matrix glscale Matrix Identity Matrix Specify transforms In OpenGL Program (.cpp file) OpenGL multiplies transforms together To form modelview matrix Applies final matrix to vertices of objects OpenGL implementations (glscale, gltranslate, etc) in Hardware (Graphics card)

Previously: OpenGL Matrices OpenGL maintained 4 matrix stacks maintained as part of OpenGL state Model-View (GL_MODELVIEW) Projection (GL_PROJECTION) Texture (GL_TEXTURE) Color(GL_COLOR)

Now: Transformations in OpenGL From OpenGL.: No transform commands (scale, rotate, etc), matrices maintained by OpenGL!! gltranslate, glscale, glrotate, OpenGL modelview matrix all deprecated!! If programmer needs transforms, matrices implement it! Optional: Programmer *may* now choose to maintain transform matrices or NOT!

Current Transformation Matrix (CTM) Conceptually user can implement a 4 x 4 homogeneous coordinate matrix, the Current Transformation Matrix (CTM) The CTM defined and updated in user program Implement in.h Header file Implement transforms Scale, rotate, etc Implement Main.cpp file p vertices Build rotate, scale matrices, put results in CTM C Transform Matrix (CTM) Vertex shader p =Cp p Transformed vertices User space Graphics card

Homegrown CTM Matrices CTM = modelview + projection Model-View (GL_MODELVIEW) Projection (GL_PROJECTION) Texture (GL_TEXTURE) Color(GL_COLOR) CTM Translate, scale, rotate go here Projection goes Here. More later

CTM Functionality LoadIdentity(); Scale(,2,); Translate(,6,4); Translate Matrix 2 4 6 2 2 2 CTM Matrix Scale Matrix Identity Matrix. We need to implement our own transforms (in header file) 2. Multiply our transforms together to form CTM matrix. Apply final matrix to vertices of objects

Implementing Transforms and CTM Where to implement transforms and CTM? We implement CTM in parts. mat.h (Header file) Implementations of translate( ), scale( ), etc 2. Application code (.cpp file) Multiply together translate( ), scale( ) = final CTM matrix. GLSL functions (vertex and fragment shader) Apply final CTM matrix to vertices

Implementing Transforms and CTM We just have to include mat.h (#include mat.h ), use it Uniformity: mat.h syntax resembles GLSL language in shaders Matrix Types: mat4 (4x4 matrix), mat (x matrix). class mat4 { } vec4 _m[4];.. Can declare CTM as mat4 type mat4 ctm = Translate(,6,4); CTM mat.h also has transform functions: Translate, Scale, Rotate, etc. 6 4 Translation Matrix mat4 Translate(const GLfloat x, const GLfloat y, const GLfloat z ) mat4 Scale( const GLfloat x, const GLfloat y, const GLfloat z )

CTM operations The CTM can be altered either by loading a new CTM or by postmutiplication Load identity matrix: C I Load arbitrary matrix: C M Load a translation matrix: C T Load a rotation matrix: C R Load a scaling matrix: C S Postmultiply by an arbitrary matrix: C CM Postmultiply by a translation matrix: C CT Postmultiply by a rotation matrix: C C R Postmultiply by a scaling matrix: C C S

Example: Creating Identity Matrix All transforms (translate, scale, rotate) converted to 4x4 matrix We put 4x4 transform matrix into CTM Example: Create an identity matrix mat4 m = Identity(); mat4 type stores 4x4 matrix Defined in mat.h CTM Matrix

Transformation matrices Formed? mat4 m = Identity(); mat4 t = Translate(,6,4); m = m*t; CTM Matrix 4 6 4 6 Translation Matrix Identity Matrix

Transformation matrices Formed? Consider following code snipet mat4 m = Identity(); mat4 s = Scale(,2,); m = m*s; CTM Matrix 2 2 Scaling Matrix Identity Matrix

Transformation matrices Formed? What of translate, then scale, then. Just multiply them together. Evaluated in reverse order!! E.g: mat4 m = Identity(); mat4 s = Scale(,2,); mat4 t = Translate(,6,4); m = m*s*t; Translate Matrix 2 4 6 2 2 2 Final CTM Matrix Scale Matrix Identity Matrix

How are Transform matrices Applied? mat4 m = Identity(); mat4 s = Scale(,2,); mat4 t = Translate(,6,4); m = m*s*t; colorcube( );. In application: Load object vertices into points[ ] array -> VBO Call gldrawarrays Application code Object Vertices CTM CTM Matrix 2 2 2 2. CTM built in application, passed to vertex shader Vertex shader. In vertex shader: Each vertex of object (cube) is multiplied by CTM to get transformed vertex position 2 2 2 4 4 5 Transformed vertex gl_position = model_view*vposition;

Passing CTM to Vertex Shader Build CTM (modelview) matrix in application program Pass matrix to shader void display( ){... mat4 m = Identity(); mat4 s = Scale(,2,); mat4 t = Translate(,6,4); m = m*s*t; Build CTM in application CTM matrix m in application is same as model_view in shader // find location of matrix variable model_view in shader // then pass matrix to shader } matrix_loc = glgetuniformlocation(program, model_view ); gluniformmatrix4fv(matrix_loc,, GL_TRUE, m);...

Implementation: Vertex Shader On gldrawarrays( ), vertex shader invoked with different vposition per shader E.g. If colorcube( ) generates 8 vertices, each vertex shader receives a vertex stored in vposition Shader calculates modified vertex position, stored in gl_position in vec4 vposition; uniform mat4 model_view; void main( ) { } vposition gl_position = model_view*vposition; Transformed vertex position Contains CTM p =Cp p p Original vertex position Vertex Shader gl_position

What Really Happens to Vertex Position Attributes? Image credit: Arcsynthesis tutorials

What About Multiple Vertex Attributes? Image credit: Arcsynthesis tutorials

Transformation matrices Formed? Example: Vertex (,, ) is one of 8 vertices of cube mat4 m = Identity(); mat4 s = Scale(,2,); m = m*s; colorcube( ); CTM (m) Each vertex of cube is multiplied by modelview matrix to get scaled vertex position * 2 Original vertex Transformed vertex 2 In application In vertex shader p p

Transformation matrices Formed? Another example: Vertex (,, ) is one of 8 vertices of cube In application mat4 m = Identity(); mat4 s = Scale(,2,); mat4 t = Translate(,6,4); m = m*s*t; colorcube( ); In vertex shader 2 2 2 CTM Matrix 4 4 5 Original vertex Transformed vertex Each vertex of cube is multiplied by modelview matrix to get scaled vertex position

Arbitrary Matrices Can multiply by matrices from transformation commands (Translate, Rotate, Scale) into CTM Can also load arbitrary 4x4 matrices into CTM Load into CTM Matrix 4 2 24 5 2 2

Example: Rotation about a Fixed Point We want C = T R T Be careful with order. Do operations in following order C I C CT C CR C CT - Each operation corresponds to one function call in the program. Note: last operation specified is first executed

Matrix Stacks CTM is actually not just matrix but a matrix STACK Multiple matrices in stack, current matrix at top Can save transformation matrices for use later (push, pop) E.g: Traversing hierarchical data structures (Ch. 8) Pre. OpenGL also maintained matrix stacks Right now just implement -level CTM Matrix stack later for hierarchical transforms

Reading Back State Can also access OpenGL variables (and other parts of the state) by query functions glgetintegerv glgetfloatv glgetbooleanv glgetdoublev glisenabled Example: to find out max. number texture units on GPU glgetintegerv(gl_max_texture_units, &MaxTextureUnits);

Using Transformations Example: use idle function to rotate a cube and mouse function to change direction of rotation Start with program that draws cube as before Centered at origin Sides aligned with axes

Recall: main.c void main(int argc, char **argv) { glutinit(&argc, argv); glutinitdisplaymode(glut_double GLUT_RGB GLUT_DEPTH); glutinitwindowsize(5, 5); glutcreatewindow("colorcube"); glutreshapefunc(myreshape); } glutdisplayfunc(display); glutidlefunc(spincube); glutmousefunc(mouse); glenable(gl_depth_test); glutmainloop(); Calls spincube continuously Whenever OpenGL program is idle

Recall: Idle and Mouse callbacks void spincube() { theta[axis] += 2.; if( theta[axis] > 6. ) theta[axis] -= 6.; glutpostredisplay(); } void mouse(int button, int state, int x, int y) { if(button==glut_left_button && state == GLUT_DOWN) axis = ; if(button==glut_middle_button && state == GLUT_DOWN) axis = ; if(button==glut_right_button && state == GLUT_DOWN) axis = 2; }

Display callback void display() Update CTM Matrix { glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); ctm = RotateX(theta[])*RotateY(theta[]) *RotateZ(theta[2]); gluniformmatrix4fv(matrix_loc,,gl_true,ctm); gldrawarrays(gl_triangles,, N); glutswapbuffers(); } Pass CTM to vertex shader Alternatively, we can send rotation angle + axis to vertex shader, Let shader form CTM then do rotation Inefficient: if mesh has, vertices each one forms CTM, redundant!!!!

Using the Model-view Matrix In OpenGL the model-view matrix used to Transform D models (translate, scale, rotate) Position camera (using LookAt function) (next) The projection matrix used to define view volume and select a camera lens (later) Although these matrices no longer part of OpenGL, good to create them in our applications (as CTM)

References Angel and Shreiner, Interactive Computer Graphics (6 th edition), Chapter