OSGShaderVariableOSG.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *               Copyright (C) 2000-2002 by the OpenSG Forum                 *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038
00039 //---------------------------------------------------------------------------
00040 //  Includes
00041 //---------------------------------------------------------------------------
00042
00043 #include <cstdlib>
00044 #include <cstdio>
00045
00046 #include "OSGConfig.h"
00047
00048 #include "OSGShaderVariableOSG.h"
00049 #include "OSGGLFuncProtos.h"
00050 #include "OSGShaderProgram.h"
00051
00052 #include "OSGDrawEnv.h"
00053 #include "OSGWindow.h"
00054
00055 OSG_USING_NAMESPACE
00056
00057 // Documentation for this class is emited in the
00058 // OSGShaderVariableOSGBase.cpp file.
00059 // To modify it, please change the .fcd file (OSGShaderVariableOSG.fcd) and
00060 // regenerate the base file.
00061
00062 /***************************************************************************\
00063  *                           Class variables                               *
00064 \***************************************************************************/
00065
00066 /***************************************************************************\
00067  *                           Class methods                                 *
00068 \***************************************************************************/
00069
00070 void ShaderVariableOSG::initMethod(InitPhase ePhase)
00071 {
00072     Inherited::initMethod(ePhase);
00073 }
00074
00075
00076 /***************************************************************************\
00077  *                           Instance methods                              *
00078 \***************************************************************************/
00079
00080 /*-------------------------------------------------------------------------*\
00081  -  private                                                                 -
00082 \*-------------------------------------------------------------------------*/
00083
00084 /*----------------------- constructors & destructors ----------------------*/
00085
00086 ShaderVariableOSG::ShaderVariableOSG(void) :
00087     Inherited()
00088 {
00089     setTypeId(ShaderVariable::SHVTypeOSG);
00090 }
00091
00092 ShaderVariableOSG::ShaderVariableOSG(const ShaderVariableOSG &source) :
00093     Inherited(source)
00094 {
00095 }
00096
00097 ShaderVariableOSG::~ShaderVariableOSG(void)
00098 {
00099 }
00100
00101 /*----------------------------- class specific ----------------------------*/
00102
00103 void ShaderVariableOSG::changed(ConstFieldMaskArg whichField,
00104                                 UInt32            origin,
00105                                 BitVector         details)
00106 {
00107     Inherited::changed(whichField, origin, details);
00108
00109     if(0x0000 != (whichField & NameFieldMask))
00110     {
00111         if(_sfName.getValue() == "OSGWorldMatrix")
00112         {
00113             setOsgVarType(OSGWorldMatrix);
00114             setDependency(SHDObject     );
00115         }
00116         else if(_sfName.getValue() == "OSGInvWorldMatrix")
00117         {
00118             setOsgVarType(OSGInvWorldMatrix);
00119             setDependency(SHDObject        );
00120         }
00121         else if(_sfName.getValue() == "OSGTransInvWorldMatrix")
00122         {
00123             setOsgVarType(OSGTransInvWorldMatrix);
00124             setDependency(SHDObject             );
00125         }
00126         else if(_sfName.getValue() == "OSGCameraOrientation")
00127         {
00128             setOsgVarType(OSGCameraOrientation);
00129             setDependency(SHDScene            );
00130         }
00131         else if(_sfName.getValue() == "OSGCameraPosition")
00132         {
00133             setOsgVarType(OSGCameraPosition);
00134             setDependency(SHDScene         );
00135         }
00136         else if(_sfName.getValue() == "OSGViewMatrix")
00137         {
00138             setOsgVarType(OSGViewMatrix);
00139             setDependency(SHDScene     );
00140         }
00141         else if(_sfName.getValue() == "OSGInvViewMatrix")
00142         {
00143             setOsgVarType(OSGInvViewMatrix);
00144             setDependency(SHDScene        );
00145         }
00146         else if(_sfName.getValue() == "OSGStereoLeftEye")
00147         {
00148             setOsgVarType(OSGStereoLeftEye);
00149             setDependency(SHDScene        );
00150         }
00151         else if(_sfName.getValue() == "OSGClusterId")
00152         {
00153             setOsgVarType(OSGClusterId);
00154             setDependency(SHDScene    );
00155         }
00156         else if(_sfName.getValue() == "OSGActiveLightsMask")
00157         {
00158             setOsgVarType(OSGActiveLightsMask);
00159             setDependency(SHDObject          );
00160         }
00161         else if(_sfName.getValue() == "OSGLight0Active")
00162         {
00163             setOsgVarType(OSGLight0Active);
00164             setDependency(SHDObject      );
00165         }
00166         else if(_sfName.getValue() == "OSGLight1Active")
00167         {
00168             setOsgVarType(OSGLight1Active);
00169             setDependency(SHDObject      );
00170         }
00171         else if(_sfName.getValue() == "OSGLight2Active")
00172         {
00173             setOsgVarType(OSGLight2Active);
00174             setDependency(SHDObject      );
00175         }
00176         else if(_sfName.getValue() == "OSGLight3Active")
00177         {
00178             setOsgVarType(OSGLight3Active);
00179             setDependency(SHDObject      );
00180         }
00181         else if(_sfName.getValue() == "OSGLight4Active")
00182         {
00183             setOsgVarType(OSGLight4Active);
00184             setDependency(SHDObject      );
00185         }
00186         else if(_sfName.getValue() == "OSGLight5Active")
00187         {
00188             setOsgVarType(OSGLight5Active);
00189             setDependency(SHDObject      );
00190         }
00191         else if(_sfName.getValue() == "OSGLight6Active")
00192         {
00193             setOsgVarType(OSGLight6Active);
00194             setDependency(SHDObject      );
00195         }
00196         else if(_sfName.getValue() == "OSGLight7Active")
00197         {
00198             setOsgVarType(OSGLight7Active);
00199             setDependency(SHDObject      );
00200         }
00201         else
00202         {
00203             setOsgVarType(OSGUnknown);
00204             setDependency(SHDObject );
00205         }
00206     }
00207 }
00208
00209 void ShaderVariableOSG::dump(      UInt32    ,
00210                              const BitVector ) const
00211 {
00212     SLOG << "Dump ShaderVariableOSG NI" << std::endl;
00213 }
00214
00215 bool ShaderVariableOSG::isProcedural(void) const
00216 {
00217     return true;
00218 }
00219
00220 void ShaderVariableOSG::setName(const std::string &value)
00221 {
00222     Inherited::setName(value);
00223
00224     if(_sfName.getValue() == "OSGWorldMatrix")
00225     {
00226         setOsgVarType(OSGWorldMatrix);
00227         setDependency(SHDObject     );
00228     }
00229     else if(_sfName.getValue() == "OSGInvWorldMatrix")
00230     {
00231         setOsgVarType(OSGInvWorldMatrix);
00232         setDependency(SHDObject        );
00233     }
00234     else if(_sfName.getValue() == "OSGTransInvWorldMatrix")
00235     {
00236         setOsgVarType(OSGTransInvWorldMatrix);
00237         setDependency(SHDObject             );
00238     }
00239     else if(_sfName.getValue() == "OSGCameraOrientation")
00240     {
00241         setOsgVarType(OSGCameraOrientation);
00242         setDependency(SHDScene            );
00243     }
00244     else if(_sfName.getValue() == "OSGCameraPosition")
00245     {
00246         setOsgVarType(OSGCameraPosition);
00247         setDependency(SHDScene         );
00248     }
00249     else if(_sfName.getValue() == "OSGViewMatrix")
00250     {
00251         setOsgVarType(OSGViewMatrix);
00252         setDependency(SHDScene     );
00253     }
00254     else if(_sfName.getValue() == "OSGInvViewMatrix")
00255     {
00256         setOsgVarType(OSGInvViewMatrix);
00257         setDependency(SHDScene        );
00258     }
00259     else if(_sfName.getValue() == "OSGStereoLeftEye")
00260     {
00261         setOsgVarType(OSGStereoLeftEye);
00262         setDependency(SHDScene        );
00263     }
00264     else if(_sfName.getValue() == "OSGClusterId")
00265     {
00266         setOsgVarType(OSGClusterId);
00267         setDependency(SHDScene    );
00268     }
00269     else if(_sfName.getValue() == "OSGActiveLightsMask")
00270     {
00271         setOsgVarType(OSGActiveLightsMask);
00272         setDependency(SHDObject          );
00273     }
00274     else if(_sfName.getValue() == "OSGLight0Active")
00275     {
00276         setOsgVarType(OSGLight0Active);
00277         setDependency(SHDObject      );
00278     }
00279     else if(_sfName.getValue() == "OSGLight1Active")
00280     {
00281         setOsgVarType(OSGLight1Active);
00282         setDependency(SHDObject      );
00283     }
00284     else if(_sfName.getValue() == "OSGLight2Active")
00285     {
00286         setOsgVarType(OSGLight2Active);
00287         setDependency(SHDObject      );
00288     }
00289     else if(_sfName.getValue() == "OSGLight3Active")
00290     {
00291         setOsgVarType(OSGLight3Active);
00292         setDependency(SHDObject      );
00293     }
00294     else if(_sfName.getValue() == "OSGLight4Active")
00295     {
00296         setOsgVarType(OSGLight4Active);
00297         setDependency(SHDObject      );
00298     }
00299     else if(_sfName.getValue() == "OSGLight5Active")
00300     {
00301         setOsgVarType(OSGLight5Active);
00302         setDependency(SHDObject      );
00303     }
00304     else if(_sfName.getValue() == "OSGLight6Active")
00305     {
00306         setOsgVarType(OSGLight6Active);
00307         setDependency(SHDObject      );
00308     }
00309     else if(_sfName.getValue() == "OSGLight7Active")
00310     {
00311         setOsgVarType(OSGLight7Active);
00312         setDependency(SHDObject      );
00313     }
00314     else
00315     {
00316         setOsgVarType(OSGUnknown);
00317         setDependency(SHDObject );
00318     }
00319 }
00320
00321 void ShaderVariableOSG::evaluate(DrawEnv *pEnv,
00322                                  Int32   &iLocation)
00323 {
00324     switch(_sfOsgVarType.getValue())
00325     {
00326         case OSGWorldMatrix:
00327         {
00328             Matrix m = pEnv->getObjectToWorld();
00329
00330             if(iLocation != -1)
00331             {
00332                 OSGGETGLFUNC(OSGglUniformMatrix4fvProc,
00333                              osgGlUniformMatrix4fv,
00334                              ShaderProgram::getFuncIdUniformMatrix4fv());
00335
00336                 osgGlUniformMatrix4fv(iLocation,
00337                                       1,
00338                                       GL_FALSE,
00339                                       m.getValues());
00340             }
00341         }
00342         break;
00343
00344         case OSGInvWorldMatrix:
00345         {
00346             Matrix m = pEnv->getObjectToWorld();
00347
00348             m.invert();
00349
00350             if(iLocation != -1)
00351             {
00352                 OSGGETGLFUNC(OSGglUniformMatrix4fvProc,
00353                              osgGlUniformMatrix4fv,
00354                              ShaderProgram::getFuncIdUniformMatrix4fv());
00355
00356                 osgGlUniformMatrix4fv(iLocation, 1, GL_FALSE, m.getValues());
00357             }
00358         }
00359         break;
00360
00361         case OSGTransInvWorldMatrix:
00362         {
00363             Matrix m = pEnv->getObjectToWorld();
00364
00365             m.invert();
00366             m.transpose();
00367
00368
00369             if(iLocation != -1)
00370             {
00371                 OSGGETGLFUNC(OSGglUniformMatrix4fvProc,
00372                              osgGlUniformMatrix4fv,
00373                              ShaderProgram::getFuncIdUniformMatrix4fv());
00374
00375                 osgGlUniformMatrix4fv(iLocation, 1, GL_FALSE, m.getValues());
00376             }
00377         }
00378         break;
00379
00380         case OSGCameraOrientation:
00381         {
00382             Matrix m = pEnv->getCameraToWorld();
00383
00384             m[3].setValues(0, 0, 0, 1);
00385
00386             if(iLocation != -1)
00387             {
00388                 OSGGETGLFUNC(OSGglUniformMatrix4fvProc,
00389                              osgGlUniformMatrix4fv,
00390                              ShaderProgram::getFuncIdUniformMatrix4fv());
00391
00392                 osgGlUniformMatrix4fv(iLocation, 1, GL_FALSE, m.getValues());
00393             }
00394         }
00395         break;
00396
00397         case OSGCameraPosition:
00398         {
00399             if(iLocation != -1)
00400             {
00401                 Matrix m = pEnv->getCameraToWorld();
00402
00403                 Vec3f cameraPos(m[3][0], m[3][1], m[3][2]);
00404
00405                 OSGGETGLFUNC(OSGglUniform3fvProc,
00406                              osgGlUniform3fv,
00407                              ShaderProgram::getFuncIdUniform3fv());
00408
00409                 osgGlUniform3fv(iLocation, 1, cameraPos.getValues());
00410             }
00411         }
00412         break;
00413
00414         case OSGViewMatrix:
00415         {
00416             Matrix m = pEnv->getCameraViewing();
00417
00418             if(iLocation != -1)
00419             {
00420                 OSGGETGLFUNC(OSGglUniformMatrix4fvProc,
00421                              osgGlUniformMatrix4fv,
00422                              ShaderProgram::getFuncIdUniformMatrix4fv());
00423
00424                 osgGlUniformMatrix4fv(iLocation, 1, GL_FALSE, m.getValues());
00425             }
00426         }
00427         break;
00428
00429         case OSGInvViewMatrix:
00430         {
00431             Matrix m = pEnv->getCameraToWorld();
00432
00433             if(iLocation != -1)
00434             {
00435                 OSGGETGLFUNC(OSGglUniformMatrix4fvProc,
00436                              osgGlUniformMatrix4fv,
00437                              ShaderProgram::getFuncIdUniformMatrix4fv());
00438
00439                 osgGlUniformMatrix4fv(iLocation, 1, GL_FALSE, m.getValues());
00440             }
00441         }
00442         break;
00443
00444         case OSGStereoLeftEye:
00445         {
00446         }
00447         break;
00448
00449         case OSGClusterId:
00450         {
00451         }
00452         break;
00453
00454         case OSGActiveLightsMask:
00455         {
00456             if(iLocation != -1)
00457             {
00458                 OSGGETGLFUNC(OSGglUniform1iProc,
00459                              osgGlUniform1i,
00460                              ShaderProgram::getFuncIdUniform1i());
00461
00462                 osgGlUniform1i(iLocation,
00463                                GLint(pEnv->getLightState()));
00464             }
00465         }
00466         break;
00467
00468         case OSGLight0Active:
00469         {
00470             if(iLocation != -1)
00471             {
00472                 OSGGETGLFUNC(OSGglUniform1iProc,
00473                              osgGlUniform1i,
00474                              ShaderProgram::getFuncIdUniform1i());
00475
00476                 osgGlUniform1i(iLocation,
00477                                GLint(pEnv->getLightState() & 0x0001));
00478             }
00479         }
00480         break;
00481
00482         case OSGLight1Active:
00483         {
00484             if(iLocation != -1)
00485             {
00486                 OSGGETGLFUNC(OSGglUniform1iProc,
00487                              osgGlUniform1i,
00488                              ShaderProgram::getFuncIdUniform1i());
00489
00490                 osgGlUniform1i(iLocation,
00491                                GLint(pEnv->getLightState() & 0x0002));
00492             }
00493         }
00494         break;
00495
00496         case OSGLight2Active:
00497         {
00498             if(iLocation != -1)
00499             {
00500                 OSGGETGLFUNC(OSGglUniform1iProc,
00501                              osgGlUniform1i,
00502                              ShaderProgram::getFuncIdUniform1i());
00503
00504                 osgGlUniform1i(iLocation,
00505                                GLint(pEnv->getLightState() & 0x0004));
00506             }
00507         }
00508         break;
00509
00510         case OSGLight3Active:
00511         {
00512             if(iLocation != -1)
00513             {
00514                 OSGGETGLFUNC(OSGglUniform1iProc,
00515                              osgGlUniform1i,
00516                              ShaderProgram::getFuncIdUniform1i());
00517
00518                 osgGlUniform1i(iLocation,
00519                                GLint(pEnv->getLightState() & 0x0008));
00520             }
00521         }
00522         break;
00523
00524         case OSGLight4Active:
00525         {
00526             if(iLocation != -1)
00527             {
00528                 OSGGETGLFUNC(OSGglUniform1iProc,
00529                              osgGlUniform1i,
00530                              ShaderProgram::getFuncIdUniform1i());
00531
00532                 osgGlUniform1i(iLocation,
00533                                GLint(pEnv->getLightState() & 0x0010));
00534             }
00535         }
00536         break;
00537
00538         case OSGLight5Active:
00539         {
00540             if(iLocation != -1)
00541             {
00542                 OSGGETGLFUNC(OSGglUniform1iProc,
00543                              osgGlUniform1i,
00544                              ShaderProgram::getFuncIdUniform1i());
00545
00546                 osgGlUniform1i(iLocation,
00547                                GLint(pEnv->getLightState() & 0x0020));
00548             }
00549         }
00550         break;
00551
00552         case OSGLight6Active:
00553         {
00554             if(iLocation != -1)
00555             {
00556                 OSGGETGLFUNC(OSGglUniform1iProc,
00557                              osgGlUniform1i,
00558                              ShaderProgram::getFuncIdUniform1i());
00559
00560                 osgGlUniform1i(iLocation,
00561                                GLint(pEnv->getLightState() & 0x0040));
00562             }
00563         }
00564         break;
00565
00566         case OSGLight7Active:
00567         {
00568             if(iLocation != -1)
00569             {
00570                 OSGGETGLFUNC(OSGglUniform1iProc,
00571                              osgGlUniform1i,
00572                              ShaderProgram::getFuncIdUniform1i());
00573
00574                 osgGlUniform1i(iLocation,
00575                                GLint(pEnv->getLightState() & 0x0080));
00576             }
00577         }
00578         break;
00579
00580         default:
00581         {
00582         }
00583         break;
00584     }
00585 }