Light shader cg and opengl

hy.
I have created a simple phong model shader in cg,
The problem is that if i debug the shader in fx composer work fine , but if i insert it in opengl c++ project, the mesh appear black and without any light.
In c++ opengl project i dont have any light so.
There is a corrispondence or a relation with opengl lights and shaders light other the cg costant buffer that i can send to the shader?



float4x4 WMatrix;
float4x4 WMatrixIT;
float4x4 WVPMatrix ;

float4 Light0WorldDirection ;
float4 Light1WorldDirection ;
float4 Light2WorldDirection ;

float4 CameraModelPosition;

float AmbientIntensity = 0.1;
float4 AmbientColor : Ambient = float4(.5,.5,.5,1);

#define MaxLights 3

float DiffuseIntensity[MaxLights] = {1,1,1};
float4 DiffuseColor[MaxLights] : Diffuse = {float4(1,0,0,1),float4(0,1,0,1),float4(0,0,1,1)};
float3 LightDirection[MaxLights] : Direction = {float3(1,0,0),float3(0,1,0),float3(0,0,1)};


struct VS_IN
{
	float4 Position : POSITION;
	float3 Normal : NORMAL;
	float3 Tex : TEXCOORD0;
};

struct VS_OUT
{
	float4 Position : POSITION;
	float3 Light[MaxLights] : TEXCOORD0;
	float3 Normal : TEXCOORD3;
	float4 View : TEXCOORD4;
	float3 Tex : TEXCOORD5;
};

struct PS_OUT
{
	float4 Color : COLOR;
};

VS_OUT mainVS(VS_IN input)
{
	VS_OUT output;

	
	output.Position = mul(input.Position,WVPMatrix);
	
	// Normalize the light directions
	if(length(Light0WorldDirection)>0)
		output.Light[0] = normalize(Light0WorldDirection).xyz;
	else
		output.Light[0] = float3(0, 0, 0);
		
	if(length(Light1WorldDirection)>0)
		output.Light[1] = normalize(Light1WorldDirection).xyz;
	else
		output.Light[1] = float3(0, 0, 0);
	
	if(length(Light2WorldDirection)>0)
		output.Light[2] = normalize(Light2WorldDirection).xyz;
	else
		output.Light[2] = float3(0, 0, 0);
	
	
	Light0WorldDirection = float4(1, 0 ,0, 0);
	output.Light[0] = Light0WorldDirection.xyz;
	
	Light1WorldDirection = float4(0, 1 ,0, 0);
	output.Light[1] = Light1WorldDirection.xyz;
		
	Light2WorldDirection = float4(-1, 1 ,0, 0);
	output.Light[2] = Light2WorldDirection.xyz;
	
	float3 norNormal;
	float3 NormalN = (mul(float4(input.Normal.xyz,1),WMatrixIT)).xyz;	
	
	if(length(NormalN)!=0)
		norNormal = normalize(NormalN);
	else
		norNormal = float3(0, 0, 0);
	
	float4 PosWorld = normalize(mul(input.Position, WMatrix)).xyzw;
    output.View = CameraModelPosition - PosWorld;         // V
    
	float3 NN =mul(float4(norNormal,1), WMatrix).xyz;
	if(length(NN)!=0 )
		output.Normal = normalize(NN);     // N
	else
		output.Normal = float3(0, 0, 0);     // N
	
	output.Tex = input.Tex;
	return output;
}




PS_OUT mainPS(VS_OUT input)
{
	PS_OUT output = (PS_OUT)0;
	
	// Get ambient light
	float4 diffuse = { 1.0f, 0.0f, 0.0f, 1.0f};
    float4 ambient = {0.1, 0.0, 0.0, 1.0}; 
	
	// Get diffuse light
	float4 TotalDiffuseColor = float4(0,0,0,0);
	float4 TotalReflectColor = float4(0,0,0,0);
	float4 TotalSpecularColor = float4(0,0,0,0);
	float3 ViewDir;   
	if(length(input.View)!=0)
    	ViewDir = normalize(input.View).xyz; 
	else
		ViewDir = float3(0, 0, 0);	
	float3 LightDir;
	float3 Reflect = float3(0, 0, 0);
	float4 specular = float4(0, 0, 0, 0);
	float4 Diff = float4(0, 0, 0, 0);
	for(int l=0;l<MaxLights;l++){
		LightDir = (input.Light[l]);
		Diff = (DiffuseIntensity[l] * DiffuseColor[l]) * saturate(dot(input.Normal,LightDir));
		//Reflect = 2 * Diff.xyz * input.Normal - LightDir;
		//specular = pow(saturate(dot(Reflect, ViewDir)), 8); //
		TotalDiffuseColor += Diff;
		//TotalReflectColor += float4(Reflect,1);
		//TotalSpecularColor += specular;

		}

	
	//float4 diffuseColorTexture = tex2D(BaseSampler,input.Tex);
	output.Color = ambient + diffuse  * TotalDiffuseColor  + TotalSpecularColor;
	
	return output;
}

technique technique0 {
	pass p0 {
		CullFaceEnable = false;	
		VertexProgram = compile vp40 mainVS();
		FragmentProgram = compile fp40 mainPS();
	}
}

thanks.

You’re using user-defined names for your uniforms. So you have to manually “plug them in” via your C++ application. As-is, they don’t correspond to the GL fixed-function light source values.

Been a while since I’ve done Cg, but used to be you could use the glstate.* values when targetting ARB_vertex_program/ARB_fragment_program (now ancient) to get Cg to pull in the fixed-function values. There was also ways to wire specific matrices (e.g. MODELVIEW) to a specific set of matrices, and then tell the C++ side to “track” changes to that fixed-function matrix automagically (TrackMatrix APIs).

Of course, you can just plug them in via the Cg API. Seems like Cg also supports BUFFERS (like UBOs) for specifying uniforms as well.

BTW, another possibility is that you’re providing bogus vertex attributes (e.g. null normals, etc.) which could be messing up your lighting.

Thanks.
I have a doubt:
if i have no light in opengl(GL_ENABLE ecc…)i have no correct beavior of the shader?
Light in opengl are “hidden” related with light of the shader?

If your shader does not reference OpenGL built-in symbols related to light sources and lighting (accessible with #version 120 and earlier), then the state of OpenGL fixed-function light attributes and enables makes absolutely no difference at all.

sorry , but i’m not understand all,
I’m need or not to create light in opengl(GL_LIGHT(LIGHT_0)ecc…) if i use lighting shader ?

i find this :
http://idlastro.gsfc.nasa.gov/idl_html_help/Lighting_Shaders.html

that say that opengl send an array of lightingposition and other charactestic to the shader,i’m understand?

thanks again and excuse me for my english

You have a choice

  1. [li]Use your own uniforms (in which case OpenGL “light” state doesn’t matter), OR[*]Use the legacy built-in uniforms (only available in GLSL 1.2 and earlier; and in which case OpenGL “light” state does matter and is fed into your shader).

As an example of the former, call your light0 diffuse uniform whatever you want (light0_diffuse, or whatever), use it in the shader, and then what you set for GL_LIGHT0’s GL_DIFFUSE makes absolutely no difference to your shader because you don’t use it. You have to populate it yourself via glUniform set API.

Make sense?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.