cubemapper

annotate src/texture.cc @ 4:2bfafdced01a

added README, COPYING, and copyright headers
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 30 Jul 2017 16:11:19 +0300
parents e308561f9889
children
rev   line source
nuclear@4 1 /*
nuclear@4 2 Cubemapper - a program for converting panoramic images into cubemaps
nuclear@4 3 Copyright (C) 2017 John Tsiombikas <nuclear@member.fsf.org>
nuclear@4 4
nuclear@4 5 This program is free software: you can redistribute it and/or modify
nuclear@4 6 it under the terms of the GNU General Public License as published by
nuclear@4 7 the Free Software Foundation, either version 3 of the License, or
nuclear@4 8 (at your option) any later version.
nuclear@4 9
nuclear@4 10 This program is distributed in the hope that it will be useful,
nuclear@4 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@4 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@4 13 GNU General Public License for more details.
nuclear@4 14
nuclear@4 15 You should have received a copy of the GNU General Public License
nuclear@4 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@4 17 */
nuclear@0 18 #include <stdio.h>
nuclear@0 19 #include <imago2.h>
nuclear@0 20 #include "texture.h"
nuclear@0 21 #include "opengl.h"
nuclear@0 22
nuclear@0 23 Texture::Texture()
nuclear@0 24 {
nuclear@0 25 width = height = tex_width = tex_height = 0;
nuclear@0 26 tex = 0;
nuclear@0 27 }
nuclear@0 28
nuclear@0 29 Texture::~Texture()
nuclear@0 30 {
nuclear@0 31 if(tex) {
nuclear@0 32 glDeleteTextures(1, &tex);
nuclear@0 33 }
nuclear@0 34 }
nuclear@0 35
nuclear@0 36 static unsigned int next_pow2(unsigned int x)
nuclear@0 37 {
nuclear@0 38 --x;
nuclear@0 39 x |= x >> 1;
nuclear@0 40 x |= x >> 2;
nuclear@0 41 x |= x >> 4;
nuclear@0 42 x |= x >> 8;
nuclear@0 43 x |= x >> 16;
nuclear@0 44 return x + 1;
nuclear@0 45 }
nuclear@0 46
nuclear@1 47 int Texture::get_width() const
nuclear@1 48 {
nuclear@1 49 return width;
nuclear@1 50 }
nuclear@1 51
nuclear@1 52 int Texture::get_height() const
nuclear@1 53 {
nuclear@1 54 return height;
nuclear@1 55 }
nuclear@1 56
nuclear@0 57 bool Texture::load(const char *fname)
nuclear@0 58 {
nuclear@0 59 img_pixmap img;
nuclear@0 60 img_init(&img);
nuclear@0 61 if(img_load(&img, fname) == -1) {
nuclear@0 62 fprintf(stderr, "failed to load texture: %s\n", fname);
nuclear@0 63 img_destroy(&img);
nuclear@0 64 return false;
nuclear@0 65 }
nuclear@0 66
nuclear@0 67 unsigned int intfmt = img_glintfmt(&img);
nuclear@0 68 unsigned int pixfmt = img_glfmt(&img);
nuclear@0 69 unsigned int pixtype = img_gltype(&img);
nuclear@0 70
nuclear@0 71 width = img.width;
nuclear@0 72 height = img.height;
nuclear@0 73 tex_width = next_pow2(width);
nuclear@0 74 tex_height = next_pow2(height);
nuclear@0 75
nuclear@0 76 if(!tex) {
nuclear@0 77 glGenTextures(1, &tex);
nuclear@0 78 }
nuclear@0 79 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@0 80
nuclear@0 81 if(GLEW_SGIS_generate_mipmap) {
nuclear@0 82 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
nuclear@0 83 } else {
nuclear@0 84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@0 85 }
nuclear@0 86
nuclear@0 87 glTexImage2D(GL_TEXTURE_2D, 0, intfmt, tex_width, tex_height, 0, pixfmt, pixtype, 0);
nuclear@0 88 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, pixfmt, pixtype, img.pixels);
nuclear@0 89
nuclear@0 90 tmat.scaling((float)width / (float)tex_width, (float)height / (float)tex_height, 1);
nuclear@0 91 return true;
nuclear@0 92 }
nuclear@0 93
nuclear@0 94 const Mat4 &Texture::texture_matrix() const
nuclear@0 95 {
nuclear@0 96 return tmat;
nuclear@0 97 }
nuclear@0 98
nuclear@0 99 void Texture::bind(bool loadmat) const
nuclear@0 100 {
nuclear@0 101 if(!tex) return;
nuclear@0 102
nuclear@0 103 if(loadmat) {
nuclear@0 104 glMatrixMode(GL_TEXTURE);
nuclear@0 105 glLoadMatrixf(tmat[0]);
nuclear@0 106 glMatrixMode(GL_MODELVIEW);
nuclear@0 107 }
nuclear@0 108
nuclear@0 109 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@0 110 }