2013-11-25 82 views
1

我一直盯着這段代碼一段時間沒有運氣。我正在將librocket集成到我自己的項目中(這個庫對於這個問題並不重要),其中一部分需要編寫一個渲染器類。我一直在試圖做到這一點,但無法獲得紋理顯示。頂點顏色和位置正常工作。讓紋理在OpenGL 3.2中工作

我正在使用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); 
} 
+2

代碼的紋理部分對我來說看起來很好。你如何生成/加載'紋理'本身?你嘗試過'gl_FragColor = vec4(texCoord,0,1);'檢查座標是否正確? – jozxyqk

回答

1

所以,我終於想通了。 jozxyqk的測試紋理座標的建議證實了我懷疑紋理座標是關閉的(每個頂點都獲得相同的座標)。問題最終是因爲我在代碼的另一部分調用了glVertexAttribDivisor(attributeLoc, 1),並且從不將它設置回每個頂點,所以它影響了我的其他着色器。考慮到OpenGL的設計,這很有必要。

很高興認定!

+0

僅當GL版本爲3.3或更高版本時,glVertexAttribDivisor纔可用。 – Justin