2016-04-12 20 views
1

Bir bitmap dokusu kullanarak bir 2D etiket oluşturmaya çalışırken, doku örneklemim siyah renk alır. RenderDoc kullanarak hata ayıklamayı denedim, ancak sorunu bulamıyorum. Doku yükleri iyi görünüyor ve doğru kayıtta saklanıyor, ancak yine de siyah renkleniyor.OpenGL siyah dokular

Doku koordinatlarını kontrol etmek için tam kırmızı bir doku kullanmayı denedim, ancak doku hala siyah olarak görünüyordu. İşte

Pipeline texture Texture output output

Ben doku oluşturma/yükleme için kullanmak koddur. Bir etiket oluşturmaya çalıştı. (genMipMaps yanlıştır).

void Texture::CreateGLTextureWithData(GLubyte* data, bool genMipMaps) { 
    if (bitmap) 
     glDeleteTextures(1, &bitmap); 

    glGenTextures(1, &bitmap); 
    glBindTexture(GL_TEXTURE_2D, bitmap); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 

    if (genMipMaps) 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); 
    else 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 

    if (genMipMaps) 
     glGenerateMipmap(GL_TEXTURE_2D); 
} 

Özel sampler:

glGenSamplers(1, &linearSampler); 
glSamplerParameteri(linearSampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
glSamplerParameteri(linearSampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
glSamplerParameteri(linearSampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); 

glSamplerParameteri(linearSampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 
glSamplerParameteri(linearSampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 

glSamplerParameteri(linearSampler, GL_TEXTURE_COMPARE_FUNC, GL_NEVER); 

glSamplerParameterf(linearSampler, GL_TEXTURE_MIN_LOD, 0); 
glSamplerParameterf(linearSampler, GL_TEXTURE_MAX_LOD, GL_TEXTURE_MAX_LOD); 
glBindSampler(0, linearSampler); 

Rendering:

glDisable(GL_DEPTH_TEST); 
glDisable(GL_STENCIL_TEST); 
glEnable(GL_CULL_FACE); 
glCullFace(GL_BACK); 
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 

glActiveTexture(GL_TEXTURE0 + 0); 
glBindTexture(GL_TEXTURE_2D, texture->getBitmap()); 
// Set index and vertex buffers 
GLuint vao; 
glGenVertexArrays(1, &vao); 
glBindVertexArray(vao); 


if (glIsBuffer(vertBuffer) && vertBuffer != lastVertBuffer) 
    glBindBuffer(GL_ARRAY_BUFFER, vertBuffer); 

if (glIsBuffer(indexBuffer) && indexBuffer != lastIndexBuffer) 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); 

shader->updateLayout(); 

// Draw 
const void* firstIndex = reinterpret_cast<const void*>(0); 
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, firstIndex); 

glBindVertexArray(0); 

Shader:

VS:

#version 430 core 

// Vertex atributes 
in vec3 a_position; 
in vec2 a_texture; 
in vec4 a_color; 


// Constant buffers 
layout(std140) uniform VertexBlock 
{ 
    mat4 u_wvp; 
    mat4 u_world; 
}; 

// Vertex shader outputs 
out vec2 v_texture; 
out vec4 v_color; 

void main() 
{ 
    v_texture = a_texture; 
    v_color = a_color; 
    gl_Position = u_wvp * vec4(a_position, 1.0); 
} 

FS: (. Ben doku kullanmadan rengi kırmızıya ayarlama çalıştı ve doğru kırmızı vermektedir)

#version 430 core 

in vec4 v_color; 
in vec2 v_texture; 

layout(binding = 0) uniform sampler2D u_texture; 

out vec4 fragColor; 

void main() 
{ 
    fragColor = v_color * texture(u_texture, v_texture); 
} 

cevap

1

sorunun ne öğrendim. 2D dokular için mipmap'ler üretmiyordum ama örnekleyici hala kullanıyordu.

Doku yükleme:

... 
if (genMipMaps) 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); 
else 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); 

if (genMipMaps) 
    glGenerateMipmap(GL_TEXTURE_2D); 
... 

Sampler: Hep dokular için Eşleşme oluşturmak olduğunu bunu düzeltmek için ne yaptık

... 
glSamplerParameteri(linearSampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 
glSamplerParameteri(linearSampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
...