bloboland

annotate src/texture.cc @ 5:2f4406cc341e

meh
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 19 Dec 2012 02:37:20 +0200
parents 9021a906c5d3
children
rev   line source
nuclear@1 1 #include "opengl.h"
nuclear@1 2 #include "texture.h"
nuclear@1 3
nuclear@1 4 void bind_texture(const Texture *tex, int texunit)
nuclear@1 5 {
nuclear@3 6 static const Texture *cur_tex[8];
nuclear@1 7
nuclear@3 8 if(tex == cur_tex[texunit]) {
nuclear@1 9 return;
nuclear@1 10 }
nuclear@1 11
nuclear@1 12 glActiveTexture(GL_TEXTURE0 + texunit);
nuclear@1 13 if(tex) {
nuclear@1 14 glEnable(tex->type);
nuclear@1 15 glBindTexture(tex->type, tex->tex);
nuclear@1 16 } else {
nuclear@3 17 glDisable(cur_tex[texunit]->type);
nuclear@1 18 }
nuclear@1 19 glActiveTexture(GL_TEXTURE0);
nuclear@1 20
nuclear@3 21 cur_tex[texunit] = tex;
nuclear@1 22 }
nuclear@1 23
nuclear@1 24
nuclear@1 25 Texture::Texture()
nuclear@1 26 {
nuclear@1 27 type = 0;
nuclear@5 28 intfmt = GL_RGBA;
nuclear@1 29
nuclear@1 30 size[0] = size[1] = size[2] = 0;
nuclear@1 31
nuclear@1 32 glGenTextures(1, &tex);
nuclear@1 33 }
nuclear@1 34
nuclear@1 35 Texture::~Texture()
nuclear@1 36 {
nuclear@1 37 if(tex) {
nuclear@1 38 glDeleteTextures(1, &tex);
nuclear@1 39 }
nuclear@1 40 }
nuclear@1 41
nuclear@5 42 void Texture::set_pixel_format(unsigned int fmt)
nuclear@5 43 {
nuclear@5 44 intfmt = fmt;
nuclear@5 45 }
nuclear@5 46
nuclear@4 47 void Texture::set_filtering(unsigned int min_filter, unsigned int mag_filter)
nuclear@4 48 {
nuclear@4 49 if(mag_filter == 0) {
nuclear@4 50 mag_filter = min_filter;
nuclear@4 51 }
nuclear@4 52
nuclear@4 53 glBindTexture(type, tex);
nuclear@4 54 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, min_filter);
nuclear@4 55 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, mag_filter);
nuclear@4 56 }
nuclear@4 57
nuclear@4 58 void Texture::set_wrapping(unsigned int wrap)
nuclear@4 59 {
nuclear@4 60 glBindTexture(type, tex);
nuclear@4 61 glTexParameteri(type, GL_TEXTURE_WRAP_S, wrap);
nuclear@4 62 glTexParameteri(type, GL_TEXTURE_WRAP_T, wrap);
nuclear@4 63 glTexParameteri(type, GL_TEXTURE_WRAP_R, wrap);
nuclear@4 64 }
nuclear@4 65
nuclear@1 66 int Texture::get_size(int idx) const
nuclear@1 67 {
nuclear@1 68 return idx >= 0 && idx < 3 ? size[idx] : 0;
nuclear@1 69 }
nuclear@1 70
nuclear@4 71 Texture1D::Texture1D()
nuclear@4 72 {
nuclear@4 73 type = GL_TEXTURE_1D;
nuclear@4 74
nuclear@4 75 glBindTexture(type, tex);
nuclear@4 76 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@4 77 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@4 78 }
nuclear@4 79
nuclear@4 80 void Texture1D::create(int sz, float *data)
nuclear@4 81 {
nuclear@4 82 glBindTexture(type, tex);
nuclear@5 83 glTexImage1D(type, 0, intfmt, sz, 0, GL_RGBA, GL_FLOAT, data);
nuclear@4 84
nuclear@4 85 size[0] = sz;
nuclear@4 86 }
nuclear@4 87
nuclear@4 88 void Texture1D::update(float *data)
nuclear@4 89 {
nuclear@4 90 glBindTexture(type, tex);
nuclear@4 91 glTexSubImage1D(type, 0, 0, size[0], GL_RGBA, GL_FLOAT, data);
nuclear@4 92 }
nuclear@4 93
nuclear@1 94 Texture2D::Texture2D()
nuclear@1 95 {
nuclear@1 96 type = GL_TEXTURE_2D;
nuclear@1 97
nuclear@1 98 glBindTexture(type, tex);
nuclear@1 99 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@1 100 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@1 101 }
nuclear@1 102
nuclear@1 103 void Texture2D::create(int xsz, int ysz, float *data)
nuclear@1 104 {
nuclear@1 105 glBindTexture(type, tex);
nuclear@5 106 glTexImage2D(type, 0, intfmt, xsz, ysz, 0, GL_RGBA, GL_FLOAT, data);
nuclear@1 107
nuclear@1 108 size[0] = xsz;
nuclear@1 109 size[1] = ysz;
nuclear@1 110 }
nuclear@1 111
nuclear@3 112 void Texture2D::update(float *data)
nuclear@3 113 {
nuclear@3 114 glBindTexture(type, tex);
nuclear@3 115 glTexSubImage2D(type, 0, 0, 0, size[0], size[1], GL_RGBA, GL_FLOAT, data);
nuclear@3 116 }
nuclear@3 117
nuclear@1 118 Texture3D::Texture3D()
nuclear@1 119 {
nuclear@1 120 type = GL_TEXTURE_3D;
nuclear@1 121
nuclear@1 122 glBindTexture(type, tex);
nuclear@1 123 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@1 124 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@3 125 glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
nuclear@3 126 glTexParameteri(type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
nuclear@3 127 glTexParameteri(type, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
nuclear@1 128 }
nuclear@1 129
nuclear@1 130 void Texture3D::create(int xsz, int ysz, int zsz, float *data)
nuclear@1 131 {
nuclear@1 132 glBindTexture(type, tex);
nuclear@5 133 glTexImage3D(type, 0, intfmt, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, data);
nuclear@1 134
nuclear@1 135 size[0] = xsz;
nuclear@1 136 size[1] = ysz;
nuclear@1 137 size[2] = zsz;
nuclear@1 138 }
nuclear@3 139
nuclear@3 140 void Texture3D::update(float *data)
nuclear@3 141 {
nuclear@3 142 glBindTexture(type, tex);
nuclear@3 143 glTexSubImage3D(type, 0, 0, 0, 0, size[0], size[1], size[2], GL_RGBA, GL_FLOAT, data);
nuclear@3 144 }