3

I am trying to figure out how to scale a color with the lighting illumination using the phong model. For example given I = KaAx, where ka is the ambient coefficient and Ax is the ambient lighting intensity where x can be r b or g, I want to apply that to a surface with a texture color of (1,0,1) for example. I tried multiplying the individual rgb values by I, (r*Ka*Ar.r,r*Kb*Ag,r*Kg*Ab) the illumination but alas, it can completely change the color which is not what I want.

1 Answer 1

0

OK I see few things:

  • ambient and diffuse lights should not be multiplied together use addition instead
  • you do not use any normal vector or light direction/position (at least I cant find it anywhere)
  • also you should use glColor parameter (unless you do not use it at all)

Try this for each color(.r,.g,.b) and directional light like Sun:

pixel_color.r=clamp_to_1                                    // clamp to <0.0,1.1>
 (
 texture_color.r*glColor.r                                  // pixel color without lighting
 *(                                                            // apply lighting
   diffuse.r*((glNormal.xyz*NormalMatrix).light_dir.xyz) // diffuse * dot product of light source direction and normal vectors. If you need also consider distance just multiply by next term...
  +ambient.r                                                  // ambient light is additive !!!  
  )
 );

PS.

  • NormalMatrix is ModelView with origin set to [0.0,0.0,0.0] (no position shift)
  • If you want also add reflectance then do not forget to compute reflected normal ... it is different (unless the skybox is infinite in size) as used for diffuse light. Also cubemap with enviroment skybox helps a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.