0
\$\begingroup\$

The Problem

I am attempting to load and display an image in OpenGL using the SOIL library. I originally started with lodePNG but I had difficulties getting the image to display so I assumed it was just a library issue and decided to switch to SOIL, however, as I am having the same issue now I believe it must be something that I am doing wrong somewhere in my own code.

So, the exact issue is that I seem to be able to load a texture correctly: the width and height attributes get set to the appropriate value, and if I just dump the data it loads to console it seems to have actually grabbed the appropriate data (Screenshots later on). The problem comes when I try to render it, I believe my shader and loading code to be correct, however I only get a black square upon rendering. I have been working on this for a couple of days now, trying lots of iterations and have even seperated out all of my framework code and have a completely barren example now that just loads up OpenGL and needed libraries and tries to render.

Imgur

I am seriously at a loss as to what I am doing wrong, I am using GL_TEXTURE1 as my active texture but I originally started with GL_TEXTURE0 so I don't think that would be the problem.
Beyond that I can't think of anything that I am glaringly doing wrong; console output from Bitmap.cpp DOES output correct data (the image to display is 64x64) and when I dump the char* it outputs a large amount of data that was loaded into it.

EDIT

I managed to fix it, I honestly can't believe I missed this but when I switched from lodePNG to SOIL I never added the 4 lines that set the attributes of the images. Adding these lines fixed everything right up and made it render

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

Bitmap.cpp

GLuint BitmapLoader::Load(std::string path) {
    glEnable(GL_TEXTURE_2D);

    GLuint texture_id;
    glGenTextures(1, &texture_id);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture_id);

    image = SOIL_load_image(path.c_str(), &width, &height, 0, SOIL_LOAD_RGBA);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);

    std::cout << "Width: " << width << ", Height: " << height << std::endl;
    std::cout << "Image ID: " << texture_id << std::endl;
    std::cout << "Image Data: " << image << std::endl;

    SOIL_free_image_data(image);

    if (texture_id == -1) {
        return -1;
    }

    return texture_id;
}
\$\endgroup\$

closed as off-topic by Kromster, bummzack, Seth Battin, Anko, MichaelHouse Oct 8 '14 at 22:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Kromster, bummzack, Seth Battin, Anko, MichaelHouse
If this question can be reworded to fit the rules in the help center, please edit the question.

3
\$\begingroup\$

I'm not sure if I should just delete the question since I solved it on my own or if I should leave it up for others to see in case they have the same problem. For now I am going to add my edit to this as an answer but if I should just delete the entire question let me know! I'm relatively new to Stack Exchange and am not really sure.

The problem with the texture loading was that I was not setting the attributes of the image upon load, setting these 4 parameters seemed to fix everything up and now causes it to render correctly.

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

Imgur

Bitmap.cpp (Full Version)

GLuint BitmapLoader::Load(std::string path) {
    glEnable(GL_TEXTURE_2D);

    GLuint texture_id;
    glGenTextures(1, &texture_id);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture_id);

    image = SOIL_load_image(path.c_str(), &width, &height, 0, SOIL_LOAD_RGBA);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);

    std::cout << "Width: " << width << ", Height: " << height << std::endl;
    std::cout << "Image ID: " << texture_id << std::endl;
    std::cout << "Image Data: " << image << std::endl;

    SOIL_free_image_data(image);

    if (texture_id == -1) {
        return -1;
    }

    return texture_id;
}
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.