File: cubemap.cpp

package info (click to toggle)
wayfire 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,764 kB
  • sloc: cpp: 52,464; xml: 2,987; ansic: 699; makefile: 161
file content (159 lines) | stat: -rw-r--r-- 4,613 bytes parent folder | download
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "cubemap.hpp"

#include <glm/gtc/matrix_transform.hpp>
#include <config.h>
#include <wayfire/core.hpp>
#include <wayfire/img.hpp>

#include "cubemap-shaders.tpp"

wf_cube_background_cubemap::wf_cube_background_cubemap()
{
    create_program();
    reload_texture();
}

wf_cube_background_cubemap::~wf_cube_background_cubemap()
{
    wf::gles::run_in_context([&]
    {
        program.free_resources();
        GL_CALL(glDeleteTextures(1, &tex));
        GL_CALL(glDeleteBuffers(1, &vbo_cube_vertices));
        GL_CALL(glDeleteBuffers(1, &ibo_cube_indices));
    });
}

void wf_cube_background_cubemap::create_program()
{
    wf::gles::run_in_context([&]
    {
        program.set_simple(
            OpenGL::compile_program(cubemap_vertex, cubemap_fragment));
    });
}

void wf_cube_background_cubemap::reload_texture()
{
    if (!last_background_image.compare(background_image))
    {
        return;
    }

    last_background_image = background_image;

    wf::gles::run_in_context([&]
    {
        if (tex == (uint32_t)-1)
        {
            GL_CALL(glGenTextures(1, &tex));
            GL_CALL(glGenBuffers(1, &vbo_cube_vertices));
            GL_CALL(glGenBuffers(1, &ibo_cube_indices));
        }

        GL_CALL(glBindTexture(GL_TEXTURE_CUBE_MAP, tex));
        if (!image_io::load_from_file(last_background_image, GL_TEXTURE_CUBE_MAP))
        {
            LOGE("Failed to load cubemap background image from \"%s\".",
                last_background_image.c_str());

            GL_CALL(glDeleteTextures(1, &tex));
            GL_CALL(glDeleteBuffers(1, &vbo_cube_vertices));
            GL_CALL(glDeleteBuffers(1, &ibo_cube_indices));
            tex = -1;
        }

        if (tex != (uint32_t)-1)
        {
            GL_CALL(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER,
                GL_LINEAR));
            GL_CALL(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER,
                GL_LINEAR));
            GL_CALL(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S,
                GL_CLAMP_TO_EDGE));
            GL_CALL(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T,
                GL_CLAMP_TO_EDGE));
            GL_CALL(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R,
                GL_CLAMP_TO_EDGE));
        }

        GL_CALL(glBindTexture(GL_TEXTURE_CUBE_MAP, 0));
    });
}

void wf_cube_background_cubemap::render_frame(const wf::render_target_t& fb,
    wf_cube_animation_attribs& attribs)
{
    reload_texture();

    if (tex == (uint32_t)-1)
    {
        GL_CALL(glClearColor(TEX_ERROR_FLAG_COLOR));
        GL_CALL(glClear(GL_COLOR_BUFFER_BIT));
        return;
    }

    program.use(wf::TEXTURE_TYPE_RGBA);
    GL_CALL(glDepthMask(GL_FALSE));

    GL_CALL(glBindTexture(GL_TEXTURE_CUBE_MAP, tex));

    GLfloat cube_vertices[] = {
        -1.0, 1.0, 1.0,
        -1.0, -1.0, 1.0,
        1.0, -1.0, 1.0,
        1.0, 1.0, 1.0,
        -1.0, 1.0, -1.0,
        -1.0, -1.0, -1.0,
        1.0, -1.0, -1.0,
        1.0, 1.0, -1.0,
    };

    GLushort cube_indices[] = {
        3, 7, 6, // right
        3, 6, 2, // right
        4, 0, 1, // left
        4, 1, 5, // left
        4, 7, 3, // top
        4, 3, 0, // top
        1, 2, 6, // bottom
        1, 6, 5, // bottom
        0, 3, 2, // front
        0, 2, 1, // front
        7, 4, 5, // back
        7, 5, 6, // back
    };

    glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices);
    glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices,
        GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_cube_indices);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_indices), cube_indices,
        GL_STATIC_DRAW);

    GLint vertex = glGetAttribLocation(program.get_program_id(
        wf::TEXTURE_TYPE_RGBA), "position");
    glEnableVertexAttribArray(vertex);
    glVertexAttribPointer(vertex, 3, GL_FLOAT, GL_FALSE, 0, 0);

    auto model = glm::rotate(glm::mat4(1.0),
        float(attribs.cube_animation.rotation),
        glm::vec3(0, 1, 0));

    glm::vec3 look_at{0.,
        (double)-attribs.cube_animation.offset_y,
        (double)attribs.cube_animation.offset_z};

    auto view = glm::lookAt(glm::vec3(0., 0., 0.), look_at, glm::vec3(0., 1., 0.));
    auto vp   = wf::gles::output_transform(fb) * attribs.projection * view;

    model = vp * model;
    program.uniformMatrix4f("cubeMapMatrix", model);

    glDrawElements(GL_TRIANGLES, 12 * 3, GL_UNSIGNED_SHORT, 0);

    program.deactivate();
    GL_CALL(glDepthMask(GL_TRUE));
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}