让纹理在OpenGL 3.2中工作

时间:2013-11-25 08:29:26

标签: c++ opengl glsl librocket

我一直盯着这段代码一段时间没有运气。我正在努力将librocket集成到我自己的项目中(该库对问题并不重要),其中一部分需要编写一个渲染器类。我一直试图这样做,但无法显示纹理。顶点颜色和位置工作正常。

我正在使用OpenGL3.2。

我暂时修改了代码以尝试绘制单个四边形。使用的唯一参数是texture参数,它只是GLuint强制转换为其他类型。

我很有可能错过了一些愚蠢的东西,但我看不到它。希望另一组眼睛会有所帮助。随意询问更多代码/信息。

// Called by Rocket when it wants to render geometry that it does not wish to optimise.
void SDLRenderInterface::RenderGeometry(Rocket::Core::Vertex* vertices, int num_vertices, int* indices, int num_indices, const Rocket::Core::TextureHandle texture, const Rocket::Core::Vector2f& translation)
{
    GLuint program;
    GLuint vertexBuffer;
    GLuint indexBuffer;
    GLuint vertexPosLoc      = 0;
    GLuint vertexColorLoc    = 0;
    GLuint vertexTexCoordLoc = 0;
    GLuint texSamplerLoc     = 0;
    GLuint translationLoc    = 0;
    GLuint viewDimLoc        = 0;


    int offset = 8;
    int vertexCount = 4;
    float vertexData[] = {-0.5, -0.5, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
                           0.5, -0.5, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0,
                           0.5,  0.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
                          -0.5,  0.5, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0};
    int indexData[] = {0,1,2,0,2,3};
    int indexCount = 6;

    // Populate vertex buffer
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float)*offset*vertexCount,
                 vertexData, GL_STATIC_DRAW);

    // Populate index buffer
    glGenBuffers(1, &indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * indexCount,
                 indexData, GL_STATIC_DRAW);

    program = shaderManager->getProgram(2, "rocketTex.vert",
                                           "rocketTex.frag");
    glUseProgram(program);
    // Set up the texture
    texSamplerLoc = glGetUniformLocation(program, "texSampler");
    vertexTexCoordLoc = glGetAttribLocation(program, "vertexTexCoord");
    if(texSamplerLoc == -1)
    {
        std::cerr << "Error: cannot find texture location." << std::endl;
        return;
    }
    if(vertexTexCoordLoc == -1)
    {
        std::cerr << "Error: cannot find texture coord location."
                  << std::endl;
        return;
    }

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, (GLuint) texture);
    glUniform1i(texSamplerLoc, 0);

    // Set up the per vertex texture coords 
    glEnableVertexAttribArray(vertexTexCoordLoc);
    glVertexAttribPointer(vertexTexCoordLoc, 2, GL_FLOAT, GL_FALSE,
                          offset * sizeof(float),
                          (void*) (sizeof(float) * 6));

    // Set up uniforms
    translationLoc = glGetUniformLocation(program, "translation");
    viewDimLoc = glGetUniformLocation(program, "viewDimensions");
    if(translationLoc == -1)
    {
        std::cerr << "Error: cannot find translation location."
                  << std::endl;
        return;
    }
    if(viewDimLoc == -1)
    {
        std::cerr << "Error: cannot find viewDim location."
                  << std::endl;
        return;
    }
    glUniform2f(translationLoc, 0,0);
    glUniform2f(viewDimLoc, 1,1);


    // Set up per-vertex attributes
    vertexPosLoc = glGetAttribLocation(program, "vertexPosition");
    vertexColorLoc = glGetAttribLocation(program, "vertexColor");
    if(vertexPosLoc == -1)
    {
        std::cerr << "Error: cannot find vertex position location."
                  << std::endl;
        return;
    }
    if(vertexColorLoc == -1)
    {
        std::cerr << "Error: cannot find vertex color location."
                  << std::endl;
        return;
    }
    glEnableVertexAttribArray(vertexPosLoc);
    glEnableVertexAttribArray(vertexColorLoc);
    glVertexAttribPointer(vertexPosLoc, 2, GL_FLOAT, GL_FALSE,
                              offset * sizeof(float), 0);
    glVertexAttribPointer(vertexColorLoc, 4, GL_FLOAT, GL_TRUE,
                              offset * sizeof(float),
                              (void*) (sizeof(float) * 2));

    // Draw the geometry
    glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);

    glDisableVertexAttribArray(vertexPosLoc);
    glDisableVertexAttribArray(vertexColorLoc);
    glDisableVertexAttribArray(vertexTexCoordLoc);
    glDeleteBuffers(1, &vertexBuffer);
    glDeleteBuffers(1, &indexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glUseProgram(0);

}

顶点着色器:

#version 120

uniform vec2 translation;
uniform vec2 viewDimensions;

attribute vec2 vertexPosition;
attribute vec4 vertexColor;
attribute vec2 vertexTexCoord;

varying vec2 texCoord;
varying vec4 fragColor;

void main(void)
{
    vec2 ndcPos = ((vertexPosition + translation)/(viewDimensions));

    texCoord = vertexTexCoord;
    fragColor = vertexColor;
    gl_Position = vec4(ndcPos, 0.0, 1.0);
}

片段着色器:

#version 120

uniform sampler2D texSampler;

varying vec2 texCoord;
varying vec4 fragColor;

void main(void)
{
    vec4 objectColor = texture2D(texSampler, texCoord);
    gl_FragColor = vec4((objectColor * fragColor).xyz, 1.0);
}

1 个答案:

答案 0 :(得分:1)

所以,我终于明白了。 jozxyqk对测试纹理坐标的建议证实了我怀疑纹理坐标是否关闭(每个顶点都得到相同的坐标)。最后问题是我在代码的另一部分调用glVertexAttribDivisor(attributeLoc, 1)并且从不将其设置回每个顶点,因此它影响了我的其他着色器。考虑到OpenGL的设计,这是必要的。

很高兴已经解决了!