universe@50: /* universe@50: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@50: * Copyright 2024 Mike Becker. All rights reserved. universe@50: * universe@50: * Redistribution and use in source and binary forms, with or without universe@50: * modification, are permitted provided that the following conditions are met: universe@50: * universe@50: * 1. Redistributions of source code must retain the above copyright universe@50: * notice, this list of conditions and the following disclaimer. universe@50: * universe@50: * 2. Redistributions in binary form must reproduce the above copyright universe@50: * notice, this list of conditions and the following disclaimer in the universe@50: * documentation and/or other materials provided with the distribution. universe@50: * universe@50: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@50: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@50: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@50: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@50: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@50: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@50: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@50: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@50: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@50: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@50: * POSSIBILITY OF SUCH DAMAGE. universe@50: */ universe@50: universe@50: #include "ascension/texture.h" universe@50: #include "ascension/error.h" universe@50: universe@50: #include universe@50: #include universe@50: universe@50: void asc_texture_bind(AscTexture const *tex, int uniform_location, int unit) { universe@50: glActiveTexture(GL_TEXTURE0 + unit); universe@50: GLenum error = glGetError(); universe@50: if (error == GL_INVALID_ENUM) { universe@50: asc_error("Tried to use more texture units than available."); universe@50: } universe@50: glBindTexture(tex->target, tex->tex_id); universe@50: glUniform1i(uniform_location, unit); universe@50: } universe@50: universe@50: void asc_texture_from_surface(AscTexture *tex, SDL_Surface const *surface) { universe@50: glBindTexture(tex->target,tex->tex_id); universe@50: glPixelStorei(GL_UNPACK_ROW_LENGTH, universe@50: surface->pitch / surface->format->BytesPerPixel); universe@50: glTexImage2D(tex->target, 0, GL_RGBA, universe@50: surface->w, surface->h, universe@50: 0, GL_BGRA, universe@50: GL_UNSIGNED_BYTE, surface->pixels); universe@50: } universe@50: universe@50: void asc_texture_init( universe@50: AscTexture *tex, universe@50: enum asc_texture_target target, universe@50: enum asc_texture_min_filter min_filter, universe@50: enum asc_texture_mag_filter mag_filter universe@50: ) { universe@50: static const GLenum texture_targets[] = { universe@50: GL_TEXTURE_RECTANGLE universe@50: }; universe@50: static const GLint texture_filters[] = { universe@50: GL_NEAREST, universe@50: GL_LINEAR, universe@50: GL_NEAREST_MIPMAP_NEAREST, universe@50: GL_LINEAR_MIPMAP_NEAREST, universe@50: GL_NEAREST_MIPMAP_LINEAR, universe@50: GL_LINEAR_MIPMAP_LINEAR universe@50: }; universe@50: assert(target < sizeof(texture_targets) / sizeof(GLenum)); universe@50: assert(min_filter < sizeof(texture_filters) / sizeof(GLint)); universe@50: assert(mag_filter < 2); // mag filter only supports nearest/linear universe@50: tex->target = texture_targets[target]; universe@50: glGenTextures(1, &tex->tex_id); universe@50: glBindTexture(tex->target, tex->tex_id); universe@50: glTexParameteri(tex->target, GL_TEXTURE_MIN_FILTER, universe@50: texture_filters[min_filter]); universe@50: glTexParameteri(tex->target, GL_TEXTURE_MAG_FILTER, universe@50: texture_filters[mag_filter]); universe@50: asc_dprintf("Generated new texture for text node: %u", tex->tex_id); universe@50: } universe@50: universe@50: void asc_texture_destroy(AscTexture *tex) { universe@50: asc_dprintf("Release text node texture: %u", tex->tex_id); universe@50: glDeleteTextures(1, &tex->tex_id); universe@50: }