View Single Post
Old 09-30-11, 10:19 AM   #11
skwasjer
The Old Man
 
Join Date: Apr 2007
Location: Netherlands
Posts: 1,547
Downloads: 26
Uploads: 3
Default

Quote:
Originally Posted by TheDarkWraith View Post
Well I'm miffed According to granny viewer all my ducks are in a row regarding textures, vertices, texture coordinates, etc. for the King George gr2 file. But the hull still renders incorrectly. Now one thing I have noticed is that on some units where the hull is rendered incorrectly if I place the camera inside the hull (inside the ship) and look at the hull it's rendered correctly (with culling set to none). Puzzling

Another thing that maybe someone knows the answer to: how does one correctly render the other maps beside the diffuse map (self-illumination and bump)? Some units have all 3 of these maps defined for a mesh and I'm currently only using the diffuse one.
1. Most definately a matrix problem
2. Shaders and correct use of index/vertex buffers...

Here's S3D's shaders (diffuse, specular and ambient occlusion, no bump)... You know, that dumbmaking tool that did similar things to what you are making now for SH5!

Code:
 
//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
int g_nNumLights;
 
// Vertex shader globals
float4x4 worldViewProjection;  // World * View * Projection matrix
float3 g_LightParams[3];   //x - type, y - Att2, z - Range
float3 g_LightSpotParams[3];  //x = cos(Phi/2), y =  cos(theta/2), z =  1 / (cos(theta/2) -  cos(Phi/2))
float4 g_LightDiffuse[3];   // Light's diffuse color
float4 g_LightSpecular[3];   // Light's specular color
float3 g_LightPos[3];    // Light's position in world space
float3 g_LightDir[3];    // Light's direction in world space
//float4 g_fogParam;
// Shared globals
float3 g_CameraPosition;
float3 g_MaterialParams;   // x - specular power, y - specular strength
// Pixel shader globals
float4 g_MaterialAmbientColor;
//float3 g_fogColor;
float  g_bUseSpecFromDiffuse;
float4  g_UseMaps; // x - use AO map, y - use spec map, z - use bump map
float2 g_BrightnessContrast = float2(0.05f, 0.05f); // x - brightness, y - contrast
float4 g_SelectionColor;    // Color used to indicate selection.
 
// Wireframe color
float4 g_WireframeColor;
 
texture g_DiffuseTexture;    // Color texture for mesh
texture g_SelfIllumTexture;    // Color texture for mesh
 
 
float    appTime;     // App's time in seconds
float4x4 worldMatrix;    // World matrix for object
 
//--------------------------------------------------------------------------------------
// Texture samplers
//--------------------------------------------------------------------------------------
sampler DiffuseMap =
sampler_state
{
 Texture = <g_DiffuseTexture>;
 MipFilter = LINEAR;
 MinFilter = LINEAR;
 MagFilter = LINEAR;
};
sampler LightMap =
sampler_state
{
 Texture = <g_SelfIllumTexture>;
 MipFilter = LINEAR;
 MinFilter = LINEAR;
 MagFilter = LINEAR;
};
//--------------------------------------------------------------------------------------
// Vertex shader output structure
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
 float4 ProjPos   : POSITION; // vertex position 
 float4 Texs       : TEXCOORD0; // vertex texture coords 
 //float  Fog        : TEXCOORD1;
 float3 Normal     : TEXCOORD2;
 float3 EyeVertex  : TEXCOORD3;
 float3 Diffuse    : COLOR0;  // vertex diffuse color
 float3 Specular   : COLOR1;  // vertex specular color
};
 
