hi. im using a vertex shader to generate different shapes (in this case a sphere) and trying to compute the normals aswell.
what im doing is passing a grid mesh of size 100x100 (the plane coordinates are from 0.0 to 1.0) and in the shader i use the vertices to generate a sphere by this function:
float3 getSpherePoint( float u, float v )
{
float x = cos(v) * sin(u);
float y = sin(v) * sin(u);
float z = cos(u);
return float3( x, y, z );
}
the vertex shader follows:
vertexOutput VS_GoochySpecular( in vertexInput IN )
{
vertexOutput OUT = (vertexOutput)0;
float step = (1/100.0); // one step
float u = (IN.position.x) * TWOPI;
float v = (IN.position.y) * PI;
float3 pos = getSpherePoint( u, v );
// Compute tangents and normals
float3 tangent = getSpherePoint( u-(step/2), v ) - getSpherePoint( u+(step/2), v );
float3 bitangent = getSpherePoint( u, v-(step/2) ) - getSpherePoint( u, v+(step/2) );
float3 normal = normalize( cross( tangent, bitangent ) );
// scale sphere
pos *= a;
float4 Po = float4( pos, 1 );
OUT.position = mul( worldViewProj, Po );
float3 worldpos = mul(matWorld, Po).xyz; // in "world" coordinates
float3 viewpos = mul(matView, float4(worldpos,1.0)).xyz; // in "view" coordinates
OUT.worldPos = worldpos;
OUT.normal = normalize( mul((float3x3)matViewIT, normal) );
float3 L = viewL - (viewpos.xyz*lightPos.w);
OUT.lightVec = L;
OUT.viewVec = normalize( float3(matViewIT[0].w, matViewIT[1].w, matViewIT[2].w) - viewpos.xyz);
return OUT;
}
the problem is that the bottom hemisphere of the sphere has the lighting inverted. i have disabled face culling. when enabled i can see the normals are inverted. my question is why everything below y=0 inverts ?
check this shot:
best regards,