220 likes | 254 Views
Enhance your understanding of rotations, trigonometry, and matrices with practical examples and programming insights. Learn about radians, unit circles, rotation matrices, and shader programming efficiency.
E N D
Rotations It seems like a bad sine of things to come
But first... • Any questions on your current homework assignment?
Today • Trigonometry • Representing rotations as matrices • Programming with rotation matrices
Radians • We will be using radians instead of degrees • Radians are better in every way except for humans • 0 radians corresponds to 0° • π/2 corresponds to 90° • π corresponds to 180° • 2π corresponds to 360° • We can convert between them for the sake of user convenience • Let's take a look at converting degrees...
Fahrenheit and Celsius • Converting between Fahrenheit and Celsius provide us with a good example of converting between different number scales • We can derive the conversion formula with the following knowledge: • -40° is the same in °F and °C • Freezing is 0°C and 32°F • Let's look at how we do this...
Converting between degrees and radians • Life is simpler here since our two ranges both have the same zero point • We still can follow the same pattern: • Covert from input range to 0...1 • Convert from 0...1 to output range
Unit circle • Sine, cosine, and tangent tell us useful things about points on a unit circle • For a given angle (measured counterclockwise from the +x axis): • cosine is the x-coordinate of that point on a circle • sine is the y-coordinate of that point on a circle • tangent is the slope of the line
Useful properties • Since all of the points are on the unit circle, we know that the distance from the origin to these points is 1 • Circles are rotationally symmetric, so we can write sine and cosine in terms of each other
Rotating a point around a circle • Suppose we take the point (1, 0) and want to rotate it by an angle θ • What would be the coordinates for this point? • Let's try programming with this...
Rotating a point around a circle • Suppose we take the point (1, 0) and want to rotate it by an angle θ • What would be the coordinates for this point? • Suppose we take the point (0, 1) and want to rotate it by an angle θ • What would be the coordinates for this point?
Rotation matrix • A 2-dimensional rotation matrix is a 2x2 matrix that specifies how to rotate any point a specified amount • Each column in the matrix specifies where you want the given axis to rotate to • Let's see some examples...
2D rotation matrix • This is the standard 2D rotation matrix • It allows us to perform a counterclockwise rotation by a given amount • If we multiply a vector by this matrix, the result will be that vector, rotated • Let's look at this in code...
Animation • In order to make an animation, we need to call our render function pretty often • Most computer displays run at 60Hz (why?) • We can use the requestAnimFrame/requestAnimationFrame function to ask for code to be ran the next time the screen is about to be drawn • Let's use this to make this gasket spin!
Efficiency • Currently, we are sending a new set of points to the graphics card for every single frame • The only thing that changes frame-to-frame is the rotation matrix • We can instead send an updated matrix to the graphics card every frame
GLSL Variable Qualifiers • Our shaders will often need to get data to and from the external world • Attribute: Input to the vertex shader that can be different for each vertex making up the primitive • Varying: Output from vertex shader that is blended across the primitive and given as input to the fragment shader • Uniform: Input to the vertex and/or fragment shader that is constant for the primitive being drawn
Modifying our vertex shader • We need to have our vertex shader take a mat2 as a uniform parameter • We need to multiply by this matrix in our shader, and use this to set the gl_Position • Because of the variable types involved, this will look a little ugly...
Setting uniform values • Setting uniform values is similar to but simpler than setting attributes • gl.getUniformLocation() • gl.uniform[1234][fi][v]() or gl.uniformMatrix[234]fv()
gl.getUnifromLocation(program, variable) • Return the location of the specified uniform variable in the shader specified by program
gl.uniformMatrix[234]fv(loc, transpose, value) • Specify a matrix for the given uniform location • transpose must always be false • The "value" parameter should be the result of calling flatten() • Let's see how this all works...