Example #1
0
        /// <summary>
        /// Renders the specified model group on a batch basis.
        /// </summary>
        private void RenderGroup(ModelGroup modelGroup, Matrix4 modelViewProjection)
        {
            // Reenable depth test
            GL.Enable(EnableCap.DepthTest);

            // Render the object
            // Send the vertices to the shader
            this.VertexBufferLookup[modelGroup].Bind();
            this.VertexBufferLookup[modelGroup].EnableAttributes();

            this.NormalBufferLookup[modelGroup].Bind();
            this.NormalBufferLookup[modelGroup].EnableAttributes();

            this.TextureCoordinateBufferLookup[modelGroup].Bind();
            this.TextureCoordinateBufferLookup[modelGroup].EnableAttributes();

            // Bind the index buffer
            this.VertexIndexBufferLookup[modelGroup].Bind();

            if (this.ShouldRenderWireframe)
            {
                this.Shader.Wireframe.SetWireframeColour(EverlookConfiguration.Instance.WireframeColour);

                // Override blend setting
                GL.Enable(EnableCap.Blend);
            }

            // Render all the different materials (opaque first, transparent after)
            foreach (RenderBatch renderBatch in modelGroup.GetRenderBatches()
                     .OrderBy(batch => batch.MaterialIndex)
                     .ThenBy(batch => this.Model.GetMaterial(batch.MaterialIndex).BlendMode))
            {
                this.Shader.Enable();

                ModelMaterial modelMaterial = this.Model.GetMaterial(renderBatch.MaterialIndex);

                this.Shader.SetMaterial(modelMaterial);
                this.Shader.SetMVPMatrix(modelViewProjection);

                // Set the texture as the first diffuse texture in unit 0
                Texture2D texture = this.Cache.GetCachedTexture(modelMaterial.Texture0);
                if (modelMaterial.Flags.HasFlag(MaterialFlags.TextureWrappingClamp))
                {
                    texture.WrappingMode = TextureWrapMode.Clamp;
                }
                else
                {
                    texture.WrappingMode = TextureWrapMode.Repeat;
                }

                this.Shader.BindTexture2D(TextureUnit.Texture0, TextureUniform.Texture0, texture);

                // Finally, draw the model
                GL.DrawRangeElements
                (
                    PrimitiveType.Triangles,
                    renderBatch.FirstPolygonIndex,
                    renderBatch.FirstPolygonIndex + renderBatch.PolygonIndexCount - 1,
                    renderBatch.PolygonIndexCount,
                    DrawElementsType.UnsignedShort,
                    new IntPtr(renderBatch.FirstPolygonIndex * 2)
                );
            }

            // Release the attribute arrays
            this.VertexBufferLookup[modelGroup].DisableAttributes();
            this.NormalBufferLookup[modelGroup].DisableAttributes();
            this.TextureCoordinateBufferLookup[modelGroup].DisableAttributes();
        }