2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
12 #include <cstdio> // FILE
13 #include <cstring> // strncmp
16 #include <boost/algorithm/string.hpp>
17 #include <boost/bind.hpp>
19 #include "dispatcher.hh"
32 * The texture implementation just contains all the information about the
33 * image which is worth having in memory. The image data itself is not
34 * worth keeping in memory if the texture has been loaded to GL, but the
35 * name of the resource is retained so that it can be reloaded if
36 * necessary. The implementation is a manager so that multiple texture
37 * objects can share the same internal objects and avoid having duplicate
38 * textures loaded to GL.
41 class texture::impl
: public manager
<impl
>
45 * Delete the texture (if it is loaded) from GL.
52 if (mObject
== gObject
)
57 glDeleteTextures(1, &mObject
);
63 * If the GL context was recreated, we need to reload the texture.
64 * This may involve reading it from disk again, but hopefully the OS
65 * was smart enough to cache it if the client has plenty of RAM.
68 void context_recreated()
70 mObject
= gObject
= 0;
75 * This is a helper method used by some of the texture loading code.
76 * It returns the first power of two which is greater than the input
80 static int power_of_two(int input
)
92 static void bind_script_constants(script
& script
)
94 script::slot g
= script
.globals();
96 g
.set_field("CLAMP", GL_CLAMP
);
97 g
.set_field("REPEAT", GL_REPEAT
);
98 g
.set_field("LINEAR", GL_LINEAR
);
99 g
.set_field("NEAREST", GL_NEAREST
);
100 g
.set_field("LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR
);
101 g
.set_field("LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST
);
102 g
.set_field("NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR
);
103 g
.set_field("NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST
);
109 * Construction is initialization.
112 mMinFilter(GL_NEAREST
),
113 mMagFilter(GL_NEAREST
),
120 // make sure we have a video context
121 video
* video
= video::current();
122 ASSERT(video
&& "should have a video context set");
124 // we want to know when the GL context is recreated
125 dispatcher
& dispatcher
= dispatcher::global();
126 mNewContextDispatch
= dispatcher
.add_target("video.newcontext",
127 boost::bind(&impl::context_recreated
, this));
136 void init(const std::string
& name
)
138 std::string
path(name
);
140 resource::find(path
);
142 mImage
= image::alloc(path
);
143 if (!mImage
->is_valid())
145 throw std::runtime_error("texture not found: " + name
);
152 bind_script_constants(script
);
155 if (script
.do_string(mImage
->comment()) != script::success
)
163 log_info("loading tiles from texture", path
);
165 script::slot globals
= script
.globals();
166 globals
.get(mTilesS
, "tiles_s");
167 globals
.get(mTilesT
, "tiles_t");
168 globals
.get(mMinFilter
, "min_filter");
169 globals
.get(mMagFilter
, "mag_filter");
170 globals
.get(mWrapS
, "wrap_s");
171 globals
.get(mWrapT
, "wrap_t");
177 * Upload the image to GL so that it will be accessible by a much more
178 * manageable handle and hopefully reside in video memory.
188 glGenTextures(1, &mObject
);
189 glBindTexture(GL_TEXTURE_2D
, mObject
);
211 * Sets some texture properties such as the filters and external
212 * coordinate behavior.
214 void set_properties()
216 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, mMinFilter
);
217 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, mMagFilter
);
218 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, mWrapS
);
219 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, mWrapT
);
222 void min_filter(GLuint filter
)
226 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, mMinFilter
);
229 void mag_filter(GLuint filter
)
233 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, mMagFilter
);
236 void wrap_s(GLuint wrap
)
240 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, mWrapS
);
243 void wrap_t(GLuint wrap
)
247 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, mWrapT
);
257 if (mObject
!= gObject
)
259 glBindTexture(GL_TEXTURE_2D
, mObject
);
265 bool tile_coordinates(int index
, scalar coords
[8]) const
267 // make sure the index represents a real tile
268 if (index
< 0 && index
>= mTilesS
* mTilesT
) return false;
270 scalar w
= 1.0 / scalar(mTilesS
);
271 scalar h
= 1.0 / scalar(mTilesT
);
273 coords
[0] = scalar(index
% mTilesS
) * w
;
274 coords
[1] = (scalar(mTilesT
- 1) - scalar(index
/ mTilesS
)) * h
;
275 coords
[2] = coords
[0] + w
;
276 coords
[3] = coords
[1];
277 coords
[4] = coords
[2];
278 coords
[5] = coords
[1] + h
;
279 coords
[6] = coords
[0];
280 coords
[7] = coords
[5];
287 GLuint mMinFilter
; ///< Minification filter.
288 GLuint mMagFilter
; ///< Magnification filter.
289 GLuint mWrapS
; ///< Wrapping behavior horizontally.
290 GLuint mWrapT
; ///< Wrapping behavior vertically.
294 GLuint mObject
; ///< GL texture handle.
295 static GLuint gObject
; ///< Global GL texture handle.
297 dispatcher::handle mNewContextDispatch
;
300 GLuint
texture::impl::gObject
= 0;
303 texture::texture(const std::string
& name
) : // FIXME: this is really weird
306 impl_(texture::impl::instance(name
)) {}
310 * Bind the GL texture for mapping, etc.
313 void texture::bind() const
321 * Get the texture object, for the curious.
324 GLuint
texture::object() const
327 return impl_
->mObject
;
331 void texture::reset_binding()
333 glBindTexture(GL_TEXTURE_2D
, 0);
338 void texture::min_filter(GLuint filter
)
341 impl_
->min_filter(filter
);
344 void texture::mag_filter(GLuint filter
)
347 impl_
->mag_filter(filter
);
350 void texture::wrap_s(GLuint wrap
)
356 void texture::wrap_t(GLuint wrap
)
363 bool texture::tile_coordinates(int index
, scalar coords
[8]) const
366 return impl_
->tile_coordinates(index
, coords
);
369 bool texture::tile_coordinates(int index
, scalar coords
[8],
370 orientation orientation
) const
372 if (tile_coordinates(index
, coords
))
374 if (orientation
& flip
)
376 // this looks kinda weird, but it's just swapping in a way that
377 // doesn't require an intermediate variable
378 coords
[1] = coords
[5];
379 coords
[5] = coords
[3];
380 coords
[3] = coords
[7];
381 coords
[7] = coords
[5];
383 if (orientation
& reverse
)
385 coords
[0] = coords
[2];
386 coords
[2] = coords
[6];
387 coords
[4] = coords
[6];
388 coords
[6] = coords
[0];