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     //---------------------------------------------------------------------
00313     //---------------------------------------------------------------------
00314     void ShadowVolumeExtrudeProgram::initialise(void)
00315     {
00316         String syntax;
00317         bool vertexProgramFinite[8] = 
00318         {
00319             false, false, false, false, 
00320                 true, true, true, true
00321         };
00322         bool vertexProgramDebug[8] = 
00323         {
00324             false, true, false, true, 
00325                 false, true, false, true
00326         };
00327         Light::LightTypes vertexProgramLightTypes[8] = 
00328         {
00329             Light::LT_POINT, Light::LT_POINT, 
00330                 Light::LT_DIRECTIONAL, Light::LT_DIRECTIONAL, 
00331                 Light::LT_POINT, Light::LT_POINT, 
00332                 Light::LT_DIRECTIONAL, Light::LT_DIRECTIONAL 
00333         };
00334 
00335         // load hardware extrusion programs for point & dir lights
00336         if (GpuProgramManager::getSingleton().isSyntaxSupported("arbvp1"))
00337         {
00338             // ARBvp1
00339             syntax = "arbvp1";
00340         }
00341         else if (GpuProgramManager::getSingleton().isSyntaxSupported("vs_1_1"))
00342         {
00343             syntax = "vs_1_1";
00344         }
00345         else
00346         {
00347             Except(Exception::ERR_INTERNAL_ERROR, 
00348                 "Vertex programs are supposedly supported, but neither "
00349                 "arbvp1 nor vs_1_1 syntaxes are present.", 
00350                 "SceneManager::initShadowVolumeMaterials");
00351         }
00352         // Create all programs
00353         for (unsigned short v = 0; v < NUM_SHADOW_EXTRUDER_PROGRAMS; ++v)
00354         {
00355             // Create debug extruders
00356             if (!GpuProgramManager::getSingleton().getByName(
00357                 programNames[v]))
00358             {
00359                 GpuProgram* vp = 
00360                     GpuProgramManager::getSingleton().createProgramFromString(
00361                     programNames[v],
00362                     ShadowVolumeExtrudeProgram::getProgramSource(
00363                     vertexProgramLightTypes[v], syntax, 
00364                     vertexProgramFinite[v], vertexProgramDebug[v]),
00365                     GPT_VERTEX_PROGRAM, syntax);
00366                 vp->load();
00367             }
00368         }
00369     }
00370     //---------------------------------------------------------------------
00371     const String& ShadowVolumeExtrudeProgram::getProgramSource(
00372         Light::LightTypes lightType, const String syntax, bool finite, bool debug)
00373     {
00374         if (lightType == Light::LT_DIRECTIONAL)
00375         {
00376             if (syntax == "arbvp1")
00377             {
00378                 if (finite)
00379                 {
00380                     if (debug)
00381                     {
00382                         return getDirectionalLightExtruderArbvp1FiniteDebug();
00383                     }
00384                     else
00385                     {
00386                         return getDirectionalLightExtruderArbvp1Finite();
00387                     }
00388                 }
00389                 else
00390                 {
00391                     if (debug)
00392                     {
00393                         return getDirectionalLightExtruderArbvp1Debug();
00394                     }
00395                     else
00396                     {
00397                         return getDirectionalLightExtruderArbvp1();
00398                     }
00399                 }
00400             }
00401             else
00402             {
00403                 if (finite)
00404                 {
00405                     if (debug)
00406                     {
00407                         return getDirectionalLightExtruderVs_1_1FiniteDebug();
00408                     }
00409                     else
00410                     {
00411                         return getDirectionalLightExtruderVs_1_1Finite();
00412                     }
00413                 }
00414                 else
00415                 {
00416                     if (debug)
00417                     {
00418                         return getDirectionalLightExtruderVs_1_1Debug();
00419                     }
00420                     else
00421                     {
00422                         return getDirectionalLightExtruderVs_1_1();
00423                     }
00424                 }
00425             }
00426         }
00427         else
00428         {
00429             if (syntax == "arbvp1")
00430             {
00431                 if (finite)
00432                 {
00433                     if (debug)
00434                     {
00435                         return getPointLightExtruderArbvp1FiniteDebug();
00436                     }
00437                     else
00438                     {
00439                         return getPointLightExtruderArbvp1Finite();
00440                     }
00441                 }
00442                 else
00443                 {
00444                     if (debug)
00445                     {
00446                         return getPointLightExtruderArbvp1Debug();
00447                     }
00448                     else
00449                     {
00450                         return getPointLightExtruderArbvp1();
00451                     }
00452                 }
00453             }
00454             else
00455             {
00456                 if (finite)
00457                 {
00458                     if (debug)
00459                     {
00460                         return getPointLightExtruderVs_1_1FiniteDebug();
00461                     }
00462                     else
00463                     {
00464                         return getPointLightExtruderVs_1_1Finite();
00465                     }
00466                 }
00467                 else
00468                 {
00469                     if (debug)
00470                     {
00471                         return getPointLightExtruderVs_1_1Debug();
00472                     }
00473                     else
00474                     {
00475                         return getPointLightExtruderVs_1_1();
00476                     }
00477                 }
00478             }
00479         }
00480         // to keep compiler happy
00481         return String::BLANK;
00482     }
00483     //---------------------------------------------------------------------
00484     const String& ShadowVolumeExtrudeProgram::getProgramName(
00485         Light::LightTypes lightType, bool finite, bool debug)
00486     {
00487         if (lightType == Light::LT_DIRECTIONAL)
00488         {
00489             if (finite)
00490             {
00491                 if (debug)
00492                 {
00493                     return programNames[DIRECTIONAL_LIGHT_FINITE_DEBUG];
00494                 }
00495                 else
00496                 {
00497                     return programNames[DIRECTIONAL_LIGHT_FINITE];
00498                 }
00499             }
00500             else
00501             {
00502                 if (debug)
00503                 {
00504                     return programNames[DIRECTIONAL_LIGHT_DEBUG];
00505                 }
00506                 else
00507                 {
00508                     return programNames[DIRECTIONAL_LIGHT];
00509                 }
00510             }
00511         }
00512         else
00513         {
00514             if (finite)
00515             {
00516                 if (debug)
00517                 {
00518                     return programNames[POINT_LIGHT_FINITE_DEBUG];
00519                 }
00520                 else
00521                 {
00522                     return programNames[POINT_LIGHT_FINITE];
00523                 }
00524             }
00525             else
00526             {
00527                 if (debug)
00528                 {
00529                     return programNames[POINT_LIGHT_DEBUG];
00530                 }
00531                 else
00532                 {
00533                     return programNames[POINT_LIGHT];
00534                 }
00535             }
00536         }
00537     }
00538 
00539 
00540 
00541 }

Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:47 2004