3.5 投光物

投光物

将光投射(Cast)到物体的光源叫做投光物(Light Caster)。

平行光

当我们使用一个假设光源处于无限远处的模型时,它就被称为定向光,因为它的所有光线都有着相同的方向,它与光源的位置是没有关系的。

因为所有的光线都是平行的,所以物体与光源的相对位置是不重要的,因为对场景中每一个物体光的方向都是一致的。由于光的位置向量保持一致,场景中每个物体的光照计算将会是类似的。

点光源

点光源是处于世界中某一个位置的光源,它会朝着所有方向发光,但光线会随着距离逐渐衰减。

随着光线传播距离的增长逐渐削减光的强度通常叫做衰减(Attenuation)。

聚光

聚光是位于环境中某个位置的光源,它只朝一个特定方向而不是所有方向照射光线。这样的结果就是只有在聚光方向的特定半径内的物体才会被照亮,其它的物体都会保持黑暗。

OpenGL中聚光是用一个世界空间位置、一个方向和一个切光角来表示的,切光角指定了聚光的半径。

code

定向光

#include "../env/glm/glm.hpp"
#include "../env/glm/gtc/matrix_transform.hpp"
#include "../env/glm/gtc/type_ptr.hpp"
#include <iostream>

#define STB_IMAGE_IMPLEMENTATION
#include "../env/std_image.h"
#include "../env/glad.h"
#include "../env/glfw3.h"
#include <fstream>
#include "../tools/shader.h"
#include "../tools/camera.h"

#define WIDTH 800
#define HEIGHT 600

GLFWwindow *initialize(int width, int height);

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);

Camera camera = Camera(glm::vec3(0.0f, 0.0f, 3.0f),
                       glm::vec3(0.0f, 0.0f, -1.0f),
                       glm::vec3(0.0f, 1.0f, 0.0f),
                       0.0f, 0.0f);

float cameraSpeed = 2.5f;
float lastTime;
float lastXPos;
float lastYPos;
bool firstMouse;
float fov = 45.0f;

int main()
{
    
    
    GLFWwindow *window = initialize(WIDTH, HEIGHT);
    float vertices[] = {
    
    
        // positions          // normals           // texture coords
        -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
        0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
        0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
        0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
        -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,

        -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
        0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
        0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
        0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
        -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,

        -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
        -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
        -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
        -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,

        0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
        0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
        0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
        0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,

        -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
        0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
        0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
        0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
        -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
        -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,

        -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
        0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
        0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
        0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
        -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
        -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f};

    glm::vec3 cubePositions[] = {
    
    
        glm::vec3(0.0f, 0.0f, 0.0f),
        glm::vec3(2.0f, 5.0f, -15.0f),
        glm::vec3(-1.5f, -2.2f, -2.5f),
        glm::vec3(-3.8f, -2.0f, -12.3f),
        glm::vec3(2.4f, -0.4f, -3.5f),
        glm::vec3(-1.7f, 3.0f, -7.5f),
        glm::vec3(1.3f, -2.0f, -2.5f),
        glm::vec3(1.5f, 2.0f, -2.5f),
        glm::vec3(1.5f, 0.2f, -1.5f),
        glm::vec3(-1.3f, 1.0f, -1.5f)};

    unsigned int myVBO;
    glGenBuffers(1, &myVBO);
    glBindBuffer(GL_ARRAY_BUFFER, myVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    GLuint objectVAO;
    glGenVertexArrays(1, &objectVAO);
    glBindVertexArray(objectVAO);
    glBindBuffer(GL_ARRAY_BUFFER, myVBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3 * sizeof(float)));
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(6 * sizeof(float)));

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);

    GLuint lightVAO;
    glGenVertexArrays(1, &lightVAO);
    glBindVertexArray(lightVAO);
    glBindBuffer(GL_ARRAY_BUFFER, myVBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
    glEnableVertexAttribArray(0);

    int width, height, channel;
    unsigned char *data = stbi_load("../images/container2.png", &width, &height, &channel, 0);

    std::cout << width << " " << height << " " << channel << std::endl;

    GLuint diffuseTexture;
    glGenTextures(1, &diffuseTexture);
    glBindTexture(GL_TEXTURE_2D, diffuseTexture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    glGenerateMipmap(GL_TEXTURE_2D);

    stbi_image_free(data);

    data = stbi_load("../images/container2_specular.png", &width, &height, &channel, 0);
    std::cout << width << " " << height << " " << channel << std::endl;

    GLuint specularTexture;
    glGenTextures(1, &specularTexture);
    glBindTexture(GL_TEXTURE_2D, specularTexture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    glGenerateMipmap(GL_TEXTURE_2D);

    glm::mat4 model, view, projection;
    Shader lampShader("1-vertex.glsl", "1-lamp-fragment.glsl");
    Shader lightShader("4-vertex.glsl", "5-fragment.glsl");

    lastTime = glfwGetTime();
    glEnable(GL_DEPTH_TEST);

    while (!glfwWindowShouldClose(window))
    {
    
    
        model = glm::mat4(1.0f);
        glm::vec3 lightPos = glm::vec3(1.2f, 1.0f, 2.0f);
        model = glm::translate(model, lightPos);
        model = glm::scale(model, glm::vec3(0.2f));

        view = camera.lookAt();
        projection = glm::perspective(glm::radians(fov), 1.0f * WIDTH / HEIGHT, 0.1f, 100.0f);

        lampShader.use();
        lampShader.setm4fv("model", GL_FALSE, glm::value_ptr(model));
        lampShader.setm4fv("view", GL_FALSE, glm::value_ptr(view));
        lampShader.setm4fv("projection", GL_FALSE, glm::value_ptr(projection));
        glBindVertexArray(lightVAO);
        glDrawArrays(GL_TRIANGLES, 0, 36);

        lightShader.use();
        lightShader.setm4fv("view", GL_FALSE, glm::value_ptr(view));
        lightShader.setm4fv("projection", GL_FALSE, glm::value_ptr(projection));

        glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
        lightShader.setv3("lightColor", glm::value_ptr(lightColor));

        glm::vec3 cameraPos = camera.getPosition();
        lightShader.setv3("viewPos", glm::value_ptr(cameraPos));

        glm::vec3 lightDirection = glm::vec3(-0.2f, -1.0f, -0.3f);
        lightShader.setv3("light.direction", glm::value_ptr(lightDirection));

        glm::vec3 ambient = glm::vec3(1.0f, 0.5f, 0.31f);
        glm::vec3 diffuse = glm::vec3(1.0f, 0.5f, 0.31f);
        glm::vec3 specular = glm::vec3(0.5f, 0.5f, 0.5f);

        float shiness = 64.0f;
        lightShader.setf("material.shiness", shiness);

        ambient = glm::vec3(0.2f, 0.2f, 0.2f);
        diffuse = glm::vec3(0.5f, 0.5f, 0.5f);
        specular = glm::vec3(1.0f, 1.0f, 1.0f);

        lightShader.setv3("light.ambient", glm::value_ptr(ambient));
        lightShader.setv3("light.diffuse", glm::value_ptr(diffuse));
        lightShader.setv3("light.specular", glm::value_ptr(specular));

        lightShader.seti("material.diffuse", 0);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, diffuseTexture);

        lightShader.seti("material.specular", 1);
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, specularTexture);

        glBindVertexArray(objectVAO);

        for (int i = 0; i < 10; ++i)
        {
    
    
            model = glm::mat4(1.0f);
            model = glm::translate(model, cubePositions[i]);
            model = glm::rotate(model, glm::radians(20.0f * i), glm::vec3(1.0f, 0.3f, 0.5f));
            lightShader.setm4fv("model", GL_FALSE, glm::value_ptr(model));
            glDrawArrays(GL_TRIANGLES, 0, 36);
        }

        processInput(window);
        glfwSwapBuffers(window);
        glfwPollEvents();
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }

    glfwTerminate();
    return 0;
}

void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
    
    
    glViewport(0, 0, width, height);
}

void processInput(GLFWwindow *window)
{
    
    
    float currentTime = glfwGetTime();
    float delta = currentTime - lastTime;
    lastTime = currentTime;
    float distance = cameraSpeed * delta;

    // 移动照相机位置
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
    {
    
    
        glfwSetWindowShouldClose(window, true);
    }
    else if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::FORWARD, distance);
    }
    else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::BACKWARD, distance);
    }
    else if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::LEFT, distance);
    }
    else if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::RIGHT, distance);
    }
    else if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::DOWN, distance);
    }
    else if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::UP, distance);
    }
}

GLFWwindow *initialize(int width, int height)
{
    
    
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "2", nullptr, nullptr);
    if (!window)
    {
    
    
        exit(-1);
    }

    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
    
    
        exit(-1);
    }

    glViewport(0, 0, width, height);

    glfwSetWindowSizeCallback(window, framebuffer_size_callback);

    // 光标初始位置
    lastXPos = 400;
    lastYPos = 300;
    firstMouse = true;
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
    glfwSetCursorPosCallback(window, mouse_callback);

    glfwSetScrollCallback(window, scroll_callback);

    return window;
}

void mouse_callback(GLFWwindow *window, double xpos, double ypos)
{
    
    
    if (firstMouse)
    {
    
    
        lastXPos = xpos;
        lastYPos = ypos;
        firstMouse = false;
    }

    float sensitivity = 0.05f;
    float xOffset = (xpos - lastXPos) * sensitivity;
    float yOffset = (lastYPos - ypos) * sensitivity;

    lastXPos = xpos;
    lastYPos = ypos;

    camera.rotate(xOffset, yOffset);
}

void scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
{
    
    
    fov -= yoffset;
    if (fov <= 1.0f)
    {
    
    
        fov = 1.0f;
    }
    if (fov >= 45.0f)
    {
    
    
        fov = 45.0f;
    }
}

5-fragment.glsl

#version 330 core

struct Material{
    sampler2D diffuse;
    sampler2D specular;
    float shiness;
};

struct Light{
    vec3 direction, ambient, diffuse, specular;
};

in vec3 Normal;
in vec3 fragPos;
in vec2 ourTexCoord;

uniform vec3 lightColor;
uniform vec3 objectColor;
uniform vec3 viewPos;
uniform Material material;
uniform Light light;

out vec4 FragColor;

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

    vec3 lightDir = normalize(-light.direction);
    vec3 normal = normalize(Normal);
    vec3 diffuseStrength = max(0.0f, dot(normal, lightDir)) * vec3(texture(material.diffuse, ourTexCoord)) * light.diffuse;
    
    vec3 viewDir = normalize(viewPos - fragPos);
    vec3 reflectDir = reflect(-lightDir, normal);
    vec3 specularStrength = pow(max(0.0f, dot(viewDir, reflectDir)), material.shiness) * vec3(texture(material.specular, ourTexCoord)) * light.specular;

    FragColor = vec4((ambientStrength + diffuseStrength + specularStrength)* lightColor, 1.0f);
}

点光源

#include "../env/glm/glm.hpp"
#include "../env/glm/gtc/matrix_transform.hpp"
#include "../env/glm/gtc/type_ptr.hpp"
#include <iostream>

#define STB_IMAGE_IMPLEMENTATION
#include "../env/std_image.h"
#include "../env/glad.h"
#include "../env/glfw3.h"
#include <fstream>
#include "../tools/shader.h"
#include "../tools/camera.h"

#define WIDTH 800
#define HEIGHT 600

GLFWwindow *initialize(int width, int height);

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);

Camera camera = Camera(glm::vec3(0.0f, 0.0f, 3.0f),
                       glm::vec3(0.0f, 0.0f, -1.0f),
                       glm::vec3(0.0f, 1.0f, 0.0f),
                       0.0f, 0.0f);

float cameraSpeed = 2.5f;
float lastTime;
float lastXPos;
float lastYPos;
bool firstMouse;
float fov = 45.0f;

int main()
{
    
    
    GLFWwindow *window = initialize(WIDTH, HEIGHT);
    float vertices[] = {
    
    
        // positions          // normals           // texture coords
        -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
        0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
        0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
        0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
        -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,

        -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
        0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
        0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
        0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
        -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,

        -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
        -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
        -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
        -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,

        0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
        0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
        0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
        0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,

        -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
        0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
        0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
        0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
        -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
        -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,

        -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
        0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
        0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
        0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
        -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
        -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f};

    glm::vec3 cubePositions[] = {
    
    
        glm::vec3(0.0f, 0.0f, 0.0f),
        glm::vec3(2.0f, 5.0f, -15.0f),
        glm::vec3(-1.5f, -2.2f, -2.5f),
        glm::vec3(-3.8f, -2.0f, -12.3f),
        glm::vec3(2.4f, -0.4f, -3.5f),
        glm::vec3(-1.7f, 3.0f, -7.5f),
        glm::vec3(1.3f, -2.0f, -2.5f),
        glm::vec3(1.5f, 2.0f, -2.5f),
        glm::vec3(1.5f, 0.2f, -1.5f),
        glm::vec3(-1.3f, 1.0f, -1.5f)};

    unsigned int myVBO;
    glGenBuffers(1, &myVBO);
    glBindBuffer(GL_ARRAY_BUFFER, myVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    GLuint objectVAO;
    glGenVertexArrays(1, &objectVAO);
    glBindVertexArray(objectVAO);
    glBindBuffer(GL_ARRAY_BUFFER, myVBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3 * sizeof(float)));
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(6 * sizeof(float)));

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);

    GLuint lightVAO;
    glGenVertexArrays(1, &lightVAO);
    glBindVertexArray(lightVAO);
    glBindBuffer(GL_ARRAY_BUFFER, myVBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
    glEnableVertexAttribArray(0);

    int width, height, channel;
    unsigned char *data = stbi_load("../images/container2.png", &width, &height, &channel, 0);

    std::cout << width << " " << height << " " << channel << std::endl;

    GLuint diffuseTexture;
    glGenTextures(1, &diffuseTexture);
    glBindTexture(GL_TEXTURE_2D, diffuseTexture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    glGenerateMipmap(GL_TEXTURE_2D);

    stbi_image_free(data);

    data = stbi_load("../images/container2_specular.png", &width, &height, &channel, 0);
    std::cout << width << " " << height << " " << channel << std::endl;

    GLuint specularTexture;
    glGenTextures(1, &specularTexture);
    glBindTexture(GL_TEXTURE_2D, specularTexture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    glGenerateMipmap(GL_TEXTURE_2D);

    glm::mat4 model, view, projection;
    Shader lampShader("1-vertex.glsl", "1-lamp-fragment.glsl");
    Shader lightShader("4-vertex.glsl", "5.1-fragment.glsl");

    lastTime = glfwGetTime();
    glEnable(GL_DEPTH_TEST);

    while (!glfwWindowShouldClose(window))
    {
    
    
        model = glm::mat4(1.0f);
        glm::vec3 lightPos = glm::vec3(1.2f, 1.0f, 2.0f);
        model = glm::translate(model, lightPos);
        model = glm::scale(model, glm::vec3(0.2f));

        view = camera.lookAt();
        projection = glm::perspective(glm::radians(fov), 1.0f * WIDTH / HEIGHT, 0.1f, 100.0f);

        lampShader.use();
        lampShader.setm4fv("model", GL_FALSE, glm::value_ptr(model));
        lampShader.setm4fv("view", GL_FALSE, glm::value_ptr(view));
        lampShader.setm4fv("projection", GL_FALSE, glm::value_ptr(projection));
        glBindVertexArray(lightVAO);
        glDrawArrays(GL_TRIANGLES, 0, 36);

        model = glm::mat4(1.0f);
        lightShader.use();
        lightShader.setm4fv("view", GL_FALSE, glm::value_ptr(view));
        lightShader.setm4fv("projection", GL_FALSE, glm::value_ptr(projection));

        glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
        lightShader.setv3("lightColor", glm::value_ptr(lightColor));

        glm::vec3 cameraPos = camera.getPosition();
        lightShader.setv3("viewPos", glm::value_ptr(cameraPos));
        lightShader.setv3("lightPos", glm::value_ptr(lightPos));

        glm::vec3 ambient = glm::vec3(1.0f, 0.5f, 0.31f);
        glm::vec3 diffuse = glm::vec3(1.0f, 0.5f, 0.31f);
        glm::vec3 specular = glm::vec3(0.5f, 0.5f, 0.5f);

        float shiness = 64.0f;
        lightShader.setf("material.shiness", shiness);

        ambient = glm::vec3(0.2f, 0.2f, 0.2f);
        diffuse = glm::vec3(0.5f, 0.5f, 0.5f);
        specular = glm::vec3(1.0f, 1.0f, 1.0f);

        lightShader.setv3("light.ambient", glm::value_ptr(ambient));
        lightShader.setv3("light.diffuse", glm::value_ptr(diffuse));
        lightShader.setv3("light.specular", glm::value_ptr(specular));

        lightShader.seti("material.diffuse", 0);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, diffuseTexture);

        lightShader.seti("material.specular", 1);
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, specularTexture);

        lightShader.setf("light.constant", 1.0f);
        lightShader.setf("light.linear", 0.09f);
        lightShader.setf("light.quadratic", 0.032f);

        glBindVertexArray(objectVAO);

        for (int i = 0; i < 10; ++i)
        {
    
    
            model = glm::mat4(1.0f);
            model = glm::translate(model, cubePositions[i]);
            model = glm::rotate(model, glm::radians(20.0f * i), glm::vec3(1.0f, 0.3f, 0.5f));
            lightShader.setm4fv("model", GL_FALSE, glm::value_ptr(model));
            glDrawArrays(GL_TRIANGLES, 0, 36);
        }

        processInput(window);
        glfwSwapBuffers(window);
        glfwPollEvents();
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }

    glfwTerminate();
    return 0;
}

void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
    
    
    glViewport(0, 0, width, height);
}

void processInput(GLFWwindow *window)
{
    
    
    float currentTime = glfwGetTime();
    float delta = currentTime - lastTime;
    lastTime = currentTime;
    float distance = cameraSpeed * delta;

    // 移动照相机位置
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
    {
    
    
        glfwSetWindowShouldClose(window, true);
    }
    else if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::FORWARD, distance);
    }
    else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::BACKWARD, distance);
    }
    else if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::LEFT, distance);
    }
    else if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::RIGHT, distance);
    }
    else if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::DOWN, distance);
    }
    else if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::UP, distance);
    }
}

GLFWwindow *initialize(int width, int height)
{
    
    
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "2", nullptr, nullptr);
    if (!window)
    {
    
    
        exit(-1);
    }

    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
    
    
        exit(-1);
    }

    glViewport(0, 0, width, height);

    glfwSetWindowSizeCallback(window, framebuffer_size_callback);

    // 光标初始位置
    lastXPos = 400;
    lastYPos = 300;
    firstMouse = true;
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
    glfwSetCursorPosCallback(window, mouse_callback);

    glfwSetScrollCallback(window, scroll_callback);

    return window;
}

void mouse_callback(GLFWwindow *window, double xpos, double ypos)
{
    
    
    if (firstMouse)
    {
    
    
        lastXPos = xpos;
        lastYPos = ypos;
        firstMouse = false;
    }

    float sensitivity = 0.05f;
    float xOffset = (xpos - lastXPos) * sensitivity;
    float yOffset = (lastYPos - ypos) * sensitivity;

    lastXPos = xpos;
    lastYPos = ypos;

    camera.rotate(xOffset, yOffset);
}

void scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
{
    
    
    fov -= yoffset;
    if (fov <= 1.0f)
    {
    
    
        fov = 1.0f;
    }
    if (fov >= 45.0f)
    {
    
    
        fov = 45.0f;
    }
}

5.1-fragment.glsl

#version 330 core

struct Material{
    sampler2D diffuse;
    sampler2D specular;
    float shiness;
};

struct Light{
    vec3 position, ambient, diffuse, specular;
    float constant, linear, quadratic;
};

in vec3 Normal;
in vec3 fragPos;
in vec2 ourTexCoord;

uniform vec3 lightColor;
uniform vec3 objectColor;
uniform vec3 lightPos;
uniform vec3 viewPos;
uniform Material material;
uniform Light light;

out vec4 FragColor;

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

    vec3 lightDir = normalize(lightPos - fragPos);
    vec3 normal = normalize(Normal);
    vec3 diffuseStrength = max(0.0f, dot(normal, lightDir)) * vec3(texture(material.diffuse, ourTexCoord)) * light.diffuse;
    
    vec3 viewDir = normalize(viewPos - fragPos);
    vec3 reflectDir = reflect(-lightDir, normal);
    vec3 specularStrength = pow(max(0.0f, dot(viewDir, reflectDir)), material.shiness) * vec3(texture(material.specular, ourTexCoord)) * light.specular;

    // 衰减项
    float dist = length(lightPos - fragPos);
    float attenuation = 1.0f / (light.constant + light.linear * dist + light.quadratic * dist * dist);
    //attenuation = 1.0f;
    FragColor = vec4((ambientStrength + diffuseStrength + specularStrength)* lightColor * attenuation, 1.0f);
}

聚光

#include "../env/glm/glm.hpp"
#include "../env/glm/gtc/matrix_transform.hpp"
#include "../env/glm/gtc/type_ptr.hpp"
#include <iostream>

#define STB_IMAGE_IMPLEMENTATION
#include "../env/std_image.h"
#include "../env/glad.h"
#include "../env/glfw3.h"
#include <fstream>
#include "../tools/shader.h"
#include "../tools/camera.h"

#define WIDTH 800
#define HEIGHT 600

GLFWwindow *initialize(int width, int height);

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);

Camera camera = Camera(glm::vec3(0.0f, 0.0f, 3.0f),
                       glm::vec3(0.0f, 0.0f, -1.0f),
                       glm::vec3(0.0f, 1.0f, 0.0f),
                       0.0f, 0.0f);

float cameraSpeed = 2.5f;
float lastTime;
float lastXPos;
float lastYPos;
bool firstMouse;
float fov = 45.0f;

int main()
{
    
    
    GLFWwindow *window = initialize(WIDTH, HEIGHT);
    float vertices[] = {
    
    
        // positions          // normals           // texture coords
        -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
        0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
        0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
        0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
        -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,

        -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
        0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
        0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
        0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
        -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,

        -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
        -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
        -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
        -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,

        0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
        0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
        0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
        0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,

        -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
        0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
        0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
        0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
        -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
        -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,

        -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
        0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
        0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
        0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
        -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
        -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f};

    glm::vec3 cubePositions[] = {
    
    
        glm::vec3(0.0f, 0.0f, 0.0f),
        glm::vec3(2.0f, 5.0f, -15.0f),
        glm::vec3(-1.5f, -2.2f, -2.5f),
        glm::vec3(-3.8f, -2.0f, -12.3f),
        glm::vec3(2.4f, -0.4f, -3.5f),
        glm::vec3(-1.7f, 3.0f, -7.5f),
        glm::vec3(1.3f, -2.0f, -2.5f),
        glm::vec3(1.5f, 2.0f, -2.5f),
        glm::vec3(1.5f, 0.2f, -1.5f),
        glm::vec3(-1.3f, 1.0f, -1.5f)};

    unsigned int myVBO;
    glGenBuffers(1, &myVBO);
    glBindBuffer(GL_ARRAY_BUFFER, myVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    GLuint objectVAO;
    glGenVertexArrays(1, &objectVAO);
    glBindVertexArray(objectVAO);
    glBindBuffer(GL_ARRAY_BUFFER, myVBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3 * sizeof(float)));
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(6 * sizeof(float)));

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);

    GLuint lightVAO;
    glGenVertexArrays(1, &lightVAO);
    glBindVertexArray(lightVAO);
    glBindBuffer(GL_ARRAY_BUFFER, myVBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
    glEnableVertexAttribArray(0);

    int width, height, channel;
    unsigned char *data = stbi_load("../images/container2.png", &width, &height, &channel, 0);

    std::cout << width << " " << height << " " << channel << std::endl;

    GLuint diffuseTexture;
    glGenTextures(1, &diffuseTexture);
    glBindTexture(GL_TEXTURE_2D, diffuseTexture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    glGenerateMipmap(GL_TEXTURE_2D);

    stbi_image_free(data);

    data = stbi_load("../images/container2_specular.png", &width, &height, &channel, 0);
    std::cout << width << " " << height << " " << channel << std::endl;

    GLuint specularTexture;
    glGenTextures(1, &specularTexture);
    glBindTexture(GL_TEXTURE_2D, specularTexture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    glGenerateMipmap(GL_TEXTURE_2D);

    glm::mat4 model, view, projection;
    Shader lampShader("1-vertex.glsl", "1-lamp-fragment.glsl");
    Shader lightShader("4-vertex.glsl", "5.2-fragment.glsl");

    lastTime = glfwGetTime();
    glEnable(GL_DEPTH_TEST);

    while (!glfwWindowShouldClose(window))
    {
    
    
        view = camera.lookAt();
        projection = glm::perspective(glm::radians(fov), 1.0f * WIDTH / HEIGHT, 0.1f, 100.0f);

        lightShader.use();
        lightShader.setm4fv("view", GL_FALSE, glm::value_ptr(view));
        lightShader.setm4fv("projection", GL_FALSE, glm::value_ptr(projection));

        glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
        lightShader.setv3("lightColor", glm::value_ptr(lightColor));

        glm::vec3 cameraPos = camera.getPosition();
        lightShader.setv3("viewPos", glm::value_ptr(cameraPos));

        glm::vec3 lightPos = camera.getPosition();
        glm::vec3 lightDirection = camera.getFront();
        lightShader.setv3("light.position", glm::value_ptr(lightPos));
        lightShader.setv3("light.direction", glm::value_ptr(lightDirection));
        lightShader.setf("light.cutOff", glm::cos(glm::radians(12.5f)));
        lightShader.setf("light.outerCutOff", glm::cos(glm::radians(17.5f)));

        glm::vec3 ambient = glm::vec3(1.0f, 0.5f, 0.31f);
        glm::vec3 diffuse = glm::vec3(1.0f, 0.5f, 0.31f);
        glm::vec3 specular = glm::vec3(0.5f, 0.5f, 0.5f);

        float shiness = 64.0f;
        lightShader.setf("material.shiness", shiness);

        ambient = glm::vec3(0.2f, 0.2f, 0.2f);
        diffuse = glm::vec3(0.5f, 0.5f, 0.5f);
        specular = glm::vec3(1.0f, 1.0f, 1.0f);

        lightShader.setv3("light.ambient", glm::value_ptr(ambient));
        lightShader.setv3("light.diffuse", glm::value_ptr(diffuse));
        lightShader.setv3("light.specular", glm::value_ptr(specular));

        lightShader.seti("material.diffuse", 0);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, diffuseTexture);

        lightShader.seti("material.specular", 1);
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, specularTexture);

        glBindVertexArray(objectVAO);
        for (int i = 0; i < 10; ++i)
        {
    
    
            model = glm::mat4(1.0f);
            model = glm::translate(model, cubePositions[i]);
            model = glm::rotate(model, glm::radians(20.0f * i), glm::vec3(1.0f, 0.3f, 0.5f));
            lightShader.setm4fv("model", GL_FALSE, glm::value_ptr(model));
            glDrawArrays(GL_TRIANGLES, 0, 36);
        }

        processInput(window);
        glfwSwapBuffers(window);
        glfwPollEvents();
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }

    glfwTerminate();
    return 0;
}

void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
    
    
    glViewport(0, 0, width, height);
}

void processInput(GLFWwindow *window)
{
    
    
    float currentTime = glfwGetTime();
    float delta = currentTime - lastTime;
    lastTime = currentTime;
    float distance = cameraSpeed * delta;

    // 移动照相机位置
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
    {
    
    
        glfwSetWindowShouldClose(window, true);
    }
    else if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::FORWARD, distance);
    }
    else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::BACKWARD, distance);
    }
    else if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::LEFT, distance);
    }
    else if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::RIGHT, distance);
    }
    else if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::DOWN, distance);
    }
    else if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
    {
    
    
        camera.move(CameraMovement::UP, distance);
    }
}

GLFWwindow *initialize(int width, int height)
{
    
    
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "2", nullptr, nullptr);
    if (!window)
    {
    
    
        exit(-1);
    }

    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
    
    
        exit(-1);
    }

    glViewport(0, 0, width, height);

    glfwSetWindowSizeCallback(window, framebuffer_size_callback);

    // 光标初始位置
    lastXPos = 400;
    lastYPos = 300;
    firstMouse = true;
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
    glfwSetCursorPosCallback(window, mouse_callback);

    glfwSetScrollCallback(window, scroll_callback);

    return window;
}

void mouse_callback(GLFWwindow *window, double xpos, double ypos)
{
    
    
    if (firstMouse)
    {
    
    
        lastXPos = xpos;
        lastYPos = ypos;
        firstMouse = false;
    }

    float sensitivity = 0.05f;
    float xOffset = (xpos - lastXPos) * sensitivity;
    float yOffset = (lastYPos - ypos) * sensitivity;

    lastXPos = xpos;
    lastYPos = ypos;

    camera.rotate(xOffset, yOffset);
}

void scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
{
    
    
    fov -= yoffset;
    if (fov <= 1.0f)
    {
    
    
        fov = 1.0f;
    }
    if (fov >= 45.0f)
    {
    
    
        fov = 45.0f;
    }
}

5.2-fragment.glsl

#version 330 core

struct Material{
    sampler2D diffuse;
    sampler2D specular;
    float shiness;
};

struct Light{
    vec3 position, direction, ambient, diffuse, specular;
    float cutOff;
    float outerCutOff;
};

in vec3 Normal;
in vec3 fragPos;
in vec2 ourTexCoord;

uniform vec3 lightColor;
uniform vec3 viewPos;
uniform Material material;
uniform Light light;

out vec4 FragColor;

void main() {

    vec3 ambientStrength = vec3(texture(material.diffuse, ourTexCoord)) * light.ambient;
    vec3 lightDir = normalize(light.position - fragPos);

    float theta = dot(lightDir, normalize(-light.direction));
    vec3 normal = normalize(Normal);
    vec3 diffuseStrength = max(0.0f, dot(normal, lightDir)) * vec3(texture(material.diffuse, ourTexCoord)) * light.diffuse;
    
    vec3 viewDir = normalize(viewPos - fragPos);
    vec3 reflectDir = reflect(-lightDir, normal);
    vec3 specularStrength = pow(max(0.0f, dot(viewDir, reflectDir)), material.shiness) * vec3(texture(material.specular, ourTexCoord)) * light.specular;

    float epsilon = light.cutOff - light.outerCutOff;
    float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0f, 1.0f);

    if(theta > light.cutOff) 
    {    
        FragColor = vec4((ambientStrength + diffuseStrength + specularStrength)* lightColor, 1.0f);
    } else if(theta > light.outerCutOff) {
        FragColor = vec4((ambientStrength + diffuseStrength * intensity+ specularStrength * intensity)* lightColor, 1.0f);
    } else {
        FragColor = vec4(ambientStrength * lightColor, 1.0f);
    }
}

猜你喜欢

转载自blog.csdn.net/NelsonCheung/article/details/109427368
3.5