bloboland

view 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
line source
1 #include "opengl.h"
2 #include "texture.h"
4 void bind_texture(const Texture *tex, int texunit)
5 {
6 static const Texture *cur_tex[8];
8 if(tex == cur_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(cur_tex[texunit]->type);
18 }
19 glActiveTexture(GL_TEXTURE0);
21 cur_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 void Texture2D::update(float *data)
65 {
66 glBindTexture(type, tex);
67 glTexSubImage2D(type, 0, 0, 0, size[0], size[1], GL_RGBA, GL_FLOAT, data);
68 }
70 Texture3D::Texture3D()
71 {
72 type = GL_TEXTURE_3D;
74 glBindTexture(type, tex);
75 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
76 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
77 glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
78 glTexParameteri(type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
79 glTexParameteri(type, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
80 }
82 void Texture3D::create(int xsz, int ysz, int zsz, float *data)
83 {
84 glBindTexture(type, tex);
85 glTexImage3D(type, 0, GL_RGBA, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, data);
87 size[0] = xsz;
88 size[1] = ysz;
89 size[2] = zsz;
90 }
92 void Texture3D::update(float *data)
93 {
94 glBindTexture(type, tex);
95 glTexSubImage3D(type, 0, 0, 0, 0, size[0], size[1], size[2], GL_RGBA, GL_FLOAT, data);
96 }