Difference between revisions of "GLSL snippets"

From vegard.wiki
Jump to navigation Jump to search
(rotate/translate)
 
Line 1: Line 1:
 +
== Transformations ==
 +
 
=== Rotation matrix ===
 
=== Rotation matrix ===
 
<pre>
 
<pre>
 +
// 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 15: Line 18:
 
}
 
}
 
</pre>
 
</pre>
Source: https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml
 
  
 
=== Translation matrix ===
 
=== Translation matrix ===
 
<pre>
 
<pre>
 +
// https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml
 
mat4 translate(vec3 v)
 
mat4 translate(vec3 v)
 
{
 
{
Line 29: Line 32:
 
}
 
}
 
</pre>
 
</pre>
Source: https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml
+
 
 +
== Noise ==
 +
 
 +
=== vec2 -> float ===
 +
 
 +
<pre>
 +
// 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);
 +
}
 +
</pre>

Revision as of 20:46, 13 December 2019

Transformations

Rotation matrix

// 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
    );
}

Translation matrix

// 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

vec2 -> float

// 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);
}