Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » DirectX » Games » Downloads
 
Add your own
alternative version

Game Engine

, 20 Nov 2009 CPL
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 ModelAnim
    {
        public enum ModelActorType
        {
            
            AnimatedX,
            
            TVA

        };


        public struct MeshSceneInfo
        {
            public string Name;
            public string Path;
            public TV_3DVECTOR Pozicija;
            public float rX;
            public float rY;
            public float rZ;
            public ModelActorType Type;
            public string MaterijalFile;
            public string ShaderFile;
            


        };

        public List<MeshSceneInfo> MeshInfo = new List<MeshSceneInfo>();


        public List<TVActor> Meshes = new List<TVActor>();
        TV_3DVECTOR InitPozicija = new TV_3DVECTOR(0, 0, 0);
        public const string ALL = "All";
        public ModelActorType TypeOf;
        private TVTextureFactory TF;
        private int IDDiffuse;
        private int IDMat;
        private TVMaterialFactory Mats;
        MeshSceneInfo Info = new MeshSceneInfo();
        string itPathMesh;

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

                }

            }
            return r;

        }

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

            foreach (TVActor mesh in Meshes)
            {
                if (mesh.GetName () == Name)
                {

                    Ret = mesh.GetPosition();
                    break;
                }

            }

            return Ret;

        }
        public void SetMeshPosition(TV_3DVECTOR Position, string Name)
        {
            foreach (TVActor mesh in Meshes)
            {
                if (mesh.GetName() == Name)
                {

                    mesh.SetPosition(Position.x, Position.y, Position.z);
                    break;
                }

            }

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

                foreach (TVActor Mesh in Meshes)
                {

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

                }
            }
            else
            {
                foreach (TVActor Mesh in Meshes)
                {
                    if (Mesh.GetName() == Name)
                    {

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


                }

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

                foreach (TVActor Mesh in Meshes)
                {

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

                }
            }
            else
            {
                foreach (TVActor Mesh in Meshes)
                {
                    if (Mesh.GetName() == 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 (TVActor m in Meshes)
            {
                if (m.GetName() == Name)
                {

                    Ret = m.GetRotation();
                }

            }
            return Ret;

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

                foreach (TVActor 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 (TVActor Mesh in Meshes)
                {
                    if (Mesh.GetName() == 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 (TVActor Mesh in Meshes)
                {
                    IDMat = Mats.CreateLightMaterial(1, 1, 1, 1, 0.015f, 1, "basic material");

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

                }
            }
            else
            {
                foreach (TVActor Mesh in Meshes)
                {
                    if (Mesh.GetName() == Name)
                    {

                        break;
                    }
                }
            }

        }
        public void UpdateObjects(string Name)
        {


            if (Name == ALL)
            {

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


        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();

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

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



            foreach (TVActor  m in Meshes)
            {



                if (m.GetName() == 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,
            ModelActorType Type, string Name, string PathToModel)
        {
            TVActor Meshin = new TVActor();
            Mats = new TVMaterialFactory();
            

            itPathMesh = PathToModel;
            Info.Path = PathToModel;
            Info.Name = Name;
            if (Type == ModelActorType.TVA )
            {

                Meshin = Scene.CreateActor(Name);
                Meshin.LoadTVA(PathToModel, true, true);

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

                //Meshin.SetMaterial(IDMat, -1);  //Set our material ID on the rancor.
                Meshin.RotateY(90);            //We rotate the actor 180 degrees, so it's facing us.
                Meshin.SetAnimationID(1);       //We want to play animation 1.
                Meshin.SetAnimationLoop(true);
                Meshin.PlayAnimation(1);
                //Meshin.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_BUMPMAPPING_TANGENTSPACE, 0, 1); 
                Meshin.SetMaterial(Globals.GetMat("Matte"));
                Meshin.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_BUMPMAPPING_TANGENTSPACE);
                Meshin.SetShadowCast(true, true);
                Meshin.SetName (Name);
                Meshes.Add(Meshin);

            }

            if (Type == ModelActorType.AnimatedX )
            {

                Meshin = Scene.CreateActor(Name);
                Meshin.LoadXFile(PathToModel, true, true);
                IDMat = Mats.CreateLightMaterial(1, 1, 1, 1, 0.015f, 0, "basic animx material" + Name );
                Meshin.SetMaterial(IDMat, -1);
                Meshin.RotateY(90);            //We rotate the actor 180 degrees, so it's facing us.
                Meshin.SetAnimationID(1);       //We want to play animation 1.
                Meshin.SetAnimationLoop(true);
                Meshin.PlayAnimation(1);
                Meshin.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_BUMPMAPPING_TANGENTSPACE, 0, 1); 
                Meshin.SetName(Name);
                Meshes.Add(Meshin);

            }

            MeshInfo.Add(Info);


            

        }
        public void DrawModels(string Para)
        {

            if (Para == "All")
            {

                foreach (TVActor mesh in Meshes)
                {

                    mesh.Render(true);

                }

            }
            else
            {

                foreach (TVActor mesh in Meshes)
                {
                    if (mesh.GetName() == Para)
                    {
                        mesh.Render(true);
                        break;
                    }

                }

            }

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

            if (Name == ALL)
            {

                foreach (TVActor Mesh in Meshes)
                {

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

                }
            }
            else
            {
                foreach (TVActor Mesh in Meshes)
                {
                    if (Mesh.GetName() == 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 (TVActor Mesh in Meshes)
                {

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

                }
            }
            else
            {
                foreach (TVActor Mesh in Meshes)
                {
                    if (Mesh.GetName() == 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 (TVActor Mesh in Meshes)
                {

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

                        break;
                    }
                }
            }

        }
        public TV_3DVECTOR GetModelScale(string Name)
        {

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

                    break;
                }
            }
            return ret;

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

            foreach (TVActor Mesh in Meshes)
            {
                if (Mesh.GetName() == Name)
                {
                    refint = Mesh.GetIndex();

                    break;
                }
            }
            return refint;


        }
        public void AddTexture(string Name, string TexturePack, ref TVGlobals Globals)
        {
            TVTextureFactory TextureFactory = new TVTextureFactory();
            foreach (TVActor Mesh in Meshes)
            {
                if (Mesh.GetName() == 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_ACTOR ,
                CONST_TV_TESTTYPE.TV_TESTTYPE_BOUNDINGBOX);
            // Check if we did have a collision with a tank
            if (CollisionResult.IsCollision())
            {
                try
                {

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

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

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


                    }
                }
                catch (Exception Exa)
                {

                }


            }


            return Ret;

        }

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


                        mesh.ShowBoundingBox(f);



                    }

                }
                else
                {

                    foreach (TVActor mesh in Meshes)
                    {
                        if (mesh.GetName() == Name)
                        {

                            mesh.ShowBoundingBox(f);

                        }

                    }
                }

            }
            catch (Exception exa)
            {

            }
        }

        public void RemoveModel(string Name)
        {
            int indexMesh = 0;
            foreach (TVActor  m in Meshes)
            {
                indexMesh++;
                if (m.GetName() == Name)
                {
                    m.Destroy();
                    break;

                }

            }

            if (indexMesh > 0)
            {
                Meshes.RemoveAt(indexMesh);
            }


        }


        public void PlayAnimation(string Animation, string Name)
        {
            foreach (TVActor Actor in Meshes)
            {

                if (Actor.GetName() == Name)
                {
                    Actor.SetAnimationID( Actor.GetAnimationIDByName(Animation));

                    break;
                }
            }

        }



        public void ChangeAnimLoop(string Name, bool f)
        {
            foreach (TVActor Actor in Meshes)
            {
                if (Actor.GetName() == Name)
                {
                    Actor.SetAnimationLoop(f);
                    break;
                }
            }
        }

        public string[] GetListAnimation(string Name)
        {
            string[] r = new string [0];



            foreach (TVActor Actor in Meshes)
            {

                if (Actor.GetName() == Name)
                {

                    r = new string[Actor.GetAnimationCount()];

                    int x=0;

                    for (x = 0; x < Actor.GetAnimationCount() - 1; x++)
                    {
                        r[x] = Actor.GetAnimationName(x);
                    }

                    break;
                }
            }

            return r;


        }

    }
}

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)

Share

About the Author

Member 3873633
Software Developer
Croatia Croatia
Programer

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 20 Nov 2009
Article Copyright 2009 by Member 3873633
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid