Search code examples
c++openglalpha

openGL single channel texture always samples 0


I'm trying to render font on screen using freetype. For that, I implemented single channel texture but it always samples 0.

cpp

unsigned char *blankData = new unsigned char[8 * 8];
for(int i = 0; i < 8 * 8; i++){
    blankData[i] = 126;
}
    
glActiveTexture(GL_TEXTURE0);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

GLuint m_texture2D;
glGenTextures(1, &m_texture2D);
glBindTexture(GL_TEXTURE_2D, m_texture2D);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 8, 8, 0, GL_RED, GL_UNSIGNED_BYTE, blankData);

glUniform1i(glGetUniformLocation(m_program, "texture2D"), 0);

checkGLError();

glGetError() returns GL_NO_ERROR

fragment Shader

#version 330 core

in vec2 texCoord;
in vec4 color;

uniform sampler2D texture2D;
out vec4 outColor;


void main()
{
    vec4 texColor = texture(texture2D, texCoord);
    outColor = vec4(texColor.r, texColor.r, texColor.r, 1.0f);

}

result enter image description here

ff00ff: clear color

black: fragment shader output texcoord bottomLeft(0, 0) to topRight(1, 1)


Solution

  • If you are not generating mipmaps (with glGenerateMipmap) it is important to set GL_TEXTURE_MIN_FILTER. Since the default filter is GL_NEAREST_MIPMAP_LINEAR, the texture would be "Mipmap Incomplete" if you do not change the minimize function to GL_NEAREST or GL_LINEAR.

    glBindTexture(GL_TEXTURE_2D, m_texture2D);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);