Transformations (Rotations with Quaternions) October 24, 2005

Similar documents
Fundamental Types of Viewing

Computer Graphics. Basic 3D Programming. Contents

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

CS354 Computer Graphics Viewing and Modeling

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

1 Transformations. Chapter 1. Transformations. Department of Computer Science and Engineering 1-1

Computer Viewing Computer Graphics I, Fall 2008

The Viewing Pipeline adaptation of Paul Bunn & Kerryn Hugo s notes

Viewing and Projection

Computer Graphics. Chapter 10 Three-Dimensional Viewing

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

Getting Started. Overview (1): Getting Started (1): Getting Started (2): Getting Started (3): COSC 4431/5331 Computer Graphics.

Viewing and Projection

Describe the Orthographic and Perspective projections. How do we combine together transform matrices?

Geometry: Outline. Projections. Orthographic Perspective

OpenGL Transformations

Transformation Pipeline

Viewing Transformation

1 (Practice 1) Introduction to OpenGL

Precept 2 Aleksey Boyko February 18, 2011

3D Viewing. With acknowledge to: Ed Angel. Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

CITSTUDENTS.IN VIEWING. Computer Graphics and Visualization. Classical and computer viewing. Viewing with a computer. Positioning of the camera

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

3.1 Viewing and Projection

Order of Transformations

CS380: Computer Graphics Viewing Transformation. Sung-Eui Yoon ( 윤성의 ) Course URL:

Computer Graphics. Bing-Yu Chen National Taiwan University

Viewing and Projection

Transformations: 2D Transforms

The View Frustum. Lecture 9 Sections 2.6, 5.1, 5.2. Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2011

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

Interaction Computer Graphics I Lecture 3

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

Overview. Viewing and perspectives. Planar Geometric Projections. Classical Viewing. Classical views Computer viewing Perspective normalization

Computer Graphics. Viewing. Fundamental Types of Viewing. Perspective views. Parallel views. October 12, finite COP (center of projection)

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

Fachhochschule Regensburg, Germany, February 15, 2017

Introduction to OpenGL Transformations, Viewing and Lighting. Ali Bigdelou

Computer graphics MN1

Computer Viewing. CITS3003 Graphics & Animation. E. Angel and D. Shreiner: Interactive Computer Graphics 6E Addison-Wesley

Matrix-Rechnung I ' z =... Universität Frankfurt

Three-Dimensional Graphics III. Guoying Zhao 1 / 67

7. 3D Viewing. Projection: why is projection necessary? CS Dept, Univ of Kentucky

Interaction. CSCI 480 Computer Graphics Lecture 3

CIS 636 Interactive Computer Graphics CIS 736 Computer Graphics Spring 2011

Windows and Viewports. Windows and Viewports. Windows and Viewports. Windows and Viewports. CSC 706 Computer Graphics

Advanced Computer Graphics (CS & SE )

Computer Graphics. Transformations. CSC 470 Computer Graphics 1

MORE OPENGL. Pramook Khungurn CS 4621, Fall 2011

Transformation, Input and Interaction. Hanyang University

Lecture 9 Sections 2.6, 5.1, 5.2. Wed, Sep 16, 2009

Lecture 4 of 41. Lab 1a: OpenGL Basics

Quaternion Rotations AUI Course Denbigh Starkey

CS D Transformation. Junqiao Zhao 赵君峤

COMP Computer Graphics and Image Processing. 5: Viewing 1: The camera. In part 1 of our study of Viewing, we ll look at ˆʹ U ˆ ʹ F ˆʹ S

Wire-Frame 3D Graphics Accelerator IP Core. C Library Specification

OpenGL Tutorial. Ceng 477 Introduction to Computer Graphics

3D computer graphics: geometric modeling of objects in the computer and rendering them

Introduction to OpenGL

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

2D and 3D Viewing Basics

Computer Graphics 7: Viewing in 3-D

CS 4204 Computer Graphics

Visualizing Molecular Dynamics

Computer Graphics (Basic OpenGL)

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

3.2 Hierarchical Modeling

Hidden Surface Removal. 3D Graphics with OpenGL. Back-Face Culling

COMS 4160: Problems on Transformations and OpenGL

