Beispiel #1
0
        public void Render(Matrix4 view)
        {
            if (vertices == null)
            {
                return;
            }

            bool buffersWereInitialized = vertVbo != 0 && boneVbo != 0 && facesIbo != 0;

            if (!buffersWereInitialized)
            {
                GenerateBuffers();
            }

            shader = OpenTkSharedResources.shaders["Mbn"];
            shader.UseProgram();

            GL.Uniform1(shader.GetUniformLocation("renderVertColor"), Runtime.renderVertColor ? 1 : 0);
            GL.Uniform1(shader.GetUniformLocation("renderType"), (int)Runtime.renderType);
            GL.Uniform1(shader.GetUniformLocation("selectedBoneIndex"), Runtime.selectedBoneIndex);

            GL.UniformMatrix4(shader.GetUniformLocation("modelview"), false, ref view);

            GL.Uniform3(shader.GetUniformLocation("difLightColor"), Runtime.lightSetParam.characterDiffuse.diffuseColor.R, Runtime.lightSetParam.characterDiffuse.diffuseColor.G, Runtime.lightSetParam.characterDiffuse.diffuseColor.B);
            GL.Uniform3(shader.GetUniformLocation("ambLightColor"), Runtime.lightSetParam.characterDiffuse.ambientColor.R, Runtime.lightSetParam.characterDiffuse.ambientColor.G, Runtime.lightSetParam.characterDiffuse.ambientColor.B);

            GL.ActiveTexture(TextureUnit.Texture10);
            RenderTools.uvTestPattern.Bind();
            GL.Uniform1(shader.GetUniformLocation("UVTestPattern"), 10);

            Matrix4[] f = skeleton.GetShaderMatrices();

            int maxUniformBlockSize = GL.GetInteger(GetPName.MaxUniformBlockSize);
            int boneCount           = skeleton.bones.Count;
            int dataSize            = boneCount * Vector4.SizeInBytes * 4;

            GL.BindBuffer(BufferTarget.UniformBuffer, boneVbo);
            GL.BufferData(BufferTarget.UniformBuffer, (IntPtr)(dataSize), IntPtr.Zero, BufferUsageHint.DynamicDraw);
            GL.BindBuffer(BufferTarget.UniformBuffer, 0);

            var blockIndex = GL.GetUniformBlockIndex(shader.Id, "bones");

            GL.BindBufferBase(BufferRangeTarget.UniformBuffer, blockIndex, boneVbo);

            if (f.Length > 0)
            {
                GL.BindBuffer(BufferTarget.UniformBuffer, boneVbo);
                GL.BufferSubData(BufferTarget.UniformBuffer, IntPtr.Zero, (IntPtr)(f.Length * Vector4.SizeInBytes * 4), f);
            }

            shader.EnableVertexAttributes();

            GL.BindBuffer(BufferTarget.ArrayBuffer, vertVbo);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(Vertex.sizeInBytes * vertices.Length), vertices, BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(shader.GetAttribLocation("pos"), 3, VertexAttribPointerType.Float, false, Vertex.sizeInBytes, 0);
            GL.VertexAttribPointer(shader.GetAttribLocation("nrm"), 3, VertexAttribPointerType.Float, false, Vertex.sizeInBytes, 12);
            GL.VertexAttribPointer(shader.GetAttribLocation("col"), 4, VertexAttribPointerType.Float, false, Vertex.sizeInBytes, 24);
            GL.VertexAttribPointer(shader.GetAttribLocation("tx0"), 2, VertexAttribPointerType.Float, false, Vertex.sizeInBytes, 40);
            GL.VertexAttribPointer(shader.GetAttribLocation("bone"), 2, VertexAttribPointerType.Float, false, Vertex.sizeInBytes, 48);
            GL.VertexAttribPointer(shader.GetAttribLocation("weight"), 2, VertexAttribPointerType.Float, false, Vertex.sizeInBytes, 56);

            GL.PointSize(4f);

            foreach (BCH_Mesh m in Nodes)
            {
                shader.SetVector4("colorSamplerUV", new Vector4(1, 1, 0, 0));

                GL.ActiveTexture(TextureUnit.Texture0);
                BCH_Material material = (BCH_Material)((BCH)Parent.Parent).Materials.Nodes[m.MaterialIndex];
                BchTexture   tex      = ((BCH)Parent.Parent).GetTexture(material.Text);
                if (tex == null)
                {
                    RenderTools.defaultTex.Bind();
                }
                else
                {
                    tex.display.Bind();
                }
                GL.Uniform1(shader.GetUniformLocation("tex"), 0);
                if (!m.Checked)
                {
                    continue;
                }

                foreach (BCH_PolyGroup pg in m.Nodes)
                {
                    GL.Uniform1(shader.GetUniformLocation("boneList"), pg.BoneList.Length, pg.BoneList);

                    GL.Disable(EnableCap.CullFace);
                    GL.CullFace(CullFaceMode.Back);

                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, facesIbo);
                    GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(pg.Faces.Length * sizeof(int)), pg.Faces, BufferUsageHint.StaticDraw);
                    GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

                    GL.DrawElements(PrimitiveType.Triangles, pg.Faces.Length, DrawElementsType.UnsignedInt, 0);
                }
            }

            shader.DisableVertexAttributes();
        }