Hot Lab #3 - Demo 13 : Material Alpha Blending - The Mail Owl Scrolls
Sign in
|
Join
|
Help
The Mail Owl Scrolls
The most misguided blog since April 2004
This Blog
Home
Contact
Syndication
RSS
Atom
Comments RSS
Search
Go
Tags
Blatant Promotions
Books
Development and Technology
Events
Game Development
The Mailowl Scrolls Social Feed
Undefined Potential
Navigation
Home
Careers
Downloads
Support
Archives
March 2006 (2)
February 2006 (10)
January 2006 (9)
December 2005 (7)
November 2005 (10)
October 2005 (8)
September 2005 (2)
August 2005 (14)
July 2005 (6)
June 2005 (9)
May 2005 (11)
April 2005 (25)
March 2005 (12)
February 2005 (9)
January 2005 (6)
December 2004 (2)
November 2004 (3)
October 2004 (3)
September 2004 (7)
August 2004 (6)
July 2004 (6)
June 2004 (2)
May 2004 (6)
April 2004 (1)
Blogs
Dotnet.org.za
Jo'blog
Gabbahead - self-serving Populism
splattermail
The Future Bank Blog
Links
SA Developer
Gamasutra
NAG Game.Dev
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
Share this post:
email it!
|
bookmark it!
|
digg it!
|
reddit!
|
kick it!
|
live it!
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
Title
(required)
Name
(required)
Your URL
(optional
)
Comments
(required)
Remember Me?
Enter the numbers above: