예제 #1
0
        /// <summary>
        /// Packs an <see cref="MDXVertex"/> for use with an OpenGL buffer.
        /// In effect, it transforms it from Z-up to Y-up.
        /// </summary>
        /// <param name="vertex">The vertex to repack.</param>
        /// <returns>A repacked vertex.</returns>
        public static byte[] PackForOpenGL(this MDXVertex vertex)
        {
            using var ms = new MemoryStream();
            using var bw = new BinaryWriter(ms);
            bw.WriteVector3(vertex.Position, AxisConfiguration.YUp);
            bw.Write(vertex.BoneWeights.ToArray());
            bw.Write(vertex.BoneIndices.ToArray());
            bw.WriteVector3(vertex.Normal, AxisConfiguration.YUp);
            bw.WriteVector2(vertex.UV1);
            bw.WriteVector2(vertex.UV2);

            return(ms.ToArray());
        }
예제 #2
0
        /// <summary>
        /// Packs an <see cref="MDXVertex"/> for use with an OpenGL buffer.
        /// In effect, it transforms it from Z-up to Y-up.
        /// </summary>
        /// <param name="vertex">The vertex to repack.</param>
        /// <returns>A repacked vertex.</returns>
        public static byte[] PackForOpenGL(this MDXVertex vertex)
        {
            using (MemoryStream ms = new MemoryStream())
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    bw.WriteVector3(vertex.Position, AxisConfiguration.YUp);
                    bw.Write(vertex.BoneWeights.ToArray());
                    bw.Write(vertex.BoneIndices.ToArray());
                    bw.WriteVector3(vertex.Normal, AxisConfiguration.YUp);
                    bw.WriteVector2(vertex.UVCoordinatesChannel1);
                    bw.WriteVector2(vertex.UVCoordinatesChannel2);

                    return(ms.ToArray());
                }
        }
예제 #3
0
 /// <summary>
 /// Reads an <see cref="MDXVertex"/> from the data stream.
 /// </summary>
 /// <param name="binaryReader">The reader.</param>
 /// <returns>The value.</returns>
 public static MDXVertex ReadMDXVertex(this BinaryReader binaryReader)
 {
     return(new MDXVertex(binaryReader.ReadBytes(MDXVertex.GetSize())));
 }
예제 #4
0
        /// <inheritdoc />
        public void Initialize()
        {
            ThrowIfDisposed();

            if (this.IsInitialized)
            {
                return;
            }

            this.Shader = this.Cache.GetShader(EverlookShader.GameModel) as GameModelShader;

            if (this.Shader == null)
            {
                throw new ShaderNullException(typeof(GameModelShader));
            }

            this.VertexBuffer = new Buffer <byte>(BufferTarget.ArrayBuffer, BufferUsageHint.StaticDraw)
            {
                Data = this.Model.Vertices.Select(v => v.PackForOpenGL()).SelectMany(b => b).ToArray()
            };

            var attributePointers = new[]
            {
                // Position
                new VertexAttributePointer(0, 3, VertexAttribPointerType.Float, MDXVertex.GetSize(), 0),
                // Bone weights
                new VertexAttributePointer(1, 4, VertexAttribPointerType.UnsignedByte, MDXVertex.GetSize(), 12),
                // Bone indexes
                new VertexAttributePointer(2, 4, VertexAttribPointerType.UnsignedByte, MDXVertex.GetSize(), 16),
                // Normal
                new VertexAttributePointer(3, 3, VertexAttribPointerType.Float, MDXVertex.GetSize(), 20),
                // UV1
                new VertexAttributePointer(4, 2, VertexAttribPointerType.Float, MDXVertex.GetSize(), 32),
                // UV2
                new VertexAttributePointer(5, 2, VertexAttribPointerType.Float, MDXVertex.GetSize(), 40)
            };

            this.VertexBuffer.AttachAttributePointers(attributePointers);

            this.BoundingBox = new RenderableBoundingBox(this.Model.BoundingBox, this.ActorTransform);
            this.BoundingBox.Initialize();

            foreach (MDXTexture texture in this.Model.Textures)
            {
                if (!this.TextureLookup.ContainsKey(texture.Filename))
                {
                    this.TextureLookup.Add
                    (
                        texture.Filename,
                        this.Cache.GetTexture(texture, this.GameContext)
                    );
                }
            }

            foreach (MDXSkin skin in this.Model.Skins)
            {
                ushort[] absoluteTriangleVertexIndexes = skin.Triangles.Select(relativeIndex => skin.VertexIndices[relativeIndex]).ToArray();
                var      skinIndexBuffer = new Buffer <ushort>(BufferTarget.ElementArrayBuffer, BufferUsageHint.StaticDraw)
                {
                    Data = absoluteTriangleVertexIndexes
                };

                this.SkinIndexArrayBuffers.Add(skin, skinIndexBuffer);

                if (this.Model.Version <= WarcraftVersion.Wrath)
                {
                    // In models earlier than Cata, we need to calculate the shader selector value at runtime.
                    foreach (var renderBatch in skin.RenderBatches)
                    {
                        ushort shaderSelector = MDXShaderHelper.GetRuntimeShaderID(renderBatch.ShaderID, renderBatch, this.Model);
                        renderBatch.ShaderID = shaderSelector;
                    }
                }
            }

            // Cache the default display info
            if (this.CurrentDisplayInfo != null)
            {
                CacheDisplayInfo(this.CurrentDisplayInfo);
            }

            this.IsInitialized = true;
        }