summaryrefslogtreecommitdiff
path: root/themes/src/summer/sky_model.cpp
blob: d700f34b164d9612dc6315c62b4a0647707b058d (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
#include "sky_model.h"
#include "../webgl_context.h"
#include "../mathlib.h"
#include "../shaders/sky_vert.h"
#include "../shaders/sky_frag.h"

SkyModel::SkyModel() {}

SkyModel::~SkyModel() { unload(); }

void SkyModel::load(WebglContext* inContext) {
    context = inContext;
    shader = loadShader(shader_sky_vert, shader_sky_frag);
    useShader(shader);

    attributes.position = getShaderAttribute(shader, "position");
    uniforms.time       = getShaderUniform(shader, "time");
    uniforms.horizon    = getShaderUniform(shader, "horizon");

    // Full-screen quad in clip space (two triangles).
    Vector2 verts[6] = {
        Vector2(-1.f, -1.f), Vector2(1.f, -1.f), Vector2(-1.f, 1.f),
        Vector2(-1.f,  1.f), Vector2(1.f, -1.f), Vector2( 1.f, 1.f)
    };

    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

    glEnableVertexAttribArray(attributes.position);
    glVertexAttribPointer(attributes.position, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), (GLvoid*)0);

    glBindVertexArray(0);
}

void SkyModel::update(f32 dtSeconds) {
    elapsedTime += dtSeconds;
}

void SkyModel::render() {
    useShader(shader);

    // Background: paint every pixel first, without touching the depth buffer so
    // it can neither reject nor be rejected by the sun/water drawn afterward.
    glDepthMask(GL_FALSE);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_BLEND);

    setShaderFloat(uniforms.time, elapsedTime);
    setShaderFloat(uniforms.horizon, horizon);

    glBindVertexArray(vao);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glBindVertexArray(0);
}

void SkyModel::unload() {
    if (vao) { glDeleteVertexArrays(1, &vao); vao = 0; }
    if (vbo) { glDeleteBuffers(1, &vbo); vbo = 0; }
    if (shader) { glDeleteProgram(shader); shader = 0; }
}