Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (degenerate_762, AbrahamR, AndrewAMD, ozgur), 667 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
BumpMap ... How to Change Sunlight to DynamicLights? #385609
10/20/11 20:47
10/20/11 20:47
Joined: Aug 2005
Posts: 142
Pattensen - Germany
R
REZ Offline OP
Member
REZ  Offline OP
Member
R

Joined: Aug 2005
Posts: 142
Pattensen - Germany
Hello,

I did generate a bumpmap-effect, using FXED (Acknex Unlimited). Now I got the problem that I want to change this Shader to use Dynamic Lights instead of the Sun.
Using A8.20 PRO.

Here is the code:


Main Script:
Code:
#include <acknex.h>
#include <default.c>
#include <mtlFX.c>
#include <mtlView.c>

#include <litec.h>
#include <d3d9.h> 

#include "NormalMapping12.c";
(...)




NormalMapping12.c
Code:
BMAP* BlechNeu_bmap = "BlechNeu.bmp";
BMAP* BlechNeu_normal_bmap = "BlechNeu_normal.tga";


MATERIAL* mtlNormalMapping12 =
{

skin1 = BlechNeu_bmap;
skin2 = BlechNeu_normal_bmap;

effect = "NormalMapping12.fx";

}

action actNormalMapping12()
{
my.material = mtlNormalMapping12;
}



NormalMapping12.fx

Code:
// variables
float AmbientIntensity = 1.0f;
float DiffuseIntensity = 1.0f;
float SpecularIntensity = 2.0f;
float SpecularPower = 8.0f;

// vectors
float4 vecSunColor;
float4 vecSunDir;
float4 vecAmbient;
float4 vecViewPos;

// matrices
const float4x4 matWorldViewProj;
const float4x4 matWorld;
float3x3 matTangent;

// textures
texture entSkin1;
texture entSkin2;

// samplers
sampler base_sampler = sampler_state
{
Texture = <entSkin1>;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
MAGFILTER = POINT;
MINFILTER = POINT;
MIPFILTER = LINEAR;
};
sampler normal_sampler = sampler_state
{
Texture = <entSkin2>;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
MAGFILTER = POINT;
MINFILTER = POINT;
MIPFILTER = NONE;
};

// shader functions
void NormalMappingPass_VS(in float4 inPos : POSITION0,in float2 inTex : TEXCOORD0,in float3 inNormal : NORMAL0,in float4 inTangent : TEXCOORD2,out float4 outPos : POSITION0,out float2 outTex : TEXCOORD0,out float3 outViewDir : TEXCOORD1,out float3 outSunDir : TEXCOORD2)
{
outPos = mul(inPos, matWorldViewProj);
outTex = inTex;

matTangent[0] = mul(inTangent.xyz, matWorld);
matTangent[1] = mul(cross(inTangent.xyz, inNormal) * inTangent.w, matWorld);
matTangent[2] = mul(inNormal, matWorld);

outViewDir = normalize(mul(matTangent, vecViewPos - mul(inPos, matWorld)));
outSunDir = normalize(mul(matTangent, -vecSunDir));
}

void NormalMappingPass_PS(in float2 inTex : TEXCOORD0,in float3 inViewDir : TEXCOORD1,in float4 inSunDir : TEXCOORD2,out float4 outColor : COLOR0)
{
float3 BumpNormal = 2 * tex2D(normal_sampler, inTex) - 1;
	
float4 Ambient = AmbientIntensity * vecAmbient;

float4 Diffuse = DiffuseIntensity * saturate(dot(inSunDir, BumpNormal));
Diffuse *= vecSunColor;

float3 R = normalize(2 * dot(BumpNormal, inSunDir) * BumpNormal - inSunDir);

inViewDir = normalize(inViewDir);
float Specular = pow(saturate(dot(R, inViewDir)), SpecularPower) * SpecularIntensity;

float4 Color = tex2D(base_sampler, inTex);

outColor = (Ambient + Diffuse + Specular) * Color;
}

