bloboland

view src/texture.cc @ 5:2f4406cc341e

meh
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 19 Dec 2012 02:37:20 +0200
parents 9021a906c5d3
children
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;
28 intfmt = GL_RGBA;
30 size[0] = size[1] = size[2] = 0;
32 glGenTextures(1, &tex);
33 }
35 Texture::~Texture()
36 {
37 if(tex) {
38 glDeleteTextures(1, &tex);
39 }
40 }
42 void Texture::set_pixel_format(unsigned int fmt)
43 {
44 intfmt = fmt;
45 }
47 void Texture::set_filtering(unsigned int min_filter, unsigned int mag_filter)
48 {
49 if(mag_filter == 0) {
50 mag_filter = min_filter;
51 }
53 glBindTexture(type, tex);
54 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, min_filter);
55 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, mag_filter);
56 }
58 void Texture::set_wrapping(unsigned int wrap)
59 {
60 glBindTexture(type, tex);
61 glTexParameteri(type, GL_TEXTURE_WRAP_S, wrap);
62 glTexParameteri(type, GL_TEXTURE_WRAP_T, wrap);
63 glTexParameteri(type, GL_TEXTURE_WRAP_R, wrap);
64 }
66 int Texture::get_size(int idx) const
67 {
68 return idx >= 0 && idx < 3 ? size[idx] : 0;
69 }
71 Texture1D::Texture1D()
72 {
73 type = GL_TEXTURE_1D;
75 glBindTexture(type, tex);
76 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
77 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
78 }
80 void Texture1D::create(int sz, float *data)
81 {
82 glBindTexture(type, tex);
83 glTexImage1D(type, 0, intfmt, sz, 0, GL_RGBA, GL_FLOAT, data);
85 size[0] = sz;
86 }
88 void Texture1D::update(float *data)
89 {
90 glBindTexture(type, tex);
91 glTexSubImage1D(type, 0, 0, size[0], GL_RGBA, GL_FLOAT, data);
92 }
94 Texture2D::Texture2D()
95 {
96 type = GL_TEXTURE_2D;
98 glBindTexture(type, tex);
99 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
100 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
101 }
103 void Texture2D::create(int xsz, int ysz, float *data)
104 {
105 glBindTexture(type, tex);
106 glTexImage2D(type, 0, intfmt, xsz, ysz, 0, GL_RGBA, GL_FLOAT, data);
108 size[0] = xsz;
109 size[1] = ysz;
110 }
112 void Texture2D::update(float *data)
113 {
114 glBindTexture(type, tex);
115 glTexSubImage2D(type, 0, 0, 0, size[0], size[1], GL_RGBA, GL_FLOAT, data);
116 }
118 Texture3D::Texture3D()
119 {
120 type = GL_TEXTURE_3D;
122 glBindTexture(type, tex);
123 glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
124 glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
125 glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
126 glTexParameteri(type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
127 glTexParameteri(type, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
128 }
130 void Texture3D::create(int xsz, int ysz, int zsz, float *data)
131 {
132 glBindTexture(type, tex);
133 glTexImage3D(type, 0, intfmt, xsz, ysz, zsz, 0, GL_RGBA, GL_FLOAT, data);
135 size[0] = xsz;
136 size[1] = ysz;
137 size[2] = zsz;
138 }
140 void Texture3D::update(float *data)
141 {
142 glBindTexture(type, tex);
143 glTexSubImage3D(type, 0, 0, 0, 0, size[0], size[1], size[2], GL_RGBA, GL_FLOAT, data);
144 }