CGT520 Transformations - HPCG...

13
CGT520 Transformations Bedrich Benes, Ph.D. Purdue University Department of Computer Graphics © Bedrich Benes GLM A concise library for transforms and OpenGL mathematics All overloaded, predefined, optimized glm (OpenGL Mathematics) Download: http://glm.g-truc.net/ It is header only It is GLSL friendly © Bedrich Benes GLM Suggestions: Do not use namespace glm causes a lot of clashes use glm::func instead Use precompiled headers, it uses a lot of templates and can become slow to compile © Bedrich Benes GLM - Example #include <glm/glm.hpp> #include <glm/gtc/type_ptr.hpp> #include <glm/gtc/matrix_transform.hpp> glm::vec4 TrMul(glm::vec4 p, glm::mat4 m) { const glm::vec4 t=glm::vec4(1.0); return glm::vec4(m*p+t); }

Transcript of CGT520 Transformations - HPCG...

CGT520 TransformationsBedrich Benes, Ph.D.Purdue UniversityDepartment of Computer Graphics

© Bedrich Benes

GLM• A concise library for transforms and

OpenGL mathematics• All overloaded, predefined, optimized• glm (OpenGL Mathematics)• Download: http://glm.g-truc.net/• It is header only• It is GLSL friendly

© Bedrich Benes

GLM• Suggestions:

• Do not use namespace glmcauses a lot of clashesuse glm::func instead

• Use precompiled headers,it uses a lot of templates and can become slow to compile

© Bedrich Benes

GLM - Example#include <glm/glm.hpp>#include <glm/gtc/type_ptr.hpp>#include <glm/gtc/matrix_transform.hpp>

glm::vec4 TrMul(glm::vec4 p, glm::mat4 m){const glm::vec4 t=glm::vec4(1.0);return glm::vec4(m*p+t);}

© Bedrich Benes

GLM

• we will see more during this lecture…

© Bedrich Benes

OpenGL transformations• Everything is done via shaders

© Bedrich Benes

OpenGL transformations• Represented using matrices• The matrix is multiplied from the right

m11 m12 m13 m14m21 m22 m23 m24m31 m32 m33 m34m41 m42 m43 m44

© Bedrich Benes

OpenGL transformations• Each stage of the transformation pipeline

can be represented by a different matrix• They can be stored into a premultiplied

matrix as a singleModelViewProjection

• or separated into three matricesModeling, Viewing, and Projection

© Bedrich Benes

OpenGL transformations• The vertex shader must perform a vertex

transformation using the fixed order

Transforms are defined in the same order

© Bedrich Benes

GLSL Vertex Shaderin vec4 iPosition;uniform mat4 model;uniform mat4 view;uniform mat4 proj;void main(){

gl_Position=proj*view*model*iPosition;}

© Bedrich Benes

GLSL Vertex Shaderin vec4 iPosition;uniform mat4 model;uniform mat4 view;uniform mat4 proj;void main(){

gl_Position=proj*view*model*iPosition;}

© Bedrich Benes

Modeling Transforms• Affect the objects in the world space• Most commonly it is a linear transform

We do not need to transform each element, but only the result.

© Bedrich Benes

Modeling Transforms• Translate

• Scale

• Rotate around an arbitrary axis

© Bedrich Benes

Translate• Translates by a vector

= 1 0 0 xT0 1 0 yT0 0 1 zT0 0 0 1

© Bedrich Benes

Scale• Scales by a vector

= xs 0 0 00 ys 0 00 0 zs 00 0 0 1

© Bedrich Benes

Rotation• Rotates by an angle

around an axis

Source Wikipedia

© Bedrich Benes

GLM - implementationmat4 translate(mat4, vec3)mat4 scale (mat4, vec3)mat4 rotate (mat4, , vec3)

If the mat4 is provided as the first argument, the result is multiplied and returned

If mat4(1.0) is provided, it returns the transform matrix itself

