Gamestudio Links
Zorro Links
Newest Posts
Why Zorro supports up to 72 cores?
by 11honza11. 04/26/24 08:55
M1 Oversampling
by 11honza11. 04/26/24 08:32
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Data from CSV not parsed correctly
by EternallyCurious. 04/25/24 10:20
Trading Journey
by howardR. 04/24/24 20:04
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (AndrewAMD, VoroneTZ, Quad, 1 invisible), 826 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 3 1 2 3
NVIDIA Shader Library Convertation start #138920
06/30/07 16:35
06/30/07 16:35
Joined: May 2005
Posts: 2,713
Lübeck
Slin Offline OP
Expert
Slin  Offline OP
Expert

Joined: May 2005
Posts: 2,713
Lübeck
Yesterday I found this awsome collection and converted the first shader.
I think that I´ll go on converting them always if I´m not motivated enough to go on with "Chopper Zone".

Normal, Parallax and Reliefmapping:
(I´ll post an example file later today or tomorrow)
The alphachannel of the normalmap gives the height.
The first Modelskin is the textur and the second the normalmap.
Only the Sunlight is supported



Code:

/*********************************************************************NVMH3****
File: $Id: //sw/devtools/ShaderLibrary/main/HLSL/relief_mapping.fx#1 $

Copyright NVIDIA Corporation 2007
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY
LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

% This material shows and compares results from four popular and
% advanced schemes for emulating displaement mapping. They are:
% Relief Mapping, Parallax Mapping, Normal Mapping, and Relief
% Mapping with Shadows. Original File by Fabio Policarpo.

keywords: material bumpmap
date: 070305

Note: Strong discontinuties in the model geometric normal (e.g., very sharp
differences from the normals in two or more parts of the same triangle)
can cause unusual overall light-attentuation errors. Re-normalizing the
rasterized normals in the pixel shader can correct this, but the case
was considered rare enough that these extra steps were eliminated for
code efficiency. If you see off lighting near sharp model edges, just
normalize "IN.normal" in the calculation of the varible "att" (shared
by all techniques).

******************************************************************************/

//// UN-TWEAKABLES - AUTOMATICALLY-TRACKED TRANSFORMS ////////////////

float4x4 matWorldViewProj;
float4x4 matView;
float4x4 matWorldView;

/////////////// Tweakables //////////

float4 vecSunPos;
float4 vecLight;
float3 DiffColor = {1,1,1};
float3 SpecColor = {0.75,0.75,0.75};

float PhongExp = 128.0;
float TileCount = 1;
float Depth = 0.1;

/*********** TEXTURES ***************/

texture entSkin1;

sampler2D ColorSampler = sampler_state
{
Texture = <entSkin1>;
MinFilter = Linear;
MipFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};

texture entSkin2;

sampler2D ReliefSampler = sampler_state
{
Texture = <entSkin2>;
MinFilter = Linear;
MipFilter = Linear;
MagFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};

/********** CONNECTOR STRUCTURES *****************/

struct AppVertexData
{
float4 pos : POSITION;
float4 color : COLOR0;
float3 normal : NORMAL; // expected to be normalized
float2 txcoord : TEXCOORD0;
float3 tangent : TEXCOORD2; // pre-normalized
};

struct VertexOutput
{
float4 hpos : POSITION;
float2 UV : TEXCOORD0;
float3 vpos : TEXCOORD1;
float3 tangent : TEXCOORD2;
float3 binormal : TEXCOORD3;
float3 normal : TEXCOORD4;
float4 lightpos : TEXCOORD5;
float4 color : COLOR0;
};

/*** SHADER FUNCTIONS **********************************************/

VertexOutput view_spaceVS(AppVertexData IN)
{
VertexOutput OUT = (VertexOutput)0;

float3 binormal = cross(IN.normal,IN.tangent);

// isolate matWorldView rotation-only part
float3x3 modelViewRotXf;
modelViewRotXf[0] = matWorldView[0].xyz;
modelViewRotXf[1] = matWorldView[1].xyz;
modelViewRotXf[2] = matWorldView[2].xyz;
float4 Po = float4(IN.pos.xyz,1.0);
OUT.hpos = mul(Po,matWorldViewProj);

// vertex position in view space (with model transformations)
OUT.vpos = mul(Po,matWorldView).xyz;

// light position in view space
float4 Lw = float4(vecSunPos.xyz,1); // this point in world space
OUT.lightpos = mul(Lw,matWorldView); // this point in view space

// tangent space vectors in view space (with model transformations)
OUT.tangent = mul(IN.tangent,modelViewRotXf);
OUT.binormal = mul(binormal,modelViewRotXf);
OUT.normal = mul(IN.normal,modelViewRotXf);

// copy color and texture coordinates
OUT.color = IN.color; // currently ignored by all techniques
OUT.UV = TileCount * IN.txcoord.xy;

return OUT;
}

