changed: gcc8 base update
[opensg.git] / Source / Contrib / CgFXMaterial / OSGCgFXTechnique.cpp
blob4255477f883f3cdd5dc30f08b48cd11c37f8ab06
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
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. *
18 * *
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. *
23 * *
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. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <stdlib.h>
44 #include <stdio.h>
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"
59 OSG_BEGIN_NAMESPACE
61 /*! \class osg::CGFXTechnique
64 /***************************************************************************\
65 * Class variables *
66 \***************************************************************************/
68 /***************************************************************************\
69 * Class methods *
70 \***************************************************************************/
72 void CgFXTechnique::initMethod(InitPhase ePhase)
74 Inherited::initMethod(ePhase);
77 void CgFXTechnique::resolveLinks(void)
79 _pCGTechnique = NULL;
83 /***************************************************************************\
84 * Instance methods *
85 \***************************************************************************/
87 /*-------------------------------------------------------------------------*\
88 - private -
89 \*-------------------------------------------------------------------------*/
91 /*----------------------- constructors & destructors ----------------------*/
93 CgFXTechnique::CgFXTechnique(void) :
94 Inherited ( ),
95 _pCGTechnique (NULL),
96 _uiValidationState(0 )
100 CgFXTechnique::CgFXTechnique(const CgFXTechnique &source) :
101 Inherited (source),
102 _pCGTechnique (NULL ),
103 _uiValidationState(0 )
107 CgFXTechnique::~CgFXTechnique(void)
111 /*----------------------------- class specific ----------------------------*/
113 void CgFXTechnique::changed(ConstFieldMaskArg whichField,
114 UInt32 origin,
115 BitVector detail)
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
136 //return true;
138 return false;
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,
156 DrawEnv *pEnv)
158 if(0x00 == (_uiValidationState & ValidationTried))
160 #if 0
161 fprintf(stderr, "Validate %p\n", _pCGTechnique);
162 #endif
164 _uiValidationState = 0x03;
166 #if 0
167 if(pWin != NULL)
169 pWin->activate();
171 #endif
173 if(_pCGTechnique == NULL ||
174 cgValidateTechnique(_pCGTechnique) == CG_FALSE )
176 CgFXMaterial::checkForCgError("cgValidateTechnique", NULL);
178 _uiValidationState = 0x02;
180 else
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)
209 CGparameter pParam =
210 cgGetNamedEffectParameter(pMat->getEffect(),
211 szTexParamName);
213 CgFXMaterial::checkForCgError("cgGetNamedEffectParameter",
214 NULL);
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",
225 NULL);
227 cgSetSamplerState (pParam );
229 CgFXMaterial::checkForCgError("cgSetSamplerState",
230 NULL);
232 CgFXVariableTexObj *pTexVarW =
233 const_cast<CgFXVariableTexObj *>(pTexVar);
235 pTexVarW->setValue(texObjId);
237 (*pTextures)[i]->deactivate(pEnv);
241 int count = 0;
243 while(pPass)
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);
259 count++;
262 pMat->updateUniformVariables();
265 #if 0
266 if(_pCGTechnique != NULL)
268 fprintf(stderr, "Validated technique %s : %x\n",
269 cgGetTechniqueName(_pCGTechnique),
270 UInt32(_uiValidationState));
272 #endif
274 #if 0
275 if(pWin != NULL)
277 pWin->deactivate();
279 #endif
282 return (_uiValidationState & TechniqueValid);
285 /*------------------------------------ Get --------------------------------*/
287 OSG_END_NAMESPACE