2013-11-25 4 views
1

나는이 코드를 지금 당장 쳐다보고 있습니다. 나는 자신의 프로젝트에 librocket을 통합하는 작업을하고있다. (라이브러리는 그다지 중요하지 않다.) 그 중 일부는 렌더러 클래스를 작성해야한다. 나는 그저 해보려고했지만 텍스처를 표시 할 수는 없습니다. 정점의 색과 위치가 잘 동작합니다.OpenGL 3.2에서 작동하는 텍스처 얻기

OpenGL3.2를 사용하고 있습니다.

단일 쿼드를 그리기 위해 코드를 임시로 수정했습니다. 사용되는 유일한 매개 변수는 GLuint이며 다른 유형으로 캐스팅 된 texture 매개 변수입니다.

내가 바보 같은 것을 놓치고있는 좋은 기회가 있지만, 나는 그것을 볼 수 없다. 다행히 눈의 또 다른 세트가 도움이 될 것입니다. 더 많은 코드/정보를 요청하십시오.

// 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

코드의 질감 부분이 나에게 잘 보입니다. 어떻게'texture' 자체를 생성 /로드합니까? 'gl_FragColor = vec4 (texCoord, 0,1); 시도 했습니까?'좌표가 괜찮은지 확인 하시겠습니까? – jozxyqk

답변

1

그래서, 나는 마침내 그것을 알아 냈다. jozxyqk의 텍스쳐 좌표 테스트에 대한 조언은 텍스쳐 좌표가 꺼져 있다는 의심을 확인시켜주었습니다 (모든 버텍스는 같은 좌표를 얻고있었습니다). 문제는 내 코드의 다른 부분에서 glVertexAttribDivisor(attributeLoc, 1)을 호출하고 버텍스별로 다시 설정하지 않기 때문에 다른 셰이더에 영향을주었습니다. OpenGL의 디자인에 대해 생각해 보면, 이것이 필요할 것이라고 생각합니다.

다행입니다!

+0

GL 버전이 3.3 이상인 경우에만 glVertexAttribDivisor를 사용할 수 있습니다. – Justin