0
votes

My problem is that OpenGL 3.3 doesn't draw my texture. I checked if it a problem of my shader (It could not be loaded well) but the shader is OK. Then, I checked if it is a problem with UV coordinates, but I watched that it is OK using this code of Fragment Shader:

#version 330 core

out vec3 outColor;

in DATA {
    vec2 UV;
} fs_in;

void main() {
    outColor = vec3(fs_in.UV.x, fs_in.UV.y, 0.0);
}

Finally, I used this code of Fragment Shader but it doesn't work:

#version 330 core

out vec3 outColor;

uniform sampler2D mySampler;

in DATA {
    vec2 UV;
} fs_in;

void main() {
    outColor = texture(mySampler, fs_in.UV).rgb;
}

The code which loads a BMP image and gives it to OpenGL is the following (I found it on a website):

GLuint loadTexture(const char *lpszTexturePath) {
    unsigned char header[54];
    unsigned int dataPos;
    unsigned int width, height;
    unsigned int imageSize;
    unsigned char * data;

    FILE *file;

    fopen_s(&file, lpszTexturePath, "rb");

    if(!file)
        printf("Image could not be opened\n"); return 0;

    if(fread(header, 1, 54, file) != 54) {
        printf("Not a correct BMP file\n");
        return false;
    }

    if(header[0] != 'B' || header[1] != 'M') {
        printf("Not a correct BMP file\n");
        return 0;
    }

    dataPos = *(int*)&(header[0x0A]);
    imageSize = *(int*)&(header[0x22]);
    width = *(int*)&(header[0x12]);
    height = *(int*)&(header[0x16]);

    if(imageSize == 0)    imageSize = width*height * 3;
    if(dataPos == 0)      dataPos = 54;

    data = new unsigned char[imageSize];
    fread(data, 1, imageSize, file);

    fclose(file);

    GLuint textureID;
    glGenTextures(1, &textureID);

    glBindTexture(GL_TEXTURE_2D, textureID);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    return textureID;
}

Part of the main code: GLuint uTextureID = loadTexture("image.bmp");

while(!glfwWindowShouldClose(lpstWndID)) {
    glfwPollEvents();

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgram(uProgID);

    glBindTexture(GL_TEXTURE_2D, uTextureID);

    object.renderObject();

    glfwSwapBuffers(lpstWndID);
}
1
Did you step through the loading code in a debugger, and convince yourself that it was loading the BMP file successfully? With the expected sizes and everything?Reto Koradi
The image was loaded correctly (I checked it)Seyro97
Also, I tried to create a pixel array: 'const char pixels[] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 }' but it doen't workSeyro97
Do you somewhere tell the shader which texture to use (glUniform1i)?BDL
It is only a texture. I never used what you say :SSeyro97

1 Answers

0
votes

Oh my god. The mistake is very... very... Watch it by youself...

Line 10 of the texture loader:

if(!file)
        printf("Image could not be opened\n"); return 0;

Do anyone see something extrange? YEAH, IT HAS A RETURN 0 OUTSIDE OF THE IF BLOCK... This function was alwais returning 0...

Thanks (I'm stupid xD)