为什么调用glEnableVertexPointer和glVertexPointer导致InvalidValue?

时间:2015-02-02 23:58:37

标签: c# opengl shader game-engine opentk

我正在创建一个包含基本游戏需求的游戏引擎。使用glslDevil,结果是我的绑定VBO方法抛出InvalidValue错误。调用glVertexPointer和调用glEnableVertexPointer会导致问题。顶点属性索引导致问题。该指数是4294967295,远远超过15.其他一切都很好。我正在使用OpenTK。这是绑定到属性方法。

public void BindToAttribute(ShaderProgram prog, string attribute)
    {
        int location = GL.GetAttribLocation(prog.ProgramID, attribute);
        GL.EnableVertexAttribArray(location);
        Bind();
        GL.VertexAttribPointer(location, Size, PointerType, true, TSize, 0);
    }
public void Bind()
    {
        GL.BindBuffer(Target, ID);
    }

如果需要,这是我的着色器。

顶点着色器:

uniform mat4 transform;
uniform mat4 projection;
uniform mat4 camera;
in vec3 vertex;
in vec3 normal;
in vec4 color;
in vec2 uv;
out vec3 rnormal;
out vec4 rcolor;
out vec2 ruv;

void main(void)
{
    rcolor = color;
    rnormal = normal;
    ruv = uv;
    gl_Position = camera * projection * transform * vec4(vertex, 1);
}

Fragment Shader:

in vec3 rnormal;
in vec4 rcolor;
in vec2 ruv;
uniform sampler2D texture;

void main(void)
{
    gl_FragColor = texture2D(texture, ruv) * rcolor;
}

我没有正确获取索引或是否存在其他问题?

1 个答案:

答案 0 :(得分:0)

你得到的索引似乎是问题所在:这是当opengl找不到具有该名称的有效属性/制服时得到的索引。

可能会发生一些事情:

  • 你传递的是一个在着色器程序中不存在的字符串(检查区分大小写等等)
  • 统一存在并且您正在传递正确的字符串,但是您没有在着色器中使用该属性,因此驱动程序已经删除了由于优化而在最终代码中出现的所有属性(因此它没有已经存在了)

总的来说,这个数字表明OpenGL无法找到你想要的制服或属性

编辑:

以下是一个技巧:让我们假设您有一些像素着色器代码,它返回的值是许多值的总和:

out vec4 color;

void main()
{
    // this shader does many calculations when you are 
    // using many values, but let's assume you want to debug 
    // just the diffuse color... how do you do it?
    // if you change the output to return just the diffuse color, 
    // the optimizer might remove code and you might have problems
    // 
    // if you have this
    color = various_calculation_1 + various_calculation_2 + ....;
    // what you can do is the following
    color *= 0.0000001f;  // so basically it's still calculated 
                          // but it almost won't show up
    color += value_to_debug; // example, the diffuse color
}