File: texture.c

package info (click to toggle)
goxel 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,232 kB
  • sloc: ansic: 87,714; cpp: 87,537; python: 128; makefile: 74; xml: 55
file content (222 lines) | stat: -rw-r--r-- 6,499 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* Goxel 3D voxels editor
 *
 * copyright (c) 2015 Guillaume Chereau <guillaume@noctua-software.com>
 *
 * Goxel is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.

 * Goxel is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.

 * You should have received a copy of the GNU General Public License along with
 * goxel.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "goxel.h"
#include <stdarg.h>

// Return the next power of 2 larger or equal to x.
static int next_pow2(int x)
{
    x--;
    x |= x >> 1;
    x |= x >> 2;
    x |= x >> 4;
    x |= x >> 8;
    x |= x >> 16;
    x++;
    return x;
}

static bool is_pow2(int x) { return x == next_pow2(x); }

static void texture_create_empty(texture_t *tex)
{
    assert(tex->flags & TF_HAS_TEX);
    GL(glGenTextures(1, &tex->tex));
    GL(glActiveTexture(GL_TEXTURE0));
    GL(glBindTexture(GL_TEXTURE_2D, tex->tex));
    GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
                       GL_LINEAR));
    GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                       GL_LINEAR));
    GL(glTexImage2D(GL_TEXTURE_2D, 0, tex->format, tex->tex_w, tex->tex_h,
            0, tex->format, GL_UNSIGNED_BYTE, NULL));
}

static void blit(const uint8_t *src, int src_w, int src_h, int bpp,
                 uint8_t *dst, int dst_w, int dst_h)
{
    int i, j;
    for (i = 0; i < src_h; i++) for (j = 0; j < src_w; j++) {
        memcpy(&dst[(i * dst_w + j) * bpp], &src[(i * src_w + j) * bpp], bpp);
    }
}

static void texture_set_data(texture_t *tex,
        const uint8_t *data, int w, int h, int bpp)
{
    uint8_t *buff0 = NULL;
    int data_type = GL_UNSIGNED_BYTE;

    if (!is_pow2(w) || !is_pow2(h)) {
        buff0 = calloc(bpp, tex->tex_w * tex->tex_h);
        blit(data, w, h, bpp, buff0, tex->tex_w, tex->tex_h);
        data = buff0;
    }

    GL(glGenTextures(1, &tex->tex));
    GL(glActiveTexture(GL_TEXTURE0));
    GL(glBindTexture(GL_TEXTURE_2D, tex->tex));
    if (!(tex->flags & TF_NEAREST)) {
        GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
        GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
            (tex->flags & TF_MIPMAP)? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR));
    } else {
        GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
        GL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
    }
    GL(glTexImage2D(GL_TEXTURE_2D, 0, tex->format, tex->tex_w, tex->tex_h,
                0, tex->format, data_type, data));
    free(buff0);

    if (tex->flags & TF_MIPMAP)
        GL(glGenerateMipmap(GL_TEXTURE_2D));
}

texture_t *texture_new_image(const char *path, int flags)
{
    texture_t *tex;
    uint8_t *img;
    int w, h, bpp = 0;
    img = img_read(path, &w, &h, &bpp);
    if (!img) {
        LOG_W("Cannot open image '%s'", path);
        return NULL;
    }
    tex = calloc(1, sizeof(*tex));
    tex->path = strdup(path);
    tex->tex_w = next_pow2(w);
    tex->tex_h = next_pow2(h);
    tex->w = w;
    tex->h = h;
    tex->flags = TF_HAS_TEX | flags;
    tex->format = (int[]){0, 0, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA}[bpp];
    texture_create_empty(tex);
    texture_set_data(tex, img, w, h, bpp);
    free(img);
    tex->ref = 1;
    return tex;
}

texture_t *texture_new_from_buf(const uint8_t *data,
                                int w, int h, int bpp, int flags)
{
    texture_t *tex;
    tex = calloc(1, sizeof(*tex));
    tex->tex_w = next_pow2(w);
    tex->tex_h = next_pow2(h);
    tex->w = w;
    tex->h = h;
    tex->flags = TF_HAS_TEX | flags;
    tex->format = (int[]){0, 0, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA}[bpp];
    texture_create_empty(tex);
    texture_set_data(tex, data, w, h, bpp);
    tex->ref = 1;
    return tex;
}

texture_t *texture_new_surface(int w, int h, int flags)
{
    texture_t *tex;
    tex = calloc(1, sizeof(*tex));
    tex->tex_w = next_pow2(w);
    tex->tex_h = next_pow2(h);
    tex->w = w;
    tex->h = h;
    tex->flags = flags | TF_HAS_TEX;
    tex->format = (flags & TF_RGB) ? GL_RGB : GL_RGBA;
    texture_create_empty(tex);
    tex->ref = 1;
    return tex;
}

texture_t *texture_new_buffer(int w, int h, int flags)
{
    texture_t *tex;
    tex = calloc(1, sizeof(*tex));
    tex->format = (flags & TF_RGB) ? GL_RGB : GL_RGBA;
    tex->tex_w = next_pow2(w);
    tex->tex_h = next_pow2(h);
    tex->w = w;
    tex->h = h;
    tex->flags = flags | TF_HAS_FB | TF_HAS_TEX;
    gl_gen_fbo(tex->tex_w, tex->tex_h, tex->format, 1,
               &tex->framebuffer, &tex->tex);
    tex->ref = 1;
    return tex;
}

void texture_delete(texture_t *tex)
{
    if (!tex) return;
    tex->ref--;
    if (tex->ref > 0) return;

    free(tex->path);
    if (tex->framebuffer) {
        GL(glBindFramebuffer(GL_FRAMEBUFFER, tex->framebuffer));
        if (tex->depth)
            GL(glDeleteRenderbuffers(1, &tex->depth));
        if (tex->stencil)
            GL(glDeleteRenderbuffers(1, &tex->stencil));
        GL(glDeleteFramebuffers(1, &tex->framebuffer));
    }
    if (tex->tex)
        GL(glDeleteTextures(1, &tex->tex));
    free(tex);
}

texture_t *texture_copy(texture_t *tex)
{
    // XXX: since texture are immutable, we just increase the ref counter.
    if (tex) tex->ref++;
    return tex;
}

void texture_get_data(const texture_t *tex, int w, int h, int bpp,
                      uint8_t *buf)
{
    uint8_t *tmp;
    size_t i, j;
    tmp = calloc(w * h, 4);
    GL(glBindFramebuffer(GL_FRAMEBUFFER, tex->framebuffer));
    GL(glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, tmp));
    // Flip output y and remove alpha if needed.
    for (i = 0; i < h; i++) {
        for (j = 0; j < w; j++) {
            memcpy(&buf[(i * w + j) * bpp],
                   &tmp[((h - i - 1) * w  + j) * 4],
                   bpp);
        }
    }
    free(tmp);
}

void texture_save_to_file(const texture_t *tex, const char *path)
{
    uint8_t *data;
    int w, h;
    LOG_I("save texture to %s", path);
    w = tex->tex_w;
    h = tex->tex_h;
    data = calloc(w * h, 4);
    texture_get_data(tex, w, h, 4, data);
    img_write(data, w, h, 4, path);
    free(data);
}