|
| 1 | +#version 450 |
| 2 | + |
| 3 | +// Get the vertex position from the vertex buffer |
| 4 | +layout (location = 0) in vec3 pos; |
| 5 | +layout (location = 1) in vec2 tex_coord; |
| 6 | + |
| 7 | +// Output texture coordinates to the fragment shader |
| 8 | +layout (location = 0) out vec2 out_tex_coord; |
| 9 | + |
| 10 | +// Output a color to the fragment shader |
| 11 | +layout (location = 1) out vec3 frag_color; |
| 12 | + |
| 13 | +// Uniforms that are pushed via SDL_PushGPUVertexUniformData |
| 14 | +layout(set = 1, binding = 0) uniform PushConstants { |
| 15 | + float rotation; |
| 16 | +}; |
| 17 | + |
| 18 | +// Generates an orthographic projection matrix |
| 19 | +mat4 ortho(float left, float right, float bottom, float top, float near, float far) { |
| 20 | + return mat4( |
| 21 | + 2.0 / (right - left), 0, 0, 0, |
| 22 | + 0, 2.0 / (top - bottom), 0, 0, |
| 23 | + // Note: this is assuming a clip space of [0, 1] on the Z axis, which is what Vulkan uses. |
| 24 | + // In OpenGL, the clip space is [-1, 1] and this would need to be adjusted. |
| 25 | + 0, 0, -1.0 / (far - near), 0, |
| 26 | + -(right + left) / (right - left), -(top + bottom) / (top - bottom), -near / (far - near), 1 |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +// Generates a simple isometric view matrix since the program isn't |
| 31 | +// passing in a uniform view matrix. Without this, we'd just see the |
| 32 | +// front side of the cube and nothing else. |
| 33 | +mat4 isometric_view_matrix() { |
| 34 | + float angleX = radians(35.26); // Tilt |
| 35 | + float angleY = radians(rotation); // Rotate |
| 36 | + |
| 37 | + mat4 rotateX = mat4( |
| 38 | + 1, 0, 0, 0, |
| 39 | + 0, cos(angleX), sin(angleX), 0, |
| 40 | + 0, -sin(angleX), cos(angleX), 0, |
| 41 | + 0, 0, 0, 1 |
| 42 | + ); |
| 43 | + |
| 44 | + mat4 rotateY = mat4( |
| 45 | + cos(angleY), 0, -sin(angleY), 0, |
| 46 | + 0, 1, 0, 0, |
| 47 | + sin(angleY), 0, cos(angleY), 0, |
| 48 | + 0, 0, 0, 1 |
| 49 | + ); |
| 50 | + |
| 51 | + return rotateX * rotateY; |
| 52 | +} |
| 53 | + |
| 54 | +void main(void) { |
| 55 | + // Calculate the final vertex position by multiplying in the projection and view matrices. |
| 56 | + // Ordinarily, these matrices would be passed in as uniforms, but here they're |
| 57 | + // being calculated in-shader to avoid pulling in a matrix multiplication library. |
| 58 | + mat4 proj_matrix = ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); |
| 59 | + mat4 view_matrix = isometric_view_matrix(); |
| 60 | + gl_Position = proj_matrix * view_matrix * vec4(pos, 1.0); |
| 61 | + out_tex_coord = tex_coord; |
| 62 | + |
| 63 | + // Create a frag color based on the vertex position |
| 64 | + frag_color = normalize(pos) * 0.5 + 0.5; |
| 65 | +} |
0 commit comments