| 1 | /* | 
|---|
| 2 | ----------------------------------------------------------------------------- | 
|---|
| 3 | This source file is part of OGRE | 
|---|
| 4 | (Object-oriented Graphics Rendering Engine) | 
|---|
| 5 | For the latest info, see http://www.ogre3d.org/ | 
|---|
| 6 |  | 
|---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd | 
|---|
| 8 | Also see acknowledgements in Readme.html | 
|---|
| 9 |  | 
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under | 
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software | 
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later | 
|---|
| 13 | version. | 
|---|
| 14 |  | 
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT | 
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. | 
|---|
| 18 |  | 
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with | 
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple | 
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to | 
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. | 
|---|
| 23 |  | 
|---|
| 24 | You may alternatively use this source under the terms of a specific version of | 
|---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from | 
|---|
| 26 | Torus Knot Software Ltd. | 
|---|
| 27 | ----------------------------------------------------------------------------- | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | #include "OgreGLGpuNvparseProgram.h" | 
|---|
| 31 | #include "OgreException.h" | 
|---|
| 32 | #include "OgreRoot.h" | 
|---|
| 33 | #include "OgreRenderSystem.h" | 
|---|
| 34 | #include "OgreRenderSystemCapabilities.h" | 
|---|
| 35 | #include "OgreLogManager.h" | 
|---|
| 36 | #include "nvparse.h" | 
|---|
| 37 |  | 
|---|
| 38 | using namespace Ogre; | 
|---|
| 39 |  | 
|---|
| 40 | GLGpuNvparseProgram::GLGpuNvparseProgram(ResourceManager* creator,  | 
|---|
| 41 |         const String& name, ResourceHandle handle,  | 
|---|
| 42 |         const String& group, bool isManual, ManualResourceLoader* loader)  | 
|---|
| 43 |         : GLGpuProgram(creator, name, handle, group, isManual, loader) | 
|---|
| 44 | { | 
|---|
| 45 |     mProgramID = glGenLists(1); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | GLGpuNvparseProgram::~GLGpuNvparseProgram() | 
|---|
| 49 | { | 
|---|
| 50 |     // have to call this here reather than in Resource destructor | 
|---|
| 51 |     // since calling virtual methods in base destructors causes crash | 
|---|
| 52 |     unload();  | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | void GLGpuNvparseProgram::bindProgram(void) | 
|---|
| 56 | { | 
|---|
| 57 |      glCallList(mProgramID); | 
|---|
| 58 |      glEnable(GL_TEXTURE_SHADER_NV); | 
|---|
| 59 |      glEnable(GL_REGISTER_COMBINERS_NV); | 
|---|
| 60 |      glEnable(GL_PER_STAGE_CONSTANTS_NV); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | void GLGpuNvparseProgram::unbindProgram(void) | 
|---|
| 64 | { | 
|---|
| 65 |  | 
|---|
| 66 |     glDisable(GL_TEXTURE_SHADER_NV); | 
|---|
| 67 |     glDisable(GL_REGISTER_COMBINERS_NV); | 
|---|
| 68 |     glDisable(GL_PER_STAGE_CONSTANTS_NV); | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | void GLGpuNvparseProgram::bindProgramParameters(GpuProgramParametersSharedPtr params) | 
|---|
| 72 | { | 
|---|
| 73 |     // NB, register combiners uses 2 constants per texture stage (0 and 1) | 
|---|
| 74 |     // We have stored these as (stage * 2) + const_index in the physical buffer | 
|---|
| 75 |         // There are no other parameters in a register combiners shader | 
|---|
| 76 |         const GpuProgramParameters::FloatConstantList& floatList =  | 
|---|
| 77 |                 params->getFloatConstantList(); | 
|---|
| 78 |         size_t index = 0; | 
|---|
| 79 |         for (GpuProgramParameters::FloatConstantList::const_iterator i = floatList.begin(); | 
|---|
| 80 |                 i != floatList.end(); ++i, ++index) | 
|---|
| 81 |         { | 
|---|
| 82 |                 GLenum combinerStage = GL_COMBINER0_NV + (unsigned int)(index / 2); | 
|---|
| 83 |                 GLenum pname = GL_CONSTANT_COLOR0_NV + (index % 2); | 
|---|
| 84 |                 glCombinerStageParameterfvNV(combinerStage, pname, &(*i)); | 
|---|
| 85 |                  | 
|---|
| 86 |         } | 
|---|
| 87 |  | 
|---|
| 88 | } | 
|---|
| 89 | void GLGpuNvparseProgram::unloadImpl(void) | 
|---|
| 90 | { | 
|---|
| 91 |     glDeleteLists(mProgramID,1); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | void GLGpuNvparseProgram::loadFromSource(void) | 
|---|
| 95 | { | 
|---|
| 96 |     glNewList(mProgramID, GL_COMPILE); | 
|---|
| 97 |  | 
|---|
| 98 |     String::size_type pos = mSource.find("!!"); | 
|---|
| 99 |  | 
|---|
| 100 |     while (pos != String::npos) { | 
|---|
| 101 |         String::size_type newPos = mSource.find("!!", pos + 1); | 
|---|
| 102 |  | 
|---|
| 103 |         String script = mSource.substr(pos, newPos - pos); | 
|---|
| 104 |         nvparse(script.c_str(), 0); | 
|---|
| 105 |  | 
|---|
| 106 |         for (char* const * errors= nvparse_get_errors(); *errors; errors++) | 
|---|
| 107 |         { | 
|---|
| 108 |             LogManager::getSingleton().logMessage("Warning: nvparse reported the following errors:"); | 
|---|
| 109 |             LogManager::getSingleton().logMessage("\t" + String(*errors)); | 
|---|
| 110 |         } | 
|---|
| 111 |          | 
|---|
| 112 |         pos = newPos; | 
|---|
| 113 |     } | 
|---|
| 114 |  | 
|---|
| 115 |     glEndList(); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|