- Main Page
- Packages
- Classes
- Files

- File List
- File Members

# D:/guillaume/guillara/3D/DXGfx/DXGfxLib/Impostor.cs

Go to the documentation of this file.

    00001 //BSD License
    00002 //DXGfx® - http://www.eteractions.com
    00003 //Copyright (c) 2005
    00004 //by Guillaume Randon
    00005 //Permission is hereby granted, free of charge, to any person obtaining a copy of this software
    00006 //and associated documentation files (the "Software"), to deal in the Software without restriction,
    00007 //including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
    00008 //and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
    00009 //subject to the following conditions:
    00010 //The above copyright notice and this permission notice shall be included in all copies or substantial
    00011 //portions of the Software.
    00012 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
    00013 //INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
    00014 //AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    00015 //DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00016 //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    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 }