CS 4204 Computer Graphics

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

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

Scientific Visualization Basics

CSC 470 Computer Graphics

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

Lecture 4. Viewing, Projection and Viewport Transformations

Jorg s Graphics Lecture Notes Coordinate Spaces 1

Introduction to 3D Graphics with OpenGL. Z-Buffer Hidden Surface Removal. Binghamton University. EngiNet. Thomas J. Watson

Evening s Goals. Mathematical Transformations. Discuss the mathematical transformations that are utilized for computer graphics

Getting Started. 1 st Week, Sun-Jeong Kim. Computer Graphics Applications

Chap 3 Viewing Pipeline Reading: Angel s Interactive Computer Graphics, Sixth ed. Sections 4.1~4.7

Graphics and Visualization

Chapter 3: Modeling Transformation

5.8.3 Oblique Projections

CSCI E-74. Simulation and Gaming

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

Graphics Programming. August 31, Programming of the Sierpinski gasket. Programming with OpenGL and C/C++

Projection: Mapping 3-D to 2-D. Orthographic Projection. The Canonical Camera Configuration. Perspective Projection

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

Today s class. Viewing transformation Menus Mandelbrot set and pixel drawing. Informationsteknologi

UNIT 7 LIGHTING AND SHADING. 1. Explain phong lighting model. Indicate the advantages and disadvantages. (Jun2012) 10M

3D Viewing Episode 2

Outline. Other Graphics Technology. OpenGL Background and History. Platform Specifics. The Drawing Process

Graphics and Computation Introduction to OpenGL

Viewing COMPSCI 464. Image Credits: Encarta and

Reminder: Affine Transformations. Viewing and Projection. Shear Transformations. Transformation Matrices in OpenGL. Specification via Ratios

CSE528 Computer Graphics: Theory, Algorithms, and Applications

C OMPUTER G RAPHICS Thursday

12. Selection. - The objects selected are specified by hit records in the selection buffer. E.R. Bachmann & P.L. McDowell MV 4202 Page 1 of 13

OpenGL. 1 OpenGL OpenGL 1.2 3D. (euske) 1. Client-Server Model OpenGL

Transcription:

Computer Graphics Transformations (Rotations with Quaternions) October 4, 5 Virtual Trackball (/3) Using the mouse position to control rotation about two axes Supporting continuous rotations of objects trackball frame

Virtual Trackball (/3) Rotation with a virtual trackball projection of the trackball position to the plane x + y + z = r y = r x z Virtual Trackball (3/3) Rotation with a virtual trackball (cont ) determination of the orientation of a plane n = p p rotation angle θ = cos Quaternions ( p p ) n θ

Quaternions (/) One real part + three imaginary part q = s + ia+ jb+ kc Properties: i = j = k ij = ji = k jk = kj = i ki = ik = j = Addition and scalar multiplication ( s + s ) + i( a + a ) + j( b + b ) + k( c ) q + + q = c dq = sd + iad + jbd + kcd Quaternions (/) Ordered-pair notation scalar s + vector v = (a, b, c) Addition: q Multiplication q q = s s v v, s v + s v + v v Magnitude q q = + q = s ( s, v) ( s +, v + v ) ( ) = s + v v Inverse ( s, v) qq = q q = (, ) q = q

Quaternions and 3D Rotation For a 3D point (α, β, γ) a unit quaternion q = ( s, a, b, c) q = ( s, a, b, c) q, α, β, γ q =, α, β, γ For q = its conjugate ( ) ( ) Rotating (α, β, γ) by angle θ about the axis parallel to (u x, u y, u z ) ( cosθ, sinθ ( u, u, u ) R q is a 3D rotation about (u x, u y, u z ) by θ R q x y ( p) = q p q z Rotations with Quaternions (/) Rotation about any axis setting up a unit quaternion (u: unit vector) θ θ s = cos, v = usin = ( a, b, c) representing any point position P in quaternion notation (p = (x, y, z)) P =, p ( ) carrying out with the quaternion operation (q - =(s, v)) P = qpq producing the new quaternion P =, p ( ) ( p v) + ( v p) + v ( v p) p = s p + v s

Rotations with Quaternions (/) Obtaining the rotation matrix by quaternion multiplication M R ( θ ) b c = ab+ sc ac sb = R Including the translations x ( θ ) R ( θ ) R ( θ ) R ( θ ) R ( θ ) R x y a y ab sc bc + sa ( θ ) = T M ( θ )T R z c y y ac + sb bc sa a x x b Header File for a Trackball typedef double vec3d[3]; #ifndef M_PI #define M_PI 3.45965 #endif class TrackBall private: int width, height; vec3d lastpos; //quaternions double s; vec3d v; public: double rmat[6]; public: TrackBall(); virtual ~TrackBall(); ; void initialize( void ); void resize( const int &cx, const int &cy ); void project( int &xi, int &yi, vec3d &v ); void start( int xi, int yi ); void end( int xi, int yi );

Vector Operations void normalize( vec3d &v ) double d =. / sqrt( v[]*v[] + v[]*v[] + v[]*v[] ); v[] *= d; v[] *= d; v[] *= d; return; double dot( vec3d &a, vec3d &b ) return (a[]*b[] + a[]*b[] + a[]*b[]); void cross( vec3d &a, vec3d &b, vec3d &c ) c[] = a[]*b[] - a[]*b[]; c[] = a[]*b[] - a[]*b[]; c[] = a[]*b[] - a[]*b[]; return; Projection to a Hemisphere TrackBall::TrackBall() initialize(); TrackBall::~TrackBall() void TrackBall::initialize( void ) s =.; v[] = v[] = v[] =.; for(int i=; i<6; i++) rmat[i] =.; rmat[] = rmat[5] = rmat[] = rmat[5]=.; return; void TrackBall::resize( const int &cx, const int &cy ) width = cx; height = cy; return; void TrackBall::project( int &xi, int &yi, vec3d &v ) v[] = (.*xi - width) / (double)width; v[] = (height -.*yi) / (double)height; double d = sqrt( v[]*v[] + v[]*v[] ); v[] = cos(m_pi *.5 * ((d<.)? d :.) ); normalize( v ); return;

Creation of Quaternions void TrackBall::start( int xi, int yi ) project( xi, yi, lastpos ); return; void TrackBall::end( int xi, int yi ) vec3d currpos, t; project( xi, yi, currpos ); t[] = currpos[] - lastpos[]; t[] = currpos[] - lastpos[]; t[] = currpos[] - lastpos[]; if( t[] t[] t[] ) double angle = M_PI*.5*sqrt( t[]*t[] + t[]*t[] + t[]*t[] ); vec3d axis; cross( currpos, lastpos, axis ); normalize( axis ); double s = sin(angle*.5); vec3d v = s*axis[], s*axis[], s*axis[] ; s = cos(angle*.5); double s = s; vec3d v = v[], v[], v[], v3; cross( v, v, v3 ); s = ( s * s ) - dot( v, v ); v[] = s*v[] + s*v[] + v3[]; v[] = s*v[] + s*v[] + v3[]; v[] = s*v[] + s*v[] + v3[]; Creation of a Rotation Matrix double d =. / sqrt( s*s + v[]*v[] + v[]*v[] + v[]*v[] ); s *= d; v[] *= d; v[] *= d; v[] *= d; // rotation with quaternions // P' = quat * P * quat^- // M = -b^-c^, ab-sc, ac+sb, // ab+sc, -a^-c^, bc-sa, // ac-sb, bc+sa, -a^-b^ ; rmat[] =. -. * (v[]*v[] + v[]*v[]); rmat[] =. * (v[]*v[] - s*v[]); rmat[] =. * (v[]*v[] + s*v[]); //rmat[3] =.; rmat[4] =. * (v[]*v[] + s*v[]); rmat[5] =. -. * (v[]*v[] + v[]*v[]); rmat[6] =. * (v[]*v[] - s*v[]); //rmat[7] =.; rmat[8] =. * (v[]*v[] - s*v[]); rmat[9] =. * (v[]*v[] + s*v[]); rmat[] =. -. * (v[]*v[] + v[]*v[]); //rmat[] =.; //rmat[] = rmat[3] = rmat[4] =.; //rmat[5] =.; lastpos[] = currpos[]; lastpos[] = currpos[]; lastpos[] = currpos[]; return;

Global Variables Initialization & Resize

Mouse Controls DrawScene ( )

Result Virtual Trackball Computer Graphics Viewing October 4, 5

Contents Projections in OpenGL Parallel viewing Orthographic projection Perspective viewing Perspective projection Walk through a scene Look-at function Shadow projection Projections in OpenGL Angle of view only objects that fit within the angle of view of the camera appear in the image View volume being clipped out of scene frustum truncated pyramid

Parallel Viewing Orthographic viewing function glmatrixmode(gl_projection); glloadidentity( ); ); glortho(xmin, xmax, ymin, ymax, near, far); OpenGL provides only this parallel-viewing function near < far!! no restriction on the sign z max = far z min = near Orthographic Projection (/3) Direction of Projection simple camera view frustum a right parallelepiped y x void glortho(gldouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far) z

