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@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@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@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@1: glTexImage2D(type, 0, GL_RGBA, 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@1: glTexImage3D(type, 0, GL_RGBA, 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: }