Jump to content
  • Advertisement
Sign in to follow this  
blueShadow

stupid problem with Cg (updated)

This topic is 4001 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi ! I'm trying a basic lighting shader with cg but nothing appears at the screen... I just have a warning which says (in the code just after the structure): "profile option 'NumTemps' value <40> too large ; clamped to 32 and an error : "invalid handle parameter" in the code just before the vertex program (indicated below), but i don't know what it is...

struct cgParameters
{
	CGcontext cgContext; 
	CGprogram cgPerPixelLightingVP, cgPerPixelLightingFP ;
	//CGprogram cgEnvMappingVP, cgEnvMappingFP;
	CGprofile cgVertexProfile; 
	CGprofile cgFragmentProfile;
	CGparameter position, color, modelViewProjMatrix, modelViewMatrix, lightPosition, shininess; 
};
...
cgParameters cgParam ;
...






 //code which seems to be correct
        cgParam.cgContext = cgCreateContext();
	cgParam.cgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
	cgGLSetOptimalOptions(cgParam.cgVertexProfile);
	cgParam.cgPerPixelLightingVP = cgCreateProgramFromFile(cgParam.cgContext, CG_SOURCE, "PerPixelLightingVP.cg", cgParam.cgVertexProfile, "vs_main", 0);
	if (!cgParam.cgPerPixelLightingVP)
	{
		cout<<"erreur VP cgCreateProgramFromFile"<<endl;
		CGerror Error = cgGetError();
		printf("%s\n",cgGetErrorString(Error));
		printf("%s\n",cgGetLastListing(cgParam.cgContext));
	}
	cgGLLoadProgram(cgParam.cgPerPixelLightingVP);
	
	cgParam.position			= cgGetNamedParameter(cgParam.cgPerPixelLightingVP, "IN.position");
	cgParam.color				= cgGetNamedParameter(cgParam.cgPerPixelLightingVP, "IN.color");
	cgParam.modelViewProjMatrix	= cgGetNamedParameter(cgParam.cgPerPixelLightingVP, "ModelViewProj");
	cgParam.modelViewMatrix		= cgGetNamedParameter(cgParam.cgPerPixelLightingVP, "ModelView");
	cgParam.lightPosition		= cgGetNamedParameter(cgParam.cgPerPixelLightingVP, "IN.lightPosition");
	cgParam.shininess			= cgGetNamedParameter(cgParam.cgPerPixelLightingVP, "IN.shininess");
	
	CGerror Error = cgGetError();
	printf("%s\n",cgGetErrorString(Error));
	printf("%s%s\n","ici", cgGetLastListing(cgParam.cgContext));

	cgParam.cgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
	cgGLSetOptimalOptions(cgParam.cgFragmentProfile);
	cgParam.cgPerPixelLightingFP = cgCreateProgramFromFile(cgParam.cgContext, CG_SOURCE, "PerPixelLightingFP.cg", cgParam.cgFragmentProfile, "ps_main", 0);
	
	if (!cgParam.cgPerPixelLightingFP)
		cout<<"erreur FP cgCreateProgramFromFile"<<endl;
	
	cgGLLoadProgram(cgParam.cgPerPixelLightingFP);





//error here : "invalid handle parameter"
	float test_white[]= {1.0f,1.0f,1.0f,1.0f };
	float shininess = 90.0f ;

	cgGLEnableProfile(myLoader->getCgParam().cgVertexProfile);
	cgGLBindProgram(myLoader->getCgParam().cgPerPixelLightingVP);
	cgGLSetParameter3fv(myLoader->getCgParam().lightPosition, lightPosition);
	cgGLSetParameter1f(myLoader->getCgParam().shininess, shininess);
	cgGLSetParameter4fv(myLoader->getCgParam().color, test_white);
	cgGLSetStateMatrixParameter(myLoader->getCgParam().modelViewMatrix, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_IDENTITY);
	cgGLSetStateMatrixParameter(myLoader->getCgParam().modelViewProjMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
	
	/*CGerror Error = cgGetError();
	printf("%s\n",cgGetErrorString(Error));
	printf("%s\n",cgGetLastListing(myLoader->getCgParam().cgContext));*/

	cgGLEnableProfile(myLoader->getCgParam().cgFragmentProfile);
	cgGLBindProgram(myLoader->getCgParam().cgPerPixelLightingFP);
	
	/*CGerror Error = cgGetError();
	printf("%s\n",cgGetErrorString(Error));
	printf("%s\n",cgGetLastListing(myLoader->getCgParam().cgContext));*/
	






Vertex program:
struct vsIn
{
   float4 position : POSITION ;
   float3 normal   : NORMAL   ;
   float4 color    : COLOR  ;
   float3 lightPosition ;
   
};

struct vsOut
{
   float4 position    : POSITION ;
   float4 color       : COLOR ;
   float3 normal      : TEXCOORD1 ;
   float3 lightVector : TEXCOORD0 ;
};

//float3 normal   : NORMAL0   ;

vsOut vs_main(vsIn IN, uniform float4x4 ModelViewProj, vsOut OUT)
{
   
   
   OUT.lightVector = IN.lightPosition.xyz - IN.position.xyz ;
   //OUT.normal.xyz = normalize(IN.normal) ;
   IN.position = mul(IN.position, ModelViewProj);
   OUT.color = float4(0, 1, 1, 1);
   OUT.normal = IN.normal ;
   OUT.position = IN.position ;
   
   return OUT ;
}





Fragment program :
struct psIn
{
   float4 color       : COLOR ;
   float3 normal      : TEXCOORD1 ;
   float3 lightVector : TEXCOORD0 ;
};

struct psOut
{
   float4 color : COLOR1 ;
};


psOut ps_main(psIn IN, psOut OUT)
{
   
   IN.lightVector = normalize(IN.lightVector);
   float intensity = max(0.0, dot(IN.normal, IN.lightVector));
   OUT.color = float4(1,1,1,1);//intensity * IN.color ;
   return OUT ;
}





[Edited by - blueShadow on November 2, 2007 11:25:53 AM]

Share this post


Link to post
Share on other sites
Advertisement
I didn't check you code in depth but I see you transform IN.position to screen space prior to using it in the lighting calculations. I guess IN.normal and lightPosition are given in world space so you have an inconsistency here (you subtract a screen space vector from a world space vector).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!