Hi! I was trying to convert this example
here.. I managed to run it but i got really weird results (sort of a void effect like it cuts out the shape of an object into game space, anyway weird.)
I probably messed up something real bad but i thought that it would only be unshaded or untextured model or something, not this
Code:
bmap tDiffuse = "difuse.tga";
bmap tBump = "normals.tga";
bmap tNormalizer = "normals.tga";
bmap tAtenuationXY = "AtenuationXY.bmp";
bmap tAtenuationZ = "AtenuationZ.bmp";
////////////// PHONG/BLINN SHADING /////////////////////////////////
MATERIAL mat_PHONG
{
//flags = tangent;
effect=
"
matrix matWorldViewProj;
matrix matView;
Texture tDiffuse;
Texture tBump;
Texture tNormalizer;
Texture tAtenuationXY;
Texture tAtenuationZ;
Vector vecSkill41;
Vector vecViewPos;
Vector vecSunDir;
Vector vecLight;
vector vColorLight;
Technique phong_shading
{
pass p0
{
ALPHABLENDENABLE = True;
SrcBlend = ONE;
DestBlend = ONE;
ColorWriteEnable = 0xf;
Texture[0] = <tDiffuse>;
Texture[1] = <tBump>;
Texture[2] = <tNormalizer>;
Texture[3] = <tNormalizer>;
Texture[4] = <tAtenuationXY>;
Texture[5] = <tAtenuationZ>;
AddressU[0] = Wrap;
AddressV[0] = Wrap;
AddressU[1] = Wrap;
AddressV[1] = Wrap;
AddressU[2] = Clamp;
AddressV[2] = Clamp;
AddressU[3] = Clamp;
AddressV[3] = Clamp;
AddressU[4] = Clamp;
AddressV[4] = Clamp;
AddressU[5] = Clamp;
AddressV[5] = Clamp;
VertexShaderConstant[0] = <matWorldViewProj>;
VertexShaderConstant1[8] = <vecLight>;
VertexShaderConstant1[9] = <vecLight>;//{0.0033f,0.0f,0.0f,0.0f}; // 1/lightrange tj. vScaleLight
VertexShaderConstant1[10] = <matView>;
PixelShaderConstant1[1] = <vColorLight>; // vColorLight
///////////////////////////////////////////////////////////
VertexShader =
decl
{
stream 0;
float v0[3]; //Position
float v1[2]; //Tangent
float v2[3]; //Binormal
float v3[3]; //Normal
float v4[3]; //Texcoord
}
asm
{
vs.1.1
//-------------------------------
//Constant registers
//-------------------------------
// c0-c3 - world*view*proj
// c8 - light position(In object space)
// c9 - 1 / lightRange
// c10 - vPosViewer(In object space)
def c6, 0.5, 0, 0, 0 // Helper constant
def c7, 2, -1, 0, 0 // Helper constant
//------------------------------
// Input registers
//------------------------------
// dcl_position v0
// dcl_tangent v1
// dcl_binormal v2
// dcl_normal v3
// dcl_texcoord v4
//
//------------------------------
// output position
//------------------------------
//
m4x4 oPos, v0, c0
//------------------------------
// Compute the tangent space matrix to transform from object space to tangent space
//------------------------------
//
mad r7.xyz, c7.x, v1, c7.y //bias vector tangent to (-1,1)
mad r8.xyz, c7.x, v2, c7.y //bias vector binormal to (-1,1)
mad r9.xyz, c7.x, v3, c7.y //bias vector normal to (-1,1)
//------------------------------
// Compute L vector in tangent space
//------------------------------
//
add r10, -v0, c8 // light position - vPos
dp3 r7.w, r10, r10 // Normalize (lightpostion-vPos)
rsq r7.w, r7.w
mul r11.xyz, r10, r7.w
m3x3 oT1.xyz, r11, r7 // t1 = L vector in tangent space
//------------------------------
// Compute H vector in tangent space
//------------------------------
//
add r4, -v0, c4 // V vector
add r6, r10, r4 // L + V
dp3 r6.w, r6, r6 // Normalize (L+V)
rsq r6.w, r6.w
mul r5.xyz, r6, r6.w
m3x3 oT2.xyz, r5, r7 // t2 = H vector in tangent space
//------------------------------
// output texture coordinates
//------------------------------
//
mov oT0.xy, v4 // t0 = texture coordinate
//------------------------------
// compute texture coordinates for attenuation
//------------------------------
//
mul r2.xyz, r10, c9 // (light position - vertex pos) / lightRange
mad r4.xyz, r2, c6.x, c6.x // map to 0 to 1 range
mov oT3.xy, r4 // t3 = texture coordinates for attenuation texture 1
mov oT4.x, r4.z // t4 = texture coordinates for attenuation texture 2
mov oT4.y, c6.w
};
pixelShader=
asm
{
ps.1.4
//-------------------------------
//Constant registers
//-------------------------------
// c1 - lightColor
def c0, 4.0f, 1.0f, 0.0f, -0.75f // helper constant
//------------------------------
// Load textures
//------------------------------
//
texld r0,t0 // Diffuse
texld r1,t0 // Normal
texld r2,t1 // L
texld r3,t2 // H
texld r4,t3 // attenuation texture 1
texld r5,t4 // attenuation texture 2
//------------------------------
// compute output color
//------------------------------
//
dp3_sat r2.w,r1_bx2,r3_bx2 // max(0,dot(N,H))
dp3_sat r1.rgb,r2_bx2,r1_bx2 // max(0,dot(N,L))
+mad_x4_sat r2.w,r2.w,r2.w,c0.w // specular = saturate(4*(dot(N,H)^2-0.75))
mul_sat r5.rgb,r4,r5 // attenuation = attenuation(x,y)*attenuation(z)
+mul r2.w,r2.w,r2.w // specular = specular*specular
mul_sat r0.rgb,r0,r1 // diffuse = max(0,dot(N,L))*diffuse color
+mul_x2_sat r2.w,r2.w,r1.w // specular = saturate(specular*gloss*2)
add_sat r0.rgb,r0,r2.w // color = saturate(diffuse + specular)
mul r0.rgb,r0,c1 // color = color*lightColor
mul r0.rgb,r0,r5 // color = color*attenuation
};
}
}
";
}
action Shader_Phong
{
my.material = mat_PHONG;
}
You can get an working example and a full c++ code from the above link.
If someone could make this work it would mean this: We could do something simmilar to Far Cry or even Doom 3.
Far Cry has this tool called Poly Bump that is free for non-commercial use. So you make a very detailed model and this tool creates a normal map for that model. Then you make a low poly model (1000-1500 poly's) and using the lighting metod above and the normal map you get a very detailed model that is still low poly. Anyway it would be cool
Good Luck!