Sign in to follow this  

Depth explosion

This topic is 3842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I found a demo on ati website. http://ati.amd.com/developer/samples/DepthExplosion.html This demo has introduced a way in which we can simulate a 3d explosion rather than 2d explosion(billboarding). this code is in c++. I tried to convert it to c# but i couldn't. Can you help me, please?

Share this post


Link to post
Share on other sites
That's an extremely general problem. I don't think anyone is prepared to translate it for you. We'd like to at least know that you have tried and failed before handing out metaphorical fish (or fishing nouse). How much C++ do you know and what exactly did you have trouble with?

Admiral

Share this post


Link to post
Share on other sites
It's my converted code :

public partial class ExplosionForm : Form
{
private const float EXPLOSION_SIZE = 40.0f;
private const float Z_NEAR = 1.0f;
private const float Z_FAR = 100.0f;
private const float EYE_Z = 50.0f;

private Device _device;
private Color _backColor;
private Material _mat;

private VertexBuffer _vb;
private VertexDeclaration _vd;

private Texture _depthTexture;
private Texture _nois1Texture;
private Texture _nois2Texture;
private Texture[] _exploTexture;

CustomVertex.PositionTextured[] _billboard;

private VertexShader _vs;
private PixelShader _ps;
private HPTimer _timer;

public ExplosionForm()
{
InitializeComponent();

//OpenFileDialog dialog = new OpenFileDialog();
//dialog.CheckFileExists = true;
//if (dialog.ShowDialog() == DialogResult.Cancel) Application.Exit();

PresentParameters presentParameters = new PresentParameters();
presentParameters.SwapEffect = SwapEffect.Discard;
presentParameters.Windowed = true;

this.Height = 700;
this.Width = 1000;
this.StartPosition = FormStartPosition.CenterScreen;

this.Setstyle(Controlstyles.AllPaintingInWmPaint | Controlstyles.Opaque, true);

_device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing
, presentParameters);

_backColor = Color.LightSkyBlue;

_device.RenderState.Lighting = true;
_device.RenderState.Ambient = Color.White;

Material material = new Material();
material.Ambient = Color.White;
_device.Material = material;

_device.Lights[0].Ambient = Color.White;
_device.Lights[0].Type = LightType.Directional;
_device.Lights[0].Direction = new Vector3(0, 0, 1);
_device.Lights[0].Enabled = true;
_device.Lights[0].Update();

_vb = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, _device, Usage.WriteOnly
, CustomVertex.PositionTextured.Format, Pool.Managed);
_vb.Created += new EventHandler(this.OnVertexBufferCreate);
OnVertexBufferCreate(_vb, null);


_depthTexture = TextureLoader.FromFile(_device, Application.StartupPath + @"\depth.tga");
_nois1Texture = TextureLoader.FromFile(_device, Application.StartupPath + @"\noise.tga");
_nois2Texture = TextureLoader.FromFile(_device, Application.StartupPath + @"\noise.tga");

_exploTexture = new Texture[30];
for (int i = 0; i < _exploTexture.Length; i++)
_exploTexture[i] = TextureLoader.FromFile(_device, Application.StartupPath + @"\fireball" + i.ToString("00") + ".tga");

_device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 3, this.Width / this.Height, Z_NEAR, Z_FAR);

Matrix mat1 = _device.Transform.World * _device.Transform.View;
Matrix mat2 = Matrix.MultiplyTranspose(mat1 , _device.Transform.Projection);

_device.SetVertexShaderConstant(0, mat2);

_device.SetSamplerState(0, SamplerStageStates.MinFilter, (int)Filter.Linear);
_device.SetSamplerState(0, SamplerStageStates.MagFilter, (int)Filter.Linear);
_device.SetSamplerState(1, SamplerStageStates.MinFilter, (int)Filter.Linear);
_device.SetSamplerState(1, SamplerStageStates.MagFilter, (int)Filter.Linear);
_device.SetSamplerState(2, SamplerStageStates.MinFilter, (int)Filter.Linear);
_device.SetSamplerState(2, SamplerStageStates.MagFilter, (int)Filter.Linear);

// Create the vertexdeclaration
VertexElement[] v = new VertexElement[] {
new VertexElement(0,0,DeclarationType.Float3,DeclarationMethod.Default,DeclarationUsage.Position,0)
, new VertexElement(0,12,DeclarationType.Float2,DeclarationMethod.Default,DeclarationUsage.TextureCoordinate,0)
, VertexElement.VertexDeclarationEnd};

