texture.cpp

Go to the documentation of this file.
00001 /*
00002  * texture.cpp
00003  *
00004  * Copyright (C) 2009  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Texture creation.  See texture.h
00032  */
00033 
00034 // includes --------------------------------------------------------------------
00035 #include "texture.h"                    // always include our own header first!
00036 
00037 #include "util/file.h"
00038 
00039 
00040 namespace glut {
00041 
00042 
00043 
00044 ////////////////////////////////////////////////////////////////////////////////
00045 //
00046 //      static helper methods
00047 //
00048 ////////////////////////////////////////////////////////////////////////////////
00049 
00050 static GLint
00051 getGlutInternalFormat
00052 (
00053 IN const media::image_t& image
00054 )
00055 {
00056         return image.bytes_per_pixel;
00057 }
00058 
00059 
00060 
00061 static GLint
00062 getGlutFormatFromColorFormat
00063 (
00064 IN const media::image_t& image
00065 )
00066 {
00067         switch (image.color_format) {
00068         case media::eColorFormat_Grayscale:     return GL_LUMINANCE;
00069         case media::eColorFormat_GrayAlpha:     return GL_LUMINANCE_ALPHA;
00070         case media::eColorFormat_RGB:           return GL_RGB;
00071         case media::eColorFormat_RGBA:          return GL_RGBA;
00072         default:
00073                 ASSERT_THROW(false, "unknown color format: "
00074                     << image.color_format);
00075         }
00076 
00077         // never get here!
00078         return 0;
00079 }
00080 
00081 
00082 
00083 static int
00084 createOpenGLTexture
00085 (
00086 IN const media::image_t& image
00087 )
00088 {
00089         ASSERT_THROW(image.isValid(), "Invalid image?");
00090 
00091         GLuint id = 0;
00092         glGenTextures(1, &id);
00093         DPRINTF("Generated texture %d...", id);
00094 
00095         glBindTexture(GL_TEXTURE_2D, id);
00096 
00097         // use linear interpolation for small/big textures
00098         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00099         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00100 
00101         // texture should wrap
00102         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00103         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00104 
00105         GLint level = 0;
00106         GLint internalFormat = getGlutInternalFormat(image);
00107         GLint format = getGlutFormatFromColorFormat(image);
00108         GLint border = 0;
00109         DPRINTF("internalFormat = %d", internalFormat);
00110         DPRINTF("format = %d", format);
00111 
00112         GLsizei width = image.width;
00113         GLsizei height = image.height;
00114         DPRINTF("  texture width:  %d", width);
00115         DPRINTF("  texture height: %d", height);
00116         glTexImage2D(GL_TEXTURE_2D, level, internalFormat, width,
00117             height, border, format, GL_UNSIGNED_BYTE, image.data);
00118 
00119         return id;
00120 }
00121 
00122 
00123 
00124 class CreateTextureTask : public Task {
00125 public:
00126         CreateTextureTask(IN const media::image_t * image) {
00127                         ASSERT(image, "null");
00128                         m_image = image;
00129                 }
00130         ~CreateTextureTask(void) throw() { }
00131 
00132         // public class methods ------------------------------------------------
00133         int getTextureId(void) const throw() { return m_textureId; }
00134 
00135         // glut::Task class interface methods ----------------------------------
00136         void doTask(void) {
00137                         ASSERT(m_image, "null");
00138                         m_textureId = createOpenGLTexture(*m_image);
00139                 }
00140 
00141 private:
00142         CreateTextureTask(void) throw() : m_image(NULL), m_textureId(0) { }
00143 
00144         // private member data -------------------------------------------------
00145         const media::image_t *  m_image;
00146         int                     m_textureId;
00147 };
00148 
00149 
00150 
00151 ////////////////////////////////////////////////////////////////////////////////
00152 //
00153 //      public API
00154 //
00155 ////////////////////////////////////////////////////////////////////////////////
00156 
00157 int
00158 createTextureFromImage
00159 (
00160 IN const media::image_t& image
00161 )
00162 {
00163         ASSERT_THROW(image.isValid(), "Invalid image?");
00164         smart_ptr<CreateTextureTask> task = new CreateTextureTask(&image);
00165         ASSERT(task, "out of memory");
00166 
00167         requestTask(task);
00168 
00169         return task->getTextureId();
00170 }
00171 
00172 
00173 
00174 };      // glut namespace
00175