-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTexture.hpp
82 lines (74 loc) · 2.31 KB
/
Texture.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class Texture {
public:
Texture()
: m_texid{0}
{}
Texture(int width, int height)
: m_texid{0}
{ create(width, height); }
void swap(Texture &other) {
std::swap(other.m_texid, m_texid);
std::swap(other.m_width, m_width);
std::swap(other.m_height, m_height);
}
Texture &create(int width, int height) {
m_width = width;
m_height = height;
glGenTextures(1, &m_texid);
glBindTexture(GL_TEXTURE_2D, m_texid);
return *this;
}
// Formats provided here are not meant to be exhaustive of all possible
// format combinations, but rather a small subset which has been tested
// on all target platforms (particularly webgl).
Texture &r32f() {
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, m_width, m_height,
0, GL_RED, GL_FLOAT, 0);
return *this;
}
Texture &rgba32f() {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, m_width, m_height,
0, GL_RGBA, GL_FLOAT, 0);
return *this;
}
Texture &depth32f() {
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, m_width, m_height,
0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
return *this;
}
Texture &nearest() {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
return *this;
}
Texture &linear() {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return *this;
}
Texture &clamp() {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
return *this;
}
void write_rgba32f(const float *pixel, int x, int y, int width, int height) {
glBindTexture(GL_TEXTURE_2D, m_texid);
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
GL_RGBA, GL_FLOAT, pixel);
}
void bind(int n = 0) const {
glActiveTexture(GL_TEXTURE0 + n);
glBindTexture(GL_TEXTURE_2D, m_texid);
}
GLuint texid() const
{ return m_texid; }
bool valid() const
{ return m_texid != 0; }
int width() const
{ return m_width; }
int height() const
{ return m_height; }
private:
GLuint m_texid;
int m_width, m_height;
};