Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with glBlendFunc and GLSL shader

Started by thekie Nov 21, 2009 at 1:40 PM 0 replies 1.6k views
Original Post
thekie
thekie
Hi, i am writing a little deferred shader application/prototype. Just to test this technique. I have a problem, doing a pass for every light.

void renderDiffuseTarget(unsigned int width, unsigned int height)
{
  /* Save current state */
  glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT |  GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT);

  /* Bind FBO, Buffer and Shader */
  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, g_iShadingBuffer);
  glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
  glUseProgram(g_iDiffuseShader[PROGRAM]);

  /* Enable the Blending */
  glEnable(GL_BLEND);
  glBlendFunc(GL_ONE, GL_ONE);

  /* Set Parameter for the firtst light */
  {
    CGVector4D lightPos = {0.0, 0.5, 0.0, 1.0};
    float projection[16];
    CGVector3D newLightPos;
    glGetFloatv(GL_MODELVIEW_MATRIX, projection);
    newLightPos[0] = lightPos[0]*projection[0] + lightPos[1]*projection[4] + lightPos[2]*projection[8] + lightPos[3]*projection[12];
    newLightPos[1] = lightPos[0]*projection[1] + lightPos[1]*projection[5] + lightPos[2]*projection[9] + lightPos[3]*projection[13];
    newLightPos[2] = lightPos[0]*projection[2] + lightPos[1]*projection[6] + lightPos[2]*projection[10] + lightPos[3]*projection[14];
    newLightPos[3] = lightPos[0]*projection[3] + lightPos[1]*projection[7] + lightPos[2]*projection[11] + lightPos[3]*projection[15];

    setUniformShaderVar3f(g_iDiffuseShader[PROGRAM], "lightPos", newLightPos[0], newLightPos[1], newLightPos[2]);
  }

  setUniformShaderVar3f(g_iDiffuseShader[PROGRAM], "lightCol", 1.0, 0.0, 0.0);
  setUniformShaderVar1f(g_iDiffuseShader[PROGRAM], "quad", 1);
  setUniformShaderTex(g_iDiffuseShader[PROGRAM], "positionMap", g_iTextures[POS],0);
  setUniformShaderTex(g_iDiffuseShader[PROGRAM], "normalMap", g_iTextures[NORMALS], 1);
  setUniformShaderTex(g_iDiffuseShader[PROGRAM], "colorMap", g_iTextures[COLOR], 2);


  /* Clear buffer with grey. (Grey = TEST) */
  glClearColor(0.3, 0.3, 0.3, 1.0);
  glClear(GL_COLOR_BUFFER_BIT);

  /* Render first light */
  renderFullscreenRect(width, height);

  /* Clean up first pass */
  glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, 0);
  glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, 0);
  glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, 0);


  /* Set parameter for the second light */
  {
    CGVector4D lightPos = {1.0, 0.5, 0.0, 1.0};
    float projection[16];
    CGVector3D newLightPos;
    glGetFloatv(GL_MODELVIEW_MATRIX, projection);
    newLightPos[0] = lightPos[0]*projection[0] + lightPos[1]*projection[4] + lightPos[2]*projection[8] + lightPos[3]*projection[12];
    newLightPos[1] = lightPos[0]*projection[1] + lightPos[1]*projection[5] + lightPos[2]*projection[9] + lightPos[3]*projection[13];
    newLightPos[2] = lightPos[0]*projection[2] + lightPos[1]*projection[6] + lightPos[2]*projection[10] + lightPos[3]*projection[14];
    newLightPos[3] = lightPos[0]*projection[3] + lightPos[1]*projection[7] + lightPos[2]*projection[11] + lightPos[3]*projection[15];

    setUniformShaderVar3f(g_iDiffuseShader[PROGRAM], "lightPos", newLightPos[0], newLightPos[1], newLightPos[2]);
  }

  setUniformShaderVar3f(g_iDiffuseShader[PROGRAM], "lightCol", 0.0, 1.0, 0.0);
  setUniformShaderVar1f(g_iDiffuseShader[PROGRAM], "quad", 1);
  setUniformShaderTex(g_iDiffuseShader[PROGRAM], "positionMap", g_iTextures[POS],0);
  setUniformShaderTex(g_iDiffuseShader[PROGRAM], "normalMap", g_iTextures[NORMALS], 1);
  setUniformShaderTex(g_iDiffuseShader[PROGRAM], "colorMap", g_iTextures[COLOR], 2);

   /* render second light */
   renderFullscreenRect(width, height);

/* clean up everything */

  glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, 0);
  glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, 0);
  glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, 0);

  glUseProgram(0);
  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  glPopAttrib();
}
The shader for one light is working very well! Here is what I get with blending enabled. http://img502.imageshack.us/img502/7732/res.png Every second row of pixels isnt blended correctly. Has anyone an idea?
vincoof
vincoof
The problem appears when you enable blending ? If you comment the "glEnable(GL_BLEND)" line, everything works well for one light ?

Also, your example shows the rendering of one light. How are added multiple light ? The same framebuffer is overwritten ? Each light is rendered in a different buffer and all buffers are added in a last pass ?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.