affine_transform
A set of functions for coordinate transformations and projections.
2D coordinate transformations
Scale coordinate p by factor s
vec2 scale(vec2 p, float s);
vec2 scale(vec2 p, vec2 s);
Shear coordinate p by vector k
vec2 shear(vec2 p, vec2 k);
Rotate coordinate p by angle a
vec2 rotate(vec2 p, float a);
Translate coordinate p by vector t
vec2 translate(vec2 p, vec2 t);
3D coordinate transformations
Scale coordinate p by factor s
vec3 scale(vec3 p, float s);
vec3 scale(vec3 p, vec3 s);
Rotate coordinate p by angle a around the X, Y, or Z axis.
vec3 rotateX(vec3 p, float a);
vec3 rotateY(vec3 p, float a);
vec3 rotateZ(vec3 p, float a);
Rotate coordinate p by angle a around the axis specified by vector axis.
vec3 rotate(vec3 p, vec3 axis, float a);
Translate coordinate p by vector t
vec2 translate(vec2 p, vec2 t);
Projections
Returns the point in affine coordinate system (vec4). The "camera" lies at (0, 0, 0) looking towards −Z. The near and far arguments specify the minimum and maximum draw distance on the Z axis and must be greater than 0.
Project a 2D coordinate p orthographically onto a frame of size frame
vec4 projectOrthographic(vec2 p, vec2 frame)
Project a 3D coordinate p orthographically onto a frame of size frame
vec4 projectOrthographic(vec3 p, vec2 frame, float near, float far);
Project a 3D coordinate p perspectively onto a frame of size frame at Z = −near
vec4 projectPerspective(vec3 p, vec2 frame, float near, float far);
Project a 3D coordinate p perspectively with a fixed horizontal (hFOV) or vertical (vFOV) field of view angle between 0 and PI. If aspect (width/height) is omitted, the aspect ratio of the viewport is used (shadron_Aspect).
vec4 projectPerspectiveHFOV(vec3 p, float aspect, float hFOV, float near, float far);
vec4 projectPerspectiveVFOV(vec3 p, float aspect, float vFOV, float near, float far);
vec4 projectPerspectiveHFOV(vec3 p, float hFOV, float near, float far);
vec4 projectPerspectiveVFOV(vec3 p, float vFOV, float near, float far);
Example
vec4 vertexShader(int index) {
vec3 coord = computeCoord(index);
coord = rotateY(coord, rotation.x);
coord = rotateX(coord, rotation.y);
coord = translate(coord, vec3(0.0, 0.0, -distance));
return projectPerspectiveHFOV(coord, fieldOfView, 1.0/256.0, 256.0);
}