private void SetupDrawCallMaterial(DrawCall drawCall, IDictionary <string, bool> shaderArguments, RenderMaterial material)
        {
            drawCall.Material = material;
            // Add shader parameters from material to the shader parameters from the draw call
            var combinedShaderParameters = shaderArguments
                                           .Concat(material.Material.GetShaderArguments())
                                           .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            // Load shader
            drawCall.Shader = guiContext.ShaderLoader.LoadShader(drawCall.Material.Material.ShaderName, combinedShaderParameters);

            //Bind and validate shader
            GL.UseProgram(drawCall.Shader.Program);

            if (!drawCall.Material.Textures.ContainsKey("g_tTintMask"))
            {
                drawCall.Material.Textures.Add("g_tTintMask", MaterialLoader.CreateSolidTexture(1f, 1f, 1f));
            }

            if (!drawCall.Material.Textures.ContainsKey("g_tNormal"))
            {
                drawCall.Material.Textures.Add("g_tNormal", MaterialLoader.CreateSolidTexture(0.5f, 1f, 0.5f));
            }
        }
        public RenderMaterial LoadMaterial(Resource resource)
        {
            if (resource == null)
            {
                return(GetErrorMaterial());
            }

            var mat = new RenderMaterial((VrfMaterial)resource.DataBlock);

            foreach (var textureReference in mat.Material.TextureParams)
            {
                var key = textureReference.Key;

                mat.Textures[key] = LoadTexture(textureReference.Value);
            }

            if (mat.Material.IntParams.ContainsKey("F_SOLID_COLOR") && mat.Material.IntParams["F_SOLID_COLOR"] == 1)
            {
                var a = mat.Material.VectorParams["g_vColorTint"];

                mat.Textures["g_tColor"] = GenerateColorTexture(1, 1, new[] { a.X, a.Y, a.Z, a.W });
            }

            if (!mat.Textures.ContainsKey("g_tColor"))
            {
                mat.Textures["g_tColor"] = GetErrorTexture();
            }

            // Since our shaders only use g_tColor, we have to find at least one texture to use here
            if (mat.Textures["g_tColor"] == GetErrorTexture())
            {
                var namesToTry = new[] { "g_tColor2", "g_tColor1", "g_tColorA", "g_tColorB", "g_tColorC" };

                foreach (var name in namesToTry)
                {
                    if (mat.Textures.ContainsKey(name))
                    {
                        mat.Textures["g_tColor"] = mat.Textures[name];
                        break;
                    }
                }
            }

            // Set default values for scale and positions
            if (!mat.Material.VectorParams.ContainsKey("g_vTexCoordScale"))
            {
                mat.Material.VectorParams["g_vTexCoordScale"] = Vector4.One;
            }

            if (!mat.Material.VectorParams.ContainsKey("g_vTexCoordOffset"))
            {
                mat.Material.VectorParams["g_vTexCoordOffset"] = Vector4.Zero;
            }

            if (!mat.Material.VectorParams.ContainsKey("g_vColorTint"))
            {
                mat.Material.VectorParams["g_vColorTint"] = Vector4.One;
            }

            return(mat);
        }
        private DrawCall CreateDrawCall(IKeyValueCollection objectDrawCall, VBIB vbib, IDictionary <string, bool> shaderArguments, RenderMaterial material)
        {
            var drawCall = new DrawCall();

            string primitiveType = objectDrawCall.GetProperty <object>("m_nPrimitiveType") switch
            {
                string primitiveTypeString => primitiveTypeString,
                byte primitiveTypeByte =>
                (primitiveTypeByte == 5) ? "RENDER_PRIM_TRIANGLES" : ("UNKNOWN_" + primitiveTypeByte),
                _ => throw new NotImplementedException("Unknown PrimitiveType in drawCall!")
            };

            switch (primitiveType)
            {
            case "RENDER_PRIM_TRIANGLES":
                drawCall.PrimitiveType = PrimitiveType.Triangles;
                break;

            default:
                throw new NotImplementedException("Unknown PrimitiveType in drawCall! (" + primitiveType + ")");
            }

            SetupDrawCallMaterial(drawCall, shaderArguments, material);

            var indexBufferObject = objectDrawCall.GetSubCollection("m_indexBuffer");

            var indexBuffer = default(DrawBuffer);

            indexBuffer.Id       = Convert.ToUInt32(indexBufferObject.GetProperty <object>("m_hBuffer"));
            indexBuffer.Offset   = Convert.ToUInt32(indexBufferObject.GetProperty <object>("m_nBindOffsetBytes"));
            drawCall.IndexBuffer = indexBuffer;

            var indexElementSize = vbib.IndexBuffers[(int)drawCall.IndexBuffer.Id].ElementSizeInBytes;

            //drawCall.BaseVertex = Convert.ToUInt32(objectDrawCall.GetProperty<object>("m_nBaseVertex"));
            //drawCall.VertexCount = Convert.ToUInt32(objectDrawCall.GetProperty<object>("m_nVertexCount"));
            drawCall.StartIndex = Convert.ToUInt32(objectDrawCall.GetProperty <object>("m_nStartIndex")) * indexElementSize;
            drawCall.IndexCount = Convert.ToInt32(objectDrawCall.GetProperty <object>("m_nIndexCount"));

            if (objectDrawCall.ContainsKey("m_vTintColor"))
            {
                var tintColor = objectDrawCall.GetSubCollection("m_vTintColor").ToVector3();
                drawCall.TintColor = new OpenTK.Vector3(tintColor.X, tintColor.Y, tintColor.Z);
            }

            if (indexElementSize == 2)
            {
                //shopkeeper_vr
                drawCall.IndexType = DrawElementsType.UnsignedShort;
            }
            else if (indexElementSize == 4)
            {
                //glados
                drawCall.IndexType = DrawElementsType.UnsignedInt;
            }
            else
            {
                throw new Exception("Unsupported index type");
            }

            var m_vertexBuffer = objectDrawCall.GetArray("m_vertexBuffers")[0]; // TODO: Not just 0

            var vertexBuffer = default(DrawBuffer);

            vertexBuffer.Id       = Convert.ToUInt32(m_vertexBuffer.GetProperty <object>("m_hBuffer"));
            vertexBuffer.Offset   = Convert.ToUInt32(m_vertexBuffer.GetProperty <object>("m_nBindOffsetBytes"));
            drawCall.VertexBuffer = vertexBuffer;

            drawCall.VertexArrayObject = guiContext.MeshBufferCache.GetVertexArrayObject(
                vbib,
                drawCall.Shader,
                drawCall.VertexBuffer.Id,
                drawCall.IndexBuffer.Id);

            return(drawCall);
        }
