c++openglstb-image

OpenGL shows black texture on quad


No matter what i try , i cant seem to make the quad show any textures, tried with solid color in the fragshader works fine, tried drawing the gradient tex coords worked fine .. as soon as i introduced a sampler and tried setting the texture it wasnt working... attached some code and debugging details below enter image description here


int width, height, nrChannels;
bool hasAlpha;
unsigned int refID;

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

// set the texture wrapping/filtering options (on the currently bound texture object)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


unsigned char* data = stbi_load("C:\\Users\\shubhz\\source\\repos\\Lumigen\\assets\\textures\\dia.png", & width, & height, & nrChannels, 0);

bool has_alpha = (nrChannels > 3) ? true : false;

if (data) {
    if (has_alpha)
        glTexImage2D(refID, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    else
        glTexImage2D(refID, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

    Debug::log("Loaded Texture from :", " w: ", width, " h:", height, " alpha : ", has_alpha, "  refID ", refID);
}
else {
    //Failed to load texture
    Debug::log("Failed to load texture : ");
}

//frees up the tempdata after upload
stbi_image_free(data);

app.initialize();

glUseProgram(Resources::GetShader("default-shader"));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, refID);
glUniform1i(glGetUniformLocation(Resources::GetShader("default-shader"), "texture1"), 0);


enter image description here

Drawing part:

void SceneRenderer::render(Mesh& _mesh, Material& _mat, glm::mat4 _modelMatrix)
{
    glUseProgram(_mat.shader);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, _mat.texture.refID);
    glBindVertexArray(_mesh.VAO);

    Shader::Mat4f(_mat.shader, "modelMatrix", _modelMatrix);
    Shader::Vec4f(_mat.shader, "_MainColor", glm::vec4(0.2f, 0.3f, 0.5f, 1));

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

}

Opened the application with render doc too, there was this weird error in the pipeline viewer showing invalid dimension in Base_level 0, but i logged the width and height they were fine

enter image description here


Solution

  • The first argument to glTexImage2D() is a texture target not the texture handle (in your case refID). So you need to replace refID with GL_TEXTURE_2D:

    if (has_alpha)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    else
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    

    This has most likely also produced errors, so this suggests that you're also not checking glGetError(). You should at the very least check glGetError() (see OpenGL Wiki).

    Even better setup Debug Output.

    Check out LearnOpenGL - Debugging for more in-depth information about how to debug OpenGL.