Main Page Namespace List Class Hierarchy Alphabetical List Compound List File List Namespace Members Compound Members File Members Related Pages

# OgreGLSLLinkProgramManager.cpp

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 © 2000-2002 The OGRE Team
    00008 Also see acknowledgements in Readme.html
    00009
    00010 This program is free software; you can redistribute it and/or modify it under
    00011 the terms of the GNU Lesser General Public License as published by the Free Software
    00012 Foundation; either version 2 of the License, or (at your option) any later
    00013 version.
    00014
    00015 This program is distributed in the hope that it will be useful, but WITHOUT
    00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    00018
    00019 You should have received a copy of the GNU Lesser General Public License along with
    00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
    00022 http://www.gnu.org/copyleft/lesser.txt.
    00023 -----------------------------------------------------------------------------
    00024 */
    00025
    00026 #include "OgreGLSLLinkProgramManager.h"
    00027 #include "OgreGLSLLinkProgram.h"
    00028 #include "OgreGLSLGpuProgram.h"
    00029 #include "OgreGLSLProgram.h"
    00030
    00031 namespace Ogre {
    00032
    00033     //-----------------------------------------------------------------------
    00034     template<> GLSLLinkProgramManager* Singleton<GLSLLinkProgramManager>::ms_Singleton = 0;
    00035
    00036     //-----------------------------------------------------------------------
    00037     GLSLLinkProgramManager* GLSLLinkProgramManager::getSingletonPtr(void)
    00038     {
    00039         return ms_Singleton;
    00040     }
    00041
    00042     //-----------------------------------------------------------------------
    00043     GLSLLinkProgramManager& GLSLLinkProgramManager::getSingleton(void)
    00044     {
    00045         assert( ms_Singleton );  return ( *ms_Singleton );
    00046     }
    00047
    00048     //-----------------------------------------------------------------------
    00049     GLSLLinkProgramManager::GLSLLinkProgramManager(void) : mActiveVertexGpuProgram(NULL),
    00050         mActiveFragmentGpuProgram(NULL), mActiveLinkProgram(NULL)
    00051     {
    00052
    00053     }
    00054
    00055     //-----------------------------------------------------------------------
    00056     GLSLLinkProgramManager::~GLSLLinkProgramManager(void)
    00057     {
    00058         // iterate through map container and delete link programs
    00059         for (LinkProgramIterator currentProgram = LinkPrograms.begin();
    00060             currentProgram != LinkPrograms.end(); ++currentProgram)
    00061         {
    00062             delete currentProgram->second;
    00063         }
    00064
    00065     }
    00066
    00067     //-----------------------------------------------------------------------
    00068     GLSLLinkProgram* GLSLLinkProgramManager::getActiveLinkProgram(void)
    00069     {
    00070         // if there is an active link program then return it
    00071         if (mActiveLinkProgram)
    00072             return mActiveLinkProgram;
    00073
    00074         // no active link program so find one or make a new one
    00075         // is there an active key?
    00076         GLuint activeKey = 0;
    00077
    00078         if (mActiveVertexGpuProgram)
    00079         {
    00080             activeKey = mActiveVertexGpuProgram->getProgramID() << 8;
    00081         }
    00082
    00083         if (mActiveFragmentGpuProgram)
    00084         {
    00085             activeKey += mActiveFragmentGpuProgram->getProgramID();
    00086         }
    00087
    00088         // only return a link program object if a vertex or fragment program exist
    00089         if (activeKey > 0)
    00090         {
    00091             // find the key in the hash map
    00092             LinkProgramIterator programFound = LinkPrograms.find(activeKey);
    00093             // program object not found for key so need to create it
    00094             if (programFound == LinkPrograms.end())
    00095             {
    00096                 mActiveLinkProgram = new GLSLLinkProgram();
    00097                 LinkPrograms[activeKey] = mActiveLinkProgram;
    00098                 // tell shaders to attach themselves to the LinkProgram
    00099                 // let the shaders do the attaching since they may have several children to attach
    00100                 if (mActiveVertexGpuProgram)
    00101                 {
    00102                     mActiveVertexGpuProgram->getGLSLProgram()->attachToProgramObject( mActiveLinkProgram->getGLHandle() );
    00103                 }
    00104
    00105                 if (mActiveFragmentGpuProgram)
    00106                 {
    00107                     mActiveFragmentGpuProgram->getGLSLProgram()->attachToProgramObject( mActiveLinkProgram->getGLHandle() );
    00108                 }
    00109
    00110
    00111             }
    00112             else
    00113             {
    00114                 // found a link program in map container so make it active
    00115                 mActiveLinkProgram = programFound->second;
    00116             }
    00117
    00118         }
    00119         // make the program object active
    00120         if (mActiveLinkProgram) mActiveLinkProgram->activate();
    00121
    00122         return mActiveLinkProgram;
    00123
    00124     }
    00125
    00126     //-----------------------------------------------------------------------
    00127     void GLSLLinkProgramManager::setActiveFragmentShader(GLSLGpuProgram* fragmentGpuProgram)
    00128     {
    00129         if (fragmentGpuProgram != mActiveFragmentGpuProgram)
    00130         {
    00131             mActiveFragmentGpuProgram = fragmentGpuProgram;
    00132             // ActiveLinkProgram is no longer valid
    00133             mActiveLinkProgram = NULL;
    00134             // change back to fixed pipeline
    00135             glUseProgramObjectARB_ptr(0);
    00136         }
    00137     }
    00138
    00139     //-----------------------------------------------------------------------
    00140     void GLSLLinkProgramManager::setActiveVertexShader(GLSLGpuProgram* vertexGpuProgram)
    00141     {
    00142         if (vertexGpuProgram != mActiveVertexGpuProgram)
    00143         {
    00144             mActiveVertexGpuProgram = vertexGpuProgram;
    00145             // ActiveLinkProgram is no longer valid
    00146             mActiveLinkProgram = NULL;
    00147             // change back to fixed pipeline
    00148             glUseProgramObjectARB_ptr(0);
    00149         }
    00150     }
    00151
    00152 }

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:26 2004