I am struggling to correctly display my objects in OpenGL. The problem is that the background texture is displayed on top of the foreground texture. (as seen in the picture with the leaves)
I am using OpenGL 4.1 Core on os x el capitan.
My shader of the palmtree:
#version 410 core
//fragment
out vec4 color;
uniform sampler2D text;
in VS_OUT{
vec4 color;
} fs_in;
in vec2 UV;
void main(void){
color = texture(text,UV);
}
//vertex shader
#version 410 core
out VS_OUT{
vec4 color;
} vs_out;
uniform mat4 cam_matrix;
uniform mat4 projection_matrix;
uniform mat4 mv_matrix;
layout(location = 0) in vec4 position;
layout(location = 1) in vec2 vertexUV;
layout(location = 2) in vec3 normals;
out vec2 UV;
void main(void){
vec4 P = projection_matrix * (cam_matrix * mv_matrix) * position;
vs_out.color = position * 2.0 + vec4(0.5, 0.5, 0.5, 0.0);
gl_Position = P;
UV = vertexUV;
}
I placed the VAO in a assetmanager, So multiple objects can use the same VAO and shader. The drawing will called like :
glUseProgram(AssetManager::getProgram(this->program));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,AssetManager::getTexture(this->texture));
glBindVertexArray(AssetManager::getMesh(this->mesh)->getVao());
glUniformMatrix4fv(this->pos_reference, 1, GL_TRUE, &mat.getRawData()[0]);
glEnableVertexAttribArray ( 0 );
glDrawArrays ( GL_TRIANGLES, 0, AssetManager::getMesh(this->mesh)->getSize());
glDisableVertexAttribArray(0);
glBindVertexArray(0);
At the start of the program I call:
glDepthFunc(GL_LEQUAL);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glFrontFace(GL_CW);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Is it possible to use the same VAO and program combination over and over in a single OpenGL context?
If this is the case. does anyone else have suggestions that might solve this problem?