Jump to content
  • Advertisement
Sign in to follow this  
stringa

Messed Up Lighting

This topic is 3619 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!