Exemplo n.º 4
0
        private DrawCall CreateDrawCall(IKeyValueCollection objectDrawCall, VBIB vbib, IDictionary <string, bool> shaderArguments, RenderMaterial material)
        {
            var drawCall = new DrawCall();

            switch (objectDrawCall.GetProperty <string>("m_nPrimitiveType"))
            {
            case "RENDER_PRIM_TRIANGLES":
                drawCall.PrimitiveType = PrimitiveType.Triangles;
                break;

            default:
                throw new Exception("Unknown PrimitiveType in drawCall! (" + objectDrawCall.GetProperty <string>("m_nPrimitiveType") + ")");
            }

            drawCall.Material = material;
            // Add shader parameters from material to the shader parameters from the draw call
            var combinedShaderParameters = shaderArguments
                                           .Concat(material.Material.GetShaderArguments())
                                           .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            // Load shader
            drawCall.Shader = guiContext.ShaderLoader.LoadShader(drawCall.Material.Material.ShaderName, combinedShaderParameters);

            //Bind and validate shader
            GL.UseProgram(drawCall.Shader.Program);

            var indexBufferObject = objectDrawCall.GetSubCollection("m_indexBuffer");

            var indexBuffer = default(DrawBuffer);

            indexBuffer.Id       = Convert.ToUInt32(indexBufferObject.GetProperty <object>("m_hBuffer"));
            indexBuffer.Offset   = Convert.ToUInt32(indexBufferObject.GetProperty <object>("m_nBindOffsetBytes"));
            drawCall.IndexBuffer = indexBuffer;

            var indexElementSize = vbib.IndexBuffers[(int)drawCall.IndexBuffer.Id].Size;

            //drawCall.BaseVertex = Convert.ToUInt32(objectDrawCall.GetProperty<object>("m_nBaseVertex"));
            //drawCall.VertexCount = Convert.ToUInt32(objectDrawCall.GetProperty<object>("m_nVertexCount"));
            drawCall.StartIndex = Convert.ToUInt32(objectDrawCall.GetProperty <object>("m_nStartIndex")) * indexElementSize;
            drawCall.IndexCount = Convert.ToInt32(objectDrawCall.GetProperty <object>("m_nIndexCount"));

            if (objectDrawCall.ContainsKey("m_vTintColor"))
            {
                var tintColor = objectDrawCall.GetSubCollection("m_vTintColor").ToVector3();
                drawCall.TintColor = new OpenTK.Vector3(tintColor.X, tintColor.Y, tintColor.Z);
            }

            if (!drawCall.Material.Textures.ContainsKey("g_tTintMask"))
            {
                drawCall.Material.Textures.Add("g_tTintMask", MaterialLoader.CreateSolidTexture(1f, 1f, 1f));
            }

            if (!drawCall.Material.Textures.ContainsKey("g_tNormal"))
            {
                drawCall.Material.Textures.Add("g_tNormal", MaterialLoader.CreateSolidTexture(0.5f, 1f, 0.5f));
            }

            if (indexElementSize == 2)
            {
                //shopkeeper_vr
                drawCall.IndexType = DrawElementsType.UnsignedShort;
            }
            else if (indexElementSize == 4)
            {
                //glados
                drawCall.IndexType = DrawElementsType.UnsignedInt;
            }
            else
            {
                throw new Exception("Unsupported index type");
            }

            var m_vertexBuffers = objectDrawCall.GetSubCollection("m_vertexBuffers");
            var m_vertexBuffer  = m_vertexBuffers.GetSubCollection("0"); // TODO: Not just 0

            var vertexBuffer = default(DrawBuffer);

            vertexBuffer.Id       = Convert.ToUInt32(m_vertexBuffer.GetProperty <object>("m_hBuffer"));
            vertexBuffer.Offset   = Convert.ToUInt32(m_vertexBuffer.GetProperty <object>("m_nBindOffsetBytes"));
            drawCall.VertexBuffer = vertexBuffer;

            drawCall.VertexArrayObject = guiContext.MeshBufferCache.GetVertexArrayObject(
                vbib,
                drawCall.Shader,
                drawCall.VertexBuffer.Id,
                drawCall.IndexBuffer.Id);

            return(drawCall);
        }
