Click here to Skip to main content
15,896,063 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 AGE_Engine3D.Math;
using AGE_Engine3D.Text;
using AGE_Engine3D.RenderObjects;
using Tao.OpenGl;

namespace AGE_Engine3D.HUD
{
    public class AGE_ScreenText : IRenderOpenGL 
    {

        #region Fields
        bool _Isvisible = true;
        AGE_Matrix_44 _WorldMatrix = AGE_Matrix_44.Identity();
        AGE_Matrix_44 ViewMatrix;
        float CurrentScreenWidth;
        float CurrentScreenHieght;
        float FontSize = 20;
        AGE_Color4f DefaultColor = new AGE_Color4f(1,0,0,1);
        FontAlign DefaultAlin = FontAlign.LEFT;
        List<AGE_ScreenTextItem> TextQueue = new List<AGE_ScreenTextItem>();
        public bool IsVisable { get { return _Isvisible; } }
        IFont FontContainer = null;
        #endregion

        #region IRenderOpenGL Members

        public void RenderOpenGL()
        {
            if (this._Isvisible && this.TextQueue.Count > 0)
            {
                this.CreatMatrixTransform();

                #region Set the Model View Matrix
                Gl.glMatrixMode(Gl.GL_PROJECTION);
                Gl.glPushMatrix();
                Gl.glLoadMatrixf(this.ViewMatrix.GetListValues());
                Gl.glMatrixMode(Gl.GL_MODELVIEW);
                Gl.glLoadIdentity();
                #endregion

                this.FontContainer = ResourceManager.GetFont("Verdana");
                AGE_Matrix_44 Location;
                foreach (AGE_ScreenTextItem TextItem in this.TextQueue)
                {
                    float FontHieght = this.FontContainer.CharHeight * TextItem.Size;
                    AGE_Vector3Df Loc = TextItem.Location;
                    Loc.Y += FontHieght;
                    Loc.Z = 1f;
                    AGE_Vector3Df Scale = new AGE_Vector3Df(FontHieght, -FontHieght, 1);

                    this.FontContainer.Alignment = TextItem.Align;
                    Gl.glColor4fv(TextItem.Color.ToArray4f());
                    foreach (string Tmptext in TextItem.TextList)
                    {
                        Location = AGE_Matrix_44.HWorld(ref Loc, ref Scale);
                        Gl.glLoadMatrixf(Location.GetListValues());

                        this.FontContainer.glPrint(Tmptext);

                        Loc.Y += FontHieght;
                    }

                }
                Gl.glMatrixMode(Gl.GL_PROJECTION);
                Gl.glPopMatrix();
                this.TextQueue.Clear();
            }
        }

        public void AddText(List<string> NewText, AGE_Vector3Df Location)
        {
            if (NewText.Count > 0)
            {
                AGE_ScreenTextItem tmp = new AGE_ScreenTextItem();
                tmp.TextList.AddRange(NewText);
                tmp.Location = Location;
                tmp.Color = this.DefaultColor;
                tmp.Align = this.DefaultAlin;
                tmp.Size = this.FontSize;
                this.TextQueue.Add(tmp);
            }
        }

        #endregion

        #region Methods

        public void CreatMatrixTransform()
        {
            if (this.CurrentScreenHieght != SceneGraph.ScreenHeight ||
                this.CurrentScreenWidth != SceneGraph.ScreenWidth)
            {
                this.CurrentScreenHieght = SceneGraph.ScreenHeight;
                this.CurrentScreenWidth = SceneGraph.ScreenWidth;
                AGE_Vector3Df Eye = new AGE_Vector3Df(0, 0, 1);
                AGE_Vector3Df Target = new AGE_Vector3Df(0, 0, -1);
                AGE_Vector3Df Up = new AGE_Vector3Df(0, 1, 0);
                AGE_Matrix_44 temp = AGE_Matrix_44.HView(ref Eye, ref Target, ref Up);
                this.ViewMatrix = AGE_Matrix_44.HOrtho(0, this.CurrentScreenWidth, 0, this.CurrentScreenHieght, 0, 1) * temp;
            }
        } 

        #endregion
    }

    class AGE_ScreenTextItem
    {
        public List<string> TextList = new List<string>();
        public AGE_Vector3Df Location;
        public FontAlign Align = FontAlign.LEFT;
        public AGE_Color4f Color;
        public float Size;

    }
}

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