码迷,mamicode.com
首页 > 其他好文 > 详细

光照贴图

时间:2019-10-03 23:53:46      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:mic   xtu   oat   ext   render   uber   struct   pre   技术   

 

  光照贴图一般有漫反射和镜面高光贴图。光照贴图用在片段着色器中,对每个片元计算color时,对于phong光照模型,diffuse和specular材质颜色可以从对应的纹理中采样,这就是光照贴图。

  片段着色器代码如下:

#version 430 core
out vec4 color;

in VS_OUT{
    vec3 FragPos;
    vec3 Normal;
    vec2 TexCoords;
}vs_in;

struct Material{
    sampler2D diffuse;
    sampler2D specular;
    float shininess;
};
struct Light{
    vec3 position;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};
uniform Material material;
uniform Light light;
uniform vec3 viewPos;

void main()
{
    vec3 ambient=light.ambient * vec3(texture(material.diffuse, vs_in.TexCoords));

    vec3 norm=normalize(vs_in.Normal);
    vec3 lightDir=normalize(light.position-vs_in.FragPos);
    float diff=max(dot(norm,lightDir),0.0f);
    vec3 diffuse=light.diffuse * diff * vec3(texture(material.diffuse, vs_in.TexCoords));

    vec3 viewDir=normalize(viewPos-vs_in.FragPos);
    vec3 reflectDir=reflect(-lightDir,norm);
    float spec=pow(max(dot(viewDir,reflectDir),0.0f),material.shininess);
    vec3 specular=light.specular * spec * vec3(texture(material.specular, vs_in.TexCoords));

    color = vec4(ambient+diffuse+specular, 1.0f);
}

 

  渲染代码如下(包含设置光照贴图):

void SceneRendering::CubeRendering() {
    //update uniform buffer
    cube->setViewMat(phc->getViewMatrix());
    cube->setProjectionMat(phc->getProjectionMatrix());
    shader_cube->use();
    shader_cube->setMat4("model", glm::scale(glm::mat4(1),glm::vec3(10)));

    shader_cube->setInt("material.diffuse", 0);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, container);
    shader_cube->setInt("material.specular", 1);
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, container_specular);
    shader_cube->setFloat("material.shininess", 500);

    shader_cube->setVec3("light.ambient", 0.2f, 0.2f, 0.2f);
    shader_cube->setVec3("light.diffuse", 0.5f, 0.5f, 0.5f);
    shader_cube->setVec3("light.specular", 1.0f, 1.0f, 1.0f);
    shader_cube->setVec3("light.position", 0.0f, 0.0f, 50.0f);

    shader_cube->setVec3("viewPos", phc->getPos());

    // draw cubes
    cube->DrawCube();
}

 

  效果图:

  技术图片

  

光照贴图

标签:mic   xtu   oat   ext   render   uber   struct   pre   技术   

原文地址:https://www.cnblogs.com/chen9510/p/11621010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!