1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
46 #include "OSGConfig.h"
48 #include "OSGShaderVariableOSG.h"
49 #include "OSGGLFuncProtos.h"
50 #include "OSGShaderProgram.h"
52 #include "OSGDrawEnv.h"
53 #include "OSGWindow.h"
56 #include "OSGConceptPropertyChecks.h"
60 // Documentation for this class is emited in the
61 // OSGShaderVariableOSGBase.cpp file.
62 // To modify it, please change the .fcd file (OSGShaderVariableOSG.fcd) and
63 // regenerate the base file.
65 /***************************************************************************\
67 \***************************************************************************/
69 /***************************************************************************\
71 \***************************************************************************/
73 void ShaderVariableOSG::initMethod(InitPhase ePhase
)
75 Inherited::initMethod(ePhase
);
79 /***************************************************************************\
81 \***************************************************************************/
83 /*-------------------------------------------------------------------------*\
85 \*-------------------------------------------------------------------------*/
87 /*----------------------- constructors & destructors ----------------------*/
89 ShaderVariableOSG::ShaderVariableOSG(void) :
92 setTypeId(ShaderVariable::SHVTypeOSG
);
95 ShaderVariableOSG::ShaderVariableOSG(const ShaderVariableOSG
&source
) :
100 ShaderVariableOSG::~ShaderVariableOSG(void)
104 /*----------------------------- class specific ----------------------------*/
106 void ShaderVariableOSG::changed(ConstFieldMaskArg whichField
,
110 Inherited::changed(whichField
, origin
, details
);
112 if(0x0000 != (whichField
& NameFieldMask
))
114 if(_sfName
.getValue() == "OSGWorldMatrix")
116 setOsgVarType(OSGWorldMatrix
);
117 setDependency(SHDObject
);
119 else if(_sfName
.getValue() == "OSGInvWorldMatrix")
121 setOsgVarType(OSGInvWorldMatrix
);
122 setDependency(SHDObject
);
124 else if(_sfName
.getValue() == "OSGTransInvWorldMatrix")
126 setOsgVarType(OSGTransInvWorldMatrix
);
127 setDependency(SHDObject
);
129 else if(_sfName
.getValue() == "OSGCameraOrientation")
131 setOsgVarType(OSGCameraOrientation
);
132 setDependency(SHDScene
);
134 else if(_sfName
.getValue() == "OSGCameraPosition")
136 setOsgVarType(OSGCameraPosition
);
137 setDependency(SHDScene
);
139 else if(_sfName
.getValue() == "OSGViewMatrix")
141 setOsgVarType(OSGViewMatrix
);
142 setDependency(SHDScene
);
144 else if(_sfName
.getValue() == "OSGInvViewMatrix")
146 setOsgVarType(OSGInvViewMatrix
);
147 setDependency(SHDScene
);
149 else if(_sfName
.getValue() == "OSGProjectionMatrix")
151 setOsgVarType(OSGProjectionMatrix
);
152 setDependency(SHDScene
);
154 else if(_sfName
.getValue() == "OSGModelViewMatrix")
156 setOsgVarType(OSGModelViewMatrix
);
157 setDependency(SHDObject
);
159 else if(_sfName
.getValue() == "OSGViewportSize")
161 setOsgVarType(OSGViewportSize
);
162 setDependency(SHDScene
);
164 #ifdef OSG_OGL_COREONLY
165 else if(_sfName
.getValue() == "OSGNormalMatrix")
167 setOsgVarType(OSGNormalMatrix
);
168 setDependency(SHDObject
);
170 else if(_sfName
.getValue() == "OSGModelViewProjectionMatrix")
172 setOsgVarType(OSGModelViewProjectionMatrix
);
173 setDependency(SHDObject
);
176 else if(_sfName
.getValue() == "OSGStereoLeftEye")
178 setOsgVarType(OSGStereoLeftEye
);
179 setDependency(SHDScene
);
181 else if(_sfName
.getValue() == "OSGDrawerId")
183 setOsgVarType(OSGDrawerId
);
184 setDependency(SHDScene
);
186 else if(_sfName
.getValue() == "OSGDrawableId")
188 setOsgVarType(OSGDrawableId
);
189 setDependency(SHDScene
);
191 else if(_sfName
.getValue() == "OSGNodeId")
193 setOsgVarType(OSGNodeId
);
194 setDependency(SHDObject
);
196 else if(_sfName
.getValue() == "OSGNodeBoxMin")
198 setOsgVarType(OSGNodeBoxMin
);
199 setDependency(SHDObject
);
201 else if(_sfName
.getValue() == "OSGNodeBoxMax")
203 setOsgVarType(OSGNodeBoxMax
);
204 setDependency(SHDObject
);
206 else if(_sfName
.getValue() == "OSGNodeBoxCenter")
208 setOsgVarType(OSGNodeBoxCenter
);
209 setDependency(SHDObject
);
211 else if(_sfName
.getValue() == "OSGNodeWorldBoxMin")
213 setOsgVarType(OSGNodeWorldBoxMin
);
214 setDependency(SHDObject
);
216 else if(_sfName
.getValue() == "OSGNodeWorldBoxMax")
218 setOsgVarType(OSGNodeWorldBoxMax
);
219 setDependency(SHDObject
);
221 else if(_sfName
.getValue() == "OSGNodeWorldBoxCenter")
223 setOsgVarType(OSGNodeWorldBoxCenter
);
224 setDependency(SHDObject
);
226 else if(_sfName
.getValue() == "OSGActiveLightsMask")
228 setOsgVarType(OSGActiveLightsMask
);
229 setDependency(SHDObject
);
231 else if(_sfName
.getValue() == "OSGLight0Active")
233 setOsgVarType(OSGLight0Active
);
234 setDependency(SHDObject
);
236 else if(_sfName
.getValue() == "OSGLight1Active")
238 setOsgVarType(OSGLight1Active
);
239 setDependency(SHDObject
);
241 else if(_sfName
.getValue() == "OSGLight2Active")
243 setOsgVarType(OSGLight2Active
);
244 setDependency(SHDObject
);
246 else if(_sfName
.getValue() == "OSGLight3Active")
248 setOsgVarType(OSGLight3Active
);
249 setDependency(SHDObject
);
251 else if(_sfName
.getValue() == "OSGLight4Active")
253 setOsgVarType(OSGLight4Active
);
254 setDependency(SHDObject
);
256 else if(_sfName
.getValue() == "OSGLight5Active")
258 setOsgVarType(OSGLight5Active
);
259 setDependency(SHDObject
);
261 else if(_sfName
.getValue() == "OSGLight6Active")
263 setOsgVarType(OSGLight6Active
);
264 setDependency(SHDObject
);
266 else if(_sfName
.getValue() == "OSGLight7Active")
268 setOsgVarType(OSGLight7Active
);
269 setDependency(SHDObject
);
273 setOsgVarType(OSGUnknown
);
274 setDependency(SHDObject
);
279 void ShaderVariableOSG::dump( UInt32
,
280 const BitVector
) const
282 SLOG
<< "Dump ShaderVariableOSG NI" << std::endl
;
285 bool ShaderVariableOSG::isProcedural(void) const
290 void ShaderVariableOSG::setName(const std::string
&value
)
292 Inherited::setName(value
);
294 if(_sfName
.getValue() == "OSGWorldMatrix")
296 setOsgVarType(OSGWorldMatrix
);
297 setDependency(SHDObject
);
299 else if(_sfName
.getValue() == "OSGInvWorldMatrix")
301 setOsgVarType(OSGInvWorldMatrix
);
302 setDependency(SHDObject
);
304 else if(_sfName
.getValue() == "OSGTransInvWorldMatrix")
306 setOsgVarType(OSGTransInvWorldMatrix
);
307 setDependency(SHDObject
);
309 else if(_sfName
.getValue() == "OSGCameraOrientation")
311 setOsgVarType(OSGCameraOrientation
);
312 setDependency(SHDScene
);
314 else if(_sfName
.getValue() == "OSGCameraPosition")
316 setOsgVarType(OSGCameraPosition
);
317 setDependency(SHDScene
);
319 else if(_sfName
.getValue() == "OSGViewMatrix")
321 setOsgVarType(OSGViewMatrix
);
322 setDependency(SHDScene
);
324 else if(_sfName
.getValue() == "OSGInvViewMatrix")
326 setOsgVarType(OSGInvViewMatrix
);
327 setDependency(SHDScene
);
329 else if(_sfName
.getValue() == "OSGProjectionMatrix")
331 setOsgVarType(OSGProjectionMatrix
);
332 setDependency(SHDScene
);
334 else if(_sfName
.getValue() == "OSGModelViewMatrix")
336 setOsgVarType(OSGModelViewMatrix
);
337 setDependency(SHDObject
);
339 else if(_sfName
.getValue() == "OSGViewportSize")
341 setOsgVarType(OSGViewportSize
);
342 setDependency(SHDScene
);
344 #ifdef OSG_OGL_COREONLY
345 else if(_sfName
.getValue() == "OSGNormalMatrix")
347 setOsgVarType(OSGNormalMatrix
);
348 setDependency(SHDObject
);
350 else if(_sfName
.getValue() == "OSGModelViewProjectionMatrix")
352 setOsgVarType(OSGModelViewProjectionMatrix
);
353 setDependency(SHDObject
);
356 else if(_sfName
.getValue() == "OSGStereoLeftEye")
358 setOsgVarType(OSGStereoLeftEye
);
359 setDependency(SHDScene
);
361 else if(_sfName
.getValue() == "OSGDrawerId")
363 setOsgVarType(OSGDrawerId
);
364 setDependency(SHDScene
);
366 else if(_sfName
.getValue() == "OSGDrawableId")
368 setOsgVarType(OSGDrawableId
);
369 setDependency(SHDScene
);
371 else if(_sfName
.getValue() == "OSGNodeId")
373 setOsgVarType(OSGNodeId
);
374 setDependency(SHDObject
);
376 else if(_sfName
.getValue() == "OSGNodeBoxMin")
378 setOsgVarType(OSGNodeBoxMin
);
379 setDependency(SHDObject
);
381 else if(_sfName
.getValue() == "OSGNodeBoxMax")
383 setOsgVarType(OSGNodeBoxMax
);
384 setDependency(SHDObject
);
386 else if(_sfName
.getValue() == "OSGNodeBoxCenter")
388 setOsgVarType(OSGNodeBoxCenter
);
389 setDependency(SHDObject
);
391 else if(_sfName
.getValue() == "OSGNodeWorldBoxMin")
393 setOsgVarType(OSGNodeWorldBoxMin
);
394 setDependency(SHDObject
);
396 else if(_sfName
.getValue() == "OSGNodeWorldBoxMax")
398 setOsgVarType(OSGNodeWorldBoxMax
);
399 setDependency(SHDObject
);
401 else if(_sfName
.getValue() == "OSGNodeWorldBoxCenter")
403 setOsgVarType(OSGNodeWorldBoxCenter
);
404 setDependency(SHDObject
);
406 else if(_sfName
.getValue() == "OSGActiveLightsMask")
408 setOsgVarType(OSGActiveLightsMask
);
409 setDependency(SHDObject
);
411 else if(_sfName
.getValue() == "OSGLight0Active")
413 setOsgVarType(OSGLight0Active
);
414 setDependency(SHDObject
);
416 else if(_sfName
.getValue() == "OSGLight1Active")
418 setOsgVarType(OSGLight1Active
);
419 setDependency(SHDObject
);
421 else if(_sfName
.getValue() == "OSGLight2Active")
423 setOsgVarType(OSGLight2Active
);
424 setDependency(SHDObject
);
426 else if(_sfName
.getValue() == "OSGLight3Active")
428 setOsgVarType(OSGLight3Active
);
429 setDependency(SHDObject
);
431 else if(_sfName
.getValue() == "OSGLight4Active")
433 setOsgVarType(OSGLight4Active
);
434 setDependency(SHDObject
);
436 else if(_sfName
.getValue() == "OSGLight5Active")
438 setOsgVarType(OSGLight5Active
);
439 setDependency(SHDObject
);
441 else if(_sfName
.getValue() == "OSGLight6Active")
443 setOsgVarType(OSGLight6Active
);
444 setDependency(SHDObject
);
446 else if(_sfName
.getValue() == "OSGLight7Active")
448 setOsgVarType(OSGLight7Active
);
449 setDependency(SHDObject
);
453 setOsgVarType(OSGUnknown
);
454 setDependency(SHDObject
);
458 void ShaderVariableOSG::evaluate(DrawEnv
*pEnv
,
461 Window
*pWin
= pEnv
->getWindow();
463 osgSinkUnusedWarning(pWin
);
465 switch(_sfOsgVarType
.getValue())
469 Matrix m
= pEnv
->getObjectToWorld();
473 OSGGETGLFUNCBYID_GL3_ES(
475 osgGlUniformMatrix4fv
,
476 ShaderProgram::getFuncIdUniformMatrix4fv(),
479 osgGlUniformMatrix4fv(iLocation
,
487 case OSGInvWorldMatrix
:
489 Matrix m
= pEnv
->getObjectToWorld();
495 OSGGETGLFUNCBYID_GL3_ES(
497 osgGlUniformMatrix4fv
,
498 ShaderProgram::getFuncIdUniformMatrix4fv(),
501 osgGlUniformMatrix4fv(iLocation
, 1, GL_FALSE
, m
.getValues());
506 case OSGTransInvWorldMatrix
:
508 Matrix m
= pEnv
->getObjectToWorld();
516 OSGGETGLFUNCBYID_GL3_ES(
518 osgGlUniformMatrix4fv
,
519 ShaderProgram::getFuncIdUniformMatrix4fv(),
522 osgGlUniformMatrix4fv(iLocation
, 1, GL_FALSE
, m
.getValues());
527 case OSGCameraOrientation
:
529 Matrix m
= pEnv
->getCameraToWorld();
531 m
[3].setValues(0, 0, 0, 1);
535 OSGGETGLFUNCBYID_GL3_ES(
537 osgGlUniformMatrix4fv
,
538 ShaderProgram::getFuncIdUniformMatrix4fv(),
541 osgGlUniformMatrix4fv(iLocation
, 1, GL_FALSE
, m
.getValues());
546 case OSGCameraPosition
:
550 Matrix m
= pEnv
->getCameraToWorld();
552 Vec3f
cameraPos(m
[3][0], m
[3][1], m
[3][2]);
554 OSGGETGLFUNCBYID_GL3_ES(
557 ShaderProgram::getFuncIdUniform3fv(),
560 osgGlUniform3fv(iLocation
, 1, cameraPos
.getValues());
567 Matrix m
= pEnv
->getCameraViewing();
571 OSGGETGLFUNCBYID_GL3_ES(
573 osgGlUniformMatrix4fv
,
574 ShaderProgram::getFuncIdUniformMatrix4fv(),
577 osgGlUniformMatrix4fv(iLocation
, 1, GL_FALSE
, m
.getValues());
582 case OSGInvViewMatrix
:
584 Matrix m
= pEnv
->getCameraToWorld();
588 OSGGETGLFUNCBYID_GL3_ES(
590 osgGlUniformMatrix4fv
,
591 ShaderProgram::getFuncIdUniformMatrix4fv(),
594 osgGlUniformMatrix4fv(iLocation
, 1, GL_FALSE
, m
.getValues());
599 case OSGProjectionMatrix
:
601 Matrix m
= pEnv
->_openGLState
.getProjection();
605 OSGGETGLFUNCBYID_GL3_ES(
607 osgGlUniformMatrix4fv
,
608 ShaderProgram::getFuncIdUniformMatrix4fv(),
611 osgGlUniformMatrix4fv(iLocation
, 1, GL_FALSE
, m
.getValues());
616 case OSGModelViewMatrix
:
618 Matrix m
= pEnv
->_openGLState
.getModelView();
622 OSGGETGLFUNCBYID_GL3_ES(
624 osgGlUniformMatrix4fv
,
625 ShaderProgram::getFuncIdUniformMatrix4fv(),
628 osgGlUniformMatrix4fv(iLocation
, 1, GL_FALSE
, m
.getValues());
633 case OSGViewportSize
:
635 Vec2f
viewportSize(pEnv
->getPixelWidth (),
636 pEnv
->getPixelHeight());
638 OSGGETGLFUNCBYID_GL3_ES(
641 ShaderProgram::getFuncIdUniform2fv(),
644 osgGlUniform2fv(iLocation
, 1, viewportSize
.getValues());
648 #ifdef OSG_OGL_COREONLY
649 case OSGNormalMatrix
:
651 Matrix m
= pEnv
->_openGLState
.getNormalMatrix();
655 OSGGETGLFUNCBYID_GL3_ES(
657 osgGlUniformMatrix4fv
,
658 ShaderProgram::getFuncIdUniformMatrix4fv(),
661 osgGlUniformMatrix4fv(iLocation
, 1, GL_FALSE
, m
.getValues());
666 case OSGModelViewProjectionMatrix
:
668 Matrix m
= pEnv
->_openGLState
.getModelViewProjection();
672 OSGGETGLFUNCBYID_GL3_ES(
674 osgGlUniformMatrix4fv
,
675 ShaderProgram::getFuncIdUniformMatrix4fv(),
678 osgGlUniformMatrix4fv(iLocation
, 1, GL_FALSE
, m
.getValues());
684 case OSGStereoLeftEye
:
693 OSGGETGLFUNCBYID_GL3_ES(glUniform1i
,
695 ShaderProgram::getFuncIdUniform1i(),
698 osgGlUniform1i(iLocation
,
699 GLint(pEnv
->getDrawerId()));
708 OSGGETGLFUNCBYID_GL3_ES(glUniform1i
,
710 ShaderProgram::getFuncIdUniform1i(),
713 osgGlUniform1i(iLocation
,
714 GLint(pEnv
->getDrawableId()));
723 if(pEnv
->getSGNode() != NULL
)
725 OSGGETGLFUNCBYID_GL3_ES(
728 ShaderProgram::getFuncIdUniform1i(),
731 osgGlUniform1i(iLocation
,
732 GLint(pEnv
->getSGNode()->getId()));
742 if(pEnv
->getSGNode() != NULL
)
744 OSGGETGLFUNCBYID_GL3_ES(
747 ShaderProgram::getFuncIdUniform3f(),
750 const BoxVolume
&bvBox
= pEnv
->getSGNode()->getVolume();
752 osgGlUniform3f(iLocation
,
765 if(pEnv
->getSGNode() != NULL
)
767 OSGGETGLFUNCBYID_GL3_ES(
770 ShaderProgram::getFuncIdUniform3f(),
773 const BoxVolume
&bvBox
= pEnv
->getSGNode()->getVolume();
775 osgGlUniform3f(iLocation
,
784 case OSGNodeBoxCenter
:
788 if(pEnv
->getSGNode() != NULL
)
790 OSGGETGLFUNCBYID_GL3_ES(
793 ShaderProgram::getFuncIdUniform3f(),
798 pEnv
->getSGNode()->getVolume().getCenter(vBoxCenter
);
800 osgGlUniform3f(iLocation
,
810 case OSGNodeWorldBoxMin
:
814 if(pEnv
->getSGNode() != NULL
)
816 OSGGETGLFUNCBYID_GL3_ES(
819 ShaderProgram::getFuncIdUniform3f(),
822 BoxVolume bvWorldBox
;
824 pEnv
->getSGNode()->getWorldVolume(bvWorldBox
);
826 osgGlUniform3f(iLocation
,
827 bvWorldBox
.getMin()[0],
828 bvWorldBox
.getMin()[1],
829 bvWorldBox
.getMin()[2]);
835 case OSGNodeWorldBoxMax
:
839 if(pEnv
->getSGNode() != NULL
)
841 OSGGETGLFUNCBYID_GL3_ES(
844 ShaderProgram::getFuncIdUniform3f(),
847 BoxVolume bvWorldBox
;
849 pEnv
->getSGNode()->getWorldVolume(bvWorldBox
);
851 osgGlUniform3f(iLocation
,
852 bvWorldBox
.getMax()[0],
853 bvWorldBox
.getMax()[1],
854 bvWorldBox
.getMax()[2]);
860 case OSGNodeWorldBoxCenter
:
864 if(pEnv
->getSGNode() != NULL
)
866 OSGGETGLFUNCBYID_GL3_ES(
869 ShaderProgram::getFuncIdUniform3f(),
872 BoxVolume bvWorldBox
;
873 Pnt3f vWorldBoxCenter
;
875 pEnv
->getSGNode()->getWorldVolume(bvWorldBox
);
877 bvWorldBox
.getCenter(vWorldBoxCenter
);
879 osgGlUniform3f(iLocation
,
888 case OSGActiveLightsMask
:
892 OSGGETGLFUNCBYID_GL3_ES(
895 ShaderProgram::getFuncIdUniform1i(),
898 osgGlUniform1i(iLocation
,
899 GLint(pEnv
->getLightState()));
904 case OSGLight0Active
:
908 OSGGETGLFUNCBYID_GL3_ES(glUniform1i
,
910 ShaderProgram::getFuncIdUniform1i(),
913 osgGlUniform1i(iLocation
,
914 GLint(pEnv
->getLightState() & 0x0001));
919 case OSGLight1Active
:
923 OSGGETGLFUNCBYID_GL3_ES(glUniform1i
,
925 ShaderProgram::getFuncIdUniform1i(),
928 osgGlUniform1i(iLocation
,
929 GLint(pEnv
->getLightState() & 0x0002));
934 case OSGLight2Active
:
938 OSGGETGLFUNCBYID_GL3_ES(glUniform1i
,
940 ShaderProgram::getFuncIdUniform1i(),
943 osgGlUniform1i(iLocation
,
944 GLint(pEnv
->getLightState() & 0x0004));
949 case OSGLight3Active
:
953 OSGGETGLFUNCBYID_GL3_ES(glUniform1i
,
955 ShaderProgram::getFuncIdUniform1i(),
958 osgGlUniform1i(iLocation
,
959 GLint(pEnv
->getLightState() & 0x0008));
964 case OSGLight4Active
:
968 OSGGETGLFUNCBYID_GL3_ES(glUniform1i
,
970 ShaderProgram::getFuncIdUniform1i(),
973 osgGlUniform1i(iLocation
,
974 GLint(pEnv
->getLightState() & 0x0010));
979 case OSGLight5Active
:
983 OSGGETGLFUNCBYID_GL3_ES(glUniform1i
,
985 ShaderProgram::getFuncIdUniform1i(),
988 osgGlUniform1i(iLocation
,
989 GLint(pEnv
->getLightState() & 0x0020));
994 case OSGLight6Active
:
998 OSGGETGLFUNCBYID_GL3_ES(glUniform1i
,
1000 ShaderProgram::getFuncIdUniform1i(),
1003 osgGlUniform1i(iLocation
,
1004 GLint(pEnv
->getLightState() & 0x0040));
1009 case OSGLight7Active
:
1013 OSGGETGLFUNCBYID_GL3_ES(glUniform1i
,
1015 ShaderProgram::getFuncIdUniform1i(),
1018 osgGlUniform1i(iLocation
,
1019 GLint(pEnv
->getLightState() & 0x0080));