Click here to Skip to main content
15,891,828 members
Articles / Programming Languages / C#

Space and Matrix Transformations - Building a 3D Engine

Rate me:
Please Sign up or sign in to vote.
4.84/5 (13 votes)
4 Sep 2009CPOL8 min read 94.9K   6.1K   101  
This Article Describes How to Navigate 3D Space
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using AGE_Engine3D.RenderObjects;
using AGE_Engine3D.Text;
using Tao.Platform.Windows;

namespace AGE_Engine3D
{
    public static class ResourceManager
    {
        #region Fields
        public static bool IsInitialized = false;
        public static IntPtr DeviseContext = IntPtr.Zero;
        public static List<string> ResourcePath = new List<string>();
        public static List<IMeshResource> MeshResources = new List<IMeshResource>();
        public static List<ITextureResource> TextureResources = new List<ITextureResource>();
        public static List<ISoundResource> SoundResources = new List<ISoundResource>();
        public static List<IFont> FontResources = new List<IFont>();

        
        #endregion

        #region Methods
        public static void Initialize(IntPtr CurrentDC)
        {
            ResourceManager.DeviseContext = CurrentDC;
            ResourceManager.ResourcePath.Clear();
            ResourceManager.CleanExistingResouses();
            GC.Collect();
            ResourcePath.Add(System.Windows.Forms.Application.StartupPath + "\\Assets\\");
            ResourcePath.Add(ResourcePath[0] + "Meshes\\");
            ResourcePath.Add(ResourcePath[0] + "Textures\\");
            ResourcePath.Add(ResourcePath[0] + "Sounds\\");
            ResourcePath.Add(ResourcePath[0] + "Compiled\\");
            DirectoryInfo tempInfo = Directory.GetParent(System.Windows.Forms.Application.StartupPath).Parent;
            ResourcePath.Add(tempInfo.FullName + "\\Assets\\");
            ResourcePath.Add(ResourcePath[5] + "Meshes\\");
            ResourcePath.Add(ResourcePath[5] + "Textures\\");
            ResourcePath.Add(ResourcePath[5] + "Sounds\\");
            ResourcePath.Add(ResourcePath[5] + "Compiled\\");
            ResourceManager.IsInitialized = true;
            ResourceManager.LoadFontOutLine("Verdana", 12, 0, 0, 0, Gdi.FW_BOLD, false, false, false);

        }
        public static void SetAssetPath(string BasePath)
        {

        }
        
        private static void CleanExistingResouses()
        {
            foreach (IResource tmp in ResourceManager.MeshResources)
                tmp.Dispose();
            ResourceManager.MeshResources.Clear();
            foreach (IResource tmp in ResourceManager.TextureResources)
                tmp.Dispose();
            ResourceManager.TextureResources.Clear();
            foreach (IResource tmp in ResourceManager.SoundResources)
                tmp.Dispose();
            ResourceManager.SoundResources.Clear();
            foreach (IFont tmp in ResourceManager.FontResources)
                tmp.Dispose();
            ResourceManager.FontResources.Clear();
        }

        public static bool LoadMeshResouse(string FileName)
        {
            bool result = false;
            string tmpPath = "";
            MeshContainer NewMesh = null;
            foreach (string path in ResourcePath)
            {
                tmpPath = path + FileName;
                if (File.Exists(tmpPath))
                {
                    string FileType = Path.GetExtension(tmpPath);
                    switch(FileType.ToUpper())
                    {
                        case ".OBJ":
                            NewMesh = MeshContainer.LoadObjFile(tmpPath);
                            NewMesh.Initialize();
                            if (NewMesh != null)
                            {
                                ResourceManager.MeshResources.Add(NewMesh);
                                result = true;
                            }
                            break;

                        default:
                            
                            break;
                    }

                    if (result)
                        break;
                }
            }
            
            return result;
        }

        public static string FindPath(string FileName)
        {
            string result = "";
            string tmpPath = "";
            foreach (string path in ResourcePath)
            {
                tmpPath = path + FileName;
                if (File.Exists(tmpPath))
                {
                    result = tmpPath;
                    return result;
                }
            }
            return result;
        }

        public static IMeshResource GetMeshObject(string MeshName)
        {
            IMeshResource result = null;
            string RealName = Path.GetFileNameWithoutExtension(MeshName);
            foreach (IMeshResource tmp in MeshResources)
            {
                if (tmp.ResourceName == RealName)
                {
                    result = tmp;
                    break;
                }
            }
            return result;
        }

        #region Font Methods
        public static bool LoadFontOutLine(string FontName, int FontHeight, int FontWidth, int AngleOfEscapement, int OrientationAngle,
                            int FontWieght, bool IsItalic, bool IsUnderline, bool IsStrikeout)
        {
            bool result = false;
            if (ResourceManager.IsInitialized)
            {
                #region Chech to see if the Font is already loaded
                bool DoesTheFontExist = false;
                foreach (IFont Font in ResourceManager.FontResources)
                {
                    if (Font.FontName == FontName)
                        DoesTheFontExist = true;
                }
                #endregion
                if (!DoesTheFontExist)
                {
                    #region Create Font
                    OutLineTypeFont NewFont = new OutLineTypeFont();
                    if (NewFont.BuildFont(ResourceManager.DeviseContext,
                                          FontName,
                                          FontHeight,
                                          FontWidth,
                                          AngleOfEscapement,
                                          OrientationAngle,
                                          FontWieght,
                                          IsItalic,
                                          IsUnderline,
                                          IsStrikeout))
                    {
                        ResourceManager.FontResources.Add(NewFont);
                        result = true;
                    }
                    else
                    {
                        NewFont = null;

                    }
                    GC.Collect();
                }
                    #endregion

            }
            return result;
        }

        public static IFont GetFont(string FontName)
        {
            IFont result = null;
            foreach (IFont Font in ResourceManager.FontResources)
            {
                if (Font.FontName == FontName)
                {
                    result = Font;
                    break;
                }
            }
            return result;
        }
        #endregion 
        #endregion
    }

    public interface IResource : IDisposable
    {
        string ResourceName { get; }
    }
    public interface IMeshResource : IRenderOpenGL, IResource 
    {

    }
    public interface ITextureResource : IResource
    {

    }
    public interface ISoundResource : IResource
    {
    }

}

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 Code Project Open License (CPOL)


Written By
Engineer Lea+Elliott, Inc.
United States United States
I am a licensed Electrical Engineer at Lea+Elliott, Inc. We specialize in the planning, procurement and implementation of transportation systems, with special emphasis on automated and emerging technologies.

Comments and Discussions