Sign in to follow this  
stringa

Messed Up Lighting

Recommended Posts

At least my point lights seem to be view dependant. I'm wondering what I'm doing wrong in this code. Where is the inverse view matrix actually multiplied... glEnable? or glLightfv( gl_POSITION )
void DeferredLightingPasses()
{  

  // ---- Let's bind a FBO and Render to that Instead


  glEnable( GL_LIGHTING );
  glEnable( GL_LIGHT0 );

  glEnable( GL_BLEND );
  glBlendFunc( GL_ONE, GL_ONE );
  glDisable( GL_DEPTH_FUNC );
  
  lightingBuffer.Enable();

  glClearColor( 0.0, 0.0, 0.0, 0.0 );
  glClear( GL_COLOR_BUFFER_BIT );

  glMatrixMode( GL_MODELVIEW );
  glLoadIdentity();


  // --- Set the Textures
  // --- Set the Position Texture

  Enable2DTexture( gBuffer.GetGLTextureID(0), 0 );
  Enable2DTexture( gBuffer.GetGLTextureID(1), 1 );
  Enable2DTexture( gBuffer.GetGLTextureID(2), 2 );

  // --------- We need to run through the Lights at this point
  std::vector<GRAPHICS::Light>::iterator begin = LIGHT_MANAGER.GetLights().begin();
  std::vector<GRAPHICS::Light>::iterator end   = LIGHT_MANAGER.GetLights().end();
  
  GLSL_SHADERS.SetCurrentShader( GLSL_PROGRAMS::ST_DS_LIGHTING );

  GLSL_SHADERS.SetUniformSampler( GLSL_PROGRAMS::ST_DS_LIGHTING, "color_tex", 0 );
  GLSL_SHADERS.SetUniformSampler( GLSL_PROGRAMS::ST_DS_LIGHTING, "position_tex", 1 );
  GLSL_SHADERS.SetUniformSampler( GLSL_PROGRAMS::ST_DS_LIGHTING, "normal_tex", 2 );
  
    

  while( begin != end )
  {

    glLoadMatrixf(current_camera->GetCameraMatrix() );

    // change to a switch statement soon
    // ---- Point Light Setup Below
    
    switch( (*begin).GetLightType() )
    {
    case GRAPHICS::LT_POINT_LIGHT:

      GLSL_SHADERS.SetUniform1i( GLSL_PROGRAMS::ST_DS_LIGHTING, "light_type", 1 );

      glLightfv( GL_LIGHT0, GL_POSITION,              (*begin).GetLightPos().vec_array );
      glLightfv( GL_LIGHT0, GL_DIFFUSE,               (*begin).GetDiffuse().vec_array );
      glLightfv( GL_LIGHT0, GL_SPECULAR,              (*begin).GetSpecular().vec_array );
      glLightf(  GL_LIGHT0, GL_LINEAR_ATTENUATION,    (*begin).GetLinearAttn() );
      glLightf(  GL_LIGHT0, GL_CONSTANT_ATTENUATION,  (*begin).GetConstAttn() );
      glLightf(  GL_LIGHT0, GL_QUADRATIC_ATTENUATION, (*begin).GetQuadAttn() );


      break;

    case GRAPHICS::LT_DIR_LIGHT:
      {

        GLSL_SHADERS.SetUniform1i( GLSL_PROGRAMS::ST_DS_LIGHTING, "light_type", 0 );

        vec4 pos = - (*begin).GetLightPos();

        glLightfv( GL_LIGHT0, GL_POSITION, pos.vec_array );
        glLightfv( GL_LIGHT0, GL_DIFFUSE,  (*begin).GetDiffuse().vec_array );
        glLightfv( GL_LIGHT0, GL_SPECULAR, (*begin).GetSpecular().vec_array );

      }

      break;

    case GRAPHICS::LT_SPOT_LIGHT:

      GLSL_SHADERS.SetUniform1i( GLSL_PROGRAMS::ST_DS_LIGHTING, "light_type", 2 );

      glLightfv( GL_LIGHT0, GL_POSITION,              (*begin).GetLightPos().vec_array );
      glLightfv( GL_LIGHT0, GL_DIFFUSE,               (*begin).GetDiffuse().vec_array );
      glLightfv( GL_LIGHT0, GL_SPECULAR,              (*begin).GetSpecular().vec_array );
      glLightf(  GL_LIGHT0, GL_LINEAR_ATTENUATION,    (*begin).GetLinearAttn() );
      glLightf(  GL_LIGHT0, GL_CONSTANT_ATTENUATION,  (*begin).GetConstAttn() );
      glLightf(  GL_LIGHT0, GL_QUADRATIC_ATTENUATION, (*begin).GetQuadAttn() );
      glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION,        (*begin).GetSpotLightDirection().vec_array );
      glLightf(  GL_LIGHT0, GL_SPOT_EXPONENT,         (*begin).GetSpotLightExponent() );
      glLightf(  GL_LIGHT0, GL_SPOT_CUTOFF,           (*begin).GetSpotLightCutoff() );

      break;

    } // End Switch

    // Clear the Camera matrix for when we draw the quad
    
    glLoadIdentity();

    DrawScreenSpaceQuad();

    ++begin;

  }
  
  glDisable( GL_LIGHT0 );
  glDisable( GL_LIGHTING );

  Disable2DTexture(0);
  Disable2DTexture(1);
  Disable2DTexture(2);

  lightingBuffer.Disable();

  glDisable( GL_BLEND );
  glEnable( GL_DEPTH_FUNC );

}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this