title: opengl transparent mesh
body: <p>I'm rendering a cube, with pure white material, and a grid as a reference floor, using GLSL. For some reason sometimes ( about 1 in 10 times ) the cube is rendered around 50% transparency. I initially thought it to be a Depth buffer issue ( disabled depth buffer, for example ), but i can't find anything about it. There's only one place in the code that touches the depth buffer, and it's initializing it.
  I've even forced the GLSL shader to have a fixed value of 1 for Alpha, but still no-go.</p>
  
  <p>Here are some screenshots i took to illustrate the problem:</p>
  
  <p><a href="https://imgur.com/EEHHEow" rel="nofollow noreferrer"><img src="https://i.imgur.com/EEHHEow.png" title="source: imgur.com" /></a></p>
  
  <p><a href="https://imgur.com/jx3Hzxk" rel="nofollow noreferrer"><img src="https://i.imgur.com/jx3Hzxk.png" title="source: imgur.com" /></a></p>
  
  <p><a href="https://imgur.com/BLVCcby" rel="nofollow noreferrer"><img src="https://i.imgur.com/BLVCcby.png" title="source: imgur.com" /></a></p>
  
  <p>And here's the shader code:</p>
  
  <p>Lighting.vs</p>
  
  <pre><code>#version 300 es
  
  in vec3 VertexPosition;
  uniform mat4 MVP;
  in vec2 TexCoord;
  out vec2 TexCoord0;
  
  void main()
      {
      gl_Position = MVP * vec4(VertexPosition, 1.0);
      TexCoord0 = TexCoord;
      }
  </code></pre>
  
  <p>Lighting.fs</p>
  
  <pre><code>#version 300 es
  
  precision lowp float;
  in vec2 TexCoord0;
  out vec4 FragColor;
  uniform sampler2D TextureSampler;
  
  uniform struct
      {
      bool Textured;
      bool Litable;
      }Flags;
  
  uniform struct
      {
      vec4 Diffuse;
      }Material;
  
  uniform struct
      {
      vec3 Color;
      float Intensity;
      }AmbientLight;
  
  vec4 ApplyTexturing ( vec4 Input )
      {
      Input *= texture(TextureSampler, TexCoord0);
      return Input;
      }
  
  vec4 ApplyLighting ( vec4 Input )
      {
      Input *= vec4(AmbientLight.Color * AmbientLight.Intensity, 1.0f );
      return Input;
      }
  
  void main()
      {
      vec4 TempFragColor = vec4 ( Material.Diffuse.xyz, 1.0f );
      if ( Flags.Textured )
      TempFragColor = ApplyTexturing( TempFragColor );
  
      if ( Flags.Litable )
      TempFragColor = ApplyLighting ( TempFragColor );
  
      FragColor = vec4(TempFragColor.xyz, 1.0f);
      }
  </code></pre>
  
  <p>This is the only place in the code that touches the depth buffer:</p>
  
  <pre><code>glEnable ( GL_CULL_FACE );
  glCullFace ( GL_BACK );
  glFrontFace ( GL_CCW );
  glEnable ( GL_DEPTH_TEST );
  glDepthMask ( GL_TRUE );
  glDepthFunc ( GL_LEQUAL );
  glDepthRange ( 1, 100 );
  </code></pre>
  
  <p>Now, as i write this, i'm wondering if i should use glClearDepth() to set a default value? I assumed the driver would use a decent default value, so i didn't specify.</p>
  
  <p>EDIT: Ok, so i changed the code to use glClearDepth. No change. This definitely looks like a transparency issue, though. Take a look at this shot, with a textured cube.</p>
  
  <p><a href="https://imgur.com/gQv6UCC" rel="nofollow noreferrer"><img src="https://i.imgur.com/gQv6UCC.png" title="source: imgur.com" /></a></p>
  
  <pre><code>bool CGLWindow::StartFrame ( void )
  {
      glfwMakeContextCurrent ( MyGLFWWindow );
      glfwPollEvents();
      glEnable ( GL_CULL_FACE );
      glCullFace ( GL_BACK );
      glFrontFace ( GL_CCW );
      glEnable ( GL_DEPTH_TEST );
      glDepthMask ( GL_TRUE );
      glDepthFunc ( GL_LEQUAL );
      glDepthRange ( 1, 100 );
      glClearColor ( 0, 0, 0, 1.0f );
      glClearDepth ( 1.0f );
  
      Clear();
      glfwGetCursorPos ( MyGLFWWindow, &amp; ( MouseCursor.CursorPos.X ), &amp; ( MouseCursor.CursorPos.Y ) );
      return glfwWindowShouldClose ( MyGLFWWindow );
  }
  
  bool CGLWindow::Clear ( void )
  {
      glViewport ( 0, 0, Size.X, Size.Y );
      glScissor ( 0, 0, Size.X, Size.Y );
      glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
      return glCheckError();
  }
  </code></pre>
  
  <p>EDIT:
  Ok, some breakthrough.
  I switched to a simple fragment shader:</p>
  
  <pre><code>#version 300 es
  precision lowp float;
  
  out vec4 FragColor;
  in vec4 Color;
  
  void main()
      {
      FragColor.r = FragColor.g = FragColor.b = FragColor.a = 1.0f;
      }
  </code></pre>
  
  <p>This isn't deterministic, but like i said, every once in a while, i'd get a transparent cube. And now, it stopped doing it. Now all is ok.</p>
  
  <p>So, i started cutting down my fragment shader. This is the minimum i got, with it failing with a transparent cube.</p>
  
  <pre><code>#version 300 es
  
  precision lowp float;
  in vec2 TexCoord0;
  out vec4 FragColor;
  uniform sampler2D TextureSampler;
  
  uniform struct
      {
      vec4 Diffuse;
      }Material;
  
  void main()
      {
      vec4 TempFragColor = vec4 ( Material.Diffuse.xyz, 1.0f );
  
      FragColor = vec4(TempFragColor.xyz, 1.0f);
      }
  </code></pre>
  
  <p>What can i possibly be doing wrong here?
  It definitely seems to be the shader.</p>