コード例 #1
0
        public void SetActiveShader(ShaderProgram shader)
        {
            if (shader == null)
            {
                Logger.LogError(Logger.ErrorState.Critical, "SetActiveShader, no shader given");
                return;
            }
            if (activeProgram != shader)
            {
                activeProgram = shader;
                activeProgram.Use();

                ShaderUniformManager man = ShaderUniformManager.GetSingleton();
                if (man.DoesShaderUseCamera(activeProgram))
                {
                    RenderCamera();
                }
                if (man.DoesShaderUseLights(activeProgram))
                {
                    RenderActiveLights();
                }
                if (man.DoesShaderSupportUniform(activeProgram, ShaderUniformName.CubeMap))
                {
                    MaterialManager matMan = MaterialManager.GetSingleton();
                    matMan.SetFromMaterialToShader(activeSkybox, ShaderUniformName.CubeMap, activeProgram);
                    if (man.DoesShaderSupportUniform(activeProgram, ShaderUniformName.SkyboxRotationMatrix))
                    {
                        int rotLoc = activeProgram.GetUniformLocation(ShaderUniformName.SkyboxRotationMatrix);
                        activeProgram.SetMatrix4Uniform(rotLoc, ref activeSkyboxRotation);
                    }
                }
            }
        }
コード例 #2
0
 public static ShaderUniformManager GetSingleton()
 {
     if (singleton == null)
     {
         singleton = new ShaderUniformManager();
     }
     return(singleton);
 }
コード例 #3
0
        private void PassLightData(IShaderDataOwner light, int lightIndex)
        {
            ShaderUniformManager man = ShaderUniformManager.GetSingleton();

            man.SetArrayData(activeProgram, ShaderUniformName.LightsArray, ShaderUniformName.LightPositionOrDirection, light, lightIndex);
            man.SetArrayData(activeProgram, ShaderUniformName.LightsArray, ShaderUniformName.LightColor, light, lightIndex);
            man.SetArrayData(activeProgram, ShaderUniformName.LightsArray, ShaderUniformName.LinearAttenuation, light, lightIndex);
            man.SetArrayData(activeProgram, ShaderUniformName.LightsArray, ShaderUniformName.QuadraticAttenuation, light, lightIndex);
        }
コード例 #4
0
ファイル: MaterialManager.cs プロジェクト: MuffinTrap/Demo
        public void SetFromMaterialToShader(Material mat, ShaderUniformName uniform, ShaderProgram program)
        {
            ShaderUniformManager man = ShaderUniformManager.GetSingleton();

            if (man.DoesShaderSupportUniform(program, uniform))
            {
                int location = man.GetDataLocation(program, uniform);
                if (mat.textureMaps.ContainsKey(uniform))
                {
                    SetTextureUniform(program, location, uniform, mat.textureMaps[uniform]);
                }
            }
        }
コード例 #5
0
        public static ShaderAttribute getAttribute(ShaderAttributeName name, ShaderProgram shaderProgram)
        {
            foreach (ShaderAttribute attr in shaderProgram.attributes)
            {
                if (attr.name == name)
                {
                    return(attr);
                }
            }
            Logger.LogError(Logger.ErrorState.Critical, "Did not get attribute " + ShaderUniformManager.GetSingleton().GetAttributeName(name));
            ShaderAttribute invalid = new ShaderAttribute(ShaderAttributeName.InvalidAttributeName, ShaderDataType.InvalidType);

            return(invalid);
        }
コード例 #6
0
        public int GetAttributeLocation(ShaderAttributeName attribName)
        {
            foreach (ShaderAttribute a in attributes)
            {
                if (a.name == attribName)
                {
                    return(a.location);
                }
            }
            ShaderUniformManager uniMan = ShaderUniformManager.GetSingleton();

            Logger.LogError(Logger.ErrorState.Limited, "Attribute " + uniMan.GetAttributeName(attribName) + " location not found");
            return(-1);
        }
コード例 #7
0
        public int GetUniformLocation(ShaderUniformName uniformName)
        {
            foreach (ShaderUniform a in uniforms)
            {
                if (a.name == uniformName)
                {
                    return(a.location);
                }
            }
            ShaderUniformManager uniMan = ShaderUniformManager.GetSingleton();

            Logger.LogError(Logger.ErrorState.Limited, "Uniform " + uniMan.GetUniformName(uniformName) + " location not found in shader program " + programName);
            return(-1);
        }
コード例 #8
0
ファイル: MaterialManager.cs プロジェクト: MuffinTrap/Demo
        // Sets all materials that apply
        public void SetMaterialToShader(Material meshMaterial, ShaderProgram program)
        {
            if (program == null)
            {
                Logger.LogError(Logger.ErrorState.Limited, "MaterialManager> No program given, cannot set material");
                return;
            }
            if (meshMaterial == null)
            {
                SetMaterialToShader(defaultMaterial, program);
                return;
            }
            ShaderUniformManager man = ShaderUniformManager.GetSingleton();

            // Must try each supported texture uniform
            foreach (KeyValuePair <ShaderUniformName, TextureMap> entry in defaultMaterial.textureMaps)
            {
                ShaderUniformName uniform = entry.Key;
                if (!man.DoesShaderSupportUniform(program, uniform))
                {
                    continue;
                }
                int location = man.GetDataLocation(program, uniform);
                if (location == ShaderUniformManager.GetInvalidDataLocation())
                {
                    Logger.LogError(Logger.ErrorState.Limited, "MaterialManager: Shader says to support texture map " + man.GetUniformName(uniform) + " but there is no location for it");
                    continue;
                }


                // IMPORTANT PART !
                if (meshMaterial.textureMaps.ContainsKey(uniform))
                {
                    SetTextureUniform(program, location, uniform, meshMaterial.textureMaps[uniform]);
                }

                else
                {
                    if (defaultMaterial.textureMaps.ContainsKey(uniform))
                    {
                        SetTextureUniform(program, location, uniform, defaultMaterial.textureMaps[uniform]);
                    }
                    else
                    {
                        Logger.LogError(Logger.ErrorState.Limited, "MaterialManager: Shader wants a texture map " + man.GetUniformName(uniform) + " that is not in default material");
                    }
                }
            }
        }
コード例 #9
0
        public void RenderCamera()
        {
            if (activeProgram == null)
            {
                Logger.LogError(Logger.ErrorState.Critical, "RenderCamera, no active shader");
                return;
            }


            ShaderUniformManager man = ShaderUniformManager.GetSingleton();

            man.TrySetData(activeProgram, ShaderUniformName.ViewMatrix, camera);
            man.TrySetData(activeProgram, ShaderUniformName.ProjectionMatrix, camera);
            man.TrySetData(activeProgram, ShaderUniformName.CameraPosition, camera);
        }
コード例 #10
0
        public void RenderShaderDataOwnerMesh(DrawableMesh mesh, IShaderDataOwner owner)
        {
            if (mesh == null)
            {
                Logger.LogError(Logger.ErrorState.Critical, "RenderMesh, no mesh given");
                return;
            }
            SetActiveShader(mesh.ShaderProgram);
            if (activeProgram == null)
            {
                Logger.LogError(Logger.ErrorState.Critical, "RenderMesh, no active shader");
                return;
            }

            List <ShaderUniformName> ownerUniforms = owner.GetUniforms();
            ShaderUniformManager     man           = ShaderUniformManager.GetSingleton();

            foreach (ShaderUniformName name in ownerUniforms)
            {
                man.SetData(activeProgram, name, owner);
            }

            if (mesh != owner)
            {
                List <ShaderUniformName> meshUniforms = mesh.GetUniforms();
                foreach (ShaderUniformName name in meshUniforms)
                {
                    man.SetData(activeProgram, name, mesh);
                }
            }

            if (mesh.BoundMaterial != null)
            {
                MaterialManager matMan = MaterialManager.GetSingleton();
                matMan.SetMaterialToShader(mesh.BoundMaterial, activeProgram);
            }

            mesh.draw();
        }