© Bedrich Benes

GLM - implementationSo this is usually much faster m=glm::translate(m,glm::vec3(1,1,1));m=glm::rotate (m,a,glm::vec3(0,0,1));m=glm::translate(m,glm::vec3(1,1,1));than thisconst glm::mat4 i(1.0); //identity matrixm=m*glm::translate(i,glm::vec3(1,1,1));m=m*glm::rotate (i,a,glm::vec3(0,0,1));m=m*glm::translate(i,glm::vec3(1,1,1));

© Bedrich Benes

GLM – optimization-Premultiply• In the previous example we did:

• But we can do

© Bedrich Benes

Modeling transformsthe order of transformation is critical!

m=glm::mat4(1.0);Display();m=rotatef(m,45,0,0,1);m=translate(m,1,0,0);Display();

© Bedrich Benes

Modeling transformsthe order of transformation is critical!

m=glm::mat4(1.0);Display();m=translate(m,1,0,0);m=rotatef(m,45,0,0,1);Display();

© Bedrich Benes

Modeling transformsthe order of transformation is criticalbecause matrix multiplication is not commutative

C’=RT C’’=TR

C’ C’’

© Bedrich Benes

Modeling TransformsThe first approach: the grand fixed coordinate system

we must read the transform in the inverse order

The second approach: local coordinate system tied to object

operations are applied to the local coordinate systemoperations occur in natural order

© Bedrich Benes

Example1: m=glm::mat4(1.0)2: m=translatef(m,1,0,0); 3: m=rotatef(m,45,0,0,1);4: DrawObject();

© Bedrich Benes

Modeling Transformthe second approach is more useful for modeling a sets of joints (robot arms) forward kinematics

the first approach is problematic in cases of scaling(non-uniform ~ axes may be non perpendicular)

© Bedrich Benes

Modeling Transform - example

© Bedrich Benes

Modeling Transform - example

27

scale

translate

© Bedrich Benes

Modeling Transform - example//shapes[0] and shapes[1] are two cubes//the lower box firstglm::mat4 m=glm::mat4(1.0); //resetm=glm::rotate(m,ftime,glm::vec3(0,0,1));m=glm::translate(m,glm::vec3(0,0.5,0));m=glm::scale(m,glm::vec3(0.1,1,0.1));//set the matrix in the shaderglUniformMatrix4fv(mP,1,GL_FALSE,

glm::value_ptr(m));shapes[0]->Render();

© Bedrich Benes

Modeling Transform - example//shapes[0] and shapes[1] are two cubes//the upper box thenm=glm::scale(m,glm::vec3(10.f,1.0f,10.f));m=glm::translate(m,glm::vec3(0,0.5,0));m=glm::rotate(m,2*ftime,glm::vec3(0,0,1));m=glm::translate(m,glm::vec3(0,0.5,0));m=glm::scale(m,glm::vec3(0.1f,1.0f,0.1f));//set the matrix in the shaderglUniformMatrix4fv(mP,1,GL_FALSE,

glm::value_ptr(m));shapes[1]->Render();

© Bedrich Benes

GLSL Vertex Shaderin vec4 iPosition;uniform mat4 model;uniform mat4 view;uniform mat4 proj;void main(){

gl_Position=proj*view*model*iPosition;}

© Bedrich Benes

GLSL Vertex Shaderin vec4 iPosition;uniform mat4 model;uniform mat4 view;uniform mat4 proj;void main(){

gl_Position=proj*view*model*iPosition;}

© Bedrich Benes

Viewing Transformviewing transformationis rotating, translating, and scaling the cameraviewing transformation should be issued before anymodeling transformation in a program

viewing transformation is issued first

modeling transformation is discussed firstviewing transformation are issued first

i.e., first compose a scene then place a camera

© Bedrich Benes

lookAtlookAt(vec3 eye, vec3 target, vec3 upVector) invalid if ||