/************ PIXEL SHADERS ******************/

float4 normal_mapPS(VertexOutput IN) : COLOR
{
float3 tNorm = tex2D(ReliefSampler,IN.UV).xyz - float3(0.5,0.5,0.5);
// transform tNorm to world space
tNorm = normalize(tNorm.x*IN.tangent -
tNorm.y*IN.binormal +
tNorm.z*IN.normal);
float3 texCol = tex2D(ColorSampler,IN.UV).xyz;
// view and light directions
float3 Vn = normalize(IN.vpos);
float3 Ln = normalize(IN.lightpos.xyz-IN.vpos);
// compute diffuse and specular terms
float att = saturate(dot(Ln,IN.normal));
float diff = saturate(dot(Ln,tNorm));
float spec = saturate(dot(normalize(Ln-Vn),tNorm));
spec = pow(spec,PhongExp);
// compute final color
float3 finalcolor = vecLight.xyz*texCol +
att*(texCol*DiffColor.xyz*diff+SpecColor*spec);
return float4(finalcolor.rgb,1.0);
}

float4 parallax_mapPS(VertexOutput IN) : COLOR
{
// view and light directions
float3 Vn = normalize(IN.vpos);
float3 Ln = normalize(IN.lightpos.xyz-IN.vpos);
float2 uv = IN.UV;
// parallax code
float3x3 tbnXf = float3x3(IN.tangent,IN.binormal,IN.normal);
float4 reliefTex = tex2D(ReliefSampler,uv);
float height = reliefTex.w * 0.06 - 0.03;
uv += height * mul(tbnXf,Vn).xy;
// normal map
float3 tNorm = reliefTex.xyz - float3(0.5,0.5,0.5);
// transform tNorm to world space
tNorm = normalize(tNorm.x*IN.tangent -
tNorm.y*IN.binormal +
tNorm.z*IN.normal);
float3 texCol = tex2D(ColorSampler,uv).xyz;
// compute diffuse and specular terms
float att = saturate(dot(Ln,IN.normal));
float diff = saturate(dot(Ln,tNorm));
float spec = saturate(dot(normalize(Ln-Vn),tNorm));
spec = pow(spec,PhongExp);
// compute final color
float3 finalcolor = vecLight.xyz*texCol +
att*(texCol*DiffColor.xyz*diff+SpecColor*spec);
return float4(finalcolor.rgb,1.0);
}

//// ray-intersect functions for relief mapping //////////

float ray_intersect_rm( // use linear and binary search
in sampler2D reliefmap,
in float2 dp,
in float2 ds)
{
const int linear_search_steps=15;

// current size of search window
float size = 1.0/linear_search_steps;
// current depth position
float depth = 0.0;
// search front to back for first point inside object
for( int i=0;i<linear_search_steps-1;i++ ) {
float4 t = tex2D(reliefmap,dp+ds*depth);
if (depth<t.w)
depth += size;
}
const int binary_search_steps=5;
// recurse around first point (depth) for closest match
for( int i=0;i<binary_search_steps;i++ ) {
size*=0.5;
float4 t = tex2D(reliefmap,dp+ds*depth);
if (depth<t.w)
depth += (2*size);
depth -= size;
}
return depth;
}

float ray_intersect_rm_lin( // only linear search for shadows
in sampler2D reliefmap,
in float2 dp,
in float2 ds)
{
const int linear_search_steps=15;
// current size of search window
float size = 1.0/linear_search_steps;
// current depth position
float depth = 0.0;
// search front to back for first point inside object
for( int i=0;i<linear_search_steps-1;i++ ) {
float4 t = tex2D(reliefmap,dp+ds*depth);
if (depth<t.w)
depth += size;
}
return depth;
}

////// relief mapping pixel shaders ////////

