[require] GL >= 4.0 GLSL >= 1.50 [vertex shader] #version 400 #define USE_COLORS 1 layout(location = 0) in vec2 inpos; #if USE_COLORS layout(location = 1) in vec3 incolors; out vec3 v_col; #endif void main() { gl_Position = vec4(inpos, -0.05, 1.0); #if USE_COLORS v_col = incolors; #endif } [geometry shader] #version 400 #extension GL_ARB_gpu_shader5 : enable #define USE_INSTANCING 1 const bool BYPASS_COLOR = false; // Set to true to ignore vertex colour input. layout(points) in; #if USE_INSTANCING const bool INSTANCED = true; layout(invocations = 3) in; layout(triangle_strip, max_vertices = 4) out; #else const bool INSTANCED = false; layout(triangle_strip, max_vertices = 12) out; #endif uniform vec3 campos; uniform mat4 Mproj, Mcam; in vec3 v_col[1]; out vec3 g_col; out vec3 g_wnorm; out vec3 g_wdpos; mat4 rotationMatrix(vec3 axis, float angle) { axis = normalize(axis); float s = sin(angle); float c = cos(angle); float oc = 1.0 - c; return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0, oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0, oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0, 0.0, 0.0, 0.0, 1.0); } void ptemit(vec3 ppos) { mat4 cam = Mcam * rotationMatrix(vec3(1.0, 0.0, 0.0), 0.1) * rotationMatrix(vec3(0.0, 1.0, 0.0), 0.1) * rotationMatrix(vec3(0.0, 0.0, 1.0), 0.1); gl_Position = Mproj * cam * vec4(ppos, 1.0); g_wdpos = ppos - campos; EmitVertex(); } void quademit(vec3 bpos, vec3 norm) { g_wnorm = norm; vec3 hnorm = norm/2.0; ptemit(bpos + hnorm - hnorm.yzx - hnorm.zxy); ptemit(bpos + hnorm + hnorm.yzx - hnorm.zxy); ptemit(bpos + hnorm - hnorm.yzx + hnorm.zxy); ptemit(bpos + hnorm + hnorm.yzx + hnorm.zxy); EndPrimitive(); } void main() { vec3 bpos = gl_in[0].gl_Position.xyz; vec3 bcol; if(BYPASS_COLOR) { bcol = vec3(1.0, 0.0, 1.0); } else { bcol = v_col[0]; } const vec3 nvx = vec3(1.0, 0.0, 0.0) * -0.4; const vec3 nvy = vec3(0.0, 1.0, 0.0) * -0.4; const vec3 nvz = vec3(0.0, 0.0, 1.0) * -0.4; g_col = bcol; if((!INSTANCED) || gl_InvocationID == 0) { quademit(bpos, bpos.x < campos.x ? nvx : -nvx); } if((!INSTANCED) || gl_InvocationID == 1) { quademit(bpos, bpos.y < campos.y ? nvy : -nvy); } if((!INSTANCED) || gl_InvocationID == 2) { quademit(bpos, bpos.z < campos.z ? nvz : -nvz); } } [fragment shader] #version 400 #define USE_COLORS 1 out vec4 color; #if USE_COLORS in vec3 g_col; #endif in vec3 g_wnorm; in vec3 g_wdpos; void main() { #if USE_COLORS float light = max(dot(normalize(g_wnorm), normalize(g_wdpos)), 0); color = vec4(g_col * light, 1.0); #else color = vec4(1.0, 1.0, 1.0, 1.0); #endif } [vertex data] incolors/float/3 inpos/float/2 1.0 1.0 1.0 0.5 0.0 1.0 1.0 1.0 -0.5 0.0 1.0 1.0 1.0 0.0 0.5 1.0 1.0 1.0 0.0 -0.5 [test] uniform mat4 Mproj 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 uniform mat4 Mcam 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 uniform vec4 campos 1.0 1.0 1.0 1.0 clear color 1.0 1.0 1.0 1.0 clear draw arrays GL_POINTS 0 4