コード例 #11
0
        public ShaderProgram(string nameParam, params Shader[] shaders)
        {
            handle = GL.CreateProgram();

            foreach (var shader in shaders)
            {
                GL.AttachShader(handle, shader.Handle);
            }

            GL.LinkProgram(handle);

            // Check link errors
            int successValue = 0;

            GL.GetProgram(handle, GetProgramParameterName.LinkStatus, out successValue);
            if (successValue == 0)
            {
                Logger.LogError(Logger.ErrorState.Limited, "Shader link failed: " + GL.GetProgramInfoLog(handle));
            }

            programName = nameParam;
            Logger.LogInfoLinePart("Creating Shader Program: ", ConsoleColor.Gray);
            Logger.LogInfoLinePart(programName, ConsoleColor.Cyan);
            Logger.LogInfoLineEnd();

            // Load Uniforms
            ///////////////////////////////////
            int uniformAmount = -1;

            GL.GetProgram(handle, GetProgramParameterName.ActiveUniforms, out uniformAmount);
            Error.checkGLError("Shader()");
            ShaderUniformManager uniManager = ShaderUniformManager.GetSingleton();

            Logger.LogInfo("Program linked.");

            uniforms = new List <ShaderUniform>(uniformAmount);

            int               maxShaderNameSize = 100;
            StringBuilder     shaderName        = new StringBuilder(maxShaderNameSize);
            int               writtenLength;
            int               uniformSize;
            ActiveUniformType type;

            Logger.LogInfo("Uniforms (" + uniformAmount + ") >>>");
            for (int i = 0; i < uniformAmount; i++)
            {
                GL.GetActiveUniform(this.handle, i, maxShaderNameSize, out writtenLength, out uniformSize, out type, shaderName);

                string         uniformName = shaderName.ToString();
                ShaderSizeInfo info        = uniManager.GetTypeSizeInfo(type);

                ShaderUniform uniform = uniManager.CreateShaderUniform(uniformName, type, GetUniformLocation(this.handle, uniformName));

                Logger.LogInfoLinePart(" " + i, ConsoleColor.White);
                Logger.LogInfoLinePart("\t" + uniManager.GetDataTypeString(uniManager.UniformTypeToDataType(type)), ConsoleColor.Cyan);
                Logger.LogInfoLinePart("\t" + uniformName + " (" + info.sizeElements + ")", ConsoleColor.White);
                if (uniform.IsValid())
                {
                    Logger.LogInfoLinePart("\t\t [", ConsoleColor.Gray);
                    Logger.LogInfoLinePart("OK", ConsoleColor.Green);
                    Logger.LogInfoLinePart("]", ConsoleColor.Gray);
                    uniforms.Add(uniform);
                }
                Logger.LogInfoLineEnd();
            }

            int attributeAmount = -1;

            GL.GetProgram(handle, GetProgramParameterName.ActiveAttributes, out attributeAmount);
            ActiveAttribType attribType;
            int attrSize = -1;

            Logger.LogInfo("Attributes (" + attributeAmount + ") >>>");

            attributes = new List <ShaderAttribute>(attributeAmount);

            for (int i = 0; i < attributeAmount; i++)
            {
                GL.GetActiveAttrib(this.handle, i, maxShaderNameSize, out writtenLength, out attrSize, out attribType, shaderName);

                string          attribName = shaderName.ToString();
                int             location   = GetAttributeLocation(handle, attribName);
                ShaderSizeInfo  info       = uniManager.GetTypeSizeInfo(attribType);
                ShaderAttribute attribute  = uniManager.CreateShaderAttribute(attribName, attribType, GetAttributeLocation(this.handle, attribName), info.sizeElements);

                Logger.LogInfoLinePart(" " + i, ConsoleColor.White);
                Logger.LogInfoLinePart("\t" + uniManager.GetDataTypeString(uniManager.AttribTypeToDataType(attribType)), ConsoleColor.Cyan);
                Logger.LogInfoLinePart("\t" + attribName + " (" + info.sizeElements + ")", ConsoleColor.White);
                Logger.LogInfoLinePart("\t " + location, ConsoleColor.Red);
                if (attribute.IsValid())
                {
                    Logger.LogInfoLinePart("\t\t [", ConsoleColor.Gray);
                    Logger.LogInfoLinePart("OK", ConsoleColor.Green);
                    Logger.LogInfoLinePart("]", ConsoleColor.Gray);
                    attributes.Add(attribute);
                }
                Logger.LogInfoLineEnd();
            }

            foreach (var shader in shaders)
            {
                GL.DeleteShader(shader.Handle);
            }
        }
