]> git.tdb.fi Git - libs/gl.git/blob - source/texture.cpp
Add Texture::bind_to() method
[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 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 istream &operator>>(istream &in, TextureFilter &tf)
18 {
19         string str;
20         in>>str;
21
22         if(str=="NEAREST")
23                 tf=NEAREST;
24         else if(str=="LINEAR")
25                 tf=LINEAR;
26         else if(str=="NEAREST_MIPMAP_NEAREST")
27                 tf=NEAREST_MIPMAP_NEAREST;
28         else if(str=="NEAREST_MIPMAP_LINEAR")
29                 tf=NEAREST_MIPMAP_LINEAR;
30         else if(str=="LINEAR_MIPMAP_NEAREST")
31                 tf=LINEAR_MIPMAP_NEAREST;
32         else if(str=="LINEAR_MIPMAP_LINEAR")
33                 tf=LINEAR_MIPMAP_LINEAR;
34         else
35                 in.setstate(ios_base::failbit);
36
37         return in;
38 }
39
40
41 void Texture::bind() const
42 {
43         if(!target)
44                 throw InvalidState("Attempt to bind a texture without target");
45
46         const Texture *cur=TexUnit::current().get_texture();
47         if(cur!=this)
48         {
49                 if(cur && cur->target!=target)
50                         glDisable(cur->target);
51                 if(!cur || cur->target!=target)
52                         glEnable(target);
53                 glBindTexture(target, id);
54                 TexUnit::current().set_texture(this);
55         }
56 }
57
58 void Texture::bind_to(unsigned i) const
59 {
60         TexUnit::activate(i);
61         bind();
62 }
63
64 void Texture::parameter(GLenum param, int value)
65 {
66         maybe_bind();
67
68         glTexParameteri(target, param, value);
69 }
70
71 void Texture::parameter(GLenum param, float value)
72 {
73         maybe_bind();
74
75         glTexParameterf(target, param, value);
76 }
77
78 Texture::~Texture()
79 {
80         glDeleteTextures(1, &id);
81 }
82
83 void Texture::unbind()
84 {
85         const Texture *cur=TexUnit::current().get_texture();
86         if(!cur)
87                 return;
88
89         glBindTexture(cur->target, 0);
90         glDisable(cur->target);
91         TexUnit::current().set_texture(0);
92 }
93
94 Texture::Texture():
95         target(0)
96 {
97         glGenTextures(1, &id);
98 }
99
100 void Texture::maybe_bind() const
101 {
102         if(TexUnit::current().get_texture()!=this)
103                 bind();
104 }
105
106
107 Texture::Loader::Loader(Texture &t):
108         tex(t)
109 {
110         add("min_filter", &Loader::min_filter);
111         add("mag_filter", &Loader::mag_filter);
112         add("generate_mipmap", &Loader::generate_mipmap);
113 }
114
115 void Texture::Loader::min_filter(TextureFilter f)
116 {
117         tex.set_min_filter(f);
118 }
119
120 void Texture::Loader::mag_filter(TextureFilter f)
121 {
122         tex.set_mag_filter(f);
123 }
124
125 void Texture::Loader::generate_mipmap(bool gm)
126 {
127         tex.parameter(GL_GENERATE_MIPMAP_SGIS, gm);
128 }
129
130 } // namespace GL
131 } // namespace Msp