In Blender when I'm creating the meshes, they are iron-maidening correctly with their normals.
Krohm did bring up a point. I am using two seperate shaders for Mesh's without textures and those WITH textures (diffuse, normal, spec, ect.)
So I am for my models using a seperate shader (so seperate pipeline) for rendering without textures. BUT for all intents and purposes the code should be indentical
minus the Texture2D type and mulitplication of that and the final fragment color.
I'll check my material shader and ill post here. Ill be gone for most of the day. Work and all that fun stuff, so thanks for the replies!
///////////////////////////////////////////////////////
//
// Material Shader
//
////////////////////////////////////////////////////////
//Load Unused Texture Sampler into GPU Register(s0)
//Load clamp Sampler for Shadow Map into GPU Register(s1)
//////////////////////////////////////////////////////////////////
SamplerState colorSampler : register(s0);
SamplerState sampleTypeClamp : register(s1);
//CONSTANT BUFFERS=========================
//CB's for Matrices, allows C++ Code to send constants
//to the shader and bind them into Registers (b0, b1, b2 respectively)
//===========================================================
cbuffer world : register(b0)
{
matrix worldMat;
}
cbuffer view : register(b1)
{
matrix viewMat;
}
cbuffer proj : register(b2)
{
matrix projMat;
}
cbuffer lView : register(b3)
{
matrix lightViewMat;
}
cbuffer lProj : register(b4)
{
matrix lightProjMat;
}
cbuffer camBuffer : register(b5)
{
float3 camPos;
float pad;
};
//==================================================
//Structures for Shader Constants
//===================================
struct Light
{
float3 dir;
float3 pos;
float range;
float3 att;
float4 diffuse;
float4 ambient;
};
//===================================
//Load the main light into the shader and bind it to register b0
cbuffer LightCB : register(b0)
{
Light light[6];
}
//Structures for Vertex and Pixel
/////////////////////////////////////////
struct VS_Input
{
float4 pos : POSITION;
float4 color : COLOR;
float3 norm : NORMAL;
};
struct PS_Input
{
float4 pos : SV_POSITION;
float4 wPos : POSITION;
float4 color : COLOR;
float3 norm : NORMAL;
float4 lightViewP : TEXCOORD1;
float3 lightPos : TEXCOORD2;
float3 viewDir : TEXCOORD3;
};
///////////////////////////////////////////
//Vertex Shader
///////////////////////////////////
PS_Input VS(VS_Input vertex)
{
//Init Output Struct
/////////////////////////////////////
PS_Input vsOut = (PS_Input)0;
vertex.pos.w = 1.0f;
float4 cameraPos = 0.0f;
//Transform Vertices into worldSpace
///////////////////////////////////////////
vsOut.pos = mul(vertex.pos, worldMat);
vsOut.pos = mul(vsOut.pos , viewMat);
vsOut.pos = mul(vsOut.pos , projMat);
//Transform LightMatrix into worldSpace - ShadowMapping
///////////////////////////////////////////
vsOut.lightViewP = mul(vertex.pos, worldMat);
vsOut.lightViewP = mul(vsOut.lightViewP, lightViewMat);
vsOut.lightViewP = mul(vsOut.lightViewP, lightProjMat);
//Transform VertexNormal into WorldSpace, normalize it
//////////////////////////////////////////
vsOut.norm = mul(vertex.norm, worldMat);
vsOut.norm = normalize(vsOut.norm);
//Grab the relative worldSpace position of a vertice (Usefull for lighting calculations)
/////////////////////////////////////////
vsOut.wPos = mul(vertex.pos, worldMat);
//Transform cameraPosition to worldSpace - SpecularMapping
////////////////////////////////////////
vsOut.viewDir = camPos.xyz - vsOut.wPos.xyz;
vsOut.viewDir = normalize(vsOut.viewDir);
vsOut.color = vertex.color;
//Return output structure as Input into PixelShader
////////////////////////////////////////
return vsOut;
}
//////////////////////////////////
//
//
float4 PS(PS_Input texel) : SV_TARGET
{
//texel.lightViewP.xyz /= texel.lightViewP.w;
//Variable Initalization
/////////////////////////////////////////////////
float2 projectTexCoord = float2(0.0f, 0.0f);
float bias = 0.0000003f;
float lightIntensity = 0.0f;
float depthValue = 0.0f;
float lightDepthValue = 0.0f;
float4 color = float4(0.0f, 0.0f, 0.0f, 0.0f);
float4 lightObject = light[5].diffuse;
float4 specular = float4(0.0f, 0.0f, 0.0f, 0.0f);
bool lightHit = false;
/////////////////////////////////////////////////
//Grab the lightPosition at the vertex
/////////////////////////////////////////
texel.lightPos = light[5].pos.xyz - texel.wPos.xyz;
texel.lightPos = normalize(texel.lightPos);
/////////////////////////////////////////
//return lightObject;
lightIntensity = saturate(dot(texel.norm, light[5].pos));
texel.lightViewP.xyz /= texel.lightViewP.w;
//if position is not visible to the light - dont illuminate it
//results in hard light frustum
if( texel.lightViewP.x < -1.0f || texel.lightViewP.x > 1.0f ||
texel.lightViewP.y < -1.0f || texel.lightViewP.y > 1.0f ||
texel.lightViewP.z < 0.0f || texel.lightViewP.z > 1.0f )
{
lightObject = light[5].ambient;
}
//transform clip space coords to texture space coords (-1:1 to 0:1)
texel.lightViewP.x = texel.lightViewP.x/2 + 0.5;
texel.lightViewP.y = texel.lightViewP.y/-2 + 0.5;
texel.lightViewP.z -= bias;
//sample shadow map - point sampler
float shadowMapDepth = colorMap.Sample(sampleTypeClamp, texel.lightViewP).r;
//if clip space z value greater than shadow map value then pixel is in shadow
if ( shadowMapDepth < texel.lightViewP.z)
{
lightObject = light[5].ambient;
}
float4 color2 = lightObject;
color = saturate(color2 + lightObject) * texel.color;
float4 finalColor = float4(0.0f, 0.0f, 0.0f, 0.0f);
[unroll]
for (int i = 0; i < 5; i++)
{
float3 lightToPixelVec = light.dir - texel.wPos;
//Find the distance between the light pos and pixel pos
float d = length(lightToPixelVec);
//If pixel is too far, return pixel color with ambient light
if( d > 100.0f)
continue;
//Turn lightToPixelVec into a unit length vector describing
//the pixels direction from the lights position
lightToPixelVec /= d;
//Calculate how much light the pixel gets by the angle
//in which the light strikes the pixels surface
float howMuchLight = dot(lightToPixelVec, texel.norm);
//If light is striking the front side of the pixel
if( howMuchLight > 0.0f )
{
//Add light to the finalColor of the pixel
finalColor += howMuchLight * texel.color * light.diffuse;
//Calculate Light's Falloff factor
finalColor /= light.att[0] + (light.att[1] * d) + (light.att[2] * (d*d));
}
//make sure the values are between 1 and 0, and add the ambient
color = saturate(color + finalColor);
}
//color += float4(0.5f, 0.0f, 0.0f, 1.0f);
return color;
}
P.s. It seems I have forgotten how to do the code brackets :p
Marcus