OpenTK / OpenGL属性名称返回乱码

时间:2017-05-21 17:58:34

标签: c# opengl graphics shader opentk

我一直在使用OpenTK进行渲染。我最近发现我不能一次使用多个着色器进行渲染,所以我改变了系统。问题是,现在当所有的属性和制服被加载并放入词典时,他们的名字就完全是胡言乱语。这种乱码的一个例子是" ü\b\aÈy7\aÀNU\u0006"(在着色器中称为" vPosition")。有时"GetActivAttrib"也会以ShaderVariable名称进入。我一直坚持这个问题已经太久了所以我很感激任何帮助。

以下是加载Shader变量的代码,我有理由相信问题就在这里:

        public static void GetShaderVariables(int shaderProgramID, int selectedShaderID, Dictionary<string, int> AttributeDictionary, Dictionary<string, int> UniformDictionary, Dictionary<string, int> VBODictionary)
    {
        int shaderVariableLocation;

        int LinkStatus;
        GL.GetProgram(shaderProgramID, GetProgramParameterName.LinkStatus, out LinkStatus);
        if (LinkStatus == 0)
        {
            GL.LinkProgram(shaderProgramID); //Linking the program to the graphics card if its not already connected
        }

        GL.GetProgram(shaderProgramID, GetProgramParameterName.LinkStatus, out LinkStatus);
        if (LinkStatus == 1)
        {
            int VBOID;
            #region Storing all the Attributes of the selected Shader
            int shaderVariableAmount;
            GL.GetProgram(shaderProgramID, GetProgramParameterName.ActiveAttributes, out shaderVariableAmount);

            for (int i = 0; i <= shaderVariableAmount; i++)
            {
                GL.GetActiveAttrib(shaderProgramID, i, textBufferSize, out length, out size, out attributeType, shaderVariableStringBuilder);

                string shaderVariableName = shaderVariableStringBuilder.ToString();
                string VBOStringName = shaderVariableName + "VBO";

                shaderVariableLocation = GL.GetAttribLocation(shaderProgramID, shaderVariableName);
                if (shaderVariableLocation == -1)
                {
                    Console.WriteLine("Shader variable doesnt exist");
                    break;
                }

                GL.GenBuffers(1, out VBOID);

                //Stores the values given that the value is not already present in the Dictionary
                if (!AttributeDictionary.ContainsKey(shaderVariableName))
                    AttributeDictionary.Add(shaderVariableName, shaderVariableLocation);

                if (!VBODictionary.ContainsKey(VBOStringName))
                    VBODictionary.Add(VBOStringName, VBOID);
            }
            #endregion

            #region Storing all the Uniforms of the selected Shader
            shaderVariableAmount = 0;
            GL.GetProgram(shaderProgramID, GetProgramParameterName.ActiveUniforms, out shaderVariableAmount);

            for (int i = 0; i <= shaderVariableAmount; i++)
            {
                GL.GetActiveUniform(shaderProgramID, i, textBufferSize, out length, out size, out uniformType, shaderVariableStringBuilder);
                string shaderUniformVarName = shaderVariableStringBuilder.ToString();

                shaderVariableLocation = GL.GetUniformLocation(shaderProgramID, shaderUniformVarName);

                if (!UniformDictionary.ContainsKey(shaderUniformVarName))
                    UniformDictionary.Add(shaderUniformVarName, shaderVariableLocation);
            }
            #endregion
        }
        else
        {
            Console.Write(GL.GetProgramInfoLog(shaderProgramID));
            GL.GetError();
            throw new System.ArgumentException("Program failed to link");
        }
    }

以下是我使用的顶点着色器示例:

#version 330

in vec3 vPosition;
in vec3 vColor;

uniform mat4 modelview;

out vec4 color;



void main()
{
    gl_Position = modelview * vec4(vPosition, 1.0);

    color = vec4(vColor, 0);
}

0 个答案:

没有答案