1

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)

Visualization of the problem 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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Valvy
  • 42
  • 1
  • 6
  • Have you tried commenting out glFrontFace? –  Jul 26 '16 at 10:36
  • Firstly, you can redraw a VAO as many times as you want, and/or reuse a shader program as many times as you want per frame. There is no such limitation. Are you clearing the depth buffer every frame? if not they might blend together to form that output. – Harish Jul 26 '16 at 12:04
  • @JohannesW. Yes, I did comment glFrontFace. Problem still persists – Valvy Jul 26 '16 at 12:11
  • @Harish I use glClearBufferfv(GL_DEPTH, 0, depth); Where depth is one before calling all the OpenGL code – Valvy Jul 26 '16 at 12:13
  • @Valvy shouldn't depth be cleared to 0 before each frame is drawn? unless your matrices are set in a way that define 1.0 as far plane depth. – Harish Jul 26 '16 at 12:25
  • @Harish setting the depth to 0 doesn't change anything – Valvy Jul 26 '16 at 12:33
  • @Valvy can you please elaborate how you setup your matrices. Whats your projection matrix zNear and zFar? Alternatively you can try checking if your depth values are as expected by scaling your `color` in your shader by fragment `depth` its `float d = p.z / p.w;` and then `d = 0.5f + (d * 0.5f)`. Multiply your `color` by `d` and see if the intensity increases with depth as expected. If the output is as expected we can rule out any problem with depth values in the output and look at other places such as blending. – Harish Jul 26 '16 at 13:17

0 Answers0