3.6多光源

多光源

将“投光物”一章的三个光源的场景整合即可。

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

    glm::vec3 pointLightPositions[] = {
    
    
        glm::vec3(0.7f, 0.2f, 2.0f),
        glm::vec3(2.3f, -3.3f, -4.0f),
        glm::vec3(-4.0f, 2.0f, -12.0f),
        glm::vec3(0.0f, 0.0f, -3.0f)};

    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, ambient, diffuse, specular;
    Shader lampShader("1-vertex.glsl", "1-lamp-fragment.glsl");
    Shader lightShader("4-vertex.glsl", "6-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);

        lampShader.use();
        lampShader.setm4fv("view", GL_FALSE, glm::value_ptr(view));
        lampShader.setm4fv("projection", GL_FALSE, glm::value_ptr(projection));
        glBindVertexArray(lightVAO);

        for (int i = 0; i < 4; ++i)
        {
    
    
            model = glm::mat4(1.0f);
            model = glm::translate(model, pointLightPositions[i]);
            model = glm::scale(model, glm::vec3(0.2f));
            lampShader.setm4fv("model", GL_FALSE, glm::value_ptr(model));
            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));

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

        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.setv3("directLight.direction", -0.2f, -1.0f, -0.3f);
        lightShader.setv3("directLight.ambient", 0.05f, 0.05f, 0.05f);
        lightShader.setv3("directLight.diffuse", 0.4f, 0.4f, 0.4f);
        lightShader.setv3("directLight.specular", 0.5f, 0.5f, 0.5f);

        lightShader.setv3("pointlights[0].position", glm::value_ptr(pointLightPositions[0]));
        lightShader.setv3("pointlights[0].ambient", 0.05f, 0.05f, 0.05f);
        lightShader.setv3("pointlights[0].diffuse", 0.8f, 0.8f, 0.8f);
        lightShader.setv3("pointlights[0].specular", 1.0f, 1.0f, 1.0f);
        lightShader.setf("pointlights[0].constant", 1.0f);
        lightShader.setf("pointlights[0].linear", 0.9f);
        lightShader.setf("pointlights[0].quadratic", 0.032f);

        lightShader.setv3("pointlights[1].position", glm::value_ptr(pointLightPositions[1]));
        lightShader.setv3("pointlights[1].ambient", 0.05f, 0.05f, 0.05f);
        lightShader.setv3("pointlights[1].diffuse", 0.8f, 0.8f, 0.8f);
        lightShader.setv3("pointlights[1].specular", 1.0f, 1.0f, 1.0f);
        lightShader.setf("pointlights[1].constant", 1.0f);
        lightShader.setf("pointlights[1].linear", 0.9f);
        lightShader.setf("pointlights[1].quadratic", 0.032f);

        lightShader.setv3("pointlights[2].position", glm::value_ptr(pointLightPositions[2]));
        lightShader.setv3("pointlights[2].ambient", 0.05f, 0.05f, 0.05f);
        lightShader.setv3("pointlights[2].diffuse", 0.8f, 0.8f, 0.8f);
        lightShader.setv3("pointlights[2].specular", 1.0f, 1.0f, 1.0f);
        lightShader.setf("pointlights[2].constant", 1.0f);
        lightShader.setf("pointlights[2].linear", 0.9f);
        lightShader.setf("pointlights[2].quadratic", 0.032f);

        lightShader.setv3("pointlights[3].position", glm::value_ptr(pointLightPositions[3]));
        lightShader.setv3("pointlights[3].ambient", 0.05f, 0.05f, 0.05f);
        lightShader.setv3("pointlights[3].diffuse", 0.8f, 0.8f, 0.8f);
        lightShader.setv3("pointlights[3].specular", 1.0f, 1.0f, 1.0f);
        lightShader.setf("pointlights[3].constant", 1.0f);
        lightShader.setf("pointlights[3].linear", 0.9f);
        lightShader.setf("pointlights[3].quadratic", 0.032f);

        glm::vec3 cameraPosition = camera.getPosition();
        glm::vec3 cameraFront = camera.getFront();

        lightShader.setv3("spotLight.position", glm::value_ptr(cameraPosition));
        lightShader.setv3("spotLight.direction", glm::value_ptr(cameraFront));
        lightShader.setv3("spotLight.ambient", 0.0f, 0.0f, 0.0f);
        lightShader.setv3("spotLight.diffuse", 1.0f, 1.0f, 1.0f);
        lightShader.setv3("spotLight.specular", 1.0f, 1.0f, 1.0f);
        lightShader.setf("spotLight.constant", 1.0f);
        lightShader.setf("spotLight.linear", 0.9f);
        lightShader.setf("spotLight.quadratic", 0.032f);
        lightShader.setf("spotLight.cutOff", glm::cos(glm::radians(12.5f)));
        lightShader.setf("spotLight.outerCutOff", glm::cos(glm::radians(15.0f)));

        lightShader.setv3("viewPos", glm::value_ptr(cameraPosition));

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

6-fragment.glsl

#version 330 core

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

out vec4 FragColor;

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

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

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

struct SpotLight{
    vec3 position, direction, ambient, diffuse, specular;
    float constant, linear, quadratic;
    float cutOff;
    float outerCutOff;
};

uniform vec3 viewPos;
uniform Material material;
uniform DirectLight directLight;
uniform PointLight pointlights[4];
uniform SpotLight spotLight;

vec3 getDirectLightEffect(DirectLight light);
vec3 getPointLightEffect(PointLight light);
vec3 getSpotLightEffect(SpotLight light);

void main() {
    vec3 color = vec3(0.0f, 0.0f, 0.0f);
    color = getDirectLightEffect(directLight);
    for( int i = 0; i < 4; ++i ) {
       color += getPointLightEffect(pointlights[i]);
    }
    color += getSpotLightEffect(spotLight);

    FragColor = vec4(color, 1.0f);
}

vec3 getDirectLightEffect(DirectLight light) {
    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;

    return (ambientStrength + diffuseStrength + specularStrength);
}

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

    vec3 lightDir = normalize(light.position - 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(light.position - fragPos);
    float attenuation = 1.0f / (light.constant + light.linear * dist + light.quadratic * dist * dist);
    
    return (ambientStrength + diffuseStrength * attenuation + specularStrength * attenuation);
}
vec3 getSpotLightEffect(SpotLight light) {
    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);

        // 衰减项
    float dist = length(light.position - fragPos);
    float attenuation = 1.0f / (light.constant + light.linear * dist + light.quadratic * dist * dist);
    
    if(theta > light.cutOff) 
    {    
        return (ambientStrength + diffuseStrength * attenuation + specularStrength * attenuation);
    } else if(theta > light.outerCutOff) {
        return (ambientStrength + diffuseStrength * intensity * attenuation + specularStrength * intensity * attenuation);
    } else {
        return ambientStrength;
    }
}

猜你喜欢

转载自blog.csdn.net/NelsonCheung/article/details/109427393
3.6