Orthographic Projection (/3) Orthographic Projection (3/3)

Perspective Viewing (/) Specification of a frustum glmatrixmode(gl_projection); glloadidentity( ); ); glfrustum(xmin, xmax, ymin, ymax, near, far); near, far: positive number!! z max = far z min = near Perspective Viewing (/) Specification using the field of view glmatrixmode(gl_projection); glloadidentity( ); ); gluperspective(fovy, aspect, near, far); fovy: the angle of view in the up (y) direction aspect ratio: width divided by height fov: angle between top and bottom planes

Perspective Projection (/5) General viewing projection plane anywhere in front of the camera void glfrustum(gldouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far) Perspective Projection (/5)

Perspective Projection (3/5) Perspective Projection (4/5)

Perspective Projection (5/5) Walking Though a Scene (/) void keys(unsigned char key, int x, int y) if(key == x ) viewer[] -=.; if(key == X ) viewer[] +=.; if(key == y ) viewer[] -=.; if(key == Y ) viewer[] +=.; if(key == z ) viewer[] -=.; if(key == Z ) viewer[] +=.; void display(void) glenable( GL_DEPTH_TEST ); glclearcolor(.f,.f,.f,.f); glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glmatrixmode(gl_modelview); glloadidentity(); glulookat(viewer[], viewer[], viewer[],,,,,,); glrotatef(theta[],.,.,.); glrotatef(theta[],.,.,.); glrotatef(theta[],.,.,.); colorcube( ); glutswapbuffers( );

Walking Though a Scene (/) void myreshape(int w, int h) glmatrixmode(gl_projection); glloadidentity( ); glviewport(,, w, h); if( w <= h ) glfrustum(-.,., -.*(GLfloat)h/(GLfloat)w,.*(GLfloat)h/(GLfloat)w,.,.); else glfrustum(-. *(GLfloat)w/(GLfloat)h,. *(GLfloat)w/(GLfloat)h, -.,.,.,.); return; Look-At Function OpenGL utility function glulookat(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz); eye-position, target-position, and up-vector look-at positioning

Definition of Eye Position glulookat ( )

Key Controls Walking Though a Scene

Exercise () Create a menu view and its sub menus orthographic, and perspective. If you choose one, your program must change its projection. Projections & Shadows (/) Shadow polygon Steps light source at (x l, y l, z l ) translation (-x l, -y l, -z l ) perspective projection through the origin translation (x l, y l, z l ) = = / l l l l l l l z y x y z y x PT T M

Projections & Shadows (/) glcolor3fv(polygon_color); glbegin(gl_polygon);.. /* draw the polygon normally */. glend( ); GLfloat m[6]; /* shadow projection matrix */ for(i=; i<6; i++) m[i] =.; m[] = m[5] = m[] =.; m[7] = -./yl; glpushmatrix( ); /* save state */ gltranslatef(xl, yl, zl); /* translate back */ glmultmatrixf(m); /* project */ gltranslatef(-xl, -yl, -zl); /* move light to origin */ glcolorfv(shadow_color); glbegin(gl_polygon);.. /* draw the polygon again */. glend( ); glpopmatrix( ); /* restore state */ Global Variables

Shadow Projections Rectangle ( )

What s Wrong? () Exercise () Fix your program

What s Wrong? () Exercise (3) Fix your program more