//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSceneVS( float4 vPos : POSITION, 
      float3 vNormal : NORMAL,
      float2 vTex0 : TEXCOORD0,
      float2 vTex1 : TEXCOORD1,
      uniform bool bWireFrame )
{
 VS_OUTPUT Out;
 Out.ProjPos = mul(vPos, worldViewProjection); 
 Out.Texs = float4(vTex0, vTex1);
// Out.Fog = 0;
 Out.Normal = vNormal;
 Out.EyeVertex = normalize(vPos.xyz - g_CameraPosition);
 Out.Diffuse = 0;
 Out.Specular = 0;
 if (bWireFrame)
 {
  Out.Diffuse = float4(1, 1, 1, 1); // Full...
  return Out;
 }
 float3 refl = reflect(Out.EyeVertex, vNormal); 
 float dist;
 for (int i=0; i < g_nNumLights; i++)
 {
  float3 lightPos = mul(g_LightPos[i], transpose(worldMatrix));
 
  dist = distance(lightPos, vPos.xyz);  
  float3 VertexLightDir = normalize(lightPos - vPos.xyz);
  float LightAtt = 1 / (1 + dist * dist * g_LightParams[i].y);
  float LightInfluence = LightAtt * saturate(dot(vNormal, VertexLightDir)); 
  float specFactor = LightAtt * saturate(dot(refl, VertexLightDir));
 
  float spot = 1;
 
  if (g_LightParams[i].x > 1.1f) //that means we have a spot light
  {
   float cosa = dot(VertexLightDir, g_LightDir[i]);
   if (cosa < g_LightSpotParams[i].x)
   {
    spot = 0;
   }
   else
   {
    if (cosa < g_LightSpotParams[i].y)
     spot = (cosa - g_LightSpotParams[i].x) * g_LightSpotParams[i].z; 
   }
  }
 
  Out.Diffuse += g_LightDiffuse[i] * LightInfluence * spot;
  Out.Specular += g_LightSpecular[i] * pow(specFactor, g_MaterialParams.x) * spot;
 }
 
 dist = distance(g_CameraPosition, vPos.xyz);
 //Out.Fog = saturate((dist - g_fogParam.x) * g_fogParam.z); 
 return Out;    
}
 
//--------------------------------------------------------------------------------------
// Pixel shader output structure
//--------------------------------------------------------------------------------------
struct PS_OUTPUT
{
 float4 RGBColor : COLOR0;  // Pixel color    
};
 