Exemplo n.º 5
0
 public MaterialRenderer(RenderMaterial renderMaterial)
 {
     material = renderMaterial;
     shader   = ShaderLoader.LoadPlaneShader(material.Material.ShaderName, material.Material.GetShaderArguments());
     quadVao  = SetupQuadBuffer();
 }
Exemplo n.º 6
0
 public MaterialRenderer(RenderMaterial renderMaterial, VrfGuiContext vrfGuiContext)
 {
     material = renderMaterial;
     shader   = vrfGuiContext.ShaderLoader.LoadPlaneShader(material.Material.ShaderName);
     quadVao  = SetupQuadBuffer();
 }
        public RenderMaterial LoadMaterial(Resource resource)
        {
            if (resource == null)
            {
                var errorMat = new RenderMaterial(new VrfMaterial());
                errorMat.Textures["g_tColor"] = GetErrorTexture();
                errorMat.Material.ShaderName  = "vrf.error";

                return(errorMat);
            }

            var mat = new RenderMaterial((VrfMaterial)resource.DataBlock);

            foreach (var textureReference in mat.Material.TextureParams)
            {
                var key = textureReference.Key;

                mat.Textures[key] = LoadTexture(textureReference.Value);
            }

            if (mat.Material.IntParams.ContainsKey("F_SOLID_COLOR") && mat.Material.IntParams["F_SOLID_COLOR"] == 1)
            {
                var a = mat.Material.VectorParams["g_vColorTint"];

                mat.Textures["g_tColor"] = GenerateColorTexture(1, 1, new[] { a.X, a.Y, a.Z, a.W });
            }

            if (!mat.Textures.ContainsKey("g_tColor"))
            {
                mat.Textures["g_tColor"] = GetErrorTexture();
            }

            // TODO: Perry, this probably needs to be in shader or something
            if (mat.Textures.ContainsKey("g_tColor2") && mat.Textures["g_tColor"] == GetErrorTexture())
            {
                mat.Textures["g_tColor"] = mat.Textures["g_tColor2"];
            }

            if (mat.Textures.ContainsKey("g_tColor1") && mat.Textures["g_tColor"] == GetErrorTexture())
            {
                mat.Textures["g_tColor"] = mat.Textures["g_tColor1"];
            }

            // Set default values for scale and positions
            if (!mat.Material.VectorParams.ContainsKey("g_vTexCoordScale"))
            {
                mat.Material.VectorParams["g_vTexCoordScale"] = Vector4.One;
            }

            if (!mat.Material.VectorParams.ContainsKey("g_vTexCoordOffset"))
            {
                mat.Material.VectorParams["g_vTexCoordOffset"] = Vector4.Zero;
            }

            if (!mat.Material.VectorParams.ContainsKey("g_vColorTint"))
            {
                mat.Material.VectorParams["g_vColorTint"] = Vector4.One;
            }

            return(mat);
        }