OgreShaderExTextureAtlasSampler.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004 (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org
00006 
00007 Copyright (c) 2000-2012 Torus Knot Software Ltd
00008 Permission is hereby granted, free of charge, to any person obtaining a copy
00009 of this software and associated documentation files (the "Software"), to deal
00010 in the Software without restriction, including without limitation the rights
00011 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012 copies of the Software, and to permit persons to whom the Software is
00013 furnished to do so, subject to the following conditions:
00014 
00015 The above copyright notice and this permission notice shall be included in
00016 all copies or substantial portions of the Software.
00017 
00018 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024 THE SOFTWARE.
00025 -----------------------------------------------------------------------------
00026 */
00027 #ifndef _ShaderSGXAtlasTexture_
00028 #define _ShaderSGXAtlasTexture_
00029 
00030 #include "OgreShaderPrerequisites.h"
00031 #ifdef RTSHADER_SYSTEM_BUILD_EXT_SHADERS
00032 #include "OgreShaderSubRenderState.h"
00033 #include "OgreShaderParameter.h"
00034 
00035 #define TAS_MAX_TEXTURES 4
00036 namespace Ogre {
00037 namespace RTShader {
00038 
00045 struct _OgreRTSSExport TextureAtlasRecord
00046 {
00047     TextureAtlasRecord(const String & texOriginalName, const String & texAtlasName, 
00048                        const float texPosU, const float texPosV, const float texWidth, const float texHeight,
00049                        const size_t texIndexInAtlas) :
00050                   posU(texPosU)
00051                 , posV(texPosV)
00052                 , width(texWidth)
00053                 , height(texHeight)
00054                 , originalTextureName(texOriginalName)
00055                 , atlasTextureName(texAtlasName)
00056                 , indexInAtlas(texIndexInAtlas)
00057     { }
00058 
00059     float posU;
00060     float posV;
00061     float width;
00062     float height;
00063     String originalTextureName;
00064     String atlasTextureName;
00065     size_t indexInAtlas;
00066 };
00067 
00068 typedef vector<TextureAtlasRecord>::type TextureAtlasTable;
00069 typedef SharedPtr<TextureAtlasTable> TextureAtlasTablePtr;
00070 typedef map<String, TextureAtlasTablePtr>::type TextureAtlasMap;
00071 
00072 
00073 
00130 class _OgreRTSSExport TextureAtlasSampler : public SubRenderState
00131 {
00132 public:
00133 
00134 // Interface.
00135 public:
00136 
00138     TextureAtlasSampler();
00139 
00143     virtual const String& getType() const;
00144 
00148     virtual int getExecutionOrder() const;
00149 
00153     virtual void copyFrom(const SubRenderState& rhs);
00154 
00158     virtual void updateGpuProgramsParams(Renderable* rend, Pass* pass,  const AutoParamDataSource* source,  const LightList* pLightList);
00159 
00163     virtual bool preAddToRenderState(const RenderState* renderState, Pass* srcPass, Pass* dstPass);
00164 
00165     static String Type;
00166 
00167 // Protected methods
00168 protected:  
00169     virtual bool            resolveParameters       (ProgramSet* programSet);   
00170     virtual bool            resolveDependencies     (ProgramSet* programSet);
00171     virtual bool            addFunctionInvocations  (ProgramSet* programSet);
00172 
00176     const char* getAdressingFunctionName(TextureUnitState::TextureAddressingMode mode);
00177 
00178 
00179 // Attributes.
00180 protected:
00181     ParameterPtr mVSInpTextureTableIndex; // The index of the information on the texture in the table
00182     
00183     TextureUnitState::UVWAddressingMode mTextureAddressings[TAS_MAX_TEXTURES]; // The addressing mode for each texture
00184     ParameterPtr mVSOutTextureDatas[TAS_MAX_TEXTURES]; // The position and size of the texture in the atlas 
00185     ParameterPtr mPSInpTextureDatas[TAS_MAX_TEXTURES]; // The position and size of the texture in the atlas
00186     UniformParameterPtr mPSTextureSizes[TAS_MAX_TEXTURES]; //A parameter carrying the sizes of the atlas textures
00187     UniformParameterPtr mVSTextureTable[TAS_MAX_TEXTURES]; // The table containing information on the textures in the atlas
00188 
00189     //The position of the texture coordinates containing the index information 
00190     ushort mAtlasTexcoordPos; 
00191     //The texture atlas table data
00192     TextureAtlasTablePtr mAtlasTableDatas[TAS_MAX_TEXTURES];
00193     //For each texture unit in the pass tells if it uses atlas texture
00194     bool mIsAtlasTextureUnits[TAS_MAX_TEXTURES];
00195     //Tells if the data in mAtlasTableData has been uploaded to the corresponding mVSTextureTable parameter
00196     bool mIsTableDataUpdated;
00197     //Tells whether border issue handling uses auto adjust polling position.
00198     bool mAutoAdjustPollPosition;
00199 };
00200 
00201 
00202 
00207 class _OgreRTSSExport TextureAtlasSamplerFactory : public SubRenderStateFactory, public Singleton<TextureAtlasSamplerFactory>
00208 {
00209 public:
00210     enum IndexPositionMode
00211     {
00212         ipmRelative,
00213         ipmAbsolute
00214     };
00215 
00216     struct TextureAtlasAttib
00217     {
00218         TextureAtlasAttib(IndexPositionMode _posMode = ipmRelative, ushort _posOffset = 1,
00219             bool _autoBorderAdjust = true) : positionMode(_posMode), positionOffset(_posOffset),
00220              autoBorderAdjust(_autoBorderAdjust) {}
00221 
00222         IndexPositionMode positionMode;
00223         ushort positionOffset;
00224         bool autoBorderAdjust;
00225     };
00226 
00227 public:
00228 
00229     //TextureAtlasSamplerFactory c_tor
00230     TextureAtlasSamplerFactory();
00231 
00232     //Singleton implementation
00233     static TextureAtlasSamplerFactory* getSingletonPtr(void);
00234     static TextureAtlasSamplerFactory& getSingleton(void);
00235     
00236 
00237 
00241     virtual const String& getType() const;
00242 
00246     virtual SubRenderState* createInstance(ScriptCompiler* compiler, PropertyAbstractNode* prop, Pass* pass, SGScriptTranslator* translator);
00247 
00251     virtual void writeInstance(MaterialSerializer* ser, SubRenderState* subRenderState, Pass* srcPass, Pass* dstPass);
00252     
00271     bool addTexutreAtlasDefinition( const Ogre::String& filename, TextureAtlasTablePtr textureAtlasTable = TextureAtlasTablePtr());
00272     bool addTexutreAtlasDefinition( DataStreamPtr stream, TextureAtlasTablePtr textureAtlasTable = TextureAtlasTablePtr());
00273 
00282     void setTextureAtlasTable(const String& textureName, const TextureAtlasTablePtr& atlasData, bool autoBorderAdjust = true);
00283     
00288     void removeTextureAtlasTable(const String& textureName);
00289 
00293     void removeAllTextureAtlasTables();
00294 
00299     const TextureAtlasTablePtr& getTextureAtlasTable(const String& textureName) const;
00300 
00311     void setDefaultAtlasingAttributes(IndexPositionMode mode, ushort offset, bool autoAdjustBorders);
00312 
00317     const TextureAtlasAttib& getDefaultAtlasingAttributes() const;
00318 
00330     void setMaterialAtlasingAttributes(Ogre::Material* material, 
00331         IndexPositionMode mode, ushort offset, bool autoAdjustBorders);
00332 
00333 
00339     bool hasMaterialAtlasingAttributes(Ogre::Material* material, TextureAtlasAttib* attrib = NULL) const;
00340     
00341 protected:
00342 
00343     
00344 
00348     virtual SubRenderState* createInstanceImpl();
00349 
00350 private:
00351 
00352     //Holds a mapping of texture names and the atlas table information associated with them
00353     TextureAtlasMap mAtlases;
00354 
00355     TextureAtlasAttib mDefaultAtlasAttrib;
00356 };
00357 
00358 _OgreRTSSExport void operator<<(std::ostream& o, const TextureAtlasSamplerFactory::TextureAtlasAttib& tai);
00359 
00363 }
00364 }
00365 
00366 #endif
00367 #endif

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Fri May 25 23:36:26 2012