//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
//       color with diffuse material color
//--------------------------------------------------------------------------------------
float4 RenderScenePS( VS_OUTPUT In,
      uniform bool bWireFrame ) : COLOR
{ 
 if (bWireFrame)
 {
  return g_WireframeColor;
 }
 float4 BaseColor = tex2D(DiffuseMap, In.Texs.xy);
 float4 SelfIllum = tex2D(LightMap, In.Texs.zw);
 
 // float specMask = lerp(1, BaseColor.w, g_bUseSpecFromDiffuse);
 float specMask = tex2D(LightMap, In.Texs.xy).w;
 specMask = lerp(specMask, BaseColor.w, g_bUseSpecFromDiffuse);
 
 if (g_UseMaps.x == 0)
  SelfIllum = float4(0.75f, 0.75f, 0.75f, 1);
 if (g_UseMaps.w == 0)
 {
  BaseColor = g_SelectionColor;
  BaseColor.w = g_MaterialAmbientColor.w;
 }
 else
 {
  BaseColor.xyz = BaseColor.xyz * g_SelectionColor.xyz;
 }
 //else if (g_UseMaps.y == 0)
//  BaseColor.xyz = (BaseColor.xyz + g_BrightnessContrast.x) * (1.0 + g_BrightnessContrast.y)/1.0; 
 
 
 if (g_UseMaps.y == 0) specMask = 0;
 //BaseColor.w = saturate(BaseColor.w + g_bUseSpecFromDiffuse); 
 
 float3 VNormal = normalize(In.Normal);
 
 float3 refl = reflect(In.EyeVertex, VNormal);
 
 float3 DiffuseColor = In.Diffuse;
 float3 SpecularColor = In.Specular;
 for (int i=0; i < g_nNumLights; i++)
 {
  float LightInfluence = saturate(dot(VNormal, g_LightDir[i].xyz)); 
  float specFactor = saturate(dot(refl, g_LightDir[i].xyz));  
  DiffuseColor += LightInfluence * g_LightDiffuse[i];
  SpecularColor += g_MaterialParams.y * pow(specFactor, g_MaterialParams.x * (specMask+0.01)) * g_LightSpecular[i];
 }
 
 //return float4(BaseColor.xyz * (DiffuseColor + g_MaterialAmbientColor.xyz), 1);
 //return float4(SpecularColor, 1);
 
 float4 Color;
 Color.xyz = BaseColor.xyz * SelfIllum.xyz * (DiffuseColor + g_MaterialAmbientColor.xyz) + SpecularColor * SelfIllum.x * specMask;
 
 Color.w = BaseColor.w;
 //Color.xyz = lerp(Color.xyz, g_fogColor, In.Fog);
 
 Color.xyz = (Color.xyz + g_BrightnessContrast.x) * (1.0 + g_BrightnessContrast.y)/1.0; 
 return Color;
}
//--------------------------------------------------------------------------------------
// Renders scene to render target
//--------------------------------------------------------------------------------------
technique RenderScene
{
 pass P0
 {          
  VertexShader = compile vs_2_0 RenderSceneVS(false);
     PixelShader  = compile ps_2_0 RenderScenePS(false);
 }
}
//--------------------------------------------------------------------------------------
// Renders scene to render target, using wireframe mode
//--------------------------------------------------------------------------------------
technique RenderSceneWireframe
{
 pass P0
 {          
     FillMode = Wireframe;
     CullMode = CCW;
     ZWriteEnable = false;
  ZFunc = Less;
     //CullMode = None;
  VertexShader = compile vs_2_0 RenderSceneVS(true);
     PixelShader  = compile ps_2_0 RenderScenePS(true);
 }
}
//--------------------------------------------------------------------------------------
// Renders scene to render target, using point mode
//--------------------------------------------------------------------------------------
technique RenderSceneVertices
{
 pass P0
 {          
     FillMode = Point;
     CullMode = CCW;
     ZWriteEnable = false;
  ZFunc = Less;
  VertexShader = compile vs_2_0 RenderSceneVS(true);
     PixelShader  = compile ps_2_0 RenderScenePS(true);
 }
}
 
//--------------------------------------------------------------------------------------
// Renders scene to render target, using wireframe mode
//--------------------------------------------------------------------------------------
technique RenderSceneSolidWireframe
{
 pass P0
 {          
     FillMode = Solid;
     CullMode = CCW;
     ZWriteEnable = true;
  ZFunc = LessEqual;
 
  VertexShader = compile vs_2_0 RenderSceneVS(false);
     PixelShader  = compile ps_2_0 RenderScenePS(false);
 }
 pass P1
 {
     FillMode = Wireframe;
     CullMode = CCW;
     ZWriteEnable = false;
  ZFunc = Less;
 
     PixelShader  = compile ps_2_0 RenderScenePS(true);
 }
}
//--------------------------------------------------------------------------------------
// Renders scene to render target, using point mode
//--------------------------------------------------------------------------------------
technique RenderSceneSolidVertices
{
 pass P0
 {          
     FillMode = Solid;
     CullMode = CCW;
     ZWriteEnable = true;
  ZFunc = LessEqual;
  VertexShader = compile vs_2_0 RenderSceneVS(false);
     PixelShader  = compile ps_2_0 RenderScenePS(false);
 }
 pass P1
 {
     FillMode = Point;
     CullMode = CCW;
     ZWriteEnable = false;
  ZFunc = Less;
     PixelShader  = compile ps_2_0 RenderScenePS(true);
 }
}
Now if you need more (technical) answers you'll have to be more specific.
skwasjer is offline   Reply With Quote