// techniques
technique ShaderTechnique
{
pass NormalMappingPass
{
VertexShader = compile vs_2_0 NormalMappingPass_VS();
PixelShader = compile ps_2_0 NormalMappingPass_PS();
}
}

// fallback
technique fallback { pass one { } }



So far it works :-)

Then I tried to make the Shader using dynamic Lights, taken this code from the Wiki (Shader Hints, For the examples below, version 6.31.4 is required):

Code:
/// Use dynamic lights in a vertex shader
  float4 vecLightPos[8];
  float4 vecLightColor[8];
  ...
// return the dynamic light on the surface
  float4 DoPointLight(float3 P, float3 N, int i)
  {
// calculate the light ray pointing from the light to the surface
    float3 D = (float3)vecLightPos[i]-P;
// calculate the angle between surface and light ray
    float NdotL = dot(N,normalize(D));
// modulate the light by the surface angle
    float4 Color = vecLightColor[i] * NdotL;

// calculate the light attenuation factor
    float fac = 0.f;
    if (NdotL >= 0.f && vecLightPos[i].w > 0.f)
    {
// get the distance factor
      float LD = length(D)/vecLightPos[i].w;
      if (LD < 1.f)
        fac = 1.f - LD;
    }
    return Color * fac;
  }

  VS_OUT TerrainLight_VS (
    float4 inPos : POSITION,
    float3 inNormal : NORMAL)
  {
    VS_OUT Out;

// transform the vector position to screen coordinates
    Out.Pos = mul(inPos,matWorldViewProj);

// Terrains don't need to rotate the normal, but it must be normalized
    float3 N = normalize(inNormal);
    float3 P = mul(inPos,matWorld);

// Add 6 dynamic lights (maximum for vs 1.1)
    Out.Color = float4(0.f,0.f,0.f,0.f);
    for (int i=0; i<6; i++)
      Out.Color += DoPointLight(P,N,i);
    ...
    return Out;
  }



And I did fit this code into mine ...
new NormalMapping12.fx:

Code:
#include <lights>

// variables
float AmbientIntensity = 1.0f;
float DiffuseIntensity = 1.0f;
float SpecularIntensity = 2.0f;
float SpecularPower = 8.0f;

// vectors
// float4 vecSunColor;
// float4 vecSunDir;

/// Use dynamic lights in a vertex shader
float4 vecLightPos[8];
float4 vecLightColor[8];

float4 vecAmbient;
float4 vecViewPos;

// return the dynamic light on the surface
  float4 DoPointLight(float3 P, float3 N, int i)
  {
// calculate the light ray pointing from the light to the surface
    float3 D = (float3)vecLightPos[i]-P;
// calculate the angle between surface and light ray
    float NdotL = dot(N,normalize(D));
// modulate the light by the surface angle
    float4 Color = vecLightColor[i] * NdotL;

// calculate the light attenuation factor
    float fac = 0.f;
    if (NdotL >= 0.f && vecLightPos[i].w > 0.f)
    {
// get the distance factor
      float LD = length(D)/vecLightPos[i].w;
      if (LD < 1.f)
        fac = 1.f - LD;
    }
    return Color * fac;
  }

  VS_OUT TerrainLight_VS (
    float4 inPos : POSITION,
    float3 inNormal : NORMAL)
  {
    VS_OUT Out;

// transform the vector position to screen coordinates
    Out.Pos = mul(inPos,matWorldViewProj);

// Terrains don't need to rotate the normal, but it must be normalized
    float3 N = normalize(inNormal);
    float3 P = mul(inPos,matWorld);

// Add 6 dynamic lights (maximum for vs 1.1)
    Out.Color = float4(0.f,0.f,0.f,0.f);
    for (int i=0; i<6; i++)
      Out.Color += DoPointLight(P,N,i);
    
    return Out;
  }

// matrices
const float4x4 matWorldViewProj;
const float4x4 matWorld;
float3x3 matTangent;