コード例 #12
0
ファイル: MaterialManager.cs プロジェクト: MuffinTrap/Demo
 public TextureMap GetMap(ShaderUniformName uniformName)
 {
     if (textureMaps.ContainsKey(uniformName))
     {
         return(textureMaps[uniformName]);
     }
     else
     {
         Logger.LogError(Logger.ErrorState.Limited, "Material " + materialName + " does not have a map for " + ShaderUniformManager.GetSingleton().GetUniformName(uniformName) + "");
         return(MaterialManager.GetSingleton().GetDefaultMap(uniformName));
     }
 }
コード例 #13
0
ファイル: MaterialManager.cs プロジェクト: MuffinTrap/Demo
 public TextureMap GetDefaultMap(ShaderUniformName uniformName)
 {
     if (defaultMaterial.textureMaps.ContainsKey(uniformName))
     {
         return(defaultMaterial.textureMaps[uniformName]);
     }
     else
     {
         Logger.LogError(Logger.ErrorState.Critical, "MaterialManager's default material does not have map for " + ShaderUniformManager.GetSingleton().GetUniformName(uniformName));
         return(null);
     }
 }
コード例 #14
0
ファイル: MaterialManager.cs プロジェクト: MuffinTrap/Demo
        public void SetTextureUniform(ShaderProgram shaderProgram, int location, ShaderUniformName uniform, TextureMap map)
        {
            int textureUnit = GetTextureUnitForMap(uniform);

            if (uniform == ShaderUniformName.DiffuseMap)
            {
                GL.ActiveTexture(TextureUnit.Texture0);
            }
            else if (uniform == ShaderUniformName.IlluminationMap)
            {
                GL.ActiveTexture(TextureUnit.Texture1);
            }
            else if (uniform == ShaderUniformName.NormalMap)
            {
                GL.ActiveTexture(TextureUnit.Texture2);
            }
            else if (uniform == ShaderUniformName.RoughnessMap)
            {
                GL.ActiveTexture(TextureUnit.Texture3);
            }
            else if (uniform == ShaderUniformName.MetallicMap)
            {
                GL.ActiveTexture(TextureUnit.Texture4);
            }
            else if (uniform == ShaderUniformName.HeightMap)
            {
                GL.ActiveTexture(TextureUnit.Texture5);
            }
            else if (uniform == ShaderUniformName.CubeMap)
            {
                GL.ActiveTexture(TextureUnit.Texture6);
            }

            if (textureUnit == -1)
            {
                Logger.LogError(Logger.ErrorState.Limited, "No defined texture unit for uniform " + ShaderUniformManager.GetSingleton().GetUniformName(uniform) + ", cannot bind");
            }

            if (uniform == ShaderUniformName.CubeMap)
            {
                GL.BindTexture(TextureTarget.TextureCubeMap, map.textureGLIndex);
            }
            else
            {
                GL.BindTexture(TextureTarget.Texture2D, map.textureGLIndex);
            }
            shaderProgram.SetSamplerUniform(location, textureUnit);
        }