예제 #1
0
        public override void InternalLoad()
        {
            /* find mesh component and load it if needed */

            _meshComponent = GameObject.Components.FirstOrDefault(c => c is MeshComponent) as MeshComponent;
            if (_meshComponent == null) throw new InvalidOperationException("No mesh component to render");

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

            /* create buffers to store vertex data */

            GL.GenBuffers(4, _bufferIds);
            GL.BindBuffer(BufferTarget.ArrayBuffer, _bufferIds[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(_meshComponent.Vertices.Length * (Vector3.SizeInBytes)),
                          _meshComponent.Vertices.ToArray(), BufferUsage.StaticDraw);

            GL.BindBuffer(BufferTarget.ArrayBuffer, _bufferIds[1]);
            GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(_meshComponent.Normals.Length * (Vector3.SizeInBytes)),
                          _meshComponent.Vertices.ToArray(), BufferUsage.StaticDraw);

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