summaryrefslogtreecommitdiff
path: root/resources/shaders/shader.vert
blob: 0994b90ad5c5eccb7a0b1632473706ab672d30a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#version 330 core

layout(location = 0) in vec3 position; // Position of the vertex
layout(location = 1) in vec3 normal;   // Normal of the vertex
layout(location = 2) in vec3 texCoords;   // Normal of the vertex

uniform float depth = -500.f;
uniform float skyHeight = 100.f;
uniform mat4 proj;
uniform mat4 view;
uniform mat4 model;
uniform mat4 inverseView;
//uniform float width;
//uniform float height; // TODO: Pass in width and height as uniform

uniform mat3 inverseTransposeModel;

out vec3 normal_cameraSpace;
out vec3 normal_worldSpace;
out vec3 camera_worldSpace;
out vec3 pos;
out vec3 reflPos;
out vec3 refrPos;
out float refrProb;
out vec2 uv;
out float matIor;

vec4 getRefrPos() {
//    float depth = -1500.f; // TODO: Pass as uniform
    vec3 w_o = normalize(pos - camera_worldSpace);
    float cos_theta_i = dot(-w_o, normal_worldSpace);
    float n_i = 1;
    float n_t = 1.33f;
//    matIor = n_t;
    float determinant = 1.f - (pow((n_i / n_t), 2.f) * (1.f - pow(cos_theta_i, 2.f)));

    float r0 = pow((n_i - n_t) / (n_i + n_t), 2.f); // variable required to calculate probability of reflection
    float prob_to_refl = r0 + ((1 - r0) * pow((1 - cos_theta_i), 5.f));

    if (determinant >= 0) {
        float cos_theta_t = sqrt(determinant);
        vec3 w_t = (n_i / n_t) * w_o + ((n_i / n_t) * cos_theta_i - cos_theta_t) * normal_worldSpace;
//        Ray reflectedRay(i.hit, w_t);
//        float attenuation = (!entering && attenuateRefract) ? std::pow(M_E, (-(ray.o - i.hit).norm()) * mat.ior) : 1;
//        L += traceRay(reflectedRay, scene, true, n_t, !is_in_refractor) * attenuation / threshold;
        float dist = position.y - depth;
        float depthScale = dist / w_t.y;
        vec3 groundContactPoint = -(w_t * depthScale) + position;
        return vec4(groundContactPoint, 1.f - prob_to_refl);
    } else {
//        Eigen::Vector3f w_i = w_o - 2 * w_o.dot(intersectNormal) * incidenceNormal;
//        Ray reflectedRay(i.hit, w_i);
//        L += traceRay(reflectedRay, scene, true, current_ior, false) / threshold;
        return vec4(0, 0, 0, 0);
    }
}

vec3 getReflPos() {
//    float depth = 500.f; // TODO: Pass as uniform
    vec3 w_o = normalize(pos - camera_worldSpace);
    vec3 reflectedRay = w_o - 2 * dot(w_o, normal_worldSpace) * normal_worldSpace;

    float dist = skyHeight - position.y;
    float depthScale = dist / reflectedRay.y;
    vec3 skyContactPoint = (reflectedRay * depthScale) + position;
    return skyContactPoint;
}

void main() {
//    float depth = -4.f;
//    float dist = position.y - depth;
    float width = 81.f * 2.f;
    float length = 81.f * 2.f;
    matIor = 1.33f;

    normal_cameraSpace = normalize(inverse(transpose(mat3(view))) * inverseTransposeModel * normal);
    camera_worldSpace = vec3(inverseView * vec4(0.f, 0.f, 0.f, 1.f));
    normal_worldSpace = normal;
    pos = vec3(model * vec4(position, 1.f)); //vec3(model * vec4(objSpacePos, 1.f));
//    pos = position;

//    float depthScale = dist / normal.y;
//    vec3 groundContactPoint = -(normal * depthScale) + position; // carries down to ground
//    groundContactPoint = vec3(model * vec4(position, 1));
//    uv = vec2((position.x + 81.f) / (162.f), groundContactPoint.z);
//    uv = vec2(normal);
    vec4 refrPos_and_prob = getRefrPos();
    refrPos = vec3(refrPos_and_prob);
    refrProb = clamp(refrPos_and_prob.w, 0.f, 1.f);

    reflPos = getReflPos();

    gl_Position = proj * view * model * vec4(position, 1);
}