GLSL snippets: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
(add category) |
(use <source> tags) |
||
Line 1: | Line 1: | ||
== Ray marching == |
== Ray marching == |
||
<source lang="GLSL"> |
|||
<pre> |
|||
// http://jamie-wong.com/2016/07/15/ray-marching-signed-distance-functions/ |
// http://jamie-wong.com/2016/07/15/ray-marching-signed-distance-functions/ |
||
vec3 rayDirection(float fieldOfView, vec2 size, vec2 fragCoord) { |
vec3 rayDirection(float fieldOfView, vec2 size, vec2 fragCoord) { |
||
Line 8: | Line 8: | ||
return normalize(vec3(xy, -z)); |
return normalize(vec3(xy, -z)); |
||
} |
} |
||
</ |
</source> |
||
<source lang="GLSL"> |
|||
<pre> |
|||
// http://jamie-wong.com/2016/07/15/ray-marching-signed-distance-functions/ |
// http://jamie-wong.com/2016/07/15/ray-marching-signed-distance-functions/ |
||
float castRay(const int scene, vec3 eye, vec3 dir, |
float castRay(const int scene, vec3 eye, vec3 dir, |
||
Line 29: | Line 29: | ||
return end; |
return end; |
||
} |
} |
||
</ |
</source> |
||
<source lang="GLSL"> |
|||
<pre> |
|||
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm |
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm |
||
vec3 estimateNormal( const int scene, const float epsilon, in vec3 p ) |
vec3 estimateNormal( const int scene, const float epsilon, in vec3 p ) |
||
Line 43: | Line 43: | ||
k.xxx*sceneSDF( scene, p + k.xxx*epsilon, m ) ); |
k.xxx*sceneSDF( scene, p + k.xxx*epsilon, m ) ); |
||
} |
} |
||
</ |
</source> |
||
== Matrix transformations == |
== Matrix transformations == |
||
<source lang="GLSL"> |
|||
<pre> |
|||
// https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml |
// https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml |
||
mat4 rotate(float a, vec3 v) |
mat4 rotate(float a, vec3 v) |
||
Line 62: | Line 62: | ||
); |
); |
||
} |
} |
||
</ |
</source> |
||
<source lang="GLSL"> |
|||
<pre> |
|||
// https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml |
// https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml |
||
mat4 translate(vec3 v) |
mat4 translate(vec3 v) |
||
Line 75: | Line 75: | ||
); |
); |
||
} |
} |
||
</ |
</source> |
||
== Noise == |
== Noise == |
||
<source lang="GLSL"> |
|||
<pre> |
|||
// https://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner |
// https://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner |
||
float rand(vec2 co) |
float rand(vec2 co) |
||
Line 85: | Line 85: | ||
return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453); |
return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453); |
||
} |
} |
||
</ |
</source> |
||
[[Category:Programming]] |
[[Category:Programming]] |
Revision as of 21:56, 13 December 2019
Ray marching
// http://jamie-wong.com/2016/07/15/ray-marching-signed-distance-functions/
vec3 rayDirection(float fieldOfView, vec2 size, vec2 fragCoord) {
vec2 xy = fragCoord - size / 2.0;
float z = size.y / tan(radians(fieldOfView) / 2.0);
return normalize(vec3(xy, -z));
}
// http://jamie-wong.com/2016/07/15/ray-marching-signed-distance-functions/
float castRay(const int scene, vec3 eye, vec3 dir,
float start, float end, float epsilon, int max_marching_steps,
out int material)
{
float depth = start;
for (int i = 0; i < max_marching_steps; i++) {
float d = sceneSDF(scene, eye + depth * dir, material);
if (d < epsilon)
return depth;
depth += d;
if (depth >= end)
break;
}
return end;
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 estimateNormal( const int scene, const float epsilon, in vec3 p )
{
int m;
const vec2 k = vec2(1,-1);
return normalize( k.xyy*sceneSDF( scene, p + k.xyy*epsilon, m ) +
k.yyx*sceneSDF( scene, p + k.yyx*epsilon, m ) +
k.yxy*sceneSDF( scene, p + k.yxy*epsilon, m ) +
k.xxx*sceneSDF( scene, p + k.xxx*epsilon, m ) );
}
Matrix transformations
// https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml
mat4 rotate(float a, vec3 v)
{
float c = cos(a);
vec3 ci = (1. - c) * v;
vec3 s = sin(a) * v;
return mat4(
ci.x * v.x + c, ci.x * v.y + s.z, ci.x * v.z - s.y, 0,
ci.y * v.x - s.z, ci.y * v.y + c, ci.y * v.z + s.x, 0,
ci.z * v.x + s.y, ci.z * v.y - s.x, ci.z * v.z + c, 0,
0, 0, 0, 1
);
}
// https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml
mat4 translate(vec3 v)
{
return mat4(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
v.x, v.y, v.z, 1
);
}
Noise
// https://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner
float rand(vec2 co)
{
return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453);
}