bloboland

annotate src/texture.cc @ 3:a39c301cdcce

terrain raytracing pretty much done
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 16 Dec 2012 14:24:16 +0200
parents cfe68befb7cc
children 9021a906c5d3
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@1 28
nuclear@1 29 size[0] = size[1] = size[2] = 0;
nuclear@1 30
nuclear@1 31 glGenTextures(1, &tex);
nuclear@1 32 }
nuclear@1 33
nuclear@1 34 Texture::~Texture()
nuclear@1 35 {
nuclear@1 36 if(tex) {
nuclear@1 37 glDeleteTextures(1, &tex);
nuclear@1 38 }
nuclear@1 39 }
nuclear@1 40
nuclear@1 41 int Texture::get_size(int idx) const
nuclear@1 42 {
nuclear@1 43 return idx >= 0 && idx < 3 ? size[idx] : 0;
nuclear@1 44 }
nuclear@1 45
nuclear@1 46 Texture2D::Texture2D()
nuclear@1 47 {
nuclear@1 48 type = GL_TEXTURE_2D;
nuclear@1 49
nuclear@1 50 glBindTexture(type, tex);
nuclear@1 51 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@1 52 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@1 53 }
nuclear@1 54
nuclear@1 55 void Texture2D::create(int xsz, int ysz, float *data)
nuclear@1 56 {
nuclear@1 57 glBindTexture(type, tex);
nuclear@1 58 glTexImage2D(type, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_FLOAT, data);
nuclear@1 59
nuclear@1 60 size[0] = xsz;
nuclear@1 61 size[1] = ysz;
nuclear@1 62 }
nuclear@1 63
nuclear@3 64 void Texture2D::update(float *data)
nuclear@3 65 {
nuclear@3 66 glBindTexture(type, tex);
nuclear@3 67 glTexSubImage2D(type, 0, 0, 0, size[0], size[1], GL_RGBA, GL_FLOAT, data);
nuclear@3 68 }
nuclear@3 69
nuclear@1 70 Texture3D::Texture3D()
nuclear@1 71 {
nuclear@1 72 type = GL_TEXTURE_3D;
nuclear@1 73
nuclear@1 74 glBindTexture(type, tex);
nuclear@1 75 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@1 76 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@3 77 glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
nuclear@3 78 glTexParameteri(type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
nuclear@3 79 glTexParameteri(type, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
nuclear@1 80 }
nuclear@1 81
nuclear@1 82 void Texture3D::create(int xsz, int ysz, int zsz, float *data)
nuclear@1 83 {
nuclear@1 84 glBindTexture(type, tex);
nuclear@1 85 glTexImage3D(type, 0, GL_RGBA, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, data);
nuclear@1 86
nuclear@1 87 size[0] = xsz;
nuclear@1 88 size[1] = ysz;
nuclear@1 89 size[2] = zsz;
nuclear@1 90 }
nuclear@3 91
nuclear@3 92 void Texture3D::update(float *data)
nuclear@3 93 {
nuclear@3 94 glBindTexture(type, tex);
nuclear@3 95 glTexSubImage3D(type, 0, 0, 0, 0, size[0], size[1], size[2], GL_RGBA, GL_FLOAT, data);
nuclear@3 96 }