bloboland

view src/texture.cc @ 4:9021a906c5d3

lots of stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 18 Dec 2012 06:13:09 +0200
parents a39c301cdcce
children 2f4406cc341e
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 void Texture::set_filtering(unsigned int min_filter, unsigned int mag_filter)
42 {
43 if(mag_filter == 0) {
44 mag_filter = min_filter;
45 }
47 glBindTexture(type, tex);
48 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, min_filter);
49 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, mag_filter);
50 }
52 void Texture::set_wrapping(unsigned int wrap)
53 {
54 glBindTexture(type, tex);
55 glTexParameteri(type, GL_TEXTURE_WRAP_S, wrap);
56 glTexParameteri(type, GL_TEXTURE_WRAP_T, wrap);
57 glTexParameteri(type, GL_TEXTURE_WRAP_R, wrap);
58 }
60 int Texture::get_size(int idx) const
61 {
62 return idx >= 0 && idx < 3 ? size[idx] : 0;
63 }
65 Texture1D::Texture1D()
66 {
67 type = GL_TEXTURE_1D;
69 glBindTexture(type, tex);
70 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
71 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
72 }
74 void Texture1D::create(int sz, float *data)
75 {
76 glBindTexture(type, tex);
77 glTexImage1D(type, 0, GL_RGBA, sz, 0, GL_RGBA, GL_FLOAT, data);
79 size[0] = sz;
80 }
82 void Texture1D::update(float *data)
83 {
84 glBindTexture(type, tex);
85 glTexSubImage1D(type, 0, 0, size[0], GL_RGBA, GL_FLOAT, data);
86 }
88 Texture2D::Texture2D()
89 {
90 type = GL_TEXTURE_2D;
92 glBindTexture(type, tex);
93 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
94 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
95 }
97 void Texture2D::create(int xsz, int ysz, float *data)
98 {
99 glBindTexture(type, tex);
100 glTexImage2D(type, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_FLOAT, data);
102 size[0] = xsz;
103 size[1] = ysz;
104 }
106 void Texture2D::update(float *data)
107 {
108 glBindTexture(type, tex);
109 glTexSubImage2D(type, 0, 0, 0, size[0], size[1], GL_RGBA, GL_FLOAT, data);
110 }
112 Texture3D::Texture3D()
113 {
114 type = GL_TEXTURE_3D;
116 glBindTexture(type, tex);
117 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
118 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
119 glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
120 glTexParameteri(type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
121 glTexParameteri(type, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
122 }
124 void Texture3D::create(int xsz, int ysz, int zsz, float *data)
125 {
126 glBindTexture(type, tex);
127 glTexImage3D(type, 0, GL_RGBA, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, data);
129 size[0] = xsz;
130 size[1] = ysz;
131 size[2] = zsz;
132 }
134 void Texture3D::update(float *data)
135 {
136 glBindTexture(type, tex);
137 glTexSubImage3D(type, 0, 0, 0, 0, size[0], size[1], size[2], GL_RGBA, GL_FLOAT, data);
138 }