© Bedrich Benes

Viewing Example• Camera flying above a sceneview=glm::lookAt(vec3(r*sin(ftime),5,r*cos(ftime)),//eyevec3(0,0,0), //destinationvec3(0,1,0)); //up

//send it to the shaderglUniformMatrix4fv(viewParameter,1,

GL_FALSE,glm::value_ptr(view));

© Bedrich Benes

GLSL Vertex Shaderin vec4 iPosition;uniform mat4 model;uniform mat4 view;uniform mat4 proj;void main(){

gl_Position=proj*view*model*iPosition;}

© Bedrich Benes

GLSL Vertex Shaderin vec4 iPosition;uniform mat4 model;uniform mat4 view;uniform mat4 proj;void main(){

gl_Position=proj*view*model*iPosition;}

© Bedrich Benes

Orthographic (Parallel) Projectionmat4::ortho(left, right,

bottom, top,near, far)

© Bedrich Benes

Orthographic (Parallel) Projection

It is in fact translation followed by scaling:

Source Wikipedia

Source Wikipedia

© Bedrich Benes

Orthographic (Parallel) ProjectionOrthographic projection comments

• camera can be inside the volume ortho(-1,1,-1,1,-1,1)then also the object behind the camera are displayed

• camera can be entirelly shiftedortho(-12,-10,-1,1,-1,1)it’s like using a periscope

© Bedrich Benes

Perspective Projectionmat4::frustum(left, right,

bottom, top,near, far)

© Bedrich Benes

Perspective Projection• Matrix

© Bedrich Benes

Perspective ProjectionFrustum • is not intuitive• can be asymmetric

© Bedrich Benes

Perspective Projectionmat4::perspective(fovy, aspect,

near, far)

© Bedrich Benes

Perspective ProjectiongluPerspective• fovy is field of view in the y axis• aspect is aspect ratio of the actual viewport• it cannot be asymmetric (sometimes desired)

fovy: 60 90 120

© Bedrich Benes

Perspective ProjectionperspectiveCould we set fovy from distance and size of an object?GLdouble FOVY(GLdouble size,GLdouble dist){GLdouble theta;theta = 2.0*atan2(size/2.0, dist);return(180.0*theta/);}

© Bedrich Benes

Perspective ProjectionHow to get image in double the maximal resolution?1) Draw it four times setting the projections as depicted2) Save the images3) Glue them in Photoshop

x=[-1, 0]y=[ 0, 1]

x=[-1, 0]y=[-1, 0]

x=[ 0, 1]y=[ 0, 1]

x=[ 0, 1]y=[-1, 0]

© Bedrich Benes

ViewportViewport is the part of the window that is used for renderingvoid glViewport(GLint x,GLint y,

GLsizei w, GLsizei h)

- x and y specifies the lower left corner- w and h are size of the viewport rectangle

windowviewport

x

y w

h

© Bedrich Benes

ViewportViewport is usually set in the reshape callbackvoid Resize(int w, int h){glViewport(0,0,w,h);

…}int main(int argc, char **argv){ glutInit(&argc, argv);

… glutDisplayFunc(Display);glutReshapeFunc(Resize);

}

© Bedrich Benes

ViewportExample - rendering into two viewports

int x = 600, y = 300;gluPerspective(60.0, x/(2.0*y), 1, 10.0);//!!!glViewport(0,0,x/2,y);RenderLeft();glViewport(x/2,0,x/2,y);RenderRight()

© Bedrich Benes

Reading• GLSL Specification

http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.clean.pdf

• OpenGL Mathematics (GLM)http://glm.g-truc.net/

© Bedrich Benes

Reading• Edward Engel

Interactive Computer Graphics: A Top-Down Approach with Shader-Based OpenGL (6th Edition) Chapters 3 and 4

• David Wolff, OpenGL 4.0 Shading Language Cookbook, PACKT Publishing 2011