Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

GlTextureName.cpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory
00004 // Project administrator: Michael D. McCool
00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa,
00006 //          Michael D. McCool
00007 // 
00008 // This software is provided 'as-is', without any express or implied
00009 // warranty. In no event will the authors be held liable for any damages
00010 // arising from the use of this software.
00011 // 
00012 // Permission is granted to anyone to use this software for any purpose,
00013 // including commercial applications, and to alter it and redistribute it
00014 // freely, subject to the following restrictions:
00015 // 
00016 // 1. The origin of this software must not be misrepresented; you must
00017 // not claim that you wrote the original software. If you use this
00018 // software in a product, an acknowledgment in the product documentation
00019 // would be appreciated but is not required.
00020 // 
00021 // 2. Altered source versions must be plainly marked as such, and must
00022 // not be misrepresented as being the original software.
00023 // 
00024 // 3. This notice may not be removed or altered from any source
00025 // distribution.
00027 #include "GlTextureName.hpp"
00028 
00029 namespace shgl {
00030 
00031 using namespace SH;
00032 
00033 GlTextureName::GlTextureName(GLenum target)
00034   : m_target(target),
00035     m_params(0, SH::ShTextureTraits::SH_FILTER_NONE,
00036              SH::ShTextureTraits::SH_WRAP_CLAMP, SH::ShTextureTraits::SH_CLAMPED),
00037     m_managed(true)
00038 {
00039   glGenTextures(1, &m_name);
00040   m_names.push_back(this);
00041 }
00042 
00043 GlTextureName::GlTextureName(GLenum target, GLuint name)
00044   : m_target(target),
00045     m_name(name),
00046     m_params(0, SH::ShTextureTraits::SH_FILTER_NONE,
00047              SH::ShTextureTraits::SH_WRAP_CLAMP, SH::ShTextureTraits::SH_CLAMPED),
00048     m_managed(false)
00049 {
00050   m_names.push_back(this);
00051 }
00052 
00053 GlTextureName::~GlTextureName()
00054 {
00055   m_names.erase(std::remove(m_names.begin(), m_names.end(), this));
00056   if (m_managed) {
00057     glDeleteTextures(1, &m_name);
00058   }
00059 }
00060 
00061 void GlTextureName::addStorage(SH::ShStorage* storage)
00062 {
00063   m_storages.push_back(storage);
00064 }
00065 
00066 void GlTextureName::removeStorage(SH::ShStorage* storage)
00067 {
00068   m_storages.erase(std::remove(m_storages.begin(), m_storages.end(), storage));
00069 }
00070 
00071 void GlTextureName::params(const SH::ShTextureTraits& params)
00072 {
00073   Binding binding(this);
00074 
00075   if (params.interpolation() == 0) {
00076     SH_GL_CHECK_ERROR(glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
00077     SH_GL_CHECK_ERROR(glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
00078   } else if (params.interpolation() == 1) {
00079     SH_GL_CHECK_ERROR(glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
00080     SH_GL_CHECK_ERROR(glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
00081   }
00082   if (params.wrapping() == SH::ShTextureTraits::SH_WRAP_CLAMP) {
00083     SH_GL_CHECK_ERROR(glTexParameteri(m_target, GL_TEXTURE_WRAP_S, GL_CLAMP));
00084     SH_GL_CHECK_ERROR(glTexParameteri(m_target, GL_TEXTURE_WRAP_T, GL_CLAMP));
00085     SH_GL_CHECK_ERROR(glTexParameteri(m_target, GL_TEXTURE_WRAP_R, GL_CLAMP));
00086   } else if (params.wrapping() == SH::ShTextureTraits::SH_WRAP_CLAMP_TO_EDGE) {
00087     SH_GL_CHECK_ERROR(glTexParameteri(m_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
00088     SH_GL_CHECK_ERROR(glTexParameteri(m_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
00089     SH_GL_CHECK_ERROR(glTexParameteri(m_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE));
00090   } else if (params.wrapping() == SH::ShTextureTraits::SH_WRAP_REPEAT) {
00091     SH_GL_CHECK_ERROR(glTexParameteri(m_target, GL_TEXTURE_WRAP_S, GL_REPEAT));
00092     SH_GL_CHECK_ERROR(glTexParameteri(m_target, GL_TEXTURE_WRAP_T, GL_REPEAT));
00093     SH_GL_CHECK_ERROR(glTexParameteri(m_target, GL_TEXTURE_WRAP_R, GL_REPEAT));
00094   }
00095   // TODO: SH_FILTER (Mipmapping)
00096   
00097   m_params = params;
00098 }
00099 
00100 GlTextureName::NameList GlTextureName::m_names = GlTextureName::NameList();
00101 
00102 GlTextureName::Binding::Binding(const ShPointer<const GlTextureName>& name)
00103 {
00104   target = name->target();
00105   
00106   switch(target) {
00107   case GL_TEXTURE_1D:
00108     SH_GL_CHECK_ERROR(glGetIntegerv(GL_TEXTURE_BINDING_1D, &last));
00109     break;
00110   case GL_TEXTURE_2D:
00111     SH_GL_CHECK_ERROR(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last));
00112     break;
00113   case GL_TEXTURE_CUBE_MAP:
00114     SH_GL_CHECK_ERROR(glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP, &last));
00115     break;
00116   case GL_TEXTURE_RECTANGLE_NV:
00117     SH_GL_CHECK_ERROR(glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_NV, &last));
00118     break;
00119   case GL_TEXTURE_3D:
00120     SH_GL_CHECK_ERROR(glGetIntegerv(GL_TEXTURE_BINDING_3D, &last));
00121     break;
00122   default:
00123     SH_DEBUG_WARN("Texture target not handled by GL backend");
00124     break;
00125   }
00126     
00127   SH_GL_CHECK_ERROR(glBindTexture(target, name->value()));
00128 }
00129 
00130 GlTextureName::Binding::~Binding()
00131 {
00132   SH_GL_CHECK_ERROR(glBindTexture(target, last));
00133 }
00134 
00135 
00136 }

Generated on Mon Jan 24 18:36:29 2005 for Sh by  doxygen 1.4.1