// textures
texture entSkin1;
texture entSkin2;

// samplers
sampler base_sampler = sampler_state
{
Texture = <entSkin1>;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
MAGFILTER = POINT;
MINFILTER = POINT;
MIPFILTER = LINEAR;
};
sampler normal_sampler = sampler_state
{
Texture = <entSkin2>;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
MAGFILTER = POINT;
MINFILTER = POINT;
MIPFILTER = NONE;
};

// shader functions
void NormalMappingPass_VS(in float4 inPos : POSITION0,in float2 inTex : TEXCOORD0,in float3 inNormal : NORMAL0,in float4 inTangent : TEXCOORD2,out float4 outPos : POSITION0,out float2 outTex : TEXCOORD0,out float3 outViewDir : TEXCOORD1,out float3 outSunDir : TEXCOORD2)
{
outPos = mul(inPos, matWorldViewProj);
outTex = inTex;

matTangent[0] = mul(inTangent.xyz, matWorld);
matTangent[1] = mul(cross(inTangent.xyz, inNormal) * inTangent.w, matWorld);
matTangent[2] = mul(inNormal, matWorld);

outViewDir = normalize(mul(matTangent, vecViewPos - mul(inPos, matWorld)));
outSunDir = normalize(mul(matTangent, -vecSunDir));
}

void NormalMappingPass_PS(in float2 inTex : TEXCOORD0,in float3 inViewDir : TEXCOORD1,in float4 inSunDir : TEXCOORD2,out float4 outColor : COLOR0)
{
float3 BumpNormal = 2 * tex2D(normal_sampler, inTex) - 1;
	
float4 Ambient = AmbientIntensity * vecAmbient;

float4 Diffuse = DiffuseIntensity * saturate(dot(inSunDir, BumpNormal));
Diffuse *= vecSunColor;

float3 R = normalize(2 * dot(BumpNormal, inSunDir) * BumpNormal - inSunDir);

inViewDir = normalize(inViewDir);
float Specular = pow(saturate(dot(R, inViewDir)), SpecularPower) * SpecularIntensity;

float4 Color = tex2D(base_sampler, inTex);

outColor = (Ambient + Diffuse + Specular) * Color;
}

// techniques
technique ShaderTechnique
{
pass NormalMappingPass
{
VertexShader = compile vs_2_0 NormalMappingPass_VS();
PixelShader = compile ps_2_0 NormalMappingPass_PS();
}
}

// fallback
technique fallback { pass one { } }



To me it looks like, that the reason is, that it does not work with the A 6.31 code from the Wiki.

Does anyone know, how my code could be fixed, so that it works with dynamic Lights ok for A 8.20?

Please do let me know :-)

C.U. R.E.Z.





Last edited by REZ; 10/20/11 21:15.

R.E.Z. Software Development (TM)
Re: BumpMap ... How to Change Sunlight to DynamicLights? [Re: REZ] #385613
10/20/11 21:00
10/20/11 21:00
Joined: Sep 2009
Posts: 993
Budapest
Aku_Aku Offline
User
Aku_Aku  Offline
User

Joined: Sep 2009
Posts: 993
Budapest
Great!
How do it work with two or more dynamic lights?

Re: BumpMap ... How to Change Sunlight to DynamicLights? [Re: Aku_Aku] #385615
10/20/11 21:10
10/20/11 21:10
Joined: Aug 2005
Posts: 142
Pattensen - Germany
R
REZ Offline OP
Member
REZ  Offline OP
Member
R

Joined: Aug 2005
Posts: 142
Pattensen - Germany
It did not work at all. BlackScreen
Only the original NormalMapping12.fx did work.
But the Sunlight is shining onto the shaded surface of the Model. But it is inside a Level and not meant to be shine on the the Sun.

C.U. R.E.Z.


R.E.Z. Software Development (TM)

Moderated by  Blink, Hummel, Superku 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1