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

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

Order of Transformations

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

CSC 470 Computer Graphics

CS D Transformation. Junqiao Zhao 赵君峤

Introduction to Computer Graphics with WebGL

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

CS452/552; EE465/505. Transformations

CS 4204 Computer Graphics

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

Introduction to Computer Graphics with WebGL

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

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

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

C OMPUTER G RAPHICS Thursday

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

Interaction. CSCI 480 Computer Graphics Lecture 3

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

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

Interaction Computer Graphics I Lecture 3

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

Vertex Buffer Objects and Transformations Week 4

Computer Graphics. Chapter 7 2D Geometric Transformations

Recall: Indexing into Cube Map

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

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

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

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

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

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

Computer Graphics Geometric Transformations

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

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

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

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

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

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

Computer graphics MN1

Computer graphics MN1

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

CS Computer Graphics: Transformations & The Synthetic Camera

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

CS452/552; EE465/505. Models & Viewing

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

Transformation, Input and Interaction. Hanyang University

OpenGL/GLUT Intro. Week 1, Fri Jan 12

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

Computer Graphics. Bing-Yu Chen National Taiwan University

Introduction to Computer Graphics. Hardware Acceleration Review

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

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

CGT520 Transformations

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

The Projection Matrix

Recall: Function Calls to Create Transform Matrices

CS 432 Interactive Computer Graphics

Programming with OpenGL Part 3: Three Dimensions

Computer Graphics (CS 4731) & 2D Graphics Systems

Affine Transformations in 3D

Computer Graphics. Transformations. CSC 470 Computer Graphics 1

Transformations. CSCI 420 Computer Graphics Lecture 4

Precept 2 Aleksey Boyko February 18, 2011

Review of Thursday. xa + yb xc + yd

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

Lecture 3 Advanced Computer Graphics (CS & SE )

Erik Anchondo cse 520 lab 4

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

Lecture 3. Understanding of OPenGL programming

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

GL_MODELVIEW transformation

CS 432 Interactive Computer Graphics

GRAFIKA KOMPUTER. ~ M. Ali Fauzi

Using OpenGL with CUDA

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

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

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

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

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

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

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

Transformations. CSCI 420 Computer Graphics Lecture 5


Transformations. OpenGL Transformations. 4x4 Model-view Matrix (this lecture) OpenGL Transformation Matrices. 4x4 Projection Matrix (next lecture)

SOURCES AND URLS BIBLIOGRAPHY AND REFERENCES

蔡侑庭 (Yu-Ting Tsai) National Chiao Tung University, Taiwan. Prof. Wen-Chieh Lin s CG Slides OpenGL 2.1 Specification

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

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

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

Comp 410/510 Computer Graphics Spring Input & Interaction

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

UNIT 4 GEOMETRIC OBJECTS AND TRANSFORMATIONS-1

Lecture 4 of 41. Lab 1a: OpenGL Basics

Reading. Hierarchical Modeling. Symbols and instances. Required: Angel, sections , 9.8. Optional:

compatibility mode core mode

Fall CSCI 420: Computer Graphics. 2.2 Transformations. Hao Li.

2a. The triangles scale increases (expands) when the 'e' key is pressed and decreases (contracts) when the 'c' key is pressed.

Introduction to OpenGL

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 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 Straight line v Vertices v Affine Transform u 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 (Translate, Rotate, Scale) 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 OpenGL multiplies transforms together To form modelview matrix Applies final matrix to vertices of objects

Previously: OpenGL Matrices OpenGL maintained 4 matrices as part of the 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 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) Implement transform commands (rotate, scale, translate) Form transform matrices, multiply together to form CTM CTM applied to vertices of objects The CTM defined and updated in user program vertices p C Transform matrix CTM p =Cp Transformed vertices

CTM in OpenGL Previously, OpenGL had model view and projection matrix in the pipeline that we can concatenate together to form CTM Essentially, emulate these two matrices using CTM Translate, scale, rotate go here Projection goes Here. More later

CTM Functionality glmatrixmode(gl_modelview) glloadidentity(); glscale(,2,); gltranslate(,6,4); gltranslate Matrix 2 4 6 2 2 2 Modelview Matrix glscale Matrix Identity Matrix. We need to implement our own transforms 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) 2. Application code (.cpp file). GLSL functions (vertex and fragment shader)

Implementing Transforms and CTM After including mat.h, we can declare mat4 type for CTM class mat4 { vec4 _m[4];.. } Transforms: Translate, Scale, RotateX (x roll), etc. E.g. mat4 Translate(const GLfloat x, const GLfloat y, const GLfloat z ) mat4 Scale( const GLfloat x, const GLfloat y, const GLfloat z ) We just have to include mat.h (#include mat.h ), use it

Implementing Transforms and CTM mat.h (Header files) implements Matrix Types: mat4 (4x4 matrix), mat (x matrix). E.g mat4 ctm = Translate(,6,4); Note: mat.h is home grown (by text) Allows easy matrix creation manipulation Uniformity: Syntax of code resembles GLSL language used in shaders

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 CR Postmultiply by a scaling matrix: C CS

Example: Rotation, Translation, Scaling Create an identity matrix: mat4 m = Identity(); Multiply on right by rotation matrix of theta in degrees where (vx, vy, vz) define axis of rotation mat4 r = Rotate(theta, vx, vy, vz) m = m*r; Do same with translation and scaling: mat4 s = Scale( sx, sy, sz) mat4 t = Transalate(dx, dy, dz); m = m*s*t;

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

Example Rotation about z axis by degrees about a fixed point (., 2.,.) mat 4 m = Identity(); m = Translate(., 2.,.)* Rotate(.,.,.,.)* Translate(-., -2., -.); Remember last matrix specified in program (i.e. translate matrix in example) is first applied

Transformation matrices Formed? Converts all transforms (translate, scale, rotate) to 4x4 matrix We put 4x4 transform matrix into CTM Example 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 gltranslate, 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

Transformation matrices Formed? 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 CTM Object Vertices 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

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 model_view is name of CTM 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; vposition void main( ) { gl_position = model_view*vposition; } Transformed vertex position Contains CTM Original vertex position Vertex Shader gl_position

Transformation matrices Formed? Example: Vertex (,, ) is one of 8 vertices of cube mat4 m = Identity(); mat4 s = Scale(,2,); m = m*s; colorcube( ); CTM Matrix 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

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

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

Matrix Stacks Sometimes want to save transformation matrices for use later E.g: Traversing hierarchical data structures (Ch. 8) Pre. OpenGL 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 maximum number of texture units 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

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

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() { glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); gluniform( ); //or gluniformmatrix gldrawarrays( ); glutswapbuffers(); } Alternatively, we can send rotation angle and axis to vertex shader, Let shader form CTM then do rotation Inefficient to apply vertex transform data in application (CPU) and send data to GPU to render

Using the Model view Matrix In OpenGL the model view matrix used to Transform D models 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)

D? Interfaces Major interactive graphics problem: how to use 2D devices (e.g. mouse) to control D objects Some alternatives Virtual trackball D input devices such as the spaceball Use areas of the screen Distance from center controls angle, position, scale depending on mouse button depressed

GLUI User Interface Library by Paul Rademacher Provides sophisticated controls and menus Not used in this class/optional Virtual trackball

References Angel and Shreiner, Chapter Hill and Kelley, appendix 4