3.1 颜色

颜色

现实世界中有无数种颜色,每一个物体都有它们自己的颜色。我们需要使用有限的数值来模拟真实世界中无限的颜色,所以并不是所有现实世界中的颜色都可以用数值来表示的。

颜色可以数字化的由红色(Red)、绿色(Green)和蓝色(Blue)三个分量组成,它们通常被缩写为RGB。仅仅用这三个值就可以组合出任意一种颜色。

我们在现实生活中看到某一物体的颜色并不是这个物体真正拥有的颜色,而是它所反射的颜色。换句话说,那些不能被物体所吸收的颜色就是我们能够感知到的物体的颜色

在图形领域中,当我们把光源的颜色与物体的颜色值相乘,所得到的就是这个物体所反射的颜色。

我们可以定义物体的颜色为物体从一个光源反射各个颜色分量的大小

GLSL向量的*是对应项相乘。

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[] = {
    
    
        -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
        0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
        0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
        0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
        -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,

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

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

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

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

        -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
        0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
        0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
        0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
        -0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
        -0.5f, 0.5f, -0.5f, 0.0f, 1.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, 5 * sizeof(float), (void *)0);
    glEnableVertexAttribArray(0);

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

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

    lastTime = glfwGetTime();

    while (!glfwWindowShouldClose(window))
    {
    
    
        model = glm::mat4(1.0f);
        model = glm::translate(model, glm::vec3(1.2f, 1.0f, 2.0f));
        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("model", GL_FALSE, glm::value_ptr(model));
        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);
        glm::vec3 objectColor = glm::vec3(1.0f, 0.5f, 0.31f);
        lightShader.setv3("lightColor", glm::value_ptr(lightColor));
        lightShader.setv3("objectColor", glm::value_ptr(objectColor));
        glBindVertexArray(objectVAO);
        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;
    }
}

1-vertex.glsl

#version 330 core
layout (location = 0) in vec3 aPos;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main() {
    gl_Position = projection * view * model * vec4(aPos, 1.0f);
}

1-fragment.glsl

#version 330 core

uniform vec3 lightColor;
uniform vec3 objectColor;

out vec4 FragColor;

void main() {
    FragColor = vec4(lightColor * objectColor, 1.0f);
}

1-lamp-vertex.glsl

#version 330 core
out vec4 FragColor;

void main() {
    FragColor = vec4(1.0f);
}

猜你喜欢

转载自blog.csdn.net/NelsonCheung/article/details/109405676
3.1