]> git.tdb.fi Git - libs/gl.git/blob - source/texture.cpp
Add support for detecting extensions
[libs/gl.git] / source / texture.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "except.h"
9 #include "texture.h"
10 #include "texunit.h"
11
12 namespace Msp {
13 namespace GL {
14
15 void Texture::bind() const
16 {
17         if(!target)
18                 throw InvalidState("Attempt to bind a texture without target");
19
20         const Texture *cur=TexUnit::current().get_texture();
21         if(cur && cur->target!=target)
22                 glDisable(cur->target);
23         if(!cur || cur->target!=target)
24                 glEnable(target);
25         glBindTexture(target, id);
26         TexUnit::current().set_texture(this);
27 }
28
29 void Texture::parameter(GLenum param, int value)
30 {
31         maybe_bind();
32
33         glTexParameteri(target, param, value);
34 }
35
36 void Texture::parameter(GLenum param, float value)
37 {
38         maybe_bind();
39
40         glTexParameterf(target, param, value);
41 }
42
43 Texture::~Texture()
44 {
45         glDeleteTextures(1, &id);
46 }
47
48 void Texture::unbind()
49 {
50         const Texture *cur=TexUnit::current().get_texture();
51         if(!cur)
52                 return;
53
54         glBindTexture(cur->target, 0);
55         glDisable(cur->target);
56         TexUnit::current().set_texture(0);
57 }
58
59 Texture::Texture():
60         target(0)
61 {
62         glGenTextures(1, &id);
63 }
64
65 void Texture::maybe_bind() const
66 {
67         if(TexUnit::current().get_texture()!=this)
68                 bind();
69 }
70
71 } // namespace GL
72 } // namespace Msp