float4 relief_map_shadowsPS(VertexOutput IN) : COLOR
{
// ray intersect in view direction
float3 p = IN.vpos;
float3 Vn = normalize(p);
float a = dot(IN.normal,-Vn);
float3 s = float3(dot(Vn,IN.tangent.xyz), dot(Vn,IN.binormal.xyz), a);
s *= Depth/a;
float2 ds = s.xy;
float2 dp = IN.UV;
float d = ray_intersect_rm(ReliefSampler,dp,ds);

// get rm and color texture points
float2 uv = dp+ds*d;
float3 texCol = tex2D(ColorSampler,uv).xyz;
float3 tNorm = tex2D(ReliefSampler,uv).xyz - float3(0.5,0.5,0.5);
tNorm = normalize(tNorm.x*IN.tangent - tNorm.y*IN.binormal + tNorm.z*IN.normal);

// compute light direction
p += Vn*d/(a*Depth);
float3 Ln = normalize(p-IN.lightpos.xyz);

// compute diffuse and specular terms
float att = saturate(dot(-Ln,IN.normal));
float diff = saturate(dot(-Ln,tNorm));
float spec = saturate(dot(normalize(-Ln-Vn),tNorm));

// ray intersect in light direction
dp+= ds*d;
a = dot(IN.normal,-Ln);
s = float3(dot(Ln,IN.tangent.xyz),dot(Ln,IN.binormal.xyz),a);
s *= Depth/a;
ds = s.xy;
dp -= ds*d;
float dl = ray_intersect_rm_lin(ReliefSampler,dp,s.xy);
if (dl<d-0.05) // if pixel in shadow
{
diff *= dot(vecLight.xyz,float3(1.0,1.0,1.0))*0.133333;
spec = 0;
}
spec = pow(spec,PhongExp);

// compute final color
float3 finalcolor = vecLight.xyz*texCol + att*(texCol*DiffColor*diff+SpecColor*spec);
return float4(finalcolor.rgb,1.0);
}

float4 relief_mapPS(VertexOutput IN) : COLOR
{
// ray intersect in view direction
float3 p = IN.vpos;
float3 Vn = normalize(p);
float a = dot(IN.normal,-Vn);
float3 s = float3(dot(Vn,IN.tangent.xyz), dot(Vn,IN.binormal.xyz), a);
s *= Depth/a;
float2 ds = s.xy;
float2 dp = IN.UV;
float d = ray_intersect_rm(ReliefSampler,dp,ds);

// get rm and color texture points
float2 uv = dp+ds*d;
float3 texCol = tex2D(ColorSampler,uv).xyz;
float3 tNorm = tex2D(ReliefSampler,uv).xyz - float3(0.5,0.5,0.5);
tNorm = normalize(tNorm.x*IN.tangent - tNorm.y*IN.binormal + tNorm.z*IN.normal);

// compute light direction
p += Vn*d/(a*Depth);
float3 Ln = normalize(p-IN.lightpos.xyz);

// compute diffuse and specular terms
float att = saturate(dot(-Ln,IN.normal));
float diff = saturate(dot(-Ln,tNorm));
float spec = saturate(dot(normalize(-Ln-Vn),tNorm));
spec = pow(spec,PhongExp);

// compute final color
float3 finalcolor = vecLight.xyz*texCol +
att*(texCol*DiffColor*diff+SpecColor*spec);
return float4(finalcolor.rgb,1.0);
}

/********************* TECHNIQUES **************/
/*
technique normal_mapping {
pass p0 {
// CullMode = CW;
VertexShader = compile vs_2_0 view_spaceVS();
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
PixelShader = compile ps_2_a normal_mapPS();
}
}
*/
/*
technique parallax_mapping {
pass p0 {
// CullMode = CW;
VertexShader = compile vs_2_0 view_spaceVS();
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
PixelShader = compile ps_2_a parallax_mapPS();
}
}
*/

technique relief_mapping {
pass p0 {
// CullMode = CW;
VertexShader = compile vs_2_0 view_spaceVS();
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
PixelShader = compile ps_2_a relief_mapPS();
}
}

/*
technique relief_mapping_shadows {
pass p0 {
// CullMode = CW;
VertexShader = compile vs_2_0 view_spaceVS();
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
PixelShader = compile ps_2_a relief_map_shadowsPS();
}
}
*/
/****************************************** EOF ***/





I hope that this could become handy for someone.

Slin

Re: NVIDIA Shader Library Convertation start [Re: Slin] #138921
06/30/07 18:01
06/30/07 18:01
Joined: Feb 2006
Posts: 2,185
mpdeveloper_B Offline
Expert
mpdeveloper_B  Offline
Expert

Joined: Feb 2006
Posts: 2,185
nice to see parallax mapping finally coming to 3DGS, very good job


- aka Manslayer101
Re: NVIDIA Shader Library Convertation start [Re: mpdeveloper_B] #138922
07/01/07 00:01
07/01/07 00:01
Joined: May 2005
Posts: 2,713
Lübeck
Slin Offline OP
Expert
Slin  Offline OP
Expert

