]> git.tdb.fi Git - libs/gl.git/blob - source/texture.cpp
Add vertex arrays and buffers
[libs/gl.git] / source / texture.cpp
1 #include <msp/core/error.h>
2 #include "texture.h"
3 #include "texunit.h"
4
5 namespace Msp {
6 namespace GL {
7
8 void Texture::bind() const
9 {
10         if(!target)
11                 throw InvalidState("Attempt to bind a texture without target");
12
13         const Texture *cur=TexUnit::current().get_texture();
14         if(cur && cur->target!=target)
15                 glDisable(cur->target);
16         if(!cur || cur->target!=target)
17                 glEnable(target);
18         glBindTexture(target, id);
19         TexUnit::current().set_texture(this);
20 }
21
22 void Texture::parameter(GLenum param, int value)
23 {
24         maybe_bind();
25
26         glTexParameteri(target, param, value);
27 }
28
29 void Texture::parameter(GLenum param, float value)
30 {
31         maybe_bind();
32
33         glTexParameterf(target, param, value);
34 }
35
36 Texture::~Texture()
37 {
38         glDeleteTextures(1, &id);
39 }
40
41 void Texture::unbind()
42 {
43         const Texture *cur=TexUnit::current().get_texture();
44         if(!cur)
45                 return;
46
47         glBindTexture(cur->target, 0);
48         glDisable(cur->target);
49         TexUnit::current().set_texture(0);
50 }
51
52 Texture::Texture():
53         target(0)
54 {
55         glGenTextures(1, &id);
56 }
57
58 void Texture::maybe_bind() const
59 {
60         if(TexUnit::current().get_texture()!=this)
61                 bind();
62 }
63
64 } // namespace GL
65 } // namespace Msp