00001 /* 00002 ----------------------------------------------------------------------------- 00003 This source file is part of OGRE 00004 (Object-oriented Graphics Rendering Engine) 00005 For the latest info, see http://ogre.sourceforge.net/ 00006 00007 Copyright © 2000-2003 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 #include "OgreStableHeaders.h" 00026 #include "OgreGpuProgram.h" 00027 #include "OgreGpuProgramManager.h" 00028 #include "OgreSDDataChunk.h" 00029 #include "OgreVector3.h" 00030 #include "OgreVector4.h" 00031 #include "OgreAutoParamDataSource.h" 00032 #include "OgreLight.h" 00033 #include "OgreControllerManager.h" 00034 00035 namespace Ogre 00036 { 00037 //----------------------------------------------------------------------------- 00038 GpuProgram::GpuProgram(const String& name, GpuProgramType gptype, const String& syntaxCode) 00039 : mType(gptype), mLoadFromFile(true), mSyntaxCode(syntaxCode) 00040 { 00041 mName = name; 00042 } 00043 //----------------------------------------------------------------------------- 00044 void GpuProgram::setSourceFile(const String& filename) 00045 { 00046 mFilename = filename; 00047 mSource = ""; 00048 mLoadFromFile = true; 00049 } 00050 //----------------------------------------------------------------------------- 00051 void GpuProgram::setSource(const String& source) 00052 { 00053 mSource = source; 00054 mFilename = ""; 00055 mLoadFromFile = false; 00056 } 00057 00058 //----------------------------------------------------------------------------- 00059 void GpuProgram::load(void) 00060 { 00061 if (mIsLoaded) 00062 { 00063 unload(); 00064 } 00065 if (mLoadFromFile) 00066 { 00067 // find & load source code 00068 SDDataChunk chunk; 00069 GpuProgramManager::getSingleton()._findResourceData(mFilename, chunk); 00070 mSource = chunk.getAsString(); 00071 } 00072 00073 // Call polymorphic load 00074 loadFromSource(); 00075 00076 mIsLoaded = true; 00077 } 00078 //----------------------------------------------------------------------------- 00079 bool GpuProgram::isSupported(void) const 00080 { 00081 return GpuProgramManager::getSingleton().isSyntaxSupported(mSyntaxCode); 00082 } 00083 //----------------------------------------------------------------------------- 00084 GpuProgramParametersSharedPtr GpuProgram::createParameters(void) 00085 { 00086 // Default implementation simply returns standard parameters. 00087 return GpuProgramManager::getSingleton().createParameters(); 00088 } 00089 //----------------------------------------------------------------------------- 00090 GpuProgramParameters::GpuProgramParameters() 00091 : mTransposeMatrices(false) 00092 { 00093 } 00094 //----------------------------------------------------------------------------- 00095 void GpuProgramParameters::setConstant(size_t index, const Vector4& vec) 00096 { 00097 setConstant(index, vec.val, 1); 00098 } 00099 //----------------------------------------------------------------------------- 00100 void GpuProgramParameters::setConstant(size_t index, const Vector3& vec) 00101 { 00102 setConstant(index, Vector4(vec.x, vec.y, vec.z, 1.0f)); 00103 } 00104 //----------------------------------------------------------------------------- 00105 void GpuProgramParameters::setConstant(size_t index, const Matrix4& m) 00106 { 00107 // set as 4x 4-element floats 00108 if (mTransposeMatrices) 00109 { 00110 Matrix4 t = m.transpose(); 00111 GpuProgramParameters::setConstant(index++, t[0], 1); 00112 GpuProgramParameters::setConstant(index++, t[1], 1); 00113 GpuProgramParameters::setConstant(index++, t[2], 1); 00114 GpuProgramParameters::setConstant(index, t[3], 1); 00115 } 00116 else 00117 { 00118 GpuProgramParameters::setConstant(index++, m[0], 1); 00119 GpuProgramParameters::setConstant(index++, m[1], 1); 00120 GpuProgramParameters::setConstant(index++, m[2], 1); 00121 GpuProgramParameters::setConstant(index, m[3], 1); 00122 } 00123 } 00124 //----------------------------------------------------------------------------- 00125 void GpuProgramParameters::setConstant(size_t index, const ColourValue& colour) 00126 { 00127 setConstant(index, colour.val, 1); 00128 } 00129 //----------------------------------------------------------------------------- 00130 void GpuProgramParameters::setConstant(size_t index, const Real *val, size_t count) 00131 { 00132 // Expand if required 00133 if (mRealConstants.size() < index + count) 00134 mRealConstants.resize(index + count); 00135 00136 // Copy in chunks of 4 00137 while (count--) 00138 { 00139 RealConstantEntry* e = &(mRealConstants[index++]); 00140 e->isSet = true; 00141 memcpy(e->val, val, sizeof(Real) * 4); 00142 val += 4; 00143 } 00144 00145 } 00146 //----------------------------------------------------------------------------- 00147 void GpuProgramParameters::setConstant(size_t index, const int *val, size_t count) 00148 { 00149 // Expand if required 00150 if (mIntConstants.size() < index + count) 00151 mIntConstants.resize(index + count); 00152 00153 // Copy in chunks of 4 00154 while (count--) 00155 { 00156 IntConstantEntry* e = &(mIntConstants[index++]); 00157 e->isSet = true; 00158 memcpy(e->val, val, sizeof(int) * 4); 00159 val += 4; 00160 } 00161 } 00162 //----------------------------------------------------------------------------- 00163 void GpuProgramParameters::setAutoConstant(size_t index, AutoConstantType acType, size_t extraInfo) 00164 { 00165 mAutoConstants.push_back(AutoConstantEntry(acType, index, extraInfo)); 00166 } 00167 //----------------------------------------------------------------------------- 00168 void GpuProgramParameters::clearAutoConstants(void) 00169 { 00170 mAutoConstants.clear(); 00171 } 00172 //----------------------------------------------------------------------------- 00173 GpuProgramParameters::AutoConstantIterator GpuProgramParameters::getAutoConstantIterator(void) 00174 { 00175 return AutoConstantIterator(mAutoConstants.begin(), mAutoConstants.end()); 00176 } 00177 //----------------------------------------------------------------------------- 00178 void GpuProgramParameters::_updateAutoParamsNoLights(const AutoParamDataSource& source) 00179 { 00180 if (!hasAutoConstants()) return; // abort early if no autos 00181 Vector3 vec3; 00182 Vector4 vec4; 00183 00184 AutoConstantList::const_iterator i, iend; 00185 iend = mAutoConstants.end(); 00186 for (i = mAutoConstants.begin(); i != iend; ++i) 00187 { 00188 switch(i->paramType) 00189 { 00190 case ACT_WORLD_MATRIX: 00191 setConstant(i->index, source.getWorldMatrix()); 00192 break; 00193 case ACT_VIEW_MATRIX: 00194 setConstant(i->index, source.getViewMatrix()); 00195 break; 00196 case ACT_PROJECTION_MATRIX: 00197 setConstant(i->index, source.getProjectionMatrix()); 00198 break; 00199 case ACT_WORLDVIEW_MATRIX: 00200 setConstant(i->index, source.getWorldViewMatrix()); 00201 break; 00202 case ACT_WORLDVIEWPROJ_MATRIX: 00203 setConstant(i->index, source.getWorldViewProjMatrix()); 00204 break; 00205 case ACT_INVERSE_WORLD_MATRIX: 00206 setConstant(i->index, source.getInverseWorldMatrix()); 00207 break; 00208 case ACT_INVERSE_WORLDVIEW_MATRIX: 00209 setConstant(i->index, source.getInverseWorldViewMatrix()); 00210 break; 00211 case ACT_CAMERA_POSITION_OBJECT_SPACE: 00212 setConstant(i->index, source.getCameraPositionObjectSpace()); 00213 break; 00214 // NB ambient light still here because it's not related to a specific light 00215 case ACT_AMBIENT_LIGHT_COLOUR: 00216 setConstant(i->index, source.getAmbientLightColour()); 00217 break; 00218 } 00219 } 00220 } 00221 //----------------------------------------------------------------------------- 00222 void GpuProgramParameters::_updateAutoParamsLightsOnly(const AutoParamDataSource& source) 00223 { 00224 if (!hasAutoConstants()) return; // abort early if no autos 00225 Vector3 vec3; 00226 Vector4 vec4; 00227 00228 AutoConstantList::const_iterator i, iend; 00229 iend = mAutoConstants.end(); 00230 for (i = mAutoConstants.begin(); i != iend; ++i) 00231 { 00232 switch(i->paramType) 00233 { 00234 case ACT_LIGHT_DIFFUSE_COLOUR: 00235 setConstant(i->index, source.getLight(i->data).getDiffuseColour()); 00236 break; 00237 case ACT_LIGHT_SPECULAR_COLOUR: 00238 setConstant(i->index, source.getLight(i->data).getSpecularColour()); 00239 break; 00240 case ACT_LIGHT_POSITION_OBJECT_SPACE: 00241 setConstant(i->index, 00242 source.getInverseWorldMatrix() * source.getLight(i->data).getDerivedPosition()); 00243 break; 00244 case ACT_LIGHT_DIRECTION_OBJECT_SPACE: 00245 vec3 = source.getInverseWorldMatrix() * 00246 source.getLight(i->data).getDerivedDirection(); 00247 vec3.normalise(); 00248 // Set as 4D vector for compatibility 00249 setConstant(i->index, Vector4(vec3.x, vec3.y, vec3.z, 1.0f)); 00250 break; 00251 case ACT_LIGHT_ATTENUATION: 00252 // range, const, linear, quad 00253 const Light& l = source.getLight(i->data); 00254 vec4.x = l.getAttenuationRange(); 00255 vec4.y = l.getAttenuationConstant(); 00256 vec4.z = l.getAttenuationLinear(); 00257 vec4.w = l.getAttenuationQuadric(); 00258 setConstant(i->index, vec4); 00259 break; 00260 } 00261 } 00262 } 00263 //--------------------------------------------------------------------------- 00264 void GpuProgramParameters::_mapParameterNameToIndex(const String& name, 00265 size_t index) 00266 { 00267 mParamNameMap[name] = index; 00268 } 00269 //--------------------------------------------------------------------------- 00270 size_t GpuProgramParameters::getParamIndex(const String& name) const 00271 { 00272 ParamNameMap::const_iterator i = mParamNameMap.find(name); 00273 if (i == mParamNameMap.end()) 00274 { 00275 Except(Exception::ERR_ITEM_NOT_FOUND, "Cannot find a parameter named " + name, 00276 "GpuProgramParameters::getParamIndex"); 00277 } 00278 return i->second; 00279 } 00280 //--------------------------------------------------------------------------- 00281 void GpuProgramParameters::setNamedConstant(const String& name, Real val) 00282 { 00283 setConstant(getParamIndex(name), val); 00284 } 00285 //--------------------------------------------------------------------------- 00286 void GpuProgramParameters::setNamedConstant(const String& name, int val) 00287 { 00288 setConstant(getParamIndex(name), val); 00289 } 00290 //--------------------------------------------------------------------------- 00291 void GpuProgramParameters::setNamedConstant(const String& name, const Vector4& vec) 00292 { 00293 setConstant(getParamIndex(name), vec); 00294 } 00295 //--------------------------------------------------------------------------- 00296 void GpuProgramParameters::setNamedConstant(const String& name, const Vector3& vec) 00297 { 00298 setConstant(getParamIndex(name), vec); 00299 } 00300 //--------------------------------------------------------------------------- 00301 void GpuProgramParameters::setNamedConstant(const String& name, const Matrix4& m) 00302 { 00303 setConstant(getParamIndex(name), m); 00304 } 00305 //--------------------------------------------------------------------------- 00306 void GpuProgramParameters::setNamedConstant(const String& name, const Real *val, size_t count) 00307 { 00308 setConstant(getParamIndex(name), val, count); 00309 } 00310 //--------------------------------------------------------------------------- 00311 void GpuProgramParameters::setNamedConstant(const String& name, const ColourValue& colour) 00312 { 00313 setConstant(getParamIndex(name), colour); 00314 } 00315 //--------------------------------------------------------------------------- 00316 void GpuProgramParameters::setNamedConstant(const String& name, const int *val, size_t count) 00317 { 00318 setConstant(getParamIndex(name), val, count); 00319 } 00320 //--------------------------------------------------------------------------- 00321 void GpuProgramParameters::setNamedAutoConstant(const String& name, AutoConstantType acType, size_t extraInfo) 00322 { 00323 setAutoConstant(getParamIndex(name), acType, extraInfo); 00324 } 00325 //--------------------------------------------------------------------------- 00326 void GpuProgramParameters::setConstantFromTime(size_t index, Real factor) 00327 { 00328 // Create controller 00329 ControllerManager::getSingleton().createGpuProgramTimerParam(this, index, factor); 00330 00331 } 00332 //--------------------------------------------------------------------------- 00333 void GpuProgramParameters::setNamedConstantFromTime(const String& name, Real factor) 00334 { 00335 setConstantFromTime(getParamIndex(name), factor); 00336 } 00337 //--------------------------------------------------------------------------- 00338 GpuProgramParameters::RealConstantIterator GpuProgramParameters::getRealConstantIterator(void) 00339 { 00340 return RealConstantIterator(mRealConstants.begin(), mRealConstants.end()); 00341 } 00342 //--------------------------------------------------------------------------- 00343 GpuProgramParameters::IntConstantIterator GpuProgramParameters::getIntConstantIterator(void) 00344 { 00345 return IntConstantIterator(mIntConstants.begin(), mIntConstants.end()); 00346 } 00347 00348 }
Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:12 2004