- Main Page
- Packages
- Classes
- Files

- File List
- File Members

# D:/guillaume/guillara/3D/DXGfx/Backup/DXGfxLib/TexturedPlane.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 System.IO;
    00021 using System.Drawing;
    00022 using Microsoft.DirectX;
    00023 using Microsoft.DirectX.Direct3D;
    00024 using Microsoft.Samples.DirectX.UtilityToolkit;
    00025
    00026 namespace DXGfxLib
    00027 {
    00031     public class TexturedPlane : MeshObject
    00032     {
    00033         //public Mesh texturedPlaneMesh;
    00034
    00038         public Texture texture;
    00042         public Material material;
    00047         public int width;
    00052         public int heigth;
    00056         public float yPos;
    00060         public int numVertX;
    00064         public int numVertZ;
    00065
    00066         public string textureFileName;
    00067
    00071         public TexturedPlane()
    00072         {
    00073         }
    00074
    00075         public TexturedPlane(int width, int heigth, int numVertX, int numVertZ, string texturefile)
    00076         {
    00077             if (DXGfxManager.GetGlobalInstance().d3dDevice == null)
    00078             {
    00079                 throw new Exception("DXGfxManager.d3dDevice is null, creation of a TexturedPlane is impossible without a valid device!");
    00080             }
    00081             Init(DXGfxManager.GetGlobalInstance().d3dDevice, width, heigth, numVertX, numVertZ, texturefile);
    00082         }
    00083
    00092         public TexturedPlane(Device d3ddevice, int width, int heigth, int numVertX, int numVertZ, string texturefile)
    00093         {
    00094             Init(d3ddevice, width, heigth, numVertX, numVertZ, texturefile);
    00095         }
    00096
    00097         public virtual void Init(Device d3ddevice, int width, int heigth, int numVertX, int numVertZ, string texturefile)
    00098         {
    00099             this.width = width;
    00100             this.heigth = heigth;
    00101             this.numVertX = numVertX;
    00102             this.numVertZ = numVertZ;
    00103             this.yPos = 0.0f;
    00104
    00105             // First load our texture.
    00106             if (File.Exists(texturefile))
    00107             {
    00108                 texture = ResourceCache.GetGlobalInstance().CreateTextureFromFile(d3ddevice, texturefile);
    00109                 textureFileName = texturefile;
    00110             }
    00111             else
    00112             {
    00113                 throw new ArgumentException("Texture file:" +texturefile+  "does not exist!");
    00114             }
    00115
    00116             // Default material
    00117             material = new Material();
    00118             material.Ambient = Color.Aqua;
    00119             material.Diffuse = Color.Azure;
    00120             material.Emissive = Color.Blue;
    00121             material.Specular = Color.Brown;
    00122
    00123             CustomVertex.PositionTextured[] v = new CustomVertex.PositionTextured[numVertX * numVertZ];
    00124
    00125             float dX = width / (numVertX - 1);
    00126             float dZ = heigth / (numVertZ - 1);
    00127             float x0 = -width / 2;
    00128             float z0 = -heigth / 2;
    00129             float dU = 1.0f / width;
    00130             float dV = 0.7f / heigth;
    00131             float x = x0;
    00132             float z = z0;
    00133             float tuTmp = 0;
    00134             float tvTmp = heigth;
    00135
    00136             //if (localBoundingBox == null)
    00137             //{
    00138             //    localBoundingBox = new BBox();
    00139             //}
    00140             //localBoundingBox.vMin = new Vector3(x0, -0.05f, z0);
    00141             //localBoundingBox.vMax = new Vector3(x0 + width, 0.05f, z0 + heigth);
    00142
    00143             for (int j = 0; j <= numVertX - 1; j++)
    00144             {
    00145                 for (int i = 0; i <= numVertZ - 1; i++)
    00146                 {
    00147                     v[i + j * (numVertX)].X = x;
    00148                     v[i + j * (numVertX)].Z = z;
    00149                     v[i + j * (numVertX)].Y = 0.0f;
    00150
    00151
    00152                     // set the texture coordinates
    00153                     v[i + j * (numVertX)].Tu = ((float)tuTmp / (float)width);
    00154                     v[i + j * (numVertX)].Tv = ((float)tvTmp / (float)heigth);
    00155
    00156
    00157                     //                                  v[i+j*(numVertX)].Nx = 0.0f;
    00158                     //                                  v[i+j*(numVertX)].Ny = 1.0f;
    00159                     //                                  v[i+j*(numVertX)].Nz = 0.0f;
    00160
    00161                     x += dX;
    00162                     tuTmp += dX;
    00163                 }
    00164
    00165                 z += dZ;
    00166                 tvTmp -= dZ;
    00167                 x = x0;
    00168                 tuTmp = 0;
    00169             }
    00170
    00171             originalMesh = new Mesh((numVertX - 1) * (numVertZ - 1) * 2, numVertX * numVertZ, MeshFlags.Managed,
    00172                 CustomVertex.PositionTextured.Format, d3ddevice);
    00173
    00174             GraphicsStream buffStream = originalMesh.LockVertexBuffer(LockFlags.NoSystemLock);
    00175
    00176             // Now let's copy our vertices to it.
    00177             for (int h = 0; h < v.Length; h++)
    00178             {
    00179                 buffStream.Write(v[h].X);
    00180                 buffStream.Write(v[h].Y);
    00181                 buffStream.Write(v[h].Z);
    00182                 //                              buffStream.Write(v[h].Nx);
    00183                 //                              buffStream.Write(v[h].Ny);
    00184                 //                              buffStream.Write(v[h].Nz);
    00185                 buffStream.Write(v[h].Tu);
    00186                 buffStream.Write(v[h].Tv);
    00187             }
    00188             originalMesh.UnlockVertexBuffer();
    00189
    00190
    00191             // Now let's build the indices.
    00192             int[] indices = new int[(numVertX - 1) * (numVertZ - 1) * 6];
    00193
    00194             for (int b = 0; b < numVertZ - 1; b++)
    00195             {
    00196                 for (int a = 0; a < numVertX - 1; a++)
    00197                 {
    00198                     indices[((a + b * (numVertX - 1)) * 6)] = a + b * numVertX;
    00199                     indices[((a + b * (numVertX - 1)) * 6) + 1] = (a + 1) + b * numVertX;
    00200                     indices[((a + b * (numVertX - 1)) * 6) + 2] = (a + 1) + (b + 1) * numVertX;
    00201
    00202                     indices[((a + b * (numVertX - 1)) * 6) + 3] = a + b * numVertX;
    00203                     indices[((a + b * (numVertX - 1)) * 6) + 4] = (a + 1) + (b + 1) * numVertX;
    00204                     indices[((a + b * (numVertX - 1)) * 6) + 5] = a + (b + 1) * numVertX;
    00205                 }
    00206             }
    00207
    00208             GraphicsStream iBuffStream = originalMesh.LockIndexBuffer(LockFlags.NoSystemLock);
    00209
    00210             for (int t = 0; t < indices.Length; t++)
    00211             {
    00212                 iBuffStream.Write((Int16)indices[t]);
    00213             }
    00214
    00215             originalMesh.UnlockIndexBuffer();
    00216
    00217             localBoundingBox = CalculateLocalBBox(originalMesh);
    00218         }
    00219
    00220         public override void Dispose()
    00221         {
    00222         }
    00223
    00228         public void ResetDevice(Device device)
    00229         {
    00230             throw new NotImplementedException();
    00231         }
    00232
    00239         public override void  Draw(Device device)
    00240         {
    00241             device.Material = material;
    00242             device.SetTexture(0, texture);
    00243
    00244             device.SetTransform(TransformType.World, worldMat);
    00245
    00246
    00247             originalMesh.DrawSubset(0);
    00248         }
    00249     }
    00250 }