Hot Lab #3 - Demo 13 : Material Alpha Blending - The Mail Owl Scrolls

Hot Lab #3 - Demo 13 : Material Alpha Blending

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DX13MaterialAlphaBlending
{
    
public class Form1 : System.Windows.Forms.Form
    {
        
private System.ComponentModel.Container components = null;
        
private Device GraphicDevice;

        
private WorldObject solidobj;
        
private TransparentObject transobj;

        
private float angle = 0;

        
public Form1()
        {
            InitializeComponent();
        }

        
protected override void Dispose( bool disposing )
        {
            
if( disposing )
            {
                
if (components != null
                {
                    components.Dispose();
                }
            }
            
base.Dispose( disposing );
        }

        
#region Windows Form Designer generated code

        private void 
InitializeComponent()
        {
            
// 
            // Form1
            // 
            
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            
this.ClientSize = new System.Drawing.Size(292, 266);
            
this.Name = "Form1";
            
this.Text = "Form1";
            
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);

        }
        
#endregion

        
[STAThread]
        
static void Main() 
        {
            Form1 frm = 
new Form1();

            frm.InitializeGraphics();
            frm.Show();

            
while (frm.Created)
            {
                frm.Render();
                Application.DoEvents();
            }

            frm.DisposeGraphics();
        }

        
public void InitializeGraphics()
        {
            PresentParameters pp = 
new PresentParameters();
            
            pp.Windowed = 
true;
            pp.BackBufferFormat = Format.X8R8G8B8;
            pp.SwapEffect = SwapEffect.Discard;
            pp.AutoDepthStencilFormat = DepthFormat.D16;
            pp.EnableAutoDepthStencil = 
true;

            GraphicDevice = 
new Device(0,DeviceType.Hardware,this,CreateFlags.SoftwareVertexProcessing,pp);

            GraphicDevice.RenderState.Lighting = 
true;
            
            GraphicDevice.RenderState.DiffuseMaterialSource = ColorSource.Material;
            
            
// Set up the device to render the alpha information.
            
GraphicDevice.RenderState.AlphaBlendEnable = true;
            GraphicDevice.RenderState.SourceBlend = Blend.SourceColor; 
            GraphicDevice.RenderState.DestinationBlend = Blend.InvSourceAlpha;

            
//instantiate cube
            
solidobj = new WorldObject(ref GraphicDevice);
            transobj = 
new TransparentObject(ref GraphicDevice);
        }

        
public void DisposeGraphics()
        {
            GraphicDevice.Dispose();
        }

        
public void Render()
        {
            GraphicDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer,Color.CornflowerBlue ,1,0);

            GraphicDevice.Lights[0].Type = LightType.Point ;
            GraphicDevice.Lights[0].Position = 
new Vector3(-7,0,-5);
            GraphicDevice.Lights[0].Attenuation1 = 0.1f;
            GraphicDevice.Lights[0].Range = 20;

            GraphicDevice.Lights[0].Enabled = 
true;

            GraphicDevice.Transform.Projection = Matrix.PerspectiveFovLH((
float)Math.PI / 4, this.Width / this.Height,1,1000);
            GraphicDevice.Transform.View = Matrix.LookAtLH(
new Vector3(0,0,-5),new Vector3(0,0,0),new Vector3(0,1,0));

            GraphicDevice.BeginScene();

            GraphicDevice.Transform.World = Matrix.Multiply(Matrix.RotationY(Geometry.DegreeToRadian(45)),Matrix.Translation(0,0,5));
            solidobj.Render(
ref GraphicDevice);

            GraphicDevice.Transform.World = Matrix.Multiply(Matrix.RotationY(Geometry.DegreeToRadian(angle)),Matrix.RotationX(Geometry.DegreeToRadian(angle)));
            GraphicDevice.Transform.World = Matrix.Multiply(GraphicDevice.Transform.World,Matrix.RotationZ(Geometry.DegreeToRadian(-angle)));
            transobj.Render(
ref GraphicDevice);


            angle += 1f;
            
if (angle == 360.0f)
            {
                angle = 0;
            }
            
            GraphicDevice.EndScene();

            GraphicDevice.Present();
        }

        
private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            
if (e.KeyChar.ToString() == "w")
            {
                
if (GraphicDevice.RenderState.FillMode == FillMode.WireFrame)
                {
                    GraphicDevice.RenderState.FillMode = FillMode.Solid;
                    GraphicDevice.RenderState.CullMode = Cull.CounterClockwise;
                }
                
else
                
{
                    GraphicDevice.RenderState.FillMode = FillMode.WireFrame;
                    GraphicDevice.RenderState.CullMode = Cull.None;
                }
            }
        }
    }

    
public class WorldObject
    {
        Mesh mesh;
        Material material;

        
public WorldObject(ref Device dev)
        {
            mesh = Mesh.Box(dev,5,5,1);
            
            material = 
new Material();
            Color c = Color.Red;

            material.Diffuse = c;
            material.Ambient = c;
            material.Specular = c;
            
//material.Emissive = c;

        
}

        
public void Render(ref Device dev)
        {
            dev.Material = material;

            mesh.DrawSubset(0);
        }
    }

    
public class TransparentObject
    {
        Mesh mesh;
        Material material;

        
public TransparentObject(ref Device dev)
        {
            mesh = Mesh.Teapot(dev);
            
            material = 
new Material();
            Color c = Color.FromArgb(127,Color.Gray);

            material.Diffuse = c;
            material.Ambient = c;
            material.Specular = c;
            material.Emissive = c;
        }

        
public void Render(ref Device dev)
        {
            dev.Material = material;

            mesh.DrawSubset(0);

        }
    }
}

powered by IMHO 1.2

Published Monday, August 01, 2005 2:45 PM by mailowl

Comments

# Direct X Hot Lab #2 - Conclusion

Monday, August 01, 2005 3:09 PM by TrackBack

# re: Hot Lab #3 - Demo 13 : Material Alpha Blending

6zsmRL Hi from Russia!

Sunday, March 16, 2008 11:07 PM by zxevil163

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: