sjm
sjm

Reputation: 37

What color is obtained from lighting and color materials in OpenGL?

Hi i have one question.

I'm trying to understand lighting and material in OpenGL.

but I do not know how the lighting affects the color and reflection of an object.

Here's the light formula I've found:

result Light = ambient + diffuse * (intensity) + specular

and here's an example usage:

ambient = 64,64,64

diffuse = 192,192,192

specular = 32,32,32 

intentisy = 0.5 

Light = 64 + 192*0.5+ 32 = 192

Result Light = (192,192,192)

Here's how it comes together to form the final output:

Object Color = (Or,Og,Ob)

Material reflect = (Mr,Mg,Mb)

Real Color = (Or * Mr , Og * Mg ,Ob * Mb )

For my question,

I do not know how the "result light" affects the "real color"? More specifically: How the final pixel output comes to fruition using all the light, material and object color inputs.

Upvotes: 0

Views: 1825

Answers (1)

namar0x0309
namar0x0309

Reputation: 2349

A little ambiguous because real can range from physical based rendering techniques to differed shading with various custom lighting shaders.

Depending on the "Material" we'll have a different equation which uses "Result Light".

Here are some basic materials:

enter image description here

Ambient Lighting:

float ambientStrength = 0.1f;
vec3 ambient = ambientStrength * lightColor;

vec3 result = ambient * objectColor;
color = vec4(result, 1.0f);

Diffuse Lighting:

in vec3 FragPos;  // input
vec3 norm = normalize(Normal);

vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);

vec3 diffuse = diff * lightColor;
vec3 result = (ambient + diffuse) * objectColor;
color = vec4(result, 1.0f);

Specular Lighting:

float specularStrength = 0.5f;
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm); 


float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * lightColor; 

vec3 result = (ambient + diffuse + specular) * objectColor;
color = vec4(result, 1.0f);

Combined Phong:

Recycle code from above.

Source

Upvotes: 3

Related Questions