|
| 1 | +#version 330 |
| 2 | + |
| 3 | +layout(location=0) in vec2 v_position; |
| 4 | +layout(location=1) in vec2 uv; |
| 5 | + |
| 6 | +out vec2 vert_uv; |
| 7 | + |
| 8 | +// camera parameters for transforming the object position |
| 9 | +// and scaling the subtex to the correct size |
| 10 | +layout (std140) uniform camera { |
| 11 | + // view matrix (world to view space) |
| 12 | + mat4 view; |
| 13 | + // projection matrix (view to clip space) |
| 14 | + mat4 proj; |
| 15 | + // inverse zoom factor (1.0 / zoom) |
| 16 | + // high zoom = upscale subtex |
| 17 | + // low zoom = downscale subtex |
| 18 | + float inv_zoom; |
| 19 | + // inverse viewport size (1.0 / viewport size) |
| 20 | + vec2 inv_viewport_size; |
| 21 | +}; |
| 22 | + |
| 23 | +// position of the object in world space |
| 24 | +uniform vec3 obj_world_position; |
| 25 | + |
| 26 | +// parameters for scaling and moving the subtex |
| 27 | +// to the correct position in clip space |
| 28 | + |
| 29 | +// animation scalefactor |
| 30 | +// scales the vertex positions so that they |
| 31 | +// match the subtex dimensions |
| 32 | +// |
| 33 | +// high animation scale = downscale subtex |
| 34 | +// low animation scale = upscale subtex |
| 35 | +uniform float scale; |
| 36 | + |
| 37 | +// size of the subtex (in pixels) |
| 38 | +uniform vec2 subtex_size; |
| 39 | + |
| 40 | +// offset of the subtex anchor point |
| 41 | +// from the subtex center (in pixels) |
| 42 | +// used to move the subtex so that the anchor point |
| 43 | +// is at the object position |
| 44 | +uniform vec2 anchor_offset; |
| 45 | + |
| 46 | +void main() { |
| 47 | + // translate the position of the object from world space to clip space |
| 48 | + // this is the position where we want to draw the subtex in 2D |
| 49 | + vec4 obj_clip_pos = proj * view * vec4(obj_world_position, 1.0); |
| 50 | + |
| 51 | + // subtex has to be scaled to account for the zoom factor |
| 52 | + // and the animation scale factor. essentially this is (animation scale / zoom). |
| 53 | + float zoom_scale = scale * inv_zoom; |
| 54 | + |
| 55 | + // Scale the subtex vertices |
| 56 | + // we have to account for the viewport size to get the correct dimensions |
| 57 | + // and then scale the subtex to the zoom factor to get the correct size |
| 58 | + vec2 vert_scale = zoom_scale * subtex_size * inv_viewport_size; |
| 59 | + |
| 60 | + // Scale the anchor offset with the same method as above |
| 61 | + // to get the correct anchor position in the viewport |
| 62 | + vec2 anchor_scale = zoom_scale * anchor_offset * inv_viewport_size; |
| 63 | + |
| 64 | + // offset the clip position by the offset of the subtex anchor |
| 65 | + // imagine this as pinning the subtex to the object position at the subtex anchor point |
| 66 | + obj_clip_pos += vec4(anchor_scale.x, anchor_scale.y, 0.0, 0.0); |
| 67 | + |
| 68 | + // create a move matrix for positioning the vertices |
| 69 | + // uses the vert scale and the transformed object position in clip space |
| 70 | + mat4 move = mat4(vert_scale.x, 0.0, 0.0, 0.0, |
| 71 | + 0.0, vert_scale.y, 0.0, 0.0, |
| 72 | + 0.0, 0.0, 1.0, 0.0, |
| 73 | + obj_clip_pos.x, obj_clip_pos.y, obj_clip_pos.z, 1.0); |
| 74 | + |
| 75 | + // calculate the final vertex position |
| 76 | + gl_Position = move * vec4(v_position, 0.0, 1.0); |
| 77 | + |
| 78 | + // flip y axis because OpenGL uses bottom-left as its origin |
| 79 | + float uv_x = uv.x; |
| 80 | + float uv_y = 1.0 - uv.y; |
| 81 | + |
| 82 | + vert_uv = vec2(uv_x, uv_y); |
| 83 | +} |
0 commit comments