Sign in to follow this  
Android_s

Cg lighting problem

Recommended Posts

Android_s    164
Hey! Was trying to make something cool with Cg shaders, but i realized how much i suck at Cg scripting, so i need some help. =) I'm tyring to make a simple lambert & phong shader, but it dosn't look correct. The diffuse lighting seems really...hmm, ambient in a way, and the specular...it really seems like an environmap. =P Ok, so this is the c++ code where i enable the shader. Not sure if i'm supposed to supply the inverse TRANSPOSE of the modelview matrix, but my resources from nVidia suggest i should...
cgGLEnableProfile(vProfile);
	cgGLEnableProfile(fProfile);
	cgGLBindProgram(vProgram);
	cgGLBindProgram(fProgram);

	cgGLSetParameter3fv(cgpLightPos, lightPos);

	glPushMatrix();
		gluLookAt(2,2,3,	0,0,0,	0,1,0);

		glPushMatrix();
			float mat[16];
			glGetFloatv(GL_MODELVIEW_MATRIX, mat);
			RotateMatrix_Y(mat, mat, scene_yRot);
			RotateMatrix_X(mat, mat, scene_xRot);
			glLoadMatrixf(mat);

			cgGLSetStateMatrixParameter(cgpModelViewProjMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
			cgGLSetStateMatrixParameter(cgpModelViewInvertMatrix, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_INVERSE_TRANSPOSE);

			glBegin(GL_QUADS);
				glNormal3f(0,0,1);
				glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 1);	glColor3f(1,0,0);	glVertex3f(-1.0f,-1.0f, 0);
				glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 0);	glColor3f(0,0,1);	glVertex3f( 1.0f,-1.0f, 0);
				glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 0);	glColor3f(0,1,1);	glVertex3f( 1.0f, 1.0f, 0);
				glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 1);	glColor3f(1,1,0);	glVertex3f(-1.0f, 1.0f, 0);
			glEnd();
		glPopMatrix();

		cgGLDisableProfile(vProfile);
		cgGLDisableProfile(fProfile);

	glPopMatrix();

I can't spot anything wrong there except perhaps that inverse transopse thing. So for the vertex shader script
struct VertIn
{
	float4 position : POSITION;
	float4 color	: COLOR;
	float2 texCoord	: TEXCOORD0;
	float4 normal	: NORMAL;
};

struct VertOut
{
	float4 position : POSITION;
	float4 color	: COLOR;
	float2 texCoord : TEXCOORD0;
	
	float3 vertEye		: TEXCOORD1;
	float3 normalEye	: TEXCOORD2;
	float3 lightVecEye	: TEXCOORD3;	
};

VertOut vertmain(	VertIn IN,
					uniform float4x4 modelViewProj,
					uniform float4x4 modelViewInv,
					uniform float3 lightPos)
{
	VertOut OUT;
	
	OUT.position = mul(modelViewProj, IN.position);
	OUT.texCoord = IN.texCoord;
	OUT.color = IN.color;
	
	// to view space
	OUT.vertEye = mul(modelViewInv, IN.position);
	OUT.lightVecEye = normalize(lightPos - OUT.vertEye);
	OUT.normalEye = mul(modelViewInv, IN.normal).xyz;
	OUT.vertEye = normalize(-OUT.vertEye);

	return OUT;
}

I actually don't know at all if this really works. I keep asking myself the question why i have to transform vert and normal to viewspace anyway, and not the lightVec. I also found out that i get fancy inverted specular reflection if i don't invert the vertEye param...omg why!? ;P Last, the fragment shader
struct FragIn
{
	float4 color	: COLOR;
	float2 texCoord : TEXCOORD0;
	
	float3 vertEye		: TEXCOORD1;
	float3 normalEye	: TEXCOORD2;
	float3 lightVecEye	: TEXCOORD3;
};

float3 reflect(float3 N, float3 L)
{
	return 2.0 * N * dot(N, L) - L;
}


float4 fragmain(FragIn IN,
				uniform sampler2D frameTex : TEXUNIT0,
				uniform sampler2D depthTex : TEXUNIT1) : COLOR
{
	float4 color;
	
	float3 V = normalize(IN.vertEye);
	float3 N = normalize(IN.normalEye);
	float3 L = normalize(IN.lightVecEye);
	
	float diffuse = clamp(dot(L, N), 0, 1);
	
	float3 R = reflect(N, L);
	float specular = clamp(pow(dot(R, V), 1.0), 0, 1);
	
	float4 alpha = IN.color;
	alpha[0] = 0;
	alpha[0] = 0;
	alpha[0] = 0;
	
	color = (diffuse * IN.color) + (specular * IN.color) + alpha;
	
	return color;	
}

This one is a mess if you ask me...although it dosn't look that wrong to me... I'm really lost...what i want to know is why to-viewspace and if anyone of you guys can see any real screw up. =) cheers!

Share this post


Link to post
Share on other sites
Android_s    164
Well, BUMP!, sorry...
Isn't there anyone that can discover something wrong with my code? Come on, you don't have to be shy... ;P

...perhaps i posted in the wrong section? =/

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this