[CgFX] Restore rendering states in MotionBuilder

Started by
0 comments, last by GuillaumeLe 15 years, 10 months ago
Hi, I'm currently working on a Soft Shadows plug-in for MotionBuilder. I started from the .cg examples provided in the sdk and switch them to use .cgfx files. My cgfx shader rendering is okay, but I'm unable to restore OpenGl rendering states after. Render with Simple.cg Render with Simple.cgfx How save and restore the rendering state in order to leave my CgfxDraw() cleanly ? i.e without perturbate other rendering Shader loading(.cg and .cgfx)
 
	if( mContext )
	{
		FBProgress Progress;
		Progress.Caption = "Loading Cg Shader...";

        // We need the full path to our Cg file.
        FBSystem lSystem;
        FBString lCgFile( lSystem.ApplicationPath );
        lCgFile += "\\..\\..\\OpenRealitySDK\\Samples\\shadersVersionnes\\cgshaderSpecular\\Specular.cg";

        //Load simple.cg program
		mProgram = cgCreateProgramFromFile(mContext, CG_SOURCE, (char*)lCgFile, mProfile, NULL, NULL);
		if( mProgram ) cgGLLoadProgram(mProgram);
		else
		{
			ShowError();
			return false;
		}

		//Get program parameters
		mParamLight = cgGetNamedParameter(mProgram, "LightVec");
		mParamMaterialSpecularColor = cgGetNamedParameter(mProgram, "SpecularMaterial");
		mParamModelViewProj = cgGetNamedParameter(mProgram, "ModelViewProj");
		mParamModelViewIT = cgGetNamedParameter(mProgram, "ModelViewIT");
	    if( !mParamLight || !mParamModelViewProj || !mParamModelViewIT  || !mParamMaterialSpecularColor)
		{
			ShowError("Unable to retrieve vertex program parameters.");
			return false;
		}

		//Cgfx part
		FBString lCgFxFile(lSystem.ApplicationPath);
		lCgFxFile += "\\..\\..\\OpenRealitySDK\\Samples\\shadersVersionnes\\ProjectFXComposer\\Simple.cgfx";
		mCgEffect = cgCreateEffectFromFile(mContext, (char*)lCgFxFile, NULL);
		CheckForCgError("creating Simple.cgfx effect");

		//Get the first available technique
		mCgTechnique = cgGetFirstTechnique(mCgEffect);
		
		//Get the different parameters
		mParamLightCGFX = cgGetNamedEffectParameter(mCgEffect, "LightVec");
		mParamMaterialSpecularColorCGFX = cgGetNamedEffectParameter(mCgEffect, "SpecularMaterial");
		mParamModelViewProjCGFX = cgGetNamedEffectParameter(mCgEffect, "ModelViewProj");
		mParamModelViewITCGFX = cgGetNamedEffectParameter(mCgEffect, "ModelViewIT");

	}





CgDraw() (provided as SDK example, ok)

if( pShaderModelInfo != NULL )
	{
		cgGLBindProgram(mProgram);		//Bind program. (You can bind only one vertex and one fragment program at a time)
		cgGLEnableProfile(mProfile);

		// Read & set from animatable parameter
		FBColor lColor1 = mColor1;
		
		cgGLSetParameter3f(mParamMaterialSpecularColor,lColor1[0],lColor1[1],lColor1[2]);

		cgGLSetParameter4f(mParamLight, 0.0, 0.0, 1.0, 1.0);

		cgGLSetStateMatrixParameter(mParamModelViewProj, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
		cgGLSetStateMatrixParameter(mParamModelViewIT, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_INVERSE_TRANSPOSE);

		glEnable(GL_LIGHTING);
			DrawGeometry(pShaderModelInfo);
		glDisable(GL_LIGHTING);

	    cgGLDisableProfile(mProfile);
	}





CgFxDraw() (Mine, problematic)

if( pShaderModelInfo != NULL )
	{
		CGpass myCGpass;

		// Read & set from animatable parameter
		FBColor lColor1 = mColor1;
		
		cgGLSetParameter3f(mParamMaterialSpecularColorCGFX,lColor1[0],lColor1[1],lColor1[2]);
		cgGLSetParameter4f(mParamLightCGFX, 0.0, 0.0, 1.0, 1.0);
		cgGLSetStateMatrixParameter(mParamModelViewProjCGFX, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
		cgGLSetStateMatrixParameter(mParamModelViewITCGFX, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_INVERSE_TRANSPOSE);

		//If several pass are needed
		myCGpass = cgGetFirstPass(mCgTechnique);
		while (myCGpass) {
			cgSetPassState(myCGpass);
			DrawGeometry(pShaderModelInfo);
			cgResetPassState(myCGpass);
			myCGpass =cgGetNextPass(myCGpass);
		}

	}





Thanks [Edited by - GuillaumeLe on July 13, 2008 12:05:20 PM]
Advertisement
I Found the problem, in the Simple.cgfx file i have to disable faceCulling (optional) and DepthTest (needed) like this :

technique technique0 {	pass p0 {		//DepthTestEnable = true;		//CullFaceEnable = true;			VertexProgram = compile vp40 simpleVS(ModelViewProj,             ModelViewIT,             LightVec,			 SpecularMaterial);		FragmentProgram = compile fp40 SimplePS();	}}


I don't figure exactly whats going on but it works.

This topic is closed to new replies.

Advertisement