Original Post
I know these kinds of questions get asked all the time around here, and I'm really sorry, but I sort of reached a point where I don't know how to progress any further... I'm running OpenGL and NVidia CG 2.2 on a GeForce GTX 260. Take a look at these two shaders: vertex shader: pixel shader: In the vertex shader, I'm moving the vertex position into world space by multiplying it with the world projection matrix. I also calculate a normalized light vector from the vertex position (the one already in world space) to the light position (supplied to the shader in world space of course). Then, in the pixel shader, I'm moving the normal vector (read from the normal map) into world space as well (again, multiplying by the world projection matrix) and then calculating the diffuse term (dot product of light and normal vector). For some reason, all I get is pure black. I'm drawing a model 100 units into the screen (at 0/0/-100 in world space coordinates), I have a light set to be at 10 units into the screen (at 0/0/-10 in world space coordinates) - how can that give me a black mesh? Here is what I do in my application: So, first of all, I'd appreciate if somebody could look at the shaders at maybe see something wrong there? Some problem with the spaces etc.? I'm still new to this all. Second, what else could be causing this? Any ideas? If I need to post more code I'm happy to do so, just let me know. Thanks ahead of time!
void main ( in float4 inPosition : POSITION,
in float2 inTexCoords : TEXCOORD0,
out float4 outPosition : POSITION,
out float2 outTexCoords : TEXCOORD0,
out float4 outLightVector : TEXCOORD1,
uniform float4x4 worldViewProj,
uniform float4 lightPosition )
{
outPosition = mul ( worldViewProj, inPosition );
outLightVector = normalize ( lightPosition - outPosition );
outTexCoords = inTexCoords;
}
void main ( in float2 inTexCoords : TEXCOORD0,
in float4 inLightVector : TEXCOORD1,
out float4 outColor : COLOR,
uniform sampler2D texture : TEXUNIT0,
uniform sampler2D normalmap : TEXUNIT1,
uniform float4x4 worldViewProj )
{
// look up diffuse color from texture
float3 texture_color = tex2D ( texture, inTexCoords );
// look up normal and clamp it into proper range
float4 normal_vector = tex2D ( normalmap, inTexCoords );
normal_vector.rgb = 2.0 * ( normal_vector - 0.5 ).rgb;
// move normal into worldspace
normal_vector = mul ( worldViewProj, normal_vector );
// calculate diffuse-factor
float3 diffuse = dot ( inLightVector.rgb, normal_vector.rgb );
// return calculated rgb and full alpha
outColor.rgb = diffuse * texture_color;
outColor.a = 1.0;
}
void Shader::AddParameter ( std::string name )
{
CGparameter parameter = cgGetNamedParameter ( program, name.c_str ( ) );
check_for_cg_error ( "Failed to add parameter to shader");
this->parameter.push_back ( parameter );
}
void Shader::SetMatrixParameter ( std::string name, CGGLenum matrix, CGGLenum transform )
{
cgGLSetStateMatrixParameter ( cgGetNamedParameter ( program, name.c_str ( ) ), matrix, transform );
check_for_cg_error ( "Failed to set matrix parameter for shader" );
}
void Shader::SetVectorParameter ( std::string name, Vector3 vector )
{
cgGLSetParameter4f ( cgGetNamedParameter ( program, name.c_str ( ) ), vector.x, vector.y, vector.z, 1.0f );
check_for_cg_error ( "Failed to set vector parameter for shader" );
}
// then, when a material is applied, before the mesh is rendered
light.position.Set ( 0.0f, 0.0f, -10.0f );
// set parameters for shaders
vertex_shader.SetMatrixParameter ( "worldViewProj", CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY );
vertex_shader.SetVectorParameter ( "lightPosition", light.position );
pixel_shader.SetMatrixParameter ( "worldViewProj", CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY );