Exemplo n.º 1
0
        private void LoadMeshes()
        {
            ObjectTreeNode meshesFolder = new ObjectTreeNode("Meshes");

            this.AddChild(meshesFolder);

            foreach (var mesh in Header.Meshes)
            {
                Dictionary <int, List <STVertex> > VertexMatMapper = new Dictionary <int, List <STVertex> >();

                int index = 0;
                foreach (var primative in mesh.Primitives)
                {
                    if (!VertexMatMapper.ContainsKey(primative.MaterialIndex))
                    {
                        VertexMatMapper.Add(primative.MaterialIndex, new List <STVertex>());
                    }

                    var vertices = VertexMatMapper[primative.MaterialIndex];
                    switch (primative.Type)
                    {
                    case PrimitiveType.Triangle:
                        vertices.Add(ToGenericVertex(mesh, primative.Vertices[0], Skeleton));
                        vertices.Add(ToGenericVertex(mesh, primative.Vertices[1], Skeleton));
                        vertices.Add(ToGenericVertex(mesh, primative.Vertices[2], Skeleton));
                        break;

                    case PrimitiveType.Quad:
                        vertices.Add(ToGenericVertex(mesh, primative.Vertices[0], Skeleton));
                        vertices.Add(ToGenericVertex(mesh, primative.Vertices[1], Skeleton));
                        vertices.Add(ToGenericVertex(mesh, primative.Vertices[2], Skeleton));
                        vertices.Add(ToGenericVertex(mesh, primative.Vertices[1], Skeleton));
                        vertices.Add(ToGenericVertex(mesh, primative.Vertices[3], Skeleton));
                        vertices.Add(ToGenericVertex(mesh, primative.Vertices[2], Skeleton));
                        break;

                    case PrimitiveType.TriangleStrip:
                        var verts = new List <STVertex>();

                        foreach (var dv in primative.Vertices)
                        {
                            verts.Add(ToGenericVertex(mesh, dv, Skeleton));
                        }
                        verts = ConvertTriStrips(verts);

                        vertices.AddRange(verts);
                        break;
                    }
                }

                foreach (var poly in VertexMatMapper)
                {
                    HSFMesh genericMesh = new HSFMesh();
                    genericMesh.Name = mesh.Name;
                    GenericMeshes.Add(genericMesh);

                    meshesFolder.AddChild(new ObjectTreeNode(mesh.Name)
                    {
                        Tag      = genericMesh,
                        ImageKey = "Mesh",
                    });

                    int objectIndex = Header.ObjectData.Objects.IndexOf(mesh.ObjectData);
                    if (objectIndex != -1)
                    {
                        var bone = Skeleton.Bones[objectIndex];
                        genericMesh.ObjectNode = bone;
                        bone.Visible           = false;
                    }

                    HSFMaterialWrapper genericMat = new HSFMaterialWrapper(this);

                    STPolygonGroup group = new STPolygonGroup();
                    group.Material = genericMat;
                    genericMesh.PolygonGroups.Add(group);

                    var matData = Header.Materials[poly.Key].MaterialData;

                    genericMat.Material = Header.Materials[poly.Key];
                    genericMat.Name     = Header.Materials[poly.Key].Name;

                    var pass_flags = matData.AltFlags & HSF_Renderer.PASS_BITS;
                    if (pass_flags != 0 || (matData.TransparencyInverted != 0 &&
                                            matData.VertexMode == 0))
                    {
                        genericMesh.IsTransparent = true;
                        group.IsTransparentPass   = true;
                    }

                    genericMat.Mesh = mesh;

                    var attributes = Header.Materials[poly.Key].Textures;
                    for (int i = 0; i < attributes.Count; i++)
                    {
                        var attribute = attributes[i].Item2;
                        var texIndex  = attribute.TextureIndex;
                        genericMat.Attributes.Add(attribute);

                        group.Material.TextureMaps.Add(new HSFMatTexture(this)
                        {
                            Attribute    = attribute,
                            Name         = Header.Textures[texIndex].Name,
                            TextureIndex = texIndex,
                            Type         = i == 0 ? STTextureType.Diffuse : STTextureType.None,
                            WrapU        = ConvertWrapMode(attribute.WrapS),
                            WrapV        = ConvertWrapMode(attribute.WrapT),
                            MagFilter    = STTextureMagFilter.Linear,
                            MinFilter    = STTextureMinFilter.Linear,
                        });
                    }

                    genericMesh.Vertices.AddRange(poly.Value);
                    genericMesh.Optmize(group);
                }
            }
        }
Exemplo n.º 2
0
        private void SetTextureUniforms(ShaderProgram shader, HSFMaterialWrapper mat)
        {
            if (uboAtt == -1)
            {
                SetAttributeUBO();
            }

            var index = GL.GetUniformBlockIndex(shader.program, "AttributeBlock");

            GL.UniformBlockBinding(shader.program, index, 0);

            GL.BindBuffer(BufferTarget.UniformBuffer, uboAtt);
            GL.BufferData(BufferTarget.UniformBuffer, TextureAttribute.Size, IntPtr.Zero, BufferUsageHint.StaticDraw);
            GL.BindBufferBase(BufferRangeTarget.UniformBuffer, index, uboAtt);
            GL.BindBuffer(BufferTarget.UniformBuffer, 0);

            LoadDebugTextureMaps(shader);

            GL.ActiveTexture(TextureUnit.Texture0 + 1);
            GL.BindTexture(TextureTarget.Texture2D, RenderTools.defaultTex.RenderableTex.TexID);

            shader.SetInt("textureCount", mat.TextureMaps.Count);
            Console.WriteLine($"textureCount {mat.TextureMaps.Count}");

            TextureAttribute[] attriutes = new TextureAttribute[6];
            for (int i = 0; i < 6; i++)
            {
                if (i == 1)
                {
                    attriutes[i].texPositionStart = new Vector2(0.5f, 1);
                    attriutes[i].texPositionEnd   = new Vector2(1, 1);
                    attriutes[i].texScaleStart    = new Vector2(1, 1);
                    attriutes[i].texScaleEnd      = new Vector2(0, 0);
                }
                else
                {
                    attriutes[i].texPositionStart = new Vector2(0, 0);
                    attriutes[i].texPositionEnd   = new Vector2(1, 1);
                    attriutes[i].texScaleStart    = new Vector2(1, 1);
                    attriutes[i].texScaleEnd      = new Vector2(0, 0);
                }
            }

            var attributeData = mat.ParentHSF.Header.AttributeData;

            for (int i = 0; i < mat.TextureMaps.Count; i++)
            {
                var hsfTexture = (HSFMatTexture)mat.TextureMaps[i];
                var texStart   = hsfTexture.Attribute.TexAnimStart;
                var texEnd     = hsfTexture.Attribute.TexAnimEnd;
                attriutes[i].texPositionStart = new Vector2(texStart.Position.X, texStart.Position.Y);
                attriutes[i].texPositionEnd   = new Vector2(texEnd.Position.X, texEnd.Position.Y);
                attriutes[i].texScaleStart    = new Vector2(texStart.Scale.X, texStart.Scale.Y);
                attriutes[i].texScaleEnd      = new Vector2(texEnd.Scale.X, texEnd.Scale.Y);
                // attriutes[i].texParam = hsfTexture.Attribute.NbtEnable;
                //  attriutes[i].blendingFlag = hsfTexture.Attribute.BlendingFlag;
                // attriutes[i].alphaFlag = hsfTexture.Attribute.AlphaFlag;

                int attIndex = attributeData.Attributes.IndexOf(hsfTexture.Attribute);
                if (HSFParent.Header.AttributeAnimControllers.ContainsKey(attIndex))
                {
                    var controller = HSFParent.Header.AttributeAnimControllers[attIndex];
                    attriutes[i].texPositionStart.X = controller.TranslateX;
                    attriutes[i].texPositionStart.Y = controller.TranslateY;
                }

                var binded = BindTexture(shader, Scene.Models[0].Textures, (HSFMatTexture)mat.TextureMaps[i], i + 1);
                shader.SetInt($"texture{i}", i + 1);
            }

            GL.BindBuffer(BufferTarget.UniformBuffer, uboAtt);
            GL.BufferSubData(BufferTarget.UniformBuffer, IntPtr.Zero, TextureAttribute.Size, attriutes);
            GL.BindBuffer(BufferTarget.UniformBuffer, 0);
        }