I want to load an image (jpg and png) with OpenCV as OpenGL Texture.
Here is how I load the image to OpenGL:
glEnable(GL_TEXTURE_2D);
textureData = loadTextureData("textures/trashbin.png");
cv::Mat image = cv::imread("textures/trashbin.png");
if(image.empty()){
std::cout << "image empty" << std::endl;
}else{
glGenTextures( 1, &textureTrash );
glBindTexture( GL_TEXTURE_2D, textureTrash );
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S , GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexImage2D(GL_TEXTURE_2D,0,3,image.cols, image.rows,0,GL_RGB,GL_UNSIGNED_BYTE, image.data);
}
The image is loaded, as "image.empty" always returns false
Here is how I render the scene using the created texture:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureTrash);
glm_ModelViewMatrix.top() = glm::translate(glm_ModelViewMatrix.top(),0.0f,-13.0f,-10.0f);
glUniformMatrix4fv(uniformLocations["modelview"], 1, false, glm::value_ptr(glm_ModelViewMatrix.top()));
std::cout << "textureShaderID: " << glGetUniformLocation(shaderProgram,"texture") << std::endl;
glUniform1i(glGetUniformLocation(shaderProgram,"texture"), 0);
objLoader->getMeshObj("trashbin")->render();
And finally the fragmentShader where I want to apply the texture to my geometry
#version 330
in vec2 tCoord;
// texture //
// TODO: set up a texture uniform //
uniform sampler2D texture;
// this defines the fragment output //
out vec4 color;
void main() {
// TODO: get the texel value from your texture at the position of the passed texture coordinate //
color = texture2D(texture, tCoord);
}
The texture coordinates are comeing from a Vertex Buffer Object and are correctly set from the .obj file. Also I can see the Object in my scene when I set the color to e.g. red in the fragment shader, or to vec4(tCoord,0,1); then the object is shaded in different color.
Unfortunately the screen stays black when I want to apply the texture... Can someone help me and tell me why is stays black?