nuclear@1: #include "opengl.h" nuclear@1: #include "texture.h" nuclear@1: nuclear@1: void bind_texture(const Texture *tex, int texunit) nuclear@1: { nuclear@3: static const Texture *cur_tex[8]; nuclear@1: nuclear@3: if(tex == cur_tex[texunit]) { nuclear@1: return; nuclear@1: } nuclear@1: nuclear@1: glActiveTexture(GL_TEXTURE0 + texunit); nuclear@1: if(tex) { nuclear@1: glEnable(tex->type); nuclear@1: glBindTexture(tex->type, tex->tex); nuclear@1: } else { nuclear@3: glDisable(cur_tex[texunit]->type); nuclear@1: } nuclear@1: glActiveTexture(GL_TEXTURE0); nuclear@1: nuclear@3: cur_tex[texunit] = tex; nuclear@1: } nuclear@1: nuclear@1: nuclear@1: Texture::Texture() nuclear@1: { nuclear@1: type = 0; nuclear@5: intfmt = GL_RGBA; nuclear@1: nuclear@1: size[0] = size[1] = size[2] = 0; nuclear@1: nuclear@1: glGenTextures(1, &tex); nuclear@1: } nuclear@1: nuclear@1: Texture::~Texture() nuclear@1: { nuclear@1: if(tex) { nuclear@1: glDeleteTextures(1, &tex); nuclear@1: } nuclear@1: } nuclear@1: nuclear@5: void Texture::set_pixel_format(unsigned int fmt) nuclear@5: { nuclear@5: intfmt = fmt; nuclear@5: } nuclear@5: nuclear@4: void Texture::set_filtering(unsigned int min_filter, unsigned int mag_filter) nuclear@4: { nuclear@4: if(mag_filter == 0) { nuclear@4: mag_filter = min_filter; nuclear@4: } nuclear@4: nuclear@4: glBindTexture(type, tex); nuclear@4: glTexParameteri(type, GL_TEXTURE_MIN_FILTER, min_filter); nuclear@4: glTexParameteri(type, GL_TEXTURE_MAG_FILTER, mag_filter); nuclear@4: } nuclear@4: nuclear@4: void Texture::set_wrapping(unsigned int wrap) nuclear@4: { nuclear@4: glBindTexture(type, tex); nuclear@4: glTexParameteri(type, GL_TEXTURE_WRAP_S, wrap); nuclear@4: glTexParameteri(type, GL_TEXTURE_WRAP_T, wrap); nuclear@4: glTexParameteri(type, GL_TEXTURE_WRAP_R, wrap); nuclear@4: } nuclear@4: nuclear@1: int Texture::get_size(int idx) const nuclear@1: { nuclear@1: return idx >= 0 && idx < 3 ? size[idx] : 0; nuclear@1: } nuclear@1: nuclear@4: Texture1D::Texture1D() nuclear@4: { nuclear@4: type = GL_TEXTURE_1D; nuclear@4: nuclear@4: glBindTexture(type, tex); nuclear@4: glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR); nuclear@4: glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); nuclear@4: } nuclear@4: nuclear@4: void Texture1D::create(int sz, float *data) nuclear@4: { nuclear@4: glBindTexture(type, tex); nuclear@5: glTexImage1D(type, 0, intfmt, sz, 0, GL_RGBA, GL_FLOAT, data); nuclear@4: nuclear@4: size[0] = sz; nuclear@4: } nuclear@4: nuclear@4: void Texture1D::update(float *data) nuclear@4: { nuclear@4: glBindTexture(type, tex); nuclear@4: glTexSubImage1D(type, 0, 0, size[0], GL_RGBA, GL_FLOAT, data); nuclear@4: } nuclear@4: nuclear@1: Texture2D::Texture2D() nuclear@1: { nuclear@1: type = GL_TEXTURE_2D; nuclear@1: nuclear@1: glBindTexture(type, tex); nuclear@1: glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR); nuclear@1: glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); nuclear@1: } nuclear@1: nuclear@1: void Texture2D::create(int xsz, int ysz, float *data) nuclear@1: { nuclear@1: glBindTexture(type, tex); nuclear@5: glTexImage2D(type, 0, intfmt, xsz, ysz, 0, GL_RGBA, GL_FLOAT, data); nuclear@1: nuclear@1: size[0] = xsz; nuclear@1: size[1] = ysz; nuclear@1: } nuclear@1: nuclear@3: void Texture2D::update(float *data) nuclear@3: { nuclear@3: glBindTexture(type, tex); nuclear@3: glTexSubImage2D(type, 0, 0, 0, size[0], size[1], GL_RGBA, GL_FLOAT, data); nuclear@3: } nuclear@3: nuclear@1: Texture3D::Texture3D() nuclear@1: { nuclear@1: type = GL_TEXTURE_3D; nuclear@1: nuclear@1: glBindTexture(type, tex); nuclear@1: glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR); nuclear@1: glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); nuclear@3: glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); nuclear@3: glTexParameteri(type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); nuclear@3: glTexParameteri(type, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); nuclear@1: } nuclear@1: nuclear@1: void Texture3D::create(int xsz, int ysz, int zsz, float *data) nuclear@1: { nuclear@1: glBindTexture(type, tex); nuclear@5: glTexImage3D(type, 0, intfmt, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, data); nuclear@1: nuclear@1: size[0] = xsz; nuclear@1: size[1] = ysz; nuclear@1: size[2] = zsz; nuclear@1: } nuclear@3: nuclear@3: void Texture3D::update(float *data) nuclear@3: { nuclear@3: glBindTexture(type, tex); nuclear@3: glTexSubImage3D(type, 0, 0, 0, 0, size[0], size[1], size[2], GL_RGBA, GL_FLOAT, data); nuclear@3: }