Esempio n. 1
0
        protected override bool InternalLoad()
        {
            if (!Resource.AssemblyResources.Contains(base.ResourceName)) return false;

            using (var stream = Resource.ResourceAssembly.GetManifestResourceStream(base.ResourceName))
            {
                var textReader = new StreamReader(stream);
                var font = BitmapFont.BitmapFontLoader.LoadFontFromTextReader(textReader);

                /* load textures */
                if (font.Pages.Length > 1) throw new NotSupportedException("Only one page font supported");

                var page = font.Pages.First();

                TextureName = string.Format("{0}.text", page.FileName.Split(new char[] { '.' })[0]);

                /* find resource */
                var resourceName = AppDomain.CurrentDomain.GetAssemblies().SelectMany(asm => asm.GetManifestResourceNames()).FirstOrDefault(name => name.Contains(TextureName));
                if (resourceName == null) throw new FileNotFoundException(resourceName + " missing.");

                if (!Scene.Resources.Any(r => r is Texture && r.ResourceName == resourceName))
                {
                    var texture = new Texture() { ResourceName = resourceName, Name = TextureName };
                    Scene.AddResource(texture);
                }

                BmpFont = font;
            }

            return true;
        }
Esempio n. 2
0
        public override bool InternalLoad()
        {
            _shader = new BackgroundShader();
            _shader.Load();

            _vertices = new Vector3[4] {
                new Vector3(-1,1,0),
                new Vector3(1,-1,0),
                new Vector3(-1,-1,0),
                new Vector3(1,1,0)
            };

            _normals = new Vector3[4] {
                new Vector3(0,0,1),
                new Vector3(0,0,1),
                new Vector3(0,0,1),
                new Vector3(0,0,1)
            };

            _indices = new short[6] {
                2,1,0,1,3,0
            };

            /* create buffers to store vertex data */

            GL.GenBuffers(2, _bufferIds);

            GLVertex[] glVertices = new GLVertex[_vertices.Length];

            for (int i = 0; i < glVertices.Length; i++)
            {
                glVertices[i].X = (short)(_vertices[i].X);
                glVertices[i].Y = (short)(_vertices[i].Y);
                glVertices[i].Z = (short)(_vertices[i].Z);
                glVertices[i].NX = (short)(_normals[i].X);
                glVertices[i].NY = (short)(_normals[i].Y);
                glVertices[i].NZ = (short)(_normals[i].Z);
            }

            unsafe
            {
                fixed (GLVertex* data = glVertices)
                {
                    GL.BindBuffer(BufferTarget.ArrayBuffer, _bufferIds[0]);
                    GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(glVertices.Length * sizeof(GLVertex)),
                               (IntPtr)data, BufferUsage.StaticDraw);
                }

                fixed (short* data = _indices.ToArray())
                {
                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, _bufferIds[1]);
                    GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(_indices.Length * sizeof(short)),
                          (IntPtr)data, BufferUsage.StaticDraw);
                }
            }

            GameObject.Position = new Vector3(0, 0, _distance);

            GameObject.Scene.OnLoaded += (a, b) =>
            {
                /* calculate level bounding box */

                var objs = GameObject.Scene.GameObjects.SelectMany(g => g.AllChildren).ToList();
                var meshComponents = objs.Select(o => o.Components.FirstOrDefault(c => c is MeshComponent) as MeshComponent).Where(c => c != null);

                Vector3 vMin = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
                Vector3 vMax = new Vector3(float.MinValue, float.MinValue, float.MinValue);

                foreach (var meshComponent in meshComponents)
                {
                    var meshMin = meshComponent.MinBox;
                    var meshMax = meshComponent.MaxBox;

                    var transform = meshComponent.GameObject.GetCompositeTransform();
                    meshMin = Vector3.Transform(meshMin, transform);
                    meshMax = Vector3.Transform(meshMax, transform);

                    if (meshMin.X < vMin.X) vMin.X = meshMin.X;
                    if (meshMin.X > vMax.X) vMax.X = meshMin.X;

                    if (meshMin.Y < vMin.Y) vMin.Y = meshMin.Y;
                    if (meshMin.Y > vMax.Y) vMax.Y = meshMin.Y;

                    if (meshMin.Z < vMin.Z) vMin.Z = meshMin.Z;
                    if (meshMin.Z > vMax.Z) vMax.Z = meshMin.Z;

                    if (meshMax.X < vMin.X) vMin.X = meshMax.X;
                    if (meshMax.X > vMax.X) vMax.X = meshMax.X;

                    if (meshMax.Y < vMin.Y) vMin.Y = meshMax.Y;
                    if (meshMax.Y > vMax.Y) vMax.Y = meshMax.Y;

                    if (meshMax.Z < vMin.Z) vMin.Z = meshMax.Z;
                    if (meshMax.Z > vMax.Z) vMax.Z = meshMax.Z;
                }

                float margin = 30.0f;
                MinBounds = new Vector2(vMin.X - margin, vMin.Y - margin);
                MaxBounds = new Vector2(vMax.X + margin, vMax.Y + margin);

                if (GameObject.Scene.CurrentCamera is PerspectiveCameraComponent)
                {
                    var cam = GameObject.Scene.CurrentCamera as PerspectiveCameraComponent;
                    var sin = System.Math.Sin(cam.FieldOfViewRadians);
                    var l = (float)(sin * (System.Math.Abs(_distance) + (cam.GameObject.Position.Z))) - 5;
                    GameObject.Scale = new Vector3(l*cam.AspectRatio, l,1);

                    cam.GameObject.OnMove += (c, d) =>
                    {
                        GameObject.Position = new Vector3(cam.GameObject.Position.X, cam.GameObject.Position.Y, _distance);
                    };
                }
            };

            this.GameObject.RenderQueuePriority = int.MaxValue;

            _bgTexture = GameObject.Scene.Resources.FirstOrDefault(r => r.Name == "bg" && r is Texture) as Texture;

            return true;
        }