00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 using System;
00018 using System.Collections.Generic;
00019 using System.Text;
00020 using Microsoft.DirectX;
00021 using Microsoft.DirectX.Direct3D;
00022 using Microsoft.Samples.DirectX.UtilityToolkit;
00023 using System.Drawing;
00024
00025 namespace DXGfxLib
00026 {
00031 public class Impostor : Batch
00032 {
00033 private Texture texture = null;
00034
00040 public void Attach(Material material, Texture texture)
00041 {
00042 this.texture = texture;
00043
00044 CustomVertex.PositionNormalTextured[] vertices = new CustomVertex.PositionNormalTextured[4];
00045
00046 vertices[0].X = -0.5f;
00047 vertices[0].Y = 0;
00048 vertices[0].Z = 0;
00049
00050 vertices[0].Nx = 0;
00051 vertices[0].Ny = 1.0f;
00052 vertices[0].Nz = 0;
00053
00054 vertices[0].Tu = 0;
00055 vertices[0].Tv = 0;
00056
00057 vertices[1].X = 0.5f;
00058 vertices[1].Y = 0;
00059 vertices[1].Z = 0;
00060
00061 vertices[1].Nx = 0;
00062 vertices[1].Ny = 1.0f;
00063 vertices[1].Nz = 0;
00064
00065 vertices[1].Tu = 1;
00066 vertices[1].Tv = 0;
00067
00068 vertices[2].X = -0.5f;
00069 vertices[2].Y = 1;
00070 vertices[2].Z = 0;
00071
00072 vertices[2].Nx = 0;
00073 vertices[2].Ny = 1.0f;
00074 vertices[2].Nz = 0;
00075
00076 vertices[2].Tu = 0;
00077 vertices[2].Tv = 1;
00078
00079 vertices[3].X = 0.5f;
00080 vertices[3].Y = 1;
00081 vertices[3].Z = 0;
00082
00083 vertices[3].Nx = 0;
00084 vertices[3].Ny = 1.0f;
00085 vertices[3].Nz = 0;
00086
00087 vertices[3].Tu = 1;
00088 vertices[3].Tv = 1;
00089
00090 Int16[] indices = new Int16[] { 0, 1, 2, 2, 1, 3 };
00091
00092 Mesh oneface = new Mesh(2, 4, MeshFlags.IbSystemMem, CustomVertex.PositionNormalTextured.Format, texture.Device);
00093
00094 GraphicsStream vertexStm = oneface.LockVertexBuffer(LockFlags.None);
00095 vertexStm.Write(vertices);
00096 oneface.UnlockVertexBuffer();
00097
00098 GraphicsStream indicesStm = oneface.LockIndexBuffer(LockFlags.None);
00099 indicesStm.Write(indices);
00100 oneface.UnlockIndexBuffer();
00101
00102 Matrix[] matrices = new Matrix[4];
00103
00104 matrices[0] = Matrix.Identity;
00105 matrices[1] = Matrix.RotationY((float)(0.25f * Math.PI));
00106 matrices[2] = Matrix.RotationY((float)(0.5f * Math.PI));
00107 matrices[3] = Matrix.RotationY((float)(0.75f * Math.PI));
00108
00109 Material[] materials = new Material[1];
00110 Texture[] textures = new Texture[1];
00111
00112 materials[0] = new Material();
00113 materials[0].AmbientColor = ColorValue.FromColor(Color.WhiteSmoke);
00114 materials[0].DiffuseColor = ColorValue.FromColor(Color.WhiteSmoke);
00115 materials[0].Specular = Color.White;
00116
00117 textures[0] = texture;
00118
00119 Populate(oneface, materials, textures, matrices);
00120 }
00121
00127 public void Attach(FrameworkMesh fMesh)
00128 {
00129 Material mat = fMesh.GetMaterial(0);
00130
00131 Device d3ddevice = fMesh.SystemMesh.Device;
00132
00133 if (d3ddevice != null)
00134 {
00135 Texture tex = new Texture(d3ddevice, 120, 120, 1, Usage.None, Format.X8R8G8B8, Pool.Managed);
00136
00137 Surface currentTarget = d3ddevice.GetRenderTarget(0);
00138
00139 d3ddevice.SetRenderTarget(0, tex.GetSurfaceLevel(0));
00140 d3ddevice.Clear(ClearFlags.ZBuffer | ClearFlags.Target, Color.Blue, 1.0f, 0);
00141
00142 d3ddevice.BeginScene();
00143
00144 fMesh.Render(fMesh.SystemMesh.Device);
00145
00146 d3ddevice.EndScene();
00147
00148 d3ddevice.SetRenderTarget(0, currentTarget);
00149
00150 Attach(mat, tex);
00151 }
00152 }
00153 }
00154 }