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 "OSGCgFXTechnique.h"
49 #include "OSGCgFXPassChunk.h"
50 #include "OSGCgFXMaterial.h"
52 #include "OSGWindow.h"
53 #include "OSGDrawEnv.h"
54 #include "OSGNameAttachment.h"
55 #include "OSGTextureObjChunk.h"
57 #include "OSGCgFXVariableTexObj.h"
61 /*! \class osg::CGFXTechnique
64 /***************************************************************************\
66 \***************************************************************************/
68 /***************************************************************************\
70 \***************************************************************************/
72 void CgFXTechnique::initMethod(InitPhase ePhase
)
74 Inherited::initMethod(ePhase
);
77 void CgFXTechnique::resolveLinks(void)
83 /***************************************************************************\
85 \***************************************************************************/
87 /*-------------------------------------------------------------------------*\
89 \*-------------------------------------------------------------------------*/
91 /*----------------------- constructors & destructors ----------------------*/
93 CgFXTechnique::CgFXTechnique(void) :
96 _uiValidationState(0 )
100 CgFXTechnique::CgFXTechnique(const CgFXTechnique
&source
) :
102 _pCGTechnique (NULL
),
103 _uiValidationState(0 )
107 CgFXTechnique::~CgFXTechnique(void)
111 /*----------------------------- class specific ----------------------------*/
113 void CgFXTechnique::changed(ConstFieldMaskArg whichField
,
117 Inherited::changed(whichField
, origin
, detail
);
120 void CgFXTechnique::dump( UInt32
,
121 const BitVector
) const
123 SLOG
<< "Dump CgFXTechnique NI" << std::endl
;
127 /*---------------------------------- Access -------------------------------*/
129 void CgFXTechnique::rebuildState(void)
133 bool CgFXTechnique::isTransparent(void) const
135 // not sure, CHECK_TOOLBOX_MERGE
141 UInt32
CgFXTechnique::getNPasses(void)
143 return UInt32(_mfRenderPassStates
.size());
146 State
*CgFXTechnique::getState(UInt32 index
)
148 OSG_ASSERT(index
< _mfRenderPassStates
.size());
150 return _mfRenderPassStates
[index
];
153 /*------------------------------------ Set --------------------------------*/
155 bool CgFXTechnique::validate(CgFXMaterial
*pMat
,
158 if(0x00 == (_uiValidationState
& ValidationTried
))
161 fprintf(stderr
, "Validate %p\n", _pCGTechnique
);
164 _uiValidationState
= 0x03;
173 if(_pCGTechnique
== NULL
||
174 cgValidateTechnique(_pCGTechnique
) == CG_FALSE
)
176 CgFXMaterial::checkForCgError("cgValidateTechnique", NULL
);
178 _uiValidationState
= 0x02;
182 CgFXMaterial::checkForCgError("cgValidateTechnique", NULL
);
184 const CgFXMaterial::MFTexturesType
*pTextures
=
185 pMat
->getMFTextures();
187 CGpass pPass
= cgGetFirstPass(_pCGTechnique
);
189 CgFXMaterial::checkForCgError("cgGetFirstPass", NULL
);
191 for(UInt32 i
= 0; i
< pTextures
->size(); ++i
)
193 const Char8
*szTexParamName
= getName((*pTextures
)[i
]);
194 Int32 iTexParamVal
= -1;
196 const ShaderVariable
*pVar
= pMat
->getVariable(szTexParamName
);
198 OSG_ASSERT(pVar
!= NULL
);
200 const CgFXVariableTexObj
*pTexVar
=
201 static_cast<const CgFXVariableTexObj
*>(pVar
);
203 OSG_ASSERT(pTexVar
!= NULL
);
205 iTexParamVal
= pTexVar
->getValue();
207 if(iTexParamVal
== -1)
210 cgGetNamedEffectParameter(pMat
->getEffect(),
213 CgFXMaterial::checkForCgError("cgGetNamedEffectParameter",
216 (*pTextures
)[i
]->activate(pEnv
, 0);
218 Window::GLObjectId texObjId
=
219 pEnv
->getWindow()->getGLObjectId(
220 (*pTextures
)[i
]->getGLId());
222 cgGLSetTextureParameter(pParam
, texObjId
);
224 CgFXMaterial::checkForCgError("cgGLSetTextureParameter",
227 cgSetSamplerState (pParam
);
229 CgFXMaterial::checkForCgError("cgSetSamplerState",
232 CgFXVariableTexObj
*pTexVarW
=
233 const_cast<CgFXVariableTexObj
*>(pTexVar
);
235 pTexVarW
->setValue(texObjId
);
237 (*pTextures
)[i
]->deactivate(pEnv
);
245 StateUnrecPtr pState
= State ::create();
246 CgFXPassChunkUnrecPtr pChunk
= CgFXPassChunk::create();
248 pChunk
->setPass (pPass
);
249 pChunk
->setMaterial(pMat
);
251 pState
->addChunk(pChunk
);
253 this->addPassState(pState
);
255 pPass
= cgGetNextPass(pPass
);
257 CgFXMaterial::checkForCgError("cgGetNextPass", NULL
);
262 pMat
->updateUniformVariables();
266 if(_pCGTechnique
!= NULL
)
268 fprintf(stderr
, "Validated technique %s : %x\n",
269 cgGetTechniqueName(_pCGTechnique
),
270 UInt32(_uiValidationState
));
282 return (_uiValidationState
& TechniqueValid
);
285 /*------------------------------------ Get --------------------------------*/