Post by Mike hartwig on Jan 2, 2010 19:32:13 GMT -5
Just copy and paste this code to the program.cs in the Shader tutorial.
Of course you need the ressources of TGMs ShaderPack in the medias
folder (get it here: rapidshare.com/files/28934132/TGMs_ShaderPack.rar.html ).
And here is the lava.hlsl (must be added to the medias folder:
Btw: PixelShader 2.0 is needed for this one. In GLSL (have a look at the original Shader pack) PixelShader 1.0 is required... Strange, or?!?
original post here:
irrlichtnetcp.sourceforge.net/phpBB2/viewtopic.php?t=475
Of course you need the ressources of TGMs ShaderPack in the medias
folder (get it here: rapidshare.com/files/28934132/TGMs_ShaderPack.rar.html ).
using System;
using System.Text;
using IrrlichtNETCP;
//Here is a new namespace... It contains all inheritable interfaces for Particle Emitters or Affectors for instance.
using IrrlichtNETCP.Inheritable;
namespace ShadersAndParticles
{
class Program
{
static VideoDriver Driver;
static SceneManager Scene;
static void Main(string[] args)
{
//We choosed OpenGL because it is cross-platform and we only have the openGL shader
//So please do not change this unless you change the shader !
IrrlichtDevice device = new IrrlichtDevice(DriverType.Direct3D9,
new Dimension2D(1024, 768),
32, false, true, true, false);
device.OnEvent += new OnEventDelegate(device_OnEvent);
//We set our handlers
Driver = device.VideoDriver;
Scene = device.SceneManager;
device.FileSystem.WorkingDirectory = "../../medias";
GPUProgrammingServices gpu = Driver.GPUProgrammingServices;
int shaderMat = (int)MaterialType.Solid; // Fallback material type
bool bCanDoHLSL_1_1 = false;
if (Driver.DriverType == DriverType.Direct3D9 )
{
bCanDoHLSL_1_1 = true;
if (!Driver.QueryFeature(VideoDriverFeature.VertexShader_2_0 ))
{
Console.WriteLine("queryFeature(Vertex_Shader_2_0) failed\n");
bCanDoHLSL_1_1 = false;
}
if (!Driver.QueryFeature(VideoDriverFeature.PixelShader_2_0))
{
Console.WriteLine("queryFeature(PixelShader_2_0) failed\n");
bCanDoHLSL_1_1 = false;
}
}
if (bCanDoHLSL_1_1)
{
Shader_Lava_Callback lava = new Shader_Lava_Callback();
shaderMat = Driver.GPUProgrammingServices.AddHighLevelShaderMaterialFromFiles(
"lava.hlsl", "vertexMain", VertexShaderType._2_0,
"lava.hlsl", "pixelMain", PixelShaderType._2_0,
lava.OnShaderSet, MaterialType.Solid, 0);
}
CameraSceneNode fpsCamera = Scene.AddCameraSceneNodeFPS( null, 150, 250, false );
fpsCamera.Position = new Vector3D( 0, 0, -100 );
device.CursorControl.Visible = false;
AnimatedMesh mesh = Scene.GetMesh("dwarf.x");
AnimatedMeshSceneNode node = Scene.AddAnimatedMeshSceneNode(mesh);
node.SetMaterialFlag(MaterialFlag.Lighting, false);
node.SetMaterialTexture(0, Driver.GetTexture("cloud.tga"));
node.SetMaterialTexture(1, Driver.GetTexture("lavatile.jpg"));
node.SetMaterialType(shaderMat);
// node.SetFrameLoop(1, 1);
int lastFPS = -1;
while(device.Run())
{
if (device.WindowActive)
{
Driver.BeginScene( true, true, new Color( 0, 0, 0, 0 ) );
Scene.DrawAll();
Driver.EndScene();
int fps = Driver.FPS;
if( lastFPS != fps )
{
device.WindowCaption = "Lava Shader Demo [" + fps + "]";
lastFPS = fps;
}
}
}
device.Dispose(); // drop device
return;
}
static bool Exit = false;
static bool device_OnEvent(Event ev)
{
if (ev.KeyPressedDown && ev.KeyCode == KeyCode.Escape)
Exit = true;
return false;
}
}
public class Shader_Lava_Callback
{
float time;
public float Time
{
get { return time; }
set { time = value; }
}
static float[] colorToArray(Colorf p_m)
{
float[] t_a = new float[4];
t_a[0] = p_m.R;
t_a[1] = p_m.G;
t_a[2] = p_m.B;
t_a[3] = p_m.A;
return t_a;
}
static float[] vectorToArray(Vector3D p_m)
{
float[] t_a = new float[4];
t_a[0] = p_m.X;
t_a[1] = p_m.Y;
t_a[2] = p_m.Z;
t_a[3] = 0;
return t_a;
}
public void OnShaderSet(MaterialRendererServices services, int userData)
{
time += 0.005f;
services.SetPixelShaderConstant("time", time );
Matrix4 worldViewProj;
worldViewProj = services.VideoDriver.GetTransform(TransformationState.Projection);
worldViewProj *= services.VideoDriver.GetTransform(TransformationState.View);
worldViewProj *= services.VideoDriver.GetTransform(TransformationState.World);
services.SetVertexShaderConstant("mWorldViewProj", worldViewProj.ToShader(), 16);
}
}
}
And here is the lava.hlsl (must be added to the medias folder:
float4x4 mWorldViewProj; // World * View * Projection transformation
struct VS_OUTPUT
{
float4 Position : POSITION; // vertex position
float2 TexCoord : TEXCOORD0; // tex coords
};
VS_OUTPUT vertexMain( in float4 vPosition : POSITION,
float2 texCoord : TEXCOORD0 )
{
VS_OUTPUT Output;
// transform position to clip space
Output.Position = mul(vPosition, mWorldViewProj);
Output.TexCoord = texCoord;
return Output;
}
struct PS_OUTPUT
{
float4 RGBColor : COLOR0; // Pixel color
};
float time;
sampler2D tex0;
sampler2D tex1;
PS_OUTPUT pixelMain( float2 TexCoord : TEXCOORD0,
float4 Position : POSITION )
{
PS_OUTPUT Output;
float4 noise = tex2D( tex0, TexCoord ); // sample color map
float2 T1 = TexCoord + float2(1.5,-1.5)*time*0.02;
float2 T2 = TexCoord + float2(-0.5,2.0)*time*0.01;
T1.x += (noise.x)*2.0;
T1.y += (noise.y)*2.0;
T2.x += (noise.y)*0.2;
T2.y += (noise.z)*0.2;
float p = tex2D( tex0, T1*2.0).a;
float4 col = tex2D( tex1, T2*2.0 );
float4 temp = col*(float4(p,p,p,p)*2.0)+(col*col-0.1);
if(temp.r > 1.0 ) { temp.bg += clamp(temp.r-2.0,0.0,100.0); }
if(temp.g > 1.0 ) { temp.rb += temp.g-1.0; }
if(temp.b > 1.0 ) { temp.rg += temp.b-1.0; }
Output.RGBColor = temp;
return Output;
}
Btw: PixelShader 2.0 is needed for this one. In GLSL (have a look at the original Shader pack) PixelShader 1.0 is required... Strange, or?!?
original post here:
irrlichtnetcp.sourceforge.net/phpBB2/viewtopic.php?t=475