Joined: May 2005
Posts: 2,713
Lübeck
Now, you can download an example level here:
http://files.filefront.com/ShaderLibrary/;7928519;;/fileinfo.html

Move with the mouse and WASD.



Last edited by Slin; 07/01/07 00:15.
Re: NVIDIA Shader Library Convertation start [Re: Slin] #138923
07/01/07 02:44
07/01/07 02:44
Joined: Oct 2003
Posts: 827
22�21'24"N 114�07'30"E
Frederick_Lim Offline
User
Frederick_Lim  Offline
User

Joined: Oct 2003
Posts: 827
22�21'24"N 114�07'30"E
Thank you!

Re: NVIDIA Shader Library Convertation start [Re: Slin] #138924
07/01/07 06:34
07/01/07 06:34
Joined: Jun 2005
Posts: 656
G
Grafton Offline
User
Grafton  Offline
User
G

Joined: Jun 2005
Posts: 656
Thanks Slin! Very Awesome 5 star contribution, especially the demo, very cool!
I really hope you are motivated to convert some more


Not two, not one.
Re: NVIDIA Shader Library Convertation start [Re: Grafton] #138925
07/01/07 07:54
07/01/07 07:54
Joined: Nov 2004
Posts: 7,121
Potsdam, Brandenburg, Germany
Machinery_Frank Offline
Senior Expert
Machinery_Frank  Offline
Senior Expert

Joined: Nov 2004
Posts: 7,121
Potsdam, Brandenburg, Germany
5 points from me as well.

Slin, youd did a very good job. The shaders work fine and produce a good fps rate for what they are doing.

If you continue this project then please try to add a few dynamic lights. If you need any textures and models for testing purposes then please let me know.

You are a good man!
Thanks!


Models, Textures and Games from Dexsoft
Re: NVIDIA Shader Library Convertation start [Re: Slin] #138926
07/01/07 09:01
07/01/07 09:01
Joined: Oct 2002
Posts: 254
UK, London
Dyc Offline
Member
Dyc  Offline
Member

Joined: Oct 2002
Posts: 254
UK, London
Thanks Slin, shame it doesn't really work very well on my ati x850xt.

Re: NVIDIA Shader Library Convertation start [Re: Dyc] #138927
07/01/07 09:51
07/01/07 09:51
Joined: Mar 2005
Posts: 969
ch
Loopix Offline
User
Loopix  Offline
User

Joined: Mar 2005
Posts: 969
ch
Yeahh...a new shader library for 3dgs Unfotunately the relief and relief/shadow don't work on my ATI x800...probable a NVIDIA buit in feature to boycott ATI

Re: NVIDIA Shader Library Convertation start [Re: Slin] #138928
07/01/07 12:26
07/01/07 12:26
Joined: Oct 2003
Posts: 827
22�21'24"N 114�07'30"E
Frederick_Lim Offline
User
Frederick_Lim  Offline
User

Joined: Oct 2003
Posts: 827
22�21'24"N 114�07'30"E
The sample works fine with my 6800 GT, but I cannot get the shader work with WED level block.

Re: NVIDIA Shader Library Convertation start [Re: Frederick_Lim] #138929
07/01/07 13:33
07/01/07 13:33
Joined: May 2005
Posts: 2,713
Lübeck
Slin Offline OP
Expert
Slin  Offline OP
Expert

Joined: May 2005
Posts: 2,713
Lübeck
Quote:


If you continue this project then please try to add a few dynamic lights. If you need any textures and models for testing purposes then please let me know.





Thanks for that offer, if want to, send me anything you want to see the shader on.
As a next step I´ll add some more lights. How many do you want?
I think I should add fog support either. And I should add something like lightranges.
My MSN adress is nils_daumann[at]online[point]de


Quote:


The sample works fine with my 6800 GT, but I cannot get the shader work with WED level block.





I the shader you have to change entSkin2 to mtlSkin1.
You have to set the flat flag in WED for that texture and you shouldn´t forget to add the normalmap skin to the material wich has to have the same name than the leveltexture:

bmap BMAP_Name = <Normalmap.tga>;
material TextureName
{
skin1 = BMAP_Name;
//...
}


Is there anyone with an ati graphicscard for wich those effects are working?
Do you get any error messages or is the model just black or what happens?


Thanks for all your feedback

Slin

Page 1 of 3 1 2 3

Moderated by  adoado, checkbutton, mk_1, Perro 

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