【LearnOpenGL学习笔记】——09.基础光照

1.了解冯氏光照模型

(1)环境光照(ambient)
(2)漫反射光照(diffuse)
(3)镜面光照(specular)

2.法向量

(1)法向量的作用:计算漫反射过程中,Lambert公式中用到了单位法向量
(2)法线矩阵:用于将法向量从模型空间转换到世界空间——解释参考

法向量的空间转换最好在CPU上进行,再通过uniform传递给着色器

3.镜面光照(高光)

(1)Phong模型的使用

4.练习

基础代码

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include <learnopengl/shader_m.h>
#include <learnopengl/camera.h>

#include <iostream>

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow* window);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

// camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;

// timing
float deltaTime = 0.0f;
float lastFrame = 0.0f;

// lighting
glm::vec3 lightPos(2.2f, 1.0f, 2.0f);

//ShaderFilePath
const char* colorVPath = "colorV.vs";
const char* colorFPath = "colorF.fs";
const char* lightVPath = "lightV.vs";
const char* lightFPath = "lightF.fs";
const glm::vec4 fragColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);

//GLSL(OpenGL Shader Language)
const char* colorV =
"#version 330 core\n" //OpenGL版本与模式设置
"layout (location = 0) in vec3 aPos;\n" //定义顶点属性的位置值
"layout (location = 1) in vec3 aNormal;\n" //定义顶点属性的法线值
"out vec3 Normal;\n"
"out vec3 FragPos;\n"

"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"

"void main()\n"
"{\n"
"FragPos = vec3(model* vec4(aPos,1.0));\n"
"gl_Position = projection * view * model * vec4(aPos, 1.0);\n" //进行顶点变换后设置位置
"Normal = mat3(transpose(inverse(model))) * aNormal;\n"
"}\0";

//Fragment GLSL
const char* colorF =
"#version 330 core\n"
"out vec4 FragColor;\n"//传出的颜色值

"in vec3 Normal;\n"
"in vec3 FragPos;\n"

"uniform vec3 objectColor;\n"
"uniform vec3 lightColor;\n"
"uniform vec3 lightPos;\n"
"uniform vec3 viewPos;\n"

"void main()\n"
"{\n"
"   float ambientStrength = 0.1;\n"
"   vec3 ambient = ambientStrength * lightColor;\n"
"   FragColor = vec4(ambient*objectColor, 1.0f);\n"

//diffuse Lambert
"   vec3 norm = normalize(Normal);\n"
"   vec3 lightDir = normalize(lightPos - FragPos);\n"
"   float diff = max(dot(norm,lightDir),0.0);\n"
"   vec3 diffuse = diff * lightColor;\n"

//specular
"   float specularStrength = 0.5;\n"
"   vec3 viewDir = normalize(viewPos - FragPos);\n"//观察方向
"   vec3 reflectDir = reflect(-lightDir,norm);"
"   float spec = pow(max(dot(viewDir,reflectDir),0.0),32);\n"//phong公式
"   vec3 specular = specularStrength * spec * lightColor;"

"   vec3 result = (ambient + diffuse + specular) * objectColor;\n"
"   FragColor = vec4(result, 1.0);\n"
"}\0";



//GLSL(OpenGL Shader Language)
const char* lightV =
"#version 330 core\n" //OpenGL版本与模式设置
"layout (location = 0) in vec3 aPos;\n" //定义顶点属性的位置值

"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"


"void main()\n"
"{\n"
"gl_Position = projection * view * model * vec4(aPos, 1.0);\n" //进行顶点变换后设置位置
"}\0";


//Fragment GLSL
const char* lightF =
"#version 330 core\n"
"out vec4 FragColor;\n"//传出的颜色值

"void main()\n"
"{\n"
"   FragColor = vec4(1.0f);\n"
"}\0";

int main()
{
   
    // glfw: initialize and configure
    // ------------------------------
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

    // glfw window creation
    // --------------------
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL10", NULL, NULL);
    if (window == NULL)
    {
   
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    glfwSetCursorPosCallback(window, mouse_callback);
    glfwSetScrollCallback(window, scroll_callback);

    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值