Aquacolor

Aquacolor



LearningOpenGL【11】

Gumdrop · 2025-09-05 · 11浏览 · 未分类



lighting中使用光源和材质概念的最终形式

为了在lighting中使用纹理,应该使用纹理的颜色对光源颜色滤波,所以:

struct Material{
    sampler2D diffuse;
    sampler2D specular;
    float shininess;
};
struct Light{
    vec3 position;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};
//或者在Light中使用vec3 lightColor和vec3 LightStrength。

// 光照计算
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(light.position - FragPos);
vec3 viewDir = normalize(viewPos - FragPos);
// Ambient
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoord));
// Diffuse
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoord));
// Specular
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = vec3(texture(material.specular, TexCoord)) * spec * light.specular;
// Combine
FragColor = vec4(ambient + diffuse + specular, 1.0);

texture map的实际作用

当我们单纯使用纹理时,形式是:

FragColor = texture(tex, TexCoord);

很明显这样做不能体现光源的影响。

对于同一图元(primitive)的片段(fragment),它们在同一个纹理上采样。

当同一图元上所有片段具有一样的材质时,其实diffuse map和specular map可以统一。

当同一图元上具有不同材质的片段时,为了表现材质的区别,片段需要从不同texture map上采样。

所以需要传入多个texture用作map,对光源进行滤波。

(不需要ambient map的原因是很少出现diffuse和map表现不一的情况,但是高光不同表现是很常见的)

2025-09-05T13:36:21.png 2025-09-05T13:36:32.png



©

comment 评论区

添加新评论

face表情



  • ©2026 bilibili.com

textsms
内容不能为空
昵称不能为空
email
邮件地址格式错误
web
beach_access
验证码不能为空
keyboard发表评论


star_outline 咱快来抢个沙发吧!




©2026 Aquacolor

Theme Romanticism2.2 by Akashi
Powered by Typecho