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

OgreShadowVolumeExtrudeProgram.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-2004 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 "OgreStableHeaders.h"
00027 #include "OgreShadowVolumeExtrudeProgram.h"
00028 #include "OgreString.h"
00029 #include "OgreGpuProgramManager.h"
00030 #include "OgreGpuProgram.h"
00031 #include "OgreException.h"
00032 
00033 namespace Ogre {
00034 
00035     // c4 is the light position/direction in these
00036     String ShadowVolumeExtrudeProgram::mPointArbvp1 = 
00037         "!!ARBvp1.0\n"
00038         "PARAM c5 = { 0, 0, 0, 0 };\n"
00039         "TEMP R0;\n"
00040         "ATTRIB v24 = vertex.texcoord[0];\n"
00041         "ATTRIB v16 = vertex.position;\n"
00042         "PARAM c0[4] = { program.local[0..3] };\n"
00043         "PARAM c4 = program.local[4];\n"
00044         "ADD R0.xyz, v16.xyzx, -c4.xyzx;\n"
00045         "MOV R0.w, c5.x;\n"
00046         "MAD R0, v24.x, c4, R0;\n"
00047         "DP4 result.position.x, c0[0], R0;\n"
00048         "DP4 result.position.y, c0[1], R0;\n"
00049         "DP4 result.position.z, c0[2], R0;\n"
00050         "DP4 result.position.w, c0[3], R0;\n"
00051         "END\n";
00052 
00053     String ShadowVolumeExtrudeProgram::mPointVs_1_1 = 
00054         "vs_1_1\n"
00055         "def c5, 0, 0, 0, 0\n"
00056         "dcl_texcoord0 v7\n"
00057         "dcl_position v0\n"
00058         "add r0.xyz, v0.xyz, -c4.xyz\n"
00059         "mov r0.w, c5.x\n"
00060         "mad r0, v7.x, c4, r0\n"
00061         "dp4 oPos.x, c0, r0\n"
00062         "dp4 oPos.y, c1, r0\n"
00063         "dp4 oPos.z, c2, r0\n"
00064         "dp4 oPos.w, c3, r0\n";
00065 
00066     String ShadowVolumeExtrudeProgram::mDirArbvp1 = 
00067         "!!ARBvp1.0\n"
00068         "TEMP R0;\n"
00069         "ATTRIB v24 = vertex.texcoord[0];\n"
00070         "ATTRIB v16 = vertex.position;\n"
00071         "PARAM c0[4] = { program.local[0..3] };\n"
00072         "PARAM c4 = program.local[4];\n"
00073         "ADD R0, v16, c4;\n"
00074         "MAD R0, v24.x, R0, -c4;\n"
00075         "DP4 result.position.x, c0[0], R0;\n"
00076         "DP4 result.position.y, c0[1], R0;\n"
00077         "DP4 result.position.z, c0[2], R0;\n"
00078         "DP4 result.position.w, c0[3], R0;\n"
00079         "END\n";
00080 
00081     String ShadowVolumeExtrudeProgram::mDirVs_1_1 = 
00082         "vs_1_1\n"
00083         "dcl_texcoord0 v7\n"
00084         "dcl_position v0\n"
00085         "add r0, v0, c4\n"
00086         "mad r0, v7.x, r0, -c4\n"
00087         "dp4 oPos.x, c0, r0\n"
00088         "dp4 oPos.y, c1, r0\n"
00089         "dp4 oPos.z, c2, r0\n"
00090         "dp4 oPos.w, c3, r0\n";
00091 
00092 
00093     String ShadowVolumeExtrudeProgram::mPointArbvp1Debug = 
00094         "!!ARBvp1.0\n"
00095         "PARAM c5 = { 0, 0, 0, 0 };\n"
00096         "PARAM c6 = { 1, 1, 1, 1 };\n"
00097         "TEMP R0;\n"
00098         "ATTRIB v24 = vertex.texcoord[0];\n"
00099         "ATTRIB v16 = vertex.position;\n"
00100         "PARAM c0[4] = { program.local[0..3] };\n"
00101         "PARAM c4 = program.local[4];\n"
00102         "ADD R0.xyz, v16.xyzx, -c4.xyzx;\n"
00103         "MOV R0.w, c5.x;\n"
00104         "MAD R0, v24.x, c4, R0;\n"
00105         "DP4 result.position.x, c0[0], R0;\n"
00106         "DP4 result.position.y, c0[1], R0;\n"
00107         "DP4 result.position.z, c0[2], R0;\n"
00108         "DP4 result.position.w, c0[3], R0;\n"
00109         "MOV result.color.front.primary, c6.x;\n"
00110         "END\n";
00111 
00112     String ShadowVolumeExtrudeProgram::mPointVs_1_1Debug = 
00113         "vs_1_1\n"
00114         "def c5, 0, 0, 0, 0\n"
00115         "def c6, 1, 1, 1, 1\n"
00116         "dcl_texcoord0 v7\n"
00117         "dcl_position v0\n"
00118         "add r0.xyz, v0.xyz, -c4.xyz\n"
00119         "mov r0.w, c5.x\n"
00120         "mad r0, v7.x, c4, r0\n"
00121         "dp4 oPos.x, c0, r0\n"
00122         "dp4 oPos.y, c1, r0\n"
00123         "dp4 oPos.z, c2, r0\n"
00124         "dp4 oPos.w, c3, r0\n"
00125         "mov oD0, c6.x\n";
00126 
00127     String ShadowVolumeExtrudeProgram::mDirArbvp1Debug = 
00128         "!!ARBvp1.0\n"
00129         "PARAM c5 = { 1, 1, 1, 1};\n"
00130         "TEMP R0;\n"
00131         "ATTRIB v24 = vertex.texcoord[0];\n"
00132         "ATTRIB v16 = vertex.position;\n"
00133         "PARAM c0[4] = { program.local[0..3] };\n"
00134         "PARAM c4 = program.local[4];\n"
00135         "ADD R0, v16, c4;\n"
00136         "MAD R0, v24.x, R0, -c4;\n"
00137         "DP4 result.position.x, c0[0], R0;\n"
00138         "DP4 result.position.y, c0[1], R0;\n"
00139         "DP4 result.position.z, c0[2], R0;\n"
00140         "DP4 result.position.w, c0[3], R0;\n"
00141         "MOV result.color.front.primary, c5.x;"
00142         "END\n";
00143 
00144     String ShadowVolumeExtrudeProgram::mDirVs_1_1Debug = 
00145         "vs_1_1\n"
00146         "def c5, 1, 1, 1, 1\n"
00147         "dcl_texcoord0 v7\n"
00148         "dcl_position v0\n"
00149         "add r0, v0, c4\n"
00150         "mad r0, v7.x, r0, -c4\n"
00151         "dp4 oPos.x, c0, r0\n"
00152         "dp4 oPos.y, c1, r0\n"
00153         "dp4 oPos.z, c2, r0\n"
00154         "dp4 oPos.w, c3, r0\n"
00155         "mov oD0, c5.x\n";
00156 
00157 
00158     // c4 is the light position/direction in these
00159     // c5 is extrusion distance
00160     String ShadowVolumeExtrudeProgram::mPointArbvp1Finite = 
00161         "!!ARBvp1.0\n" 
00162         "PARAM c6 = { 1, 0, 0, 0 };\n"
00163         "TEMP R0;\n"
00164         "ATTRIB v24 = vertex.texcoord[0];\n"
00165         "ATTRIB v16 = vertex.position;\n"
00166         "PARAM c0[4] = { program.local[0..3] };\n"
00167         "PARAM c5 = program.local[5];\n"
00168         "PARAM c4 = program.local[4];\n"
00169         "ADD R0.x, c6.x, -v24.x;\n"
00170         "MUL R0.w, R0.x, c5.x;\n"
00171         "ADD R0.xyz, v16.xyzx, -c4.xyzx;\n"
00172         "MAD R0.xyz, R0.w, R0.xyzx, v16.xyzx;\n"
00173         "DPH result.position.x, R0.xyzz, c0[0];\n"
00174         "DPH result.position.y, R0.xyzz, c0[1];\n"
00175         "DPH result.position.z, R0.xyzz, c0[2];\n"
00176         "DPH result.position.w, R0.xyzz, c0[3];\n"
00177         "END\n";
00178 
00179     String ShadowVolumeExtrudeProgram::mPointVs_1_1Finite = 
00180         "vs_1_1\n"
00181         "def c6, 1, 0, 0, 0\n"
00182         "dcl_texcoord0 v7\n"
00183         "dcl_position v0\n"
00184         "add r0.x, c6.x, -v7.x\n"
00185         "mul r1.x, r0.x, c5.x\n"
00186         "add r0.yzw, v0.xxyz, -c4.xxyz\n"
00187         "dp3 r0.x, r0.yzw, r0.yzw\n"
00188         "rsq r0.x, r0.x\n"
00189         "mul r0.xyz, r0.x, r0.yzw\n"
00190         "mad r0.xyz, r1.x, r0.xyz, v0.xyz\n"
00191         "mov r0.w, c6.x\n"
00192         "dp4 oPos.x, c0, r0\n"
00193         "dp4 oPos.y, c1, r0\n"
00194         "dp4 oPos.z, c2, r0\n"
00195         "dp4 oPos.w, c3, r0\n";
00196     String ShadowVolumeExtrudeProgram::mDirArbvp1Finite = 
00197         "!!ARBvp1.0\n"
00198         "PARAM c6 = { 1, 0, 0, 0 };\n"
00199         "TEMP R0;\n"
00200         "ATTRIB v24 = vertex.texcoord[0];\n"
00201         "ATTRIB v16 = vertex.position;\n"
00202         "PARAM c0[4] = { program.local[0..3] };\n"
00203         "PARAM c4 = program.local[4];\n"
00204         "PARAM c5 = program.local[5];\n"
00205         "ADD R0.x, c6.x, -v24.x;\n"
00206         "MUL R0.x, R0.x, c5.x;\n"
00207         "MAD R0.xyz, -R0.x, c4.xyzx, v16.xyzx;\n"
00208         "DPH result.position.x, R0.xyzz, c0[0];\n"
00209         "DPH result.position.y, R0.xyzz, c0[1];\n"
00210         "DPH result.position.z, R0.xyzz, c0[2];\n"
00211         "DPH result.position.w, R0.xyzz, c0[3];\n"
00212         "END\n";
00213     String ShadowVolumeExtrudeProgram::mDirVs_1_1Finite = 
00214         "vs_1_1\n"
00215         "def c6, 1, 0, 0, 0\n"
00216         "dcl_texcoord0 v7\n"
00217         "dcl_position v0\n"
00218         "add r0.x, c6.x, -v7.x\n"
00219         "mul r0.x, r0.x, c5.x\n"
00220         "mad r0.xyz, -r0.x, c4.xyz, v0.xyz\n"
00221         "mov r0.w, c6.x\n"
00222         "dp4 oPos.x, c0, r0\n"
00223         "dp4 oPos.y, c1, r0\n"
00224         "dp4 oPos.z, c2, r0\n"
00225         "dp4 oPos.w, c3, r0\n";
00226     String ShadowVolumeExtrudeProgram::mPointArbvp1FiniteDebug = 
00227         "!!ARBvp1.0\n"
00228         "PARAM c6 = { 1, 0, 0, 0 };\n"
00229         "TEMP R0, R1;\n"
00230         "ATTRIB v24 = vertex.texcoord[0];\n"
00231         "ATTRIB v16 = vertex.position;\n"
00232         "PARAM c0[4] = { program.local[0..3] };\n"
00233         "PARAM c5 = program.local[5];\n"
00234         "PARAM c4 = program.local[4];\n"
00235         "MOV result.color.front.primary, c6.x;\n"
00236         "ADD R0.x, c6.x, -v24.x;\n"
00237         "MUL R1.x, R0.x, c5.x;\n"
00238         "ADD R0.yzw, v16.xxyz, -c4.xxyz;\n"
00239         "DP3 R0.x, R0.yzwy, R0.yzwy;\n"
00240         "RSQ R0.x, R0.x;\n"
00241         "MUL R0.xyz, R0.x, R0.yzwy;\n"
00242         "MAD R0.xyz, R1.x, R0.xyzx, v16.xyzx;\n"
00243         "DPH result.position.x, R0.xyzz, c0[0];\n"
00244         "DPH result.position.y, R0.xyzz, c0[1];\n"
00245         "DPH result.position.z, R0.xyzz, c0[2];\n"
00246         "DPH result.position.w, R0.xyzz, c0[3];\n"
00247         "END\n";
00248     String ShadowVolumeExtrudeProgram::mPointVs_1_1FiniteDebug = 
00249         "vs_1_1\n"
00250         "def c6, 1, 0, 0, 0\n"
00251         "dcl_texcoord0 v7\n"
00252         "dcl_position v0\n"
00253         "mov oD0, c6.x\n"
00254         "add r0.x, c6.x, -v7.x\n"
00255         "mul r1.x, r0.x, c5.x\n"
00256         "add r0.yzw, v0.xxyz, -c4.xxyz\n"
00257         "dp3 r0.x, r0.yzw, r0.yzw\n"
00258         "rsq r0.x, r0.x\n"
00259         "mul r0.xyz, r0.x, r0.yzw\n"
00260         "mad r0.xyz, r1.x, r0.xyz, v0.xyz\n"
00261         "mov r0.w, c6.x\n"
00262         "dp4 oPos.x, c0, r0\n"
00263         "dp4 oPos.y, c1, r0\n"
00264         "dp4 oPos.z, c2, r0\n"
00265         "dp4 oPos.w, c3, r0\n";
00266     String ShadowVolumeExtrudeProgram::mDirArbvp1FiniteDebug = 
00267         "!!ARBvp1.0\n"
00268         "PARAM c6 = { 1, 0, 0, 0 };\n"
00269         "TEMP R0;\n"
00270         "ATTRIB v24 = vertex.texcoord[0];\n"
00271         "ATTRIB v16 = vertex.position;\n"
00272         "PARAM c0[4] = { program.local[0..3] };\n"
00273         "PARAM c4 = program.local[4];\n"
00274         "PARAM c5 = program.local[5];\n"
00275         "MOV result.color.front.primary, c6.x;\n"
00276         "ADD R0.x, c6.x, -v24.x;\n"
00277         "MUL R0.x, R0.x, c5.x;\n"
00278         "MAD R0.xyz, -R0.x, c4.xyzx, v16.xyzx;\n"
00279         "DPH result.position.x, R0.xyzz, c0[0];\n"
00280         "DPH result.position.y, R0.xyzz, c0[1];\n"
00281         "DPH result.position.z, R0.xyzz, c0[2];\n"
00282         "DPH result.position.w, R0.xyzz, c0[3];\n"
00283         "END\n";
00284     String ShadowVolumeExtrudeProgram::mDirVs_1_1FiniteDebug = 
00285         "vs_1_1\n"
00286         "def c6, 1, 0, 0, 0\n"
00287         "dcl_texcoord0 v7\n"
00288         "dcl_position v0\n"
00289         "mov oD0, c6.x\n"
00290         "add r0.x, c6.x, -v7.x\n"
00291         "mul r0.x, r0.x, c5.x\n"
00292         "mad r0.xyz, -r0.x, c4.xyz, v0.xyz\n"
00293         "mov r0.w, c6.x\n"
00294         "dp4 oPos.x, c0, r0\n"
00295         "dp4 oPos.y, c1, r0\n"
00296         "dp4 oPos.z, c2, r0\n"
00297         "dp4 oPos.w, c3, r0\n";
00298 
00299 
00300     const String ShadowVolumeExtrudeProgram::programNames[NUM_SHADOW_EXTRUDER_PROGRAMS] = 
00301     {
00302         "Ogre/ShadowExtrudePointLight",
00303             "Ogre/ShadowExtrudePointLightDebug",
00304             "Ogre/ShadowExtrudeDirLight",
00305             "Ogre/ShadowExtrudeDirLightDebug",
00306             "Ogre/ShadowExtrudePointLightFinite",
00307             "Ogre/ShadowExtrudePointLightFiniteDebug",
00308             "Ogre/ShadowExtrudeDirLightFinite",
00309             "Ogre/ShadowExtrudeDirLightFiniteDebug"
00310     };
00311 
00312     bool ShadowVolumeExtrudeProgram::mInitialised = false;
00313     //---------------------------------------------------------------------
00314     //---------------------------------------------------------------------
00315     void ShadowVolumeExtrudeProgram::initialise(void)
00316     {
00317         if (!mInitialised)
00318         {
00319             String syntax;
00320             bool vertexProgramFinite[8] = 
00321             {
00322                 false, false, false, false, 
00323                     true, true, true, true
00324             };
00325             bool vertexProgramDebug[8] = 
00326             {
00327                 false, true, false, true, 
00328                     false, true, false, true
00329             };
00330             Light::LightTypes vertexProgramLightTypes[8] = 
00331             {
00332                 Light::LT_POINT, Light::LT_POINT, 
00333                     Light::LT_DIRECTIONAL, Light::LT_DIRECTIONAL, 
00334                     Light::LT_POINT, Light::LT_POINT, 
00335                     Light::LT_DIRECTIONAL, Light::LT_DIRECTIONAL 
00336             };
00337 
00338             // load hardware extrusion programs for point & dir lights
00339             if (GpuProgramManager::getSingleton().isSyntaxSupported("arbvp1"))
00340             {
00341                 // ARBvp1
00342                 syntax = "arbvp1";
00343             }
00344             else if (GpuProgramManager::getSingleton().isSyntaxSupported("vs_1_1"))
00345             {
00346                 syntax = "vs_1_1";
00347             }
00348             else
00349             {
00350                 Except(Exception::ERR_INTERNAL_ERROR, 
00351                     "Vertex programs are supposedly supported, but neither "
00352                     "arbvp1 nor vs_1_1 syntaxes are present.", 
00353                     "SceneManager::initShadowVolumeMaterials");
00354             }
00355             // Create all programs
00356             for (unsigned short v = 0; v < NUM_SHADOW_EXTRUDER_PROGRAMS; ++v)
00357             {
00358                 // Create debug extruders
00359                 if (!GpuProgramManager::getSingleton().getByName(
00360                     programNames[v]))
00361                 {
00362                     GpuProgram* vp = 
00363                         GpuProgramManager::getSingleton().createProgramFromString(
00364                         programNames[v],
00365                         ShadowVolumeExtrudeProgram::getProgramSource(
00366                         vertexProgramLightTypes[v], syntax, 
00367                         vertexProgramFinite[v], vertexProgramDebug[v]),
00368                         GPT_VERTEX_PROGRAM, syntax);
00369                     vp->load();
00370                 }
00371             }
00372             mInitialised = true;
00373         }
00374     }
00375     //---------------------------------------------------------------------
00376     const String& ShadowVolumeExtrudeProgram::getProgramSource(
00377         Light::LightTypes lightType, const String syntax, bool finite, bool debug)
00378     {
00379         if (lightType == Light::LT_DIRECTIONAL)
00380         {
00381             if (syntax == "arbvp1")
00382             {
00383                 if (finite)
00384                 {
00385                     if (debug)
00386                     {
00387                         return getDirectionalLightExtruderArbvp1FiniteDebug();
00388                     }
00389                     else
00390                     {
00391                         return getDirectionalLightExtruderArbvp1Finite();
00392                     }
00393                 }
00394                 else
00395                 {
00396                     if (debug)
00397                     {
00398                         return getDirectionalLightExtruderArbvp1Debug();
00399                     }
00400                     else
00401                     {
00402                         return getDirectionalLightExtruderArbvp1();
00403                     }
00404                 }
00405             }
00406             else
00407             {
00408                 if (finite)
00409                 {
00410                     if (debug)
00411                     {
00412                         return getDirectionalLightExtruderVs_1_1FiniteDebug();
00413                     }
00414                     else
00415                     {
00416                         return getDirectionalLightExtruderVs_1_1Finite();
00417                     }
00418                 }
00419                 else
00420                 {
00421                     if (debug)
00422                     {
00423                         return getDirectionalLightExtruderVs_1_1Debug();
00424                     }
00425                     else
00426                     {
00427                         return getDirectionalLightExtruderVs_1_1();
00428                     }
00429                 }
00430             }
00431         }
00432         else
00433         {
00434             if (syntax == "arbvp1")
00435             {
00436                 if (finite)
00437                 {
00438                     if (debug)
00439                     {
00440                         return getPointLightExtruderArbvp1FiniteDebug();
00441                     }
00442                     else
00443                     {
00444                         return getPointLightExtruderArbvp1Finite();
00445                     }
00446                 }
00447                 else
00448                 {
00449                     if (debug)
00450                     {
00451                         return getPointLightExtruderArbvp1Debug();
00452                     }
00453                     else
00454                     {
00455                         return getPointLightExtruderArbvp1();
00456                     }
00457                 }
00458             }
00459             else
00460             {
00461                 if (finite)
00462                 {
00463                     if (debug)
00464                     {
00465                         return getPointLightExtruderVs_1_1FiniteDebug();
00466                     }
00467                     else
00468                     {
00469                         return getPointLightExtruderVs_1_1Finite();
00470                     }
00471                 }
00472                 else
00473                 {
00474                     if (debug)
00475                     {
00476                         return getPointLightExtruderVs_1_1Debug();
00477                     }
00478                     else
00479                     {
00480                         return getPointLightExtruderVs_1_1();
00481                     }
00482                 }
00483             }
00484         }
00485         // to keep compiler happy
00486         return StringUtil::BLANK;
00487     }
00488     //---------------------------------------------------------------------
00489     const String& ShadowVolumeExtrudeProgram::getProgramName(
00490         Light::LightTypes lightType, bool finite, bool debug)
00491     {
00492         if (lightType == Light::LT_DIRECTIONAL)
00493         {
00494             if (finite)
00495             {
00496                 if (debug)
00497                 {
00498                     return programNames[DIRECTIONAL_LIGHT_FINITE_DEBUG];
00499                 }
00500                 else
00501                 {
00502                     return programNames[DIRECTIONAL_LIGHT_FINITE];
00503                 }
00504             }
00505             else
00506             {
00507                 if (debug)
00508                 {
00509                     return programNames[DIRECTIONAL_LIGHT_DEBUG];
00510                 }
00511                 else
00512                 {
00513                     return programNames[DIRECTIONAL_LIGHT];
00514                 }
00515             }
00516         }
00517         else
00518         {
00519             if (finite)
00520             {
00521                 if (debug)
00522                 {
00523                     return programNames[POINT_LIGHT_FINITE_DEBUG];
00524                 }
00525                 else
00526                 {
00527                     return programNames[POINT_LIGHT_FINITE];
00528                 }
00529             }
00530             else
00531             {
00532                 if (debug)
00533                 {
00534                     return programNames[POINT_LIGHT_DEBUG];
00535                 }
00536                 else
00537                 {
00538                     return programNames[POINT_LIGHT];
00539                 }
00540             }
00541         }
00542     }
00543 
00544 
00545 
00546 }

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