]> git.tdb.fi Git - libs/gl.git/blob - source/texture.cpp
Style update: add spaces around assignment operators
[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 void Texture::unbind_from(unsigned i)
95 {
96         TexUnit::activate(i);
97         unbind();
98 }
99
100 Texture::Texture():
101         target(0)
102 {
103         glGenTextures(1, &id);
104 }
105
106 void Texture::maybe_bind() const
107 {
108         if(TexUnit::current().get_texture()!=this)
109                 bind();
110 }
111
112
113 Texture::Loader::Loader(Texture &t):
114         DataFile::ObjectLoader<Texture>(t)
115 {
116         add("min_filter", &Loader::min_filter);
117         add("mag_filter", &Loader::mag_filter);
118         add("generate_mipmap", &Loader::generate_mipmap);
119 }
120
121 void Texture::Loader::min_filter(TextureFilter f)
122 {
123         obj.set_min_filter(f);
124 }
125
126 void Texture::Loader::mag_filter(TextureFilter f)
127 {
128         obj.set_mag_filter(f);
129 }
130
131 void Texture::Loader::generate_mipmap(bool gm)
132 {
133         obj.parameter(GL_GENERATE_MIPMAP_SGIS, gm);
134 }
135
136 } // namespace GL
137 } // namespace Msp