Skip to content

Instantly share code, notes, and snippets.

@rikusalminen
Last active January 26, 2022 15:24
Show Gist options
  • Save rikusalminen/9393151 to your computer and use it in GitHub Desktop.
Save rikusalminen/9393151 to your computer and use it in GitHub Desktop.
Cube vertex shader - usage: glDrawArrays(GL_TRIANGLES, 0, 36), disable all vertex arrays
#version 420
uniform mat4 projection_matrix;
uniform mat4 model_matrix;
void main()
{
int tri = gl_VertexID / 3;
int idx = gl_VertexID % 3;
int face = tri / 2;
int top = tri % 2;
int dir = face % 3;
int pos = face / 3;
int nz = dir >> 1;
int ny = dir & 1;
int nx = 1 ^ (ny | nz);
vec3 d = vec3(nx, ny, nz);
float flip = 1 - 2 * pos;
vec3 n = flip * d;
vec3 u = -d.yzx;
vec3 v = flip * d.zxy;
float mirror = -1 + 2 * top;
vec3 xyz = n + mirror*(1-2*(idx&1))*u + mirror*(1-2*(idx>>1))*v;
gl_Position = projection_matrix * model_matrix * vec4(xyz, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment