src/texture.c

Sun, 11 Aug 2024 16:11:30 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 11 Aug 2024 16:11:30 +0200
changeset 68
823c03733e42
parent 50
d8d2e4817db1
permissions
-rw-r--r--

add mouse and window focus - resolves #382

plus some minor code improvements

50
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 * Copyright 2024 Mike Becker. All rights reserved.
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 *
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * modification, are permitted provided that the following conditions are met:
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 *
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 *
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 * 2. Redistributions in binary form must reproduce the above copyright
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * notice, this list of conditions and the following disclaimer in the
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * documentation and/or other materials provided with the distribution.
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 *
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * POSSIBILITY OF SUCH DAMAGE.
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 */
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 #include "ascension/texture.h"
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "ascension/error.h"
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 #include <assert.h>
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include <GL/glew.h>
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 void asc_texture_bind(AscTexture const *tex, int uniform_location, int unit) {
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 glActiveTexture(GL_TEXTURE0 + unit);
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 GLenum error = glGetError();
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 if (error == GL_INVALID_ENUM) {
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 asc_error("Tried to use more texture units than available.");
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 }
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 glBindTexture(tex->target, tex->tex_id);
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 glUniform1i(uniform_location, unit);
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 }
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 void asc_texture_from_surface(AscTexture *tex, SDL_Surface const *surface) {
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 glBindTexture(tex->target,tex->tex_id);
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 glPixelStorei(GL_UNPACK_ROW_LENGTH,
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 surface->pitch / surface->format->BytesPerPixel);
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 glTexImage2D(tex->target, 0, GL_RGBA,
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 surface->w, surface->h,
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 0, GL_BGRA,
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 GL_UNSIGNED_BYTE, surface->pixels);
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 }
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 void asc_texture_init(
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 AscTexture *tex,
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 enum asc_texture_target target,
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 enum asc_texture_min_filter min_filter,
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 enum asc_texture_mag_filter mag_filter
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 ) {
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 static const GLenum texture_targets[] = {
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 GL_TEXTURE_RECTANGLE
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 };
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 static const GLint texture_filters[] = {
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 GL_NEAREST,
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 GL_LINEAR,
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 GL_NEAREST_MIPMAP_NEAREST,
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 GL_LINEAR_MIPMAP_NEAREST,
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 GL_NEAREST_MIPMAP_LINEAR,
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 GL_LINEAR_MIPMAP_LINEAR
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 };
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 assert(target < sizeof(texture_targets) / sizeof(GLenum));
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 assert(min_filter < sizeof(texture_filters) / sizeof(GLint));
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 assert(mag_filter < 2); // mag filter only supports nearest/linear
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 tex->target = texture_targets[target];
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 glGenTextures(1, &tex->tex_id);
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 glBindTexture(tex->target, tex->tex_id);
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 glTexParameteri(tex->target, GL_TEXTURE_MIN_FILTER,
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 texture_filters[min_filter]);
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 glTexParameteri(tex->target, GL_TEXTURE_MAG_FILTER,
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 texture_filters[mag_filter]);
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 asc_dprintf("Generated new texture for text node: %u", tex->tex_id);
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 }
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 void asc_texture_destroy(AscTexture *tex) {
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 asc_dprintf("Release text node texture: %u", tex->tex_id);
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 glDeleteTextures(1, &tex->tex_id);
d8d2e4817db1 add texture.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 }

mercurial