Example #1
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;
        }
Example #2
0
        public override bool InternalLoad()
        {
            /* find mesh component and load it if needed */

            _meshComponent = GameObject.Components.FirstOrDefault(c => c is MeshComponent) as MeshComponent;
            if (_meshComponent == null)
            {
                return false;
            }

            if (!_meshComponent.IsLoaded) _meshComponent.Load();

            int[] cachedBuffers;
            if (!Game.InDesignMode && !string.IsNullOrEmpty(_meshComponent.MeshResourceName) &&
                GameObject.Scene.MeshBufferCache.TryGetValue(_meshComponent.MeshResourceName, out cachedBuffers))
            {
                /* can reuse a rendercomponent + gl buffers */
                _bufferIds = cachedBuffers;
                _isClone = true;
            }
            else if (!_isClone)
            {

                /* create buffers to store vertex data */

                GL.GenBuffers(2, _bufferIds);

                GLVertex[] glVertices = new GLVertex[_meshComponent.Vertices.Length];

                bool hasUV = _meshComponent.UV.Length == _meshComponent.Vertices.Length;

                for (int i = 0; i < glVertices.Length; i++)
                {
                    glVertices[i].X = (short)(_meshComponent.Vertices[i].X * _shortFloatFactor);
                    glVertices[i].Y = (short)(_meshComponent.Vertices[i].Y * _shortFloatFactor);
                    glVertices[i].Z = (short)(_meshComponent.Vertices[i].Z * _shortFloatFactor);
                    glVertices[i].NX = (short)(_meshComponent.Normals[i].X * _shortFloatFactor);
                    glVertices[i].NY = (short)(_meshComponent.Normals[i].Y * _shortFloatFactor);
                    glVertices[i].NZ = (short)(_meshComponent.Normals[i].Z * _shortFloatFactor);

                    if (hasUV)
                    {
                        glVertices[i].U = (short)(_meshComponent.UV[i].X * _shortFloatFactor);
                        glVertices[i].V = (short)(_meshComponent.UV[i].Y * _shortFloatFactor);
                    }
                }

                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 = _meshComponent.Indices.ToArray())
                    {
                        GL.BindBuffer(BufferTarget.ElementArrayBuffer, _bufferIds[1]);
                        GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(_meshComponent.Indices.Length * sizeof(short)),
                              (IntPtr)data, BufferUsage.StaticDraw);
                    }
                }

                if (!Game.InDesignMode && !string.IsNullOrEmpty(_meshComponent.MeshResourceName))
                {
                    GameObject.Scene.MeshBufferCache.Add(_meshComponent.MeshResourceName, _bufferIds);
                }
            }

            return true;
        }