Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

Convert GLSL 1.2 to 4.0

$
0
0

@taketori7616 wrote:

This is a vertex shader giving out grid points, but this is written in version 120.
I'd like to rewrite this to version 400, but what should I rewrite?

#version 120

attribute float vertexId;
attribute float sound;
uniform float vertexCount;
uniform float time;
uniform vec2 resolution;

#define PI radians(180.0)




mat4 trans(vec3 trans) {
    return mat4(
                1, 0, 0, 0,
                0, 1, 0, 0,
                0, 0, 1, 0,
                trans, 1);
}

mat4 ident() {
    return mat4(
                1, 0, 0, 0,
                0, 1, 0, 0,
                0, 0, 1, 0,
                0, 0, 0, 1);
}


mat4 uniformScale(float s) {
    return mat4(
                s, 0, 0, 0,
                0, s, 0, 0,
                0, 0, s, 0,
                0, 0, 0, 1);
}

mat4 persp(float fov, float aspect, float zNear, float zFar) {
    float f = tan(PI * 0.5 - 0.5 * fov);
    float rangeInv = 1.0 / (zNear - zFar);

    return mat4(
                f / aspect, 0, 0, 0,
                0, f, 0, 0,
                0, 0, (zNear + zFar) * rangeInv, -1,
                0, 0, zNear * zFar * rangeInv * 2., 0);
}




mat4 lookAt(vec3 eye, vec3 target, vec3 up) {
    vec3 zAxis = normalize(eye - target);
    vec3 xAxis = normalize(cross(up, zAxis));
    vec3 yAxis = cross(zAxis, xAxis);

    return mat4(
                xAxis, 0,
                yAxis, 0,
                zAxis, 0,
                eye, 1);
}

mat4 inverse(mat4 m) {
    float
    a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3],
    a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3],
    a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3],
    a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3],

    b00 = a00 * a11 - a01 * a10,
    b01 = a00 * a12 - a02 * a10,
    b02 = a00 * a13 - a03 * a10,
    b03 = a01 * a12 - a02 * a11,
    b04 = a01 * a13 - a03 * a11,
    b05 = a02 * a13 - a03 * a12,
    b06 = a20 * a31 - a21 * a30,
    b07 = a20 * a32 - a22 * a30,
    b08 = a20 * a33 - a23 * a30,
    b09 = a21 * a32 - a22 * a31,
    b10 = a21 * a33 - a23 * a31,
    b11 = a22 * a33 - a23 * a32,

    det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;

    return mat4(
                a11 * b11 - a12 * b10 + a13 * b09,
                a02 * b10 - a01 * b11 - a03 * b09,
                a31 * b05 - a32 * b04 + a33 * b03,
                a22 * b04 - a21 * b05 - a23 * b03,
                a12 * b08 - a10 * b11 - a13 * b07,
                a00 * b11 - a02 * b08 + a03 * b07,
                a32 * b02 - a30 * b05 - a33 * b01,
                a20 * b05 - a22 * b02 + a23 * b01,
                a10 * b10 - a11 * b08 + a13 * b06,
                a01 * b08 - a00 * b10 - a03 * b06,
                a30 * b04 - a31 * b02 + a33 * b00,
                a21 * b02 - a20 * b04 - a23 * b00,
                a11 * b07 - a10 * b09 - a12 * b06,
                a00 * b09 - a01 * b07 + a02 * b06,
                a31 * b01 - a30 * b03 - a32 * b00,
                a20 * b03 - a21 * b01 + a22 * b00) / det;
}

mat4 cameraLookAt(vec3 eye, vec3 target, vec3 up) {
#if 1
    return inverse(lookAt(eye, target, up));
#else
    vec3 zAxis = normalize(target - eye);
    vec3 xAxis = normalize(cross(up, zAxis));
    vec3 yAxis = cross(zAxis, xAxis);

    return mat4(
                xAxis, 0,
                yAxis, 0,
                zAxis, 0,
                -dot(xAxis, eye), -dot(yAxis, eye), -dot(zAxis, eye), 1);
#endif

}



void main() {
    float size = floor(pow(vertexCount, 1./3.));
    float div  = size - 1.;
    vec3 p = vec3(
                  mod(vertexId, size),
                  mod(floor(vertexId / size), size),
                  floor(floor(vertexId / size) / size));

    float a = time * 0.1;
    float c = cos(a);
    float s = sin(a);
    float y = sin(time * 0.11);

    vec3 target = vec3(0,0,0);
    vec3 up = vec3(0,1,0);
    vec3 eye = vec3(c, y, s);


    mat4 m = ident();
    m *= persp(radians(45.), resolution.x / resolution.y, 0.1, 2.);
    m *= cameraLookAt(eye, target, up);
    m *= trans(vec3(-1, -1, -1));
    m *= uniformScale(2. / div);

    gl_Position = m * vec4(p, 1);

    float hue = 1.;//(time * 0.01 + count * 1.001);
    //v_color = vec4(p / div, 1); //vec4(hsv2rgb(vec3(hue, 1, 1)), 1);
    gl_PointSize = mix(30.0, 0.0, gl_Position.z * 0.5 + 0.5);
    gl_FrontColor = vec4(p / div, 1);
}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles