]> git.tdb.fi Git - libs/gl.git/blob - source/texture.cpp
Support embedding textures in datafiles
[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 && cur->target!=target)
48                 glDisable(cur->target);
49         if(!cur || cur->target!=target)
50                 glEnable(target);
51         glBindTexture(target, id);
52         TexUnit::current().set_texture(this);
53 }
54
55 void Texture::parameter(GLenum param, int value)
56 {
57         maybe_bind();
58
59         glTexParameteri(target, param, value);
60 }
61
62 void Texture::parameter(GLenum param, float value)
63 {
64         maybe_bind();
65
66         glTexParameterf(target, param, value);
67 }
68
69 Texture::~Texture()
70 {
71         glDeleteTextures(1, &id);
72 }
73
74 void Texture::unbind()
75 {
76         const Texture *cur=TexUnit::current().get_texture();
77         if(!cur)
78                 return;
79
80         glBindTexture(cur->target, 0);
81         glDisable(cur->target);
82         TexUnit::current().set_texture(0);
83 }
84
85 Texture::Texture():
86         target(0)
87 {
88         glGenTextures(1, &id);
89 }
90
91 void Texture::maybe_bind() const
92 {
93         if(TexUnit::current().get_texture()!=this)
94                 bind();
95 }
96
97
98 Texture::Loader::Loader(Texture &t):
99         tex(t)
100 {
101         add("min_filter", &Loader::min_filter);
102         add("mag_filter", &Loader::mag_filter);
103         add("generate_mipmap", &Loader::generate_mipmap);
104 }
105
106 void Texture::Loader::min_filter(TextureFilter f)
107 {
108         tex.set_min_filter(f);
109 }
110
111 void Texture::Loader::mag_filter(TextureFilter f)
112 {
113         tex.set_mag_filter(f);
114 }
115
116 void Texture::Loader::generate_mipmap(bool gm)
117 {
118         tex.parameter(GL_GENERATE_MIPMAP_SGIS, gm);
119 }
120
121 } // namespace GL
122 } // namespace Msp