Vertex shader reminder Vertex shader transforms all vertices

  • Slides: 10
Download presentation
Vertex shader reminder Vertex shader transforms all vertices of an object from the model's

Vertex shader reminder Vertex shader transforms all vertices of an object from the model's coordinate system to the screen's Model CS Modeling TR World CS View TR View/Camera CS Projective TR This is done in 3 steps (see figure) All are 4× 4 matrices which all points are multiplied with Vertex shader is working with homogenous (4 D) coordinates (x, y, z)->(x, y, z, 1)

Example: rotation matrix In case of a translation, 3× 3 matrices are not enough.

Example: rotation matrix In case of a translation, 3× 3 matrices are not enough. To have a general representation, all transforms are represented with 4× 4 matrices

Reminder: Uniform variables How does the shader know about the transformation Model CS Modeling

Reminder: Uniform variables How does the shader know about the transformation Model CS Modeling TR World CS View TR View/Camera CS Projective TR Uniform variable: Variable in the shader that can be set from the program running on the CPU. Matrices will be set from C++ in uniform variables. They are able to change with time (for animations)

Setting uniform variables In case of matrices glm: : mat 4 matrix = glm:

Setting uniform variables In case of matrices glm: : mat 4 matrix = glm: : mat 4(1. 0 f); gl. Uniform. Matrix 4 fv(id, 1, GL_FALSE, &( matrix[0][0])); This copies the matrix into the GPU's memory Other functions for uniform variables

Creating matrices in Open. GL Of course there are functions to generate matrices from

Creating matrices in Open. GL Of course there are functions to generate matrices from key parameters Model transformations: a bit later. . . View transformation: Parameters ● Camera location ● Which point it's directed to ● Upwards direction m_mat. View = glm: : look. At(glm: : vec 3(0, 0, 5), glm: : vec 3(0, 0, 0), glm: : vec 3(0, 1, 0));

Creating matrices in Open. GL Projective transformation: m_mat. Proj = glm: : perspective(45. 0

Creating matrices in Open. GL Projective transformation: m_mat. Proj = glm: : perspective(45. 0 f, 640/480. 0 f, 1000. 0 f); Parameters: ● ● View angle Aspect ratio Near clipping distance Far clipping distance

World coordinate system We would like to place and orient our objects in the

World coordinate system We would like to place and orient our objects in the 3 D world The 3 basic transformations we can use glm: : rotate<float>( angle, glm: : vec 3(x, y, z) ) glm: : translate<float>( glm: : vec 3(x, y, z) ) glm: : scale<float>( glm: : vec 3(x, y, z) )

World coordinate system Right-hand coordinate system

World coordinate system Right-hand coordinate system

Multiple transformations M 1, M 2, M 3: transformations in this order. Applying them

Multiple transformations M 1, M 2, M 3: transformations in this order. Applying them is equivalent to M 3*M 2*M 1 (as column vectors are coming from the right, i. e. (M 3*M 2*M 1)*p = M 3*(M 2*(M 1*p)) For example if we want to first translate then rotate, we should give matrix as rotate*translate

Multiple drawing of same object If we want to draw an object twice. .

Multiple drawing of same object If we want to draw an object twice. . . We could put the vertices twice into the VBO then draw them. Now we can put just one object into the VBO, then draw it multiple times: first with first transformation, then with the second, etc.