]>
Dogcows Code - chaz/yoink/blob - src/Moof/Texture.cc
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *******************************************************************************/
29 #include <cstdio> // FILE
30 #include <cstring> // strncmp
32 #include <boost/bind.hpp>
34 #include "Dispatch.hh"
36 #include "Exception.hh"
48 * The texture implementation just contains all the information about the image
49 * which is worth having in memory. The image data itself is not worth keeping
50 * in memory if the texture has been loaded to GL, but the name of the resource
51 * is retained so that it can be reloaded if necessary. The implementation is a
52 * library so that multiple texture objects can share the same internal objects
53 * and avoid having duplicate textures loaded to GL.
56 class Texture::Impl
: public Library
<Impl
>
60 * Delete the texture (if it is loaded) from GL.
67 if (mObject
== gObject
)
72 glDeleteTextures(1, &mObject
);
78 * If the GL context was recreated, we need to reload the texture. This may
79 * involve reading it from disk again, but hopefully the OS was smart enough
80 * to cache it if the client has plenty of RAM.
83 void contextRecreated()
85 mObject
= gObject
= 0;
90 * This is a helper method used by some of the texture loading code. It
91 * returns the first power of two which is greater than the input value.
94 static int powerOfTwo(int input
)
108 * Construction is initialization.
111 explicit Impl(const std::string
& name
) :
114 mImage(Texture::getPath(getName())),
118 mMinFilter(GL_NEAREST
),
119 mMagFilter(GL_NEAREST
),
124 // make sure the engine is initialized
125 Engine
& engine
= Engine::getInstance();
126 VideoP video
= engine
.getVideo();
127 ASSERT(video
&& "cannot load textures without a current video context");
129 // we want to know when the GL context is recreated
130 mDispatchHandler
= engine
.addHandler("video.newcontext",
131 boost::bind(&Impl::contextRecreated
, this));
143 * Adapted from some public domain code. This stuff is common enough that
144 * it really should be included in SDL_image... We need this because images
145 * loaded with SDL_image aren't exactly GL-ready right out of the box. This
146 * method makes them ready.
150 static SDL_Surface* prepareImageForGL(SDL_Surface* surface)
152 int w = powerOfTwo(surface->w);
153 int h = powerOfTwo(surface->h);
155 // 2. OpenGL textures make more sense within the coordinate system when
156 // they are "upside down," so let's flip it.
158 flipSurface(surface);
160 // 1. OpenGL images must (generally) have dimensions of a power-of-two.
161 // If this one doesn't, we can at least be more friendly by expanding
162 // the dimensions so that they are, though there will be some empty
163 // space within the range of normal texture coordinates. It's better if
164 // textures are the right size to begin with.
166 SDL_Surface* image = SDL_CreateRGBSurface
171 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
189 Uint32 savedFlags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
190 Uint8 savedAlpha = surface->format->alpha;
191 if (savedFlags & SDL_SRCALPHA)
193 SDL_SetAlpha(surface, 0, 0);
196 SDL_Rect srcArea, destArea;
197 srcArea.x = 0; destArea.x = 0;
198 srcArea.y = 0; destArea.y = h - surface->h;
199 srcArea.w = surface->w;
200 srcArea.h = surface->h;
201 SDL_BlitSurface(surface, &srcArea, image, &destArea);
203 if (savedFlags & SDL_SRCALPHA)
205 SDL_SetAlpha(surface, savedFlags, savedAlpha);
213 * Use SDL_image to load images from file. A surface with the image data is
215 * @return Image data.
220 if (!mImage
.isValid())
222 logWarning
<< "texture not found: " << getName() << std::endl
;
223 throw Exception(ErrorCode::RESOURCE_NOT_FOUND
, getName());
228 mWidth
= mImage
.getWidth();
229 mHeight
= mImage
.getHeight();
230 mMode
= mImage
.getColorMode();
235 * Upload the image to GL so that it will be accessible by a much more
236 * manageable handle and hopefully reside in video memory.
247 //if (!mContext) loadFromFile();
249 glGenTextures(1, &mObject
);
250 glBindTexture(GL_TEXTURE_2D
, mObject
);
269 //SDL_FreeSurface(mContext);
275 * Sets some texture properties such as the filters and external coordinate
281 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, mMinFilter
);
282 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, mMagFilter
);
283 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, mWrapS
);
284 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, mWrapT
);
287 inline void setMinFilter(GLuint filter
)
291 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, mMinFilter
);
294 inline void setMagFilter(GLuint filter
)
298 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, mMagFilter
);
301 inline void setWrapS(GLuint wrap
)
305 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, mWrapS
);
308 inline void setWrapT(GLuint wrap
)
312 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, mWrapT
);
322 if (mObject
!= gObject
)
324 glBindTexture(GL_TEXTURE_2D
, mObject
);
331 unsigned mWidth
; ///< Horizontal dimension of the image.
332 unsigned mHeight
; ///< Vertical dimension.
334 GLuint mMode
; ///< GL_RGB or GL_RGBA.
335 GLuint mMinFilter
; ///< Minifcation filter.
336 GLuint mMagFilter
; ///< Magnification filter.
337 GLuint mWrapS
; ///< Wrapping behavior horizontally.
338 GLuint mWrapT
; ///< Wrapping behavior vertically.
340 GLuint mObject
; ///< GL texture handle.
341 static GLuint gObject
; ///< Global GL texture handle.
343 Dispatch::Handler mDispatchHandler
;
346 GLuint
Texture::Impl::gObject
= 0;
349 Texture::Texture(const std::string
& name
) :
351 mImpl(Texture::Impl::getInstance(name
)) {}
355 * Bind the GL texture for mapping, etc.
358 void Texture::bind() const
366 * Get the texture object, for the curious.
369 GLuint
Texture::getObject() const
372 return mImpl
->mObject
;
376 void Texture::resetBind()
378 glBindTexture(GL_TEXTURE_2D
, 0);
383 unsigned Texture::getWidth() const
386 return mImpl
->mWidth
;
389 unsigned Texture::getHeight() const
392 return mImpl
->mHeight
;
396 void Texture::setMinFilter(GLuint filter
)
399 mImpl
->setMinFilter(filter
);
402 void Texture::setMagFilter(GLuint filter
)
405 mImpl
->setMagFilter(filter
);
408 void Texture::setWrapS(GLuint wrap
)
411 mImpl
->setWrapS(wrap
);
414 void Texture::setWrapT(GLuint wrap
)
417 mImpl
->setWrapT(wrap
);
421 std::string
Texture::getPath(const std::string
& name
)
423 std::string path
= Resource::getPath("textures/" + name
+ ".png");
430 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.060267 seconds and 5 git commands to generate.