参考:这里
上一节的材质只能设置物体表面一种颜色,所以我们需要贴图来设置不同位置不同颜色。
其实就采样贴图获取物体的相应颜色。
程序:
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
void mouse_callback(GLFWwindow* window, double x, double y);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
const int SCR_WIDTH = 800;
const int SCR_HEIGHT = 600;
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = SCR_WIDTH / 2, lastY = SCR_WIDTH / 2;
bool firstMouse = true;
float deltaTime = 0.0f;
float lastFrame = 0.0f;
glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
int main()
{
//Init
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
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "hahaha", NULL, NULL);
if (window == NULL)
{
cout << "Failed to create GLFW window !" << endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
cout << "Failed to init GLAD !" << endl;
}
//configure
glEnable(GL_DEPTH_TEST);
//隐藏光标
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
//build and compile shader
Shader cubeShader("Shaders/Learn12_Cube.vs", "Shaders/Learn12_Cube.fs");
Shader lightShader("Shaders/Learn12_Light.vs", "Shaders/Learn12_Light.fs");
//set vertex data
float vertices[] = {
// positions // normals // texture coords
-0.5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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
};
//set buffer
//create cube
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
//bind
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
//set data
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//set pointer:position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
//create light
unsigned int lightVAO;
glGenVertexArrays(1, &lightVAO);
glBindVertexArray(lightVAO);
//再次绑定VBO,不需要设置VBO,上面已经设置了,只需要绑定即可
glBindBuffer(GL_ARRAY_BUFFER, VBO);
//设置灯的立方体顶点属性(对灯来说只有顶点数据),顶点数据里还有uv,但未用,步长还是5
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
//load textures
unsigned int texture1, texture2,emissionMap;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
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);
//load
int width, height, channels;
auto data = stbi_load("Images/container2.png", &width, &height, &channels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
cout << "failed to load texture!" << endl;
}
stbi_image_free(data);
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
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);
data = stbi_load("Images/container2_specular.png", &width, &height, &channels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
cout << "failed to load texture!" << endl;
}
stbi_image_free(data);
glGenTextures(1, &emissionMap);
glBindTexture(GL_TEXTURE_2D, emissionMap);
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);
data = stbi_load("Images/matrix.jpg", &width, &height, &channels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
cout << "failed to load texture!" << endl;
}
stbi_image_free(data);
//set shader uniform value
cubeShader.use();//设置uniform值之前要先激活shader
cubeShader.setInt("material.diffuse", 0);
cubeShader.setInt("material.specular",1);
cubeShader.setInt("material.emission",2);
cubeShader.setFloat("material.shininess", 32.0f);
cubeShader.setVec3("light.specular", 1.0f, 1.0f, 1.0f);
cubeShader.setVec3("light.position", lightPos);
cubeShader.setVec3("viewPos", camera.Position);
lightShader.use();
lightShader.setVec3("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
lightShader.setVec3("objectColor", glm::vec3(1.0f, 1.0f, 1.0f));
//render loop
while (!glfwWindowShouldClose(window))
{
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
//input
processInput(window);
//render
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, emissionMap);
//set shader uniform value
cubeShader.use();
//需要变化的放到循环里设置
cubeShader.setVec3("viewPos", camera.Position);
cubeShader.setFloat("offsetX",sin(glfwGetTime()*10)*0.1f);
cubeShader.setFloat("offsetY", cos(glfwGetTime()*10)*0.1f);
glm::vec3 lightColor;
lightColor.x = sin(glfwGetTime() * 2.0f);
lightColor.y = sin(glfwGetTime() * 0.7f);
lightColor.z = sin(glfwGetTime() * 1.3f);
glm::vec3 diffuseColor = lightColor * glm::vec3(0.5f); // 降低影响
glm::vec3 ambientColor = diffuseColor * glm::vec3(0.2f); // 很低的影响
cubeShader.setVec3("light.ambient", 0.2f, 0.2f, 0.2f);
cubeShader.setVec3("light.diffuse", 0.5f, 0.5f, 0.5f);
//active shader
cubeShader.use();
//create matrix transformations;
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
view = camera.GetViewMatrix();
projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
//set matrix
cubeShader.setMat4("model", model);
cubeShader.setMat4("view", view);
cubeShader.setMat4("projection", projection);
//render boxes
//bind data
glBindVertexArray(VAO);
model = glm::mat4(1.0f);
cubeShader.setMat4("model", model);
//draw
glDrawArrays(GL_TRIANGLES, 0, 36);
//光位置立方体
lightShader.use();
model = glm::mat4(1.0f);
model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.5f));
lightShader.setMat4("model", model);
lightShader.setMat4("view", view);
lightShader.setMat4("projection", projection);
glBindVertexArray(lightVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
//swap buffer
glfwSwapBuffers(window);
//call event
glfwPollEvents();
}
//optional:de-allocate all resurces once they've outlived their pupose
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
cubeShader.deleteShader();
lightShader.deleteShader();
//terminate clearing all previously allocated GLFW resources
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
}
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
}
void mouse_callback(GLFWwindow* window, double x, double y)
{
if (firstMouse)
{
lastX = x;
lastY = y;
firstMouse = false;
}
float xoffset = x - lastX;
float yoffset = lastY - y;
lastX = x;
lastY = y;
camera.ProcessMouseMovement(xoffset, yoffset);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.ProcessMouseScroll(yoffset);
}
顶点着色器:
#version 330 core
layout(location=0)in vec3 aPos;
layout(location=1)in vec3 aNormal;
layout(location=2)in vec2 aTexcoord;
out vec3 normal;
out vec3 fragPos;
out vec2 uv;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position=projection*view*model*vec4(aPos,1.0);
fragPos=vec3(model*vec4(aPos,1.0));
normal=mat3(transpose(inverse(model)))*aNormal;
uv=aTexcoord;
}
片元着色器:添加了自发光贴图
#version 330 core
out vec4 fragColor;
struct Material
{
sampler2D diffuse;
sampler2D specular;
sampler2D emission;
float shininess;
};
struct Light
{
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
uniform Light light;
uniform Material material;
in vec2 uv;
in vec3 normal;
in vec3 fragPos;
uniform float offsetX;
uniform float offsetY;
uniform vec3 lightColor;
uniform vec3 lightPos;
uniform vec3 viewPos;
void main()
{
vec3 worldNormal=normalize(normal);
vec3 lightDir=normalize(light.position-fragPos);
vec3 viewDir=normalize(viewPos-fragPos);
vec3 albedo=texture(material.diffuse,uv).rgb;
vec3 specularMask=texture(material.specular,uv).rgb;
vec3 emission=texture(material.emission,vec2(uv.x-offsetX,uv.y-offsetY)).rgb;
//环境光
vec3 ambient=light.ambient*albedo;
//漫反射
float dif=max(dot(worldNormal,lightDir),0.0);
vec3 diffuse=light.diffuse*dif*albedo;
//高光
vec3 reflectDir=reflect(-lightDir,worldNormal);
float spe=pow(max(dot(viewDir,reflectDir),0.0),material.shininess);
vec3 specular=light.specular*spe*specularMask;
vec3 finalColor=ambient+diffuse+specular+emission;
fragColor=vec4(finalColor,1.0);
}