GLSL Shader

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Dog-E
Posts: 28
Joined: Sun Mar 30, 2008 5:52 am

GLSL Shader

Post by Dog-E »

Hello everyone,
I've been having an issue with this simple directional light shader that I wrote (GLSL). I had a few compilation errors on Irrlicht compiler but fixed those. The problem is, as soon as I run the application, I get access violation error.

I'm posting the code here to see if anyone would like to add an input to this.


Irrlicht:

Code: Select all

void FXDirectionalLight::OnSetConstants(
		IMaterialRendererServices* services, 
		s32 userData)
{	
	f32 lightParam[18] = {
			1, 1, 1, 
			1, 1, 1, 
			1, 1, 1, 1, 
			1, 1, 1, 1,
			1, 1, 1, 1
			};
	
	//LIGHT_PARAMETERS light1;
	//light1.position 	= VEC3D(1,1,1);
	//light1.halfVector 	= VEC3D(1,1,1);
	//light1.ambient 		= VEC4D(1,1,1,1);
	//light1.diffuse 		= VEC4D(1,1,1,1);
	//light1.specular 	= VEC4D(1,1,1,1);
	
	f32 lightcount = 1;
	services->setVertexShaderConstant("lightCount", &lightcount, 1);
	services->setVertexShaderConstant("lightList", lightParam, 18);
}

VERT

Code: Select all

struct lightStruct
{
	vec3 position;
	vec3 halfVector;
	vec4 ambient;
	vec4 diffuse;
	vec4 specular;
};

uniform int lightCount;
uniform lightStruct lightList;


void DirectionalLight(	in int i,
						inout vec4 ambient,
						inout vec4 diffuse,
						inout vec4 specular );
void main()
{
	vec4 finalAmbient =  vec4(0.0, 0.0, 0.0, 0.0);
	vec4 finalDiffuse =  vec4(0.0, 0.0, 0.0, 0.0);
	vec4 finalSpecular = vec4(0.0, 0.0, 0.0, 0.0);
	
	gl_Position = gl_ModelViewProjectionMatrix  * gl_Vertex;	

	int i = 0;

	for(i=0; i<lightCount; i++)
	{
		DirectionalLight( i,
				  finalAmbient,
				  finalDiffuse,
				  finalSpecular );
	}
	
	gl_FrontColor = gl_BackColor = finalAmbient + finalDiffuse + finalSpecular;
	gl_TexCoord[0] = gl_MultiTexCoord0;
	
}

void DirectionalLight(	in int i,
						inout vec4 ambient,
						inout vec4 diffuse,
						inout vec4 specular )
{
     float nDotVP;         // normal . light direction
     float nDotHV;         // normal . light half vector
     float pf;             // power factor

     nDotVP = max(0.0, dot(gl_Normal, normalize(vec3(lightList.position))));
     nDotHV = max(0.0, dot(gl_Normal, vec3(lightList.halfVector)));

     if (nDotVP == 0.0)
         pf = 0.0;
     else
         pf = pow(nDotHV, gl_FrontMaterial.shininess);

     ambient  += lightList.ambient;
     diffuse  += lightList.diffuse * nDotVP;
     specular += lightList.specular * pf;
}	
Frag:

Code: Select all

uniform sampler2D myTexture;

void main (void)
{
    vec4 col = texture2D(myTexture, vec2(gl_TexCoord[0]));
    col *= gl_Color;
	gl_FragColor = col * 4.0;
}


Thanks a lot!
Dog-E
Posts: 28
Joined: Sun Mar 30, 2008 5:52 am

Post by Dog-E »

*sigh*, fixed.

Well, if you derive from IShaderConstantSetCallBack and then derive from the derived class again, make sure you do not point to the first derived class in addHighLevelShaderMaterialFromFiles.
Post Reply