Skip to content

Instantly share code, notes, and snippets.

@ds84182
Last active September 29, 2015 20:35
Show Gist options
  • Save ds84182/10ab40964a092ccc44a5 to your computer and use it in GitHub Desktop.
Save ds84182/10ab40964a092ccc44a5 to your computer and use it in GitHub Desktop.
Drawing functions for GLSL in GLSL Sandbox
#ifdef GL_ES
precision mediump float;
#endif
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
//draw.glsl: Drawing functions for GLSL
#define SET_COLOR4(COLOR) gl_FragColor = COLOR
#define SET_COLOR3(COLOR) gl_FragColor = vec4(COLOR, 1.0)
#define EQUALS(X, Y) (X > Y-0.001 && X < Y+0.001)
#define BLEND(A, B) mix(A, B, A.a*B.a)
float aspect;
vec2 uvUnit;
vec2 position;
mat4 projection;
void draw_point(vec2 pposition, vec4 color) {
if (EQUALS(pposition.x, position.x) && EQUALS(pposition.y, position.y)) {
SET_COLOR3(BLEND(gl_FragColor, color).xyz);
}
}
void draw_circle(vec2 cposition, float radius, vec4 color) {
vec2 np = pow(position-cposition, vec2(2.0));
float dist = sqrt(np.x+np.y);
if (dist < radius) {
SET_COLOR3(BLEND(gl_FragColor, color).xyz);
}
}
void draw_rectangle(vec2 rposition, vec2 size, vec4 color) {
if (position.x > rposition.x && position.y > rposition.y && position.x < rposition.x+size.x && position.y < rposition.y+size.y) {
SET_COLOR3(BLEND(gl_FragColor, color).xyz);
}
}
void draw_triangle(vec2 a, vec2 b, vec2 c, vec4 color) {
vec2 v0 = c-a;
vec2 v1 = b-a;
vec2 v2 = position-a;
float dot00 = dot(v0, v0);
float dot01 = dot(v0, v1);
float dot02 = dot(v0, v2);
float dot11 = dot(v1, v1);
float dot12 = dot(v1, v2);
float invDenom = 1.0 / (dot00*dot11-dot01*dot01);
float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
if (u >= 0.0 && v >= 0.0 && u+v < 1.0) {
SET_COLOR3(BLEND(gl_FragColor, color).xyz);
}
}
mat4 guPerspective(float fovy,float aspect,float n,float f) {
float cot,angle,tmp;
mat4 mt;
angle = fovy*0.5;
angle = radians(angle);
cot = 1.0/tan(angle);
mt[0][0] = cot/aspect;
mt[0][1] = 0.0;
mt[0][2] = 0.0;
mt[0][3] = 0.0;
mt[1][0] = 0.0;
mt[1][1] = cot;
mt[1][2] = 0.0;
mt[1][3] = 0.0;
tmp = 1.0/(f-n);
mt[2][0] = 0.0;
mt[2][1] = 0.0;
mt[2][2] = -(n)*tmp;
mt[2][3] = -(f*n)*tmp;
mt[3][0] = 0.0;
mt[3][1] = 0.0;
mt[3][2] = -1.0;
mt[3][3] = 0.0;
return mt;
}
mat4 makeRotationX(float theta) {
float c = cos(theta);
float s = sin(theta);
return mat4(1, 0, 0, 0,
0, c, - s, 0,
0, s, c, 0,
0, 0, 0, 1);
}
mat4 makeRotationY(float theta) {
float c = cos(theta);
float s = sin(theta);
return mat4(c, 0, s, 0,
0, 1, 0, 0,
- s, 0, c, 0,
0, 0, 0, 1);
}
mat4 makeRotationZ(float theta) {
float c = cos(theta);
float s = sin(theta);
return mat4(c, - s, 0, 0,
s, c, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
}
mat4 makeTranslation(float x, float y, float z) {
return mat4(1, 0, 0, x,
0, 1, 0, y,
0, 0, 1, z,
0, 0, 0, 1);
}
void draw_triangle3d(vec3 a, vec3 b, vec3 c, vec4 color) {
draw_triangle((projection*vec4(a,1.0)).xy, (projection*vec4(b,1.0)).xy, (projection*vec4(c,1.0)).xy, color);
}
#define X 0.25
#define Y -0.25
#define CLR vec4(1.0,1.0,1.0,position.x)
void main( void ) {
aspect = resolution.x/resolution.y;
uvUnit = 1.0 / resolution.xy;
vec2 uv = ( gl_FragCoord.xy / resolution.xy );
position = (uv-0.5);
position.x *= aspect;
position += 0.5;
projection = guPerspective(75.0, aspect, 0.0, 10.0)*makeRotationX(time)*makeRotationY(time)*makeRotationZ(time);
gl_FragColor = vec4( position, 0.0, 1.0 );
position -= 0.5;
/*draw_point(vec2(0.5, 0.5), vec4(vec3(1.0),0.5));
draw_circle(vec2(0.5, 0.5), 0.2, vec4(vec3(1.0),0.5));
draw_rectangle(vec2(0.25,0.25),vec2(0.5),vec4(vec3(1.0),0.5));
draw_triangle(vec2(0.25,0.25), vec2(0.75,0.25), vec2(0.5, 0.75), vec4(vec3(1.0),0.5));*/
//front
draw_triangle3d(vec3(Y,Y,Y),vec3(Y,X,Y),vec3(X,X,Y),CLR);
draw_triangle3d(vec3(X,X,Y),vec3(X,Y,Y),vec3(Y,Y,Y),CLR);
//bottom
draw_triangle3d(vec3(Y,Y,Y),vec3(Y,Y,X),vec3(X,Y,X),CLR);
draw_triangle3d(vec3(X,Y,X),vec3(X,Y,Y),vec3(Y,Y,Y),CLR);
//back
draw_triangle3d(vec3(Y,Y,X),vec3(Y,X,X),vec3(X,X,X),CLR);
draw_triangle3d(vec3(X,X,X),vec3(X,Y,X),vec3(Y,Y,X),CLR);
//top
draw_triangle3d(vec3(Y,X,Y),vec3(Y,X,X),vec3(X,X,X),CLR);
draw_triangle3d(vec3(X,X,X),vec3(X,X,Y),vec3(Y,X,Y),CLR);
//left
draw_triangle3d(vec3(Y,Y,Y),vec3(Y,Y,X),vec3(Y,X,X),CLR);
draw_triangle3d(vec3(Y,X,X),vec3(Y,X,Y),vec3(Y,Y,Y),CLR);
//right
draw_triangle3d(vec3(X,Y,Y),vec3(X,Y,X),vec3(X,X,X),CLR);
draw_triangle3d(vec3(X,X,X),vec3(X,X,Y),vec3(X,Y,Y),CLR);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment