bloboland

diff src/texture.cc @ 1:cfe68befb7cc

some progress
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 15 Dec 2012 23:43:03 +0200
parents
children a39c301cdcce
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/texture.cc	Sat Dec 15 23:43:03 2012 +0200
     1.3 @@ -0,0 +1,81 @@
     1.4 +#include "opengl.h"
     1.5 +#include "texture.h"
     1.6 +
     1.7 +void bind_texture(const Texture *tex, int texunit)
     1.8 +{
     1.9 +	static const Texture *curr_tex[8];
    1.10 +
    1.11 +	if(tex == curr_tex[texunit]) {
    1.12 +		return;
    1.13 +	}
    1.14 +
    1.15 +	glActiveTexture(GL_TEXTURE0 + texunit);
    1.16 +	if(tex) {
    1.17 +		glEnable(tex->type);
    1.18 +		glBindTexture(tex->type, tex->tex);
    1.19 +	} else {
    1.20 +		glDisable(tex->type);
    1.21 +	}
    1.22 +	glActiveTexture(GL_TEXTURE0);
    1.23 +
    1.24 +	curr_tex[texunit] = tex;
    1.25 +}
    1.26 +
    1.27 +
    1.28 +Texture::Texture()
    1.29 +{
    1.30 +	type = 0;
    1.31 +
    1.32 +	size[0] = size[1] = size[2] = 0;
    1.33 +
    1.34 +	glGenTextures(1, &tex);
    1.35 +}
    1.36 +
    1.37 +Texture::~Texture()
    1.38 +{
    1.39 +	if(tex) {
    1.40 +		glDeleteTextures(1, &tex);
    1.41 +	}
    1.42 +}
    1.43 +
    1.44 +int Texture::get_size(int idx) const
    1.45 +{
    1.46 +	return idx >= 0 && idx < 3 ? size[idx] : 0;
    1.47 +}
    1.48 +
    1.49 +Texture2D::Texture2D()
    1.50 +{
    1.51 +	type = GL_TEXTURE_2D;
    1.52 +
    1.53 +	glBindTexture(type, tex);
    1.54 +	glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.55 +	glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.56 +}
    1.57 +
    1.58 +void Texture2D::create(int xsz, int ysz, float *data)
    1.59 +{
    1.60 +	glBindTexture(type, tex);
    1.61 +	glTexImage2D(type, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_FLOAT, data);
    1.62 +
    1.63 +	size[0] = xsz;
    1.64 +	size[1] = ysz;
    1.65 +}
    1.66 +
    1.67 +Texture3D::Texture3D()
    1.68 +{
    1.69 +	type = GL_TEXTURE_3D;
    1.70 +
    1.71 +	glBindTexture(type, tex);
    1.72 +	glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.73 +	glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.74 +}
    1.75 +
    1.76 +void Texture3D::create(int xsz, int ysz, int zsz, float *data)
    1.77 +{
    1.78 +	glBindTexture(type, tex);
    1.79 +	glTexImage3D(type, 0, GL_RGBA, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, data);
    1.80 +
    1.81 +	size[0] = xsz;
    1.82 +	size[1] = ysz;
    1.83 +	size[2] = zsz;
    1.84 +}