_vd = new VertexDeclaration(_device, v);

string error = "";
GraphicsStream gs = ShaderLoader.FromFile(Application.StartupPath + @"\v.vsh", null, ShaderFlags.None, out error);
//GraphicsStream xs = ShaderLoader.CompileShaderFromFile(Application.StartupPath + @"\v.vsh"
// , "", null, "vs_1_1", ShaderFlags.SkipValidation);
_vs = new VertexShader(_device, gs);

gs = ShaderLoader.FromFile(Application.StartupPath + @"\d.psh", null, ShaderFlags.None, out error);
_ps = new PixelShader(_device, gs);

_timer = new HPTimer();
_timer.Start();
}

private void OnVertexBufferCreate(object sender, EventArgs e)
{
VertexBuffer buffer = (VertexBuffer)sender;

_billboard = new CustomVertex.PositionTextured[4];
_billboard[0] = new CustomVertex.PositionTextured(0, 0, 0, 0, 0);
_billboard[1] = new CustomVertex.PositionTextured(128, 0, 0, 1, 0);
_billboard[2] = new CustomVertex.PositionTextured(128, 128, 0, 1, 1);
_billboard[3] = new CustomVertex.PositionTextured(0, 128, 0, 0, 1);

buffer.SetData(_billboard, 0, LockFlags.None);
}

int _frame1 = 0;

protected override void OnPaint(PaintEventArgs e)
{
_device.Clear(ClearFlags.Target, _backColor, 0, 0);

float size, f;
Vector4 v;

f = (float)Math.IEEERemainder(Math.Max(_timer.Time - 2.0f, 0.0f) / 6.0f, 1.2f);
if (f < 1.0f)
size = EXPLOSION_SIZE * (float)Math.Pow(f, 0.3f);
else
size = 0;

// billboard size
v = new Vector4(size, 0.0f, 0.0f, 1.0f);
_device.SetVertexShaderConstant(9, v);

// billboard position
v = new Vector4(0.0f, -6.0f, 0.0f, 0.0f);
_device.SetVertexShaderConstant(10, v);

// depth bias & scale for sprite
float depthScale = MapDepth(EYE_Z) - MapDepth(EYE_Z - size);
float depthBias = MapDepth(EYE_Z);
v = new Vector4(depthScale, depthBias, 0.0f, 0.0f);
_device.SetVertexShaderConstant(11, v);

// noise texture xform
v = new Vector4(0.1f, 0.1f - (float)Math.IEEERemainder(_timer.Time / 4.0f, 1.0f), 1.5f, 1.0f);
_device.SetVertexShaderConstant(12, v);

// fade between fireball and smoke
float fireFade;
if (f > 0.3f)
fireFade = 0.0f;
else
fireFade = 1.0f - f / 0.3f;

// fade out the explosion
float fade = 1.0f - (float)Math.Pow(f, 5.0f);

v = new Vector4(fireFade, fade, 0.0f, 0.0f);
_device.SetPixelShaderConstant(1, v);

// compute currect frame in fireball animation
_frame1 = (int)(Math.Min(1.0f, f / 0.3f) * (_exploTexture.Length - 1));

try
{
_device.BeginScene();

_device.RenderState.Lighting = false;
_device.RenderState.AlphaBlendEnable = true;
_device.RenderState.SourceBlend = Blend.SourceAlpha;
_device.RenderState.DestinationBlend = Blend.InvSourceAlpha;

_device.SetTexture(0, _depthTexture);
_device.SetTexture(1, _nois1Texture);
_device.SetTexture(2, _nois2Texture);
_device.SetTexture(3, _exploTexture[_frame1]);

_device.PixelShader = _ps;
_device.VertexDeclaration = _vd;
_device.VertexShader = _vs;

//_device.VertexFormat = CustomVertex.TransformedTextured.Format;
//_device.SetStreamSource(0, _vb, 0);
//_device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);
_device.DrawUserPrimitives(PrimitiveType.TriangleFan, 2, _billboard);


//_frame1 = (_frame1 + 1) % _exploTexture.Length;
}
finally
{
_device.EndScene();
_device.Present();
}

// Thread.Sleep(10);
this.Invalidate();
}

Share this post


Link to post
Share on other sites

This topic is 3842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this