Click here to Skip to main content
15,887,135 members
Articles / Multimedia / DirectX

Game Engine

Rate me:
Please Sign up or sign in to vote.
3.78/5 (6 votes)
20 Nov 2009CPL2 min read 47.4K   1.5K   34  
A game engine first prototype
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using MTV3D65;


namespace gefx
{
    public  class Model
    {
        public enum ModelType
        {
            StaticX,
           
            TVM
           

        };
        public List<TVMesh> Meshes = new List<TVMesh>();
        TV_3DVECTOR InitPozicija = new TV_3DVECTOR(0, 0, 0);
        public const string ALL = "All";
        string itPathMesh;
        public List<gefx.ModelAnim.MeshSceneInfo> MeshInfo = new List<gefx.ModelAnim.MeshSceneInfo>();

        public string GetMeshPath(string MeshName)
        {
            string r = "";
            foreach (gefx.ModelAnim.MeshSceneInfo m in MeshInfo)
            {
                if (m.Name == MeshName)
                {
                    r = m.Path;

                }

            }
            return r;

        }

        public struct Dimensions
        {

            public float X;
            public float Y;
            public float Z;

            public float radius;
            public int NumSlices;
            public int NumStacks;

        };
        public ModelType TypeOf;
        private TVTextureFactory TF;
        private int IDDiffuse;
        private int IDMat;
        private TVMaterialFactory Mats;
        public TV_3DVECTOR GetMeshPosition(string Name)
        {
            TV_3DVECTOR Ret = new TV_3DVECTOR(0, 0, 0);

            foreach (TVMesh mesh in Meshes)
            {
                if (mesh.GetMeshName() == Name)
                {

                    Ret = mesh.GetPosition();
                    break;
                }

            }

            return Ret;

        }
        public void SetMeshPosition(TV_3DVECTOR Position, string Name)
        {
            foreach (TVMesh mesh in Meshes)
            {
                try
                {
                    if (mesh.GetMeshName() == Name)
                    {

                        mesh.SetPosition(Position.x, Position.y, Position.z);
                        break;
                    }
                }
                catch (Exception Exa)
                {


                }
            }

        }
        public void SetMeshRotation(TV_3DVECTOR Rotation, string Name)
        {
            if (Name == ALL)
            {

                foreach (TVMesh Mesh in Meshes)
                {

                    Mesh.SetRotation(Rotation.x, Rotation.y, Rotation.z);

                }
            }
            else
            {
                foreach (TVMesh Mesh in Meshes)
                {
                    if (Mesh.GetMeshName() == Name)
                    {

                        Mesh.SetRotation(Rotation.x, Rotation.y, Rotation.z);
                        break;
                    }


                }

            }
        }
        public void SetMeshRotation(TV_3DVECTOR Rotation, float fbyTime, string Name)
        {
            if (Name == ALL)
            {

                foreach (TVMesh Mesh in Meshes)
                {

                    Mesh.SetRotation(Rotation.x * fbyTime, Rotation.y * fbyTime, Rotation.z * fbyTime);

                }
            }
            else
            {
                foreach (TVMesh Mesh in Meshes)
                {
                    if (Mesh.GetMeshName() == Name)
                    {

                        Mesh.SetRotation(Rotation.x * fbyTime, Rotation.y * fbyTime, Rotation.z * fbyTime);
                        break;
                    }


                }

            }

        }
        public TV_3DVECTOR GetMeshRotation(string Name)
        {
            TV_3DVECTOR Ret = new TV_3DVECTOR(0, 0, 0);

            foreach (TVMesh m in Meshes)
            {
                if (m.GetMeshName() == Name)
                {

                    Ret = m.GetRotation();
                }

            }
            return Ret;

        }
        public void AddTexture(string Name, string TexturePath, string TextureName)
        {
            TF = new TVTextureFactory();
            if (Name == ALL)
            {

                foreach (TVMesh Mesh in Meshes)
                {
                    IDDiffuse = TF.LoadTexture(TexturePath, TextureName, -1, -1, CONST_TV_COLORKEY.TV_COLORKEY_NO, true);
                    Mesh.SetTexture(IDDiffuse);     //Apply the texture to the cube
                }
            }
            else
            {
                foreach (TVMesh Mesh in Meshes)
                {
                    if (Mesh.GetMeshName() == Name)
                    {
                        IDDiffuse = TF.LoadTexture(TexturePath, TextureName, -1, -1, CONST_TV_COLORKEY.TV_COLORKEY_NO, true);
                        Mesh.SetTexture(IDDiffuse);     //Apply the texture to the cube
                        break;
                    }
                }
            }

        }
        public void AddMaterial(string Name)
        {
            Mats = new TVMaterialFactory();

            if (Name == ALL)
            {

                foreach (TVMesh Mesh in Meshes)
                {
                    IDMat = Mats.CreateLightMaterial(1, 1, 1, 1, 0.015f, 1, "basic material" + Mesh.GetMeshName());

                    Mesh.SetMaterial(IDMat, -1);
                    Mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_NORMAL,
                        0, 1);

                }
            }
            else
            {
                foreach (TVMesh Mesh in Meshes)
                {
                    if (Mesh.GetMeshName() == Name)
                    {
                        IDMat = Mats.CreateLightMaterial(1, 1, 1, 1, 0.015f, 1, "basic material" + Mesh.GetMeshName());

                        Mesh.SetMaterial(IDMat, -1);
                        Mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_NORMAL,
                            0, 1);

                        break;
                    }
                }
            }

        }
        public void UpdateObjects(string Name)
        {


            if (Name == ALL)
            {

                foreach (TVMesh Mesh in Meshes)
                {
                    Mesh.Update();
                }
            }
            else
            {
                foreach (TVMesh Mesh in Meshes)
                {
                    if (Mesh.GetMeshName() == Name)
                    {
                        Mesh.Update();
                        break;
                    }
                }
            }
        }

        private void ReadMatFile(string Fname)
        {
           
        }

        public void AddMaterialToModel(string FName,string ModelName,ref TVMaterialFactory mat, ref TVGlobals glob)
        {
            string[] para = new string[19];
            string line;
            int counter = 0;
            TextReader tr = new StreamReader(FName);
            while ((line = tr.ReadLine()) != null)
            {
                para[counter] = line;
                counter++;
            }
            string[] D = para[0].Split(',');
            string[] A = para[1].Split(',');
            string[] S = para[2].Split(',');
            string[] P = para[3].Split(',');
            string[] E = para[4].Split(',');
            //"D",0,0,255
            //"A",255,0,0
            //"S",255,255,0
            //"P",0,0,0
            //"E",0,255,0
            tr.Close();

            int i = 0;


            gefx.ModelAnim.MeshSceneInfo Info = new ModelAnim.MeshSceneInfo();

            Info.MaterijalFile = FName;
            Info.Name = ModelName;
            MeshInfo.Add(Info);

            

            foreach (TVMesh m in Meshes)
            {



                if (m.GetMeshName() == ModelName)
                {
                    string Name = System.IO.Path.GetFileName(FName);
                    mat.CreateMaterial("Mat" + Name );
                    mat.SetAmbient(glob.GetMat("Mat" + Name), 
                        (float )System.Convert.ToSingle(A[1]),
                        (float )System.Convert.ToSingle(A[2]),
                        (float )System.Convert.ToSingle(A[3]),
                        1);
                    mat.SetDiffuse(glob.GetMat("Mat" + Name), 
                        (float)System.Convert.ToSingle(D[1]),
                        (float)System.Convert.ToSingle(D[2]),
                        (float)System.Convert.ToSingle(D[3]), 
                        1);
                    mat.SetSpecular(glob.GetMat("Mat" + Name), 
                        (float)System.Convert.ToSingle(S[1]),
                        (float)System.Convert.ToSingle(S[2]),
                        (float)System.Convert.ToSingle(S[3]), 
                        1);
                    mat.SetEmissive(glob.GetMat("Mat" + Name), 
                        (float)System.Convert.ToSingle(E[1]),
                        (float)System.Convert.ToSingle(E[2]),
                        (float)System.Convert.ToSingle(E[3]),
                        1);
                    mat.SetPower(glob.GetMat("Mat" + Name),(float)System.Convert.ToSingle(P[1]));

                    m.SetMaterial(glob.GetMat("Mat" + Name));


                }

            }


        }

        public void AddModel(ref TVScene Scene, ref TVGlobals Globals,
            ModelType Type, string Name, string PathToModel)
        {
            TVMesh Meshin = new TVMesh();
            Mats = new TVMaterialFactory();
            itPathMesh = PathToModel;
            gefx.ModelAnim.MeshSceneInfo Info = new ModelAnim.MeshSceneInfo() ;

            Info.Path = PathToModel;
            Info.Name = Name;
            MeshInfo.Add(Info);
            if (Type == ModelType.TVM )
            {

                Meshin = Scene.CreateMeshBuilder(Name);
                Meshin.LoadTVM(PathToModel, true, true);

                //IDMat = Mats.CreateLightMaterial(1, 1, 1, 1, 0.015f, 1, "basic material" + Name );
                

                //Meshin.SetMaterial(IDMat, -1);
                //Meshin.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_NORMAL,
                //    0, 1);

                

                Meshin.SetMaterial(Globals.GetMat("Matte"));
                Meshin.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_BUMPMAPPING_TANGENTSPACE);
                Meshin.SetShadowCast(true, true);
                Meshin.SetMeshName(Name);
                Meshes.Add(Meshin);

            }


            

            if (Type == ModelType.StaticX )
            {

                Meshin = Scene.CreateMeshBuilder(Name);
                Meshin.LoadXFile(PathToModel, true, true);

                Meshin.SetMaterial(Globals.GetMat("Matte"));
                Meshin.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_BUMPMAPPING_TANGENTSPACE);
                Meshin.SetShadowCast(true, true);
                Meshin.SetMeshName(Name);
                Meshes.Add(Meshin);

            }
            
            

        }
        public void DrawModels(string Para)
        {

            if (Para == "All")
            {

                foreach (TVMesh mesh in Meshes)
                {

                    mesh.Render();

                }

            }
            else
            {

                foreach (TVMesh mesh in Meshes)
                {
                    if (mesh.GetMeshName() == Para)
                    {
                        mesh.Render();
                        break;
                    }

                }

            }

        }

        public TVMesh   GetRefMesh(string Name)  
        {
            TVMesh mm= null ;
            foreach (TVMesh m in Meshes)
            {
                if (m.GetMeshName() == Name)
                {
                    mm= m;
                    break;
                }

            }

            return mm;


        }

        public void RotateModel(float x, float y, float z, string Name)
        {

            if (Name == ALL)
            {

                foreach (TVMesh Mesh in Meshes)
                {

                    Mesh.RotateX(x);
                    Mesh.RotateY(y);
                    Mesh.RotateZ(z);

                }
            }
            else
            {
                foreach (TVMesh Mesh in Meshes)
                {
                    if (Mesh.GetMeshName() == Name)
                    {

                        Mesh.RotateX(x);
                        Mesh.RotateY(y);
                        Mesh.RotateZ(z);
                        break;
                    }


                }

            }
        }
        public void RotateModel(float x, float y, float z, float time, string Name)
        {

            if (Name == ALL)
            {

                foreach (TVMesh Mesh in Meshes)
                {

                    Mesh.RotateX(x * time);
                    Mesh.RotateY(y * time);
                    Mesh.RotateZ(z * time);

                }
            }
            else
            {
                foreach (TVMesh Mesh in Meshes)
                {
                    if (Mesh.GetMeshName() == Name)
                    {

                        Mesh.RotateX(x * time);
                        Mesh.RotateY(y * time);
                        Mesh.RotateZ(z * time);
                        break;
                    }


                }

            }
        }
        public void SetModelScale(float x,float y, float z,string  Name)
        {
            if (Name == ALL)
            {

                foreach (TVMesh Mesh in Meshes)
                {

                    Mesh.SetScale(x, y, z);
                }
            }
            else
            {
                foreach (TVMesh Mesh in Meshes)
                {
                    if (Mesh.GetMeshName() == Name)
                    {
                        Mesh.SetScale(x, y, z);

                        break;
                    }
                }
            }

        }
        public TV_3DVECTOR GetModelScale(string Name)
        {

            TV_3DVECTOR ret= new TV_3DVECTOR(0,0,0);
            foreach (TVMesh Mesh in Meshes)
            {
                if (Mesh.GetMeshName() == Name)
                {
                    ret = Mesh.GetScale();

                    break;
                }
            }
            return ret;

        }
        public int GetModelID(string Name)
        {
            int refint=0;

            foreach (TVMesh Mesh in Meshes)
            {
                if (Mesh.GetMeshName() == Name)
                {
                    refint = Mesh.GetIndex();

                    break;
                }
            }
            return refint;


        }
        public void AddTexture(string Name, string TexturePack,ref TVGlobals Globals)
        {
            TVTextureFactory TextureFactory = new TVTextureFactory();
            foreach (TVMesh Mesh in Meshes)
            {
                if (Mesh.GetMeshName() == Name)
                {
                    TextureFactory.LoadTexture(TexturePack, "Texture" + Name, -1, -1, CONST_TV_COLORKEY.TV_COLORKEY_NO, true);
                    Mesh.SetTexture(Globals.GetTex("Texture" + Name), -1);



                    break;
                }
            }

        }

        public string CheckMousePick(int Mx, int My,ref TVScene Scene)
        {
            TVCollisionResult CollisionResult;
            string Ret = "";

            CollisionResult = Scene.MousePick(Mx, My,
                (int)CONST_TV_OBJECT_TYPE.TV_OBJECT_MESH,
                CONST_TV_TESTTYPE.TV_TESTTYPE_BOUNDINGBOX);
            // Check if we did have a collision with a tank
            if (CollisionResult.IsCollision())
            {
                

                    foreach (TVMesh Mesh in Meshes)
                    {
                        TVMesh colMesh;
                        TV_3DVECTOR Tankpos;
                        TV_3DVECTOR ColMeshpos;

                        colMesh = CollisionResult.GetCollisionMesh();
                        Tankpos = Mesh.GetPosition();
                        ColMeshpos = colMesh.GetPosition();

                        if ((Tankpos.x == ColMeshpos.x) && (Tankpos.z == ColMeshpos.z))
                        {
                            Ret = Mesh.GetMeshName();
                            break;
                        }


                    }

               


                }


            return Ret;

        }

        public void ShowBoundingBox(string Name,bool f)
        {
            if (Name == "All")
            {
                foreach (TVMesh mesh in Meshes)
                {

                    if (mesh != null)
                    {
                        mesh.ShowBoundingBox(f);
                    }
                    

                }

            }
            else
            {

                foreach (TVMesh mesh in Meshes)
                {
                    try
                    {

                        if (mesh.GetMeshName() == Name)
                        {

                            mesh.ShowBoundingBox(f);

                        }
                    }
                    catch (Exception exa)
                    {


                    }

                }
            }
            
        }

        //gefxem.ExplodeMesh EksplozijaModela = new ExplodeMesh();
        //public void ExpoldeModel(string Name, ref TVScene Scene, ref TVGlobals Tvg)
        //{
        //    foreach (TVMesh m in Meshes)
        //    {

        //        if (m.GetMeshName() == Name)
        //        {
        //            EksplozijaModela.Mesh = m;

        //            EksplozijaModela.Scene = Scene;
        //            EksplozijaModela.power = "10";
        //            EksplozijaModela.Tvg = Tvg;

        //            EksplozijaModela.InitObject(m.GetIndex());

        //        }

        //    }

        //}

        public void RemoveModel(string Name,ref TVScene Scene)
        {
            int indexMesh = 0;

            try
            {

                foreach (TVMesh m in Meshes)
                {
                    indexMesh++;
                    if (m.GetMeshName() == Name)
                    {
                        //m.Destroy();
                        Scene.DestroyMesh(m);
                        break;

                    }

                }

                if (indexMesh > 0)
                {
                    TVScene s;
                    
                    Meshes.RemoveAt(indexMesh);
                }
            }
            catch (Exception Exa)
            {

            }
            

        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


Written By
Software Developer
Croatia Croatia
Programer

Comments and Discussions