bloboland

view 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 source
1 #include "opengl.h"
2 #include "texture.h"
4 void bind_texture(const Texture *tex, int texunit)
5 {
6 static const Texture *curr_tex[8];
8 if(tex == curr_tex[texunit]) {
9 return;
10 }
12 glActiveTexture(GL_TEXTURE0 + texunit);
13 if(tex) {
14 glEnable(tex->type);
15 glBindTexture(tex->type, tex->tex);
16 } else {
17 glDisable(tex->type);
18 }
19 glActiveTexture(GL_TEXTURE0);
21 curr_tex[texunit] = tex;
22 }
25 Texture::Texture()
26 {
27 type = 0;
29 size[0] = size[1] = size[2] = 0;
31 glGenTextures(1, &tex);
32 }
34 Texture::~Texture()
35 {
36 if(tex) {
37 glDeleteTextures(1, &tex);
38 }
39 }
41 int Texture::get_size(int idx) const
42 {
43 return idx >= 0 && idx < 3 ? size[idx] : 0;
44 }
46 Texture2D::Texture2D()
47 {
48 type = GL_TEXTURE_2D;
50 glBindTexture(type, tex);
51 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
52 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
53 }
55 void Texture2D::create(int xsz, int ysz, float *data)
56 {
57 glBindTexture(type, tex);
58 glTexImage2D(type, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_FLOAT, data);
60 size[0] = xsz;
61 size[1] = ysz;
62 }
64 Texture3D::Texture3D()
65 {
66 type = GL_TEXTURE_3D;
68 glBindTexture(type, tex);
69 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
70 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
71 }
73 void Texture3D::create(int xsz, int ysz, int zsz, float *data)
74 {
75 glBindTexture(type, tex);
76 glTexImage3D(type, 0, GL_RGBA, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, data);
78 size[0] = xsz;
79 size[1] = ysz;
80 size[2] = zsz;
81 }