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);
}