opengl - GLSL设置纹理单元统一问题

标签 opengl graphics shader opentk

编辑: 我将 GLTextureUnitID 从 uint 更改为 int,这导致错误停止出现,但渲染的不是纹理,而是黑色方 block 。当我注释掉对 SetTexture 的调用时,即使我从未在该行之前设置该统一,它仍然呈现良好。

编辑2: 看来 GLTextureUnitID 的值为 1,而正在设置的纹理的 ID 为 0。

编辑3: 将 SetTexture 函数更改为以下内容似乎已经解决了问题,但感觉有点肮脏的解决方案。

GL.BindTexture(TextureTarget.Texture2D, (int)texture.GLTextureUnitID);
GL.Uniform1(GLTextureUnitLocation, 0);

原始问题: 我开始向我的项目添加对纹理文件的支持,但每当我为纹理着色器程序调用 GL.Uniform1 时,我都会收到“InvalidOperation”错误。更奇怪的是,当我注释掉对导致错误的函数的调用时,纹理仍然会被渲染。

渲染带有纹理的四边形的 RenderGUI 函数是:

    public override void RenderGUI()
    {
        // Translate Matrix to Position of GUI Element
        Matrices.Translate(SetMatrixParam.ModelViewMatrix, Position.X, Position.Y, 0);

        // Bind Shaders and Send Matrices
        Shader.Bind();
        Shader.SendMatrices();

        // Set Texture Information
        Shader.SetTexture(Texture); // <- This causes the error, it's just a call to Uniform1 using a property from the texture object, if commented out the error doesn't appear.
        Shader.SetTextureColor(Color4.White);

        GL.Begin(BeginMode.Triangles);

        // Render Quad
        Vector3 topLeft = new Vector3(0, 0, 0);
        Vector3 bottomLeft = new Vector3(0, Size.Y, 0);
        Vector3 topRight = new Vector3(Size.X, 0, 0);
        Vector3 bottomRight = new Vector3(Size.X, Size.Y, 0);

        Shader.SetTextureCoord(0, 0); GL.Vertex3(topLeft); // Top Left
        Shader.SetTextureCoord(0, 1); GL.Vertex3(bottomLeft); // Bottom Left
        Shader.SetTextureCoord(1, 1); GL.Vertex3(bottomRight); // Bottom Right

        Shader.SetTextureCoord(0, 0); GL.Vertex3(topLeft); // Top Left
        Shader.SetTextureCoord(1, 1); GL.Vertex3(bottomRight); // Bottom Right
        Shader.SetTextureCoord(1, 0); GL.Vertex3(topRight); // Top Right

        GL.End();
    }

正在调用的函数会导致出现错误:

    public void SetTexture(Texture2D texture)
    {
        if (texture.GLTextureUnitID == null)
            throw new ArgumentException();

        GL.Uniform1(GLTextureUnitLocation, (uint)texture.GLTextureUnitID);
    }

This can also be found at the github for this project.以及Texture2D classvertexfragment正在使用的着色器和 class handling the shader program that uses those shaders .

最佳答案

哪里有错误的代码?

来自http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml glUniform1 返回 GL_INVALID_OPERATION 的原因有很多。我建议您仔细检查每一项,并亲自确认它适用或不适用。

这是列表:

Errors

GL_INVALID_OPERATION is generated if there is no current program object.

GL_INVALID_OPERATION is generated if the size of the uniform variable declared in the shader does not match the size indicated by the glUniform command.

GL_INVALID_OPERATION is generated if one of the signed or unsigned integer variants of this function is used to load a uniform variable of type float, vec2, vec3, vec4, or an array of these, or if one of the floating-point variants of this function is used to load a uniform variable of type int, ivec2, ivec3, ivec4, unsigned int, uvec2, uvec3, uvec4, or an array of these.

GL_INVALID_OPERATION is generated if one of the signed integer variants of this function is used to load a uniform variable of type unsigned int, uvec2, uvec3, uvec4, or an array of these.

GL_INVALID_OPERATION is generated if one of the unsigned integer variants of this function is used to load a uniform variable of type int, ivec2, ivec3, ivec4, or an array of these.

GL_INVALID_OPERATION is generated if location is an invalid uniform location for the current program object and location is not equal to -1.

GL_INVALID_OPERATION is generated if count is greater than 1 and the indicated uniform variable is not an array variable.

GL_INVALID_OPERATION is generated if a sampler is loaded using a command other than glUniform1i and glUniform1iv.

关于opengl - GLSL设置纹理单元统一问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17277048/

相关文章:

c++ - OpenGl - 纹理 glutSolidSphere?

c++ - 我需要帮助将数组添加到 C++ 中的 vector

r - 在另一个角上绘制一个图形

math - 计算机图形 : Math to Code

OpenGL 2D非矩形区域裁剪

unity3d - Unity Compute Shader 中调用 numthreads 和 Dispatch 的区别

c++ - 如何渲染多个纹理?

c - setbkcolor() 在 Dev-C++ 4.9.9.2 中无效

opengl - 如何在OpenGL中实现没有重复顶点的平面着色?

java - LWJGL 平滑动画