【问题标题】:OpenGL: Why these codes draws nothing, with interleaved VBO and GLSLOpenGL:为什么这些代码什么都不画,交错的 VBO 和 GLSL
【发布时间】:2013-08-29 07:30:49
【问题描述】:

我查看了很多论文和博客,最后我找到了这些代码来生成一个四边形: gProgram 中的程序存储

    vert shader:---------------------
    #version 330
    layout(location = 0) in vec3 attrib_position;  
    layout(location = 6) in vec2 attrib_texcoord;  
    out Varing
    {
        vec4    pos;
        vec2    texcoord;
    } VS_StateOutput;

    uniform mat4 gtransform;
    void main(void)
    {
        VS_StateOutput.pos = gtransform * vec4(attrib_position,1.0f);
        VS_StateOutput.texcoord = attrib_texcoord;
    }

    frag shader:--------------------
    #version 330
    uniform sampler2D texUnit;

    in  Varing
    {
        vec4    pos;
        vec2    texcoord;
    } PS_StateInput;
    layout(location = 0) out vec4 color0;
    void main(void)
    {
        color0 = texture(texUnit, PS_StateInput.texcoord);
    }

这是我的顶点数据:存储在 gVertexVBO

    float data[] =
    { 
        -1.0f,  1.0f, 0.0f,   0.0f, 0.0f,
         1.0f,  1.0f, 0.0f,   1.0f, 0.0f,
         1.0f, -1.0f, 0.0f,   1.0f, 1.0f,
        -1.0f, -1.0f, 0.0f,   0.0f, 1.0f
    };

和索引数据:存储在gIndexVBO中

    unsigned short idata[] =
    {
        0, 3, 2,  0, 2, 1
    };

在绘图部分,我喜欢这样:

ps:gtransform 设置为单位矩阵

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        
    glUseProgram(gProgram);        
    glProgramUniformMatrix4fv(gProgram, gtLoc, 1, GL_FALSE, matrix);
    glProgramUniform1uiv(gProgram, texLoc, 1, &colorTex);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gIndexVBO);

    glBindBuffer(GL_ARRAY_BUFFER, gVertexVBO);
    char* offset = NULL;
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*5, offset);
    glEnableVertexAttribArray(0);

    glVertexAttribPointer(6, 2, GL_FLOAT, GL_FALSE, sizeof(float)*5, offset+sizeof(float)*3);
    glEnableVertexAttribArray(6);

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, NULL);  

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(6);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

除了清除后的颜色,我什么也没得到,我认为错误位于这些代码中,但我无法弄清楚它在哪里,感谢任何帮助。

如果您需要查看初始化部分的代码,我会在回复中发布,谢谢!

【问题讨论】:

    标签: opengl vertex-buffer vertex-attributes


    【解决方案1】:

    您没有在顶点着色器中提供内置的gl_Position

    uniform mat4 gtransform;
    void main(void)
    {
        vec4 outPosition = gtransform * vec4(attrib_position,1.0f);
        gl_Position = outPosition;
        // if you need the position in the fragment shader
        VS_StateOutput.pos = outPosition;
        VS_StateOutput.texcoord = attrib_texcoord;
    }
    

    OpenGL 不会知道您的 VS_StateOutput 结构,也不知道将新顶点放在哪里。 See here 供快速参考。 (第 7 页)

    This link 提供了更好的内置结构细节。

    编辑:

    接下来,我会将片段着色器的主要内容更改为:

    void main(void)
    {
        color0 = vec4(1.0); //texture(texUnit, PS_StateInput.texcoord);
    }
    

    排除任何纹理问题。有时纹理问题会导致空白的黑色纹理。如果你得到一个白色的颜色,那么你就知道它是一个纹理问题,可以去那里看看。

    【讨论】:

    • 谢谢!这确实是一个错误,但在我修复它之后,我仍然无法获得四边形......
    • 我会尝试在很多不同的地方打电话给glGetError。它应该返回一个错误代码,为您提供更多信息。对不起,我现在没有太多时间。您还应该在设置缓冲区的位置发布代码。 glGetError Link here
    • 还是谢谢你,我用gDEBugger检查了那个程序,看起来没问题。缓冲区设置正确,也许我应该转向管道状态。
    • 好吧,在我将片段着色器更改为: void main(void) { gl_FragColor = vec4(1.0); },我得到了四边形,也许我只是忘了绑定输出位置?
    • 根据我的经验,只要布局设置为 location=0,它仍然可以工作,具体取决于 GLSL 中的 #version。很高兴它正在工作! :)
    猜你喜欢
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    相关资源
    最近更新 更多