- Main Page
- Packages
- Classes
- Files

- File List
- File Members

# D:/guillaume/guillara/3D/DXGfx/Backup/DXGfxLib/RawObject.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.IO;
    00024
    00025 namespace DXGfxLib
    00026 {
    00027     public class RawObject : SceneObject
    00028     {
    00032         public Vector3 barycenter = new Vector3();
    00033
    00037         public float bSphereRadius;
    00038
    00042         public AABBox boundingBox;
    00043
    00047         private VertexBuffer vBuff = null;
    00048
    00049         protected VertexBuffer VBuff
    00050         {
    00051             get { return vBuff; }
    00052             set { vBuff = value; }
    00053         }
    00054
    00055
    00059         private IndexBuffer iBuff = null;
    00060
    00061         protected IndexBuffer IBuff
    00062         {
    00063             get { return iBuff; }
    00064             set { iBuff = value; }
    00065         }
    00066
    00070         private VertexFormats vFormat;
    00071
    00072         protected VertexFormats VFormat
    00073         {
    00074             get { return vFormat; }
    00075             set { vFormat = value; }
    00076         }
    00077
    00082         protected Material[] meshMaterials;
    00083
    00087         protected Texture[] meshTextures;
    00088
    00092         protected EffectInstance[] effects;
    00093
    00097         public float objectRadius = 0.0f;
    00098
    00102         public Vector3 objectCenter;
    00103
    00107         public RawObject()
    00108         {
    00109         }
    00110
    00115         public RawObject(string fileName)
    00116         {
    00117             if (DXGfxManager.GetGlobalInstance().d3dDevice == null)
    00118             {
    00119                 throw new Exception("Manager does not have any reference to any valid d3ddevice, this constructor can not be executed");
    00120             }
    00121             LoadFromFile(DXGfxManager.GetGlobalInstance().d3dDevice, fileName);
    00122         }
    00123
    00129         public RawObject(Device d3ddevice, string fileName)
    00130         {
    00131             LoadFromFile(d3ddevice, fileName);
    00132         }
    00133
    00137         public override void GetPrepared()
    00138         {
    00139             base.GetPrepared();
    00140
    00141             Device dxDevice = DXGfxManager.GetGlobalInstance().d3dDevice;
    00142
    00143             string currDir = Directory.GetCurrentDirectory();
    00144
    00145             if (!File.Exists(fileName))
    00146             {
    00147                 throw new Exception("The ressource file does not exist! There is a problem somewhere.... did you delete or moved any file?");
    00148             }
    00149             else
    00150             {
    00151                 LoadFromFile(dxDevice, fileName);
    00152             }
    00153
    00154         }
    00155
    00160         //public virtual void Attach(Mesh mesh)
    00161         //{
    00162         //    originalMesh = mesh;
    00163         //    localBoundingBox = CalculateLocalBBox(mesh);
    00164         //    extents = localBoundingBox.Size();
    00165
    00166         //    // Find the mesh's center, then generate a centering matrix
    00167         //    using (VertexBuffer vb = mesh.VertexBuffer)
    00168         //    {
    00169         //        using (GraphicsStream stm = vb.Lock(0, 0, LockFlags.NoSystemLock))
    00170         //        {
    00171         //            try
    00172         //            {
    00173         //                this.bSphereRadius = Geometry.ComputeBoundingSphere(stm,
    00174         //                    mesh.NumberVertices, mesh.VertexFormat, out this.barycenter);
    00175         //            }
    00176         //            finally
    00177         //            {
    00178         //                vb.Unlock();
    00179         //            }
    00180         //        }
    00181         //    }
    00182         //}
    00183
    00184
    00190         //public override void Draw(Device device)
    00191         //{
    00192         //    device.RenderState.CullMode = Cull.None;
    00193         //    device.SetTransform(TransformType.World, worldMat);
    00194
    00195         //    base.Draw(device);
    00196
    00197         //    for (int i = 0; i < originalMesh.NumberAttributes; i++)
    00198         //    {
    00199         //        device.SetTexture(i, meshTextures[i]);
    00200         //        originalMesh.DrawSubset(i);
    00201         //    }
    00202         //}
    00203
    00211         //public virtual void DrawSubset(int index)
    00212         //{
    00213         //    originalMesh.DrawSubset(index);
    00214         //}
    00215
    00225         //public override bool Intersect(Vector3 rayPos, Vector3 rayDir, out IntersectInformation closestHit)
    00226         //{
    00227         //    // Let's transform rayPos and rayDir to local coordinates
    00228         //    Matrix invWorldMat = Matrix.Invert(worldMat);
    00229         //    Vector3 rayPosLocal = Vector3.TransformCoordinate(rayPos, invWorldMat);
    00230         //    Vector3 rayDirLocal = Vector3.TransformNormal(rayDir, invWorldMat);
    00231         //    rayDirLocal.Normalize();
    00232         //    bool collision = false;
    00233         //    if (originalMesh != null)
    00234         //    {
    00235         //        collision = originalMesh.Intersect(rayPosLocal, rayDirLocal, out closestHit);
    00236         //    }
    00237         //    else
    00238         //    {
    00239         //        closestHit = new IntersectInformation();
    00240         //    }
    00241         //    return collision;
    00242         //}
    00243
    00252         //public override bool Intersect(Vector3 rayOri, Vector3 rayDir, ref List<SceneObject> collidingSceneObjects)
    00253         //{
    00254         //    // First let's quickly check if we have any chance to collide with anything.
    00255         //    bool any = false;
    00256
    00257         //    AABBox aabox = new AABBox(worldAABBox);
    00258         //    bool collision = true;
    00259         //    if (((rayOri.X < aabox.vMin.X) && (rayDir.X <= 0))
    00260         //        || ((rayOri.X > aabox.vMax.X) && (rayDir.X >= 0)))
    00261         //        collision = false;
    00262         //    if (((rayOri.Y < aabox.vMin.Y) && (rayDir.Y <= 0))
    00263         //        || ((rayOri.Y > aabox.vMax.Y) && (rayDir.Y >= 0)))
    00264         //        collision = false;
    00265         //    if (((rayOri.Z < aabox.vMin.Z) && (rayDir.Z <= 0))
    00266         //        || ((rayOri.Z > aabox.vMax.Z) && (rayDir.Z >= 0)))
    00267         //        collision = false;
    00268
    00269         //    if (collision)
    00270         //    {
    00271         //        foreach (SceneNode child in children)
    00272         //        {
    00273         //            if (child.Intersect(rayOri, rayDir, ref collidingSceneObjects))
    00274         //                any = true;
    00275         //        }
    00276
    00277         //        Matrix invWorldMat = Matrix.Invert(worldMat);
    00278         //        Vector3 rayPosLocal = Vector3.TransformCoordinate(rayOri, invWorldMat);
    00279         //        Vector3 rayDirLocal = Vector3.TransformNormal(rayDir, invWorldMat);
    00280         //        rayDirLocal.Normalize();
    00281
    00282         //        if (originalMesh != null)
    00283         //        {
    00284         //            if (originalMesh.Intersect(rayPosLocal, rayDirLocal))
    00285         //            {
    00286         //                collidingSceneObjects.Add(this);
    00287         //                any = true;
    00288         //            }
    00289         //        }
    00290         //        else
    00291         //        {
    00292         //            collidingSceneObjects.Add(this);
    00293         //            any = true;
    00294         //        }
    00295         //    }
    00296
    00297         //    return any;
    00298         //}
    00299
    00309         //public override bool Intersect(Vector3 rayOri, Vector3 rayDir, ref List<SceneObject> collidingSceneObjects, System.Type desiredType)
    00310         //{
    00311         //    bool any = false;
    00312
    00313         //    AABBox aabox = worldAABBox;
    00314         //    bool collision = true;
    00315         //    if (((rayOri.X < aabox.vMin.X) && (rayDir.X <= 0))
    00316         //        || ((rayOri.X > aabox.vMax.X) && (rayDir.X >= 0)))
    00317         //        collision = false;
    00318         //    if (((rayOri.Y < aabox.vMin.Y) && (rayDir.Y <= 0))
    00319         //        || ((rayOri.Y > aabox.vMax.Y) && (rayDir.Y >= 0)))
    00320         //        collision = false;
    00321         //    if (((rayOri.Z < aabox.vMin.Z) && (rayDir.Z <= 0))
    00322         //        || ((rayOri.Z > aabox.vMax.Z) && (rayDir.Z >= 0)))
    00323         //        collision = false;
    00324
    00325         //    if (collision)
    00326         //    {
    00327         //        foreach (SceneNode child in children)
    00328         //        {
    00329         //            if (child.Intersect(rayOri, rayDir, ref collidingSceneObjects))
    00330         //                any = true;
    00331         //        }
    00332
    00333         //        Matrix invWorldMat = Matrix.Invert(worldMat);
    00334         //        Vector3 rayPosLocal = Vector3.TransformCoordinate(rayOri, invWorldMat);
    00335         //        Vector3 rayDirLocal = Vector3.TransformNormal(rayDir, invWorldMat);
    00336         //        rayDirLocal.Normalize();
    00337
    00338         //        if (originalMesh != null)
    00339         //        {
    00340         //            if ((originalMesh.Intersect(rayPosLocal, rayDirLocal)) && (desiredType.IsInstanceOfType(this)))
    00341         //            {
    00342         //                collidingSceneObjects.Add(this);
    00343         //                any = true;
    00344         //            }
    00345         //        }
    00346         //        else
    00347         //        {
    00348         //            collidingSceneObjects.Add(this);
    00349         //            any = true;
    00350         //        }
    00351         //    }
    00352         //    return any;
    00353         //}
    00354     }
    00355 }