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 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
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
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
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
00137
00138
00139
00140
00141
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
00153 v[i + j * (numVertX)].Tu = ((float)tuTmp / (float)width);
00154 v[i + j * (numVertX)].Tv = ((float)tvTmp / (float)heigth);
00155
00156
00157
00158
00159
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
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
00183
00184
00185 buffStream.Write(v[h].Tu);
00186 buffStream.Write(v[h].Tv);
00187 }
00188 originalMesh.UnlockVertexBuffer();
00189
00190
00191
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 }