feat(OpenGL-hello): basic light
This commit is contained in:
parent
1c5b887de1
commit
6ca0e0315c
8 changed files with 139 additions and 122 deletions
|
@ -9,30 +9,49 @@
|
|||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <iostream>
|
||||
|
||||
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,
|
||||
float vertices[] = {
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 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, 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, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||
-0.5f, -0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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.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, -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.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
|
||||
};
|
||||
|
||||
glm::vec3 cubePositions[] = {
|
||||
glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(2.0f, 5.0f, -15.0f),
|
||||
|
@ -47,6 +66,7 @@ float lastFrame = 0.0f; // 上一帧的时间
|
|||
// settings
|
||||
const unsigned int SCR_WIDTH = 800;
|
||||
const unsigned int SCR_HEIGHT = 800;
|
||||
glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
|
||||
|
||||
// 创建摄像机
|
||||
Camera camera = Camera(SCR_HEIGHT / 2.0f, SCR_HEIGHT / 2.0f);
|
||||
|
|
25
OpenGL/hello/src/cube.fs
Normal file
25
OpenGL/hello/src/cube.fs
Normal file
|
@ -0,0 +1,25 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec3 Normal;
|
||||
in vec3 FragPos;
|
||||
|
||||
uniform vec3 lightPos;
|
||||
uniform vec3 lightColor;
|
||||
uniform vec3 objectColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
// ambient
|
||||
float ambientStrength = 0.1;
|
||||
vec3 ambient = ambientStrength * lightColor;
|
||||
|
||||
// diffuse
|
||||
vec3 norm = normalize(Normal);
|
||||
vec3 lightDir = normalize(lightPos - FragPos);
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = diff * lightColor;
|
||||
|
||||
vec3 result = (ambient + diffuse) * objectColor;
|
||||
FragColor = vec4(result, 1.0);
|
||||
}
|
18
OpenGL/hello/src/cube.vs
Normal file
18
OpenGL/hello/src/cube.vs
Normal file
|
@ -0,0 +1,18 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
|
||||
out vec3 FragPos;
|
||||
out vec3 Normal;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragPos = vec3(model * vec4(aPos, 1.0));
|
||||
Normal = aNormal;
|
||||
|
||||
gl_Position = projection * view * vec4(FragPos, 1.0);
|
||||
}
|
8
OpenGL/hello/src/light.fs
Normal file
8
OpenGL/hello/src/light.fs
Normal file
|
@ -0,0 +1,8 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
uniform vec3 lightColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(lightColor, 1.0); // set all 4 vector values to 1.0
|
||||
}
|
11
OpenGL/hello/src/light.vs
Normal file
11
OpenGL/hello/src/light.vs
Normal file
|
@ -0,0 +1,11 @@
|
|||
#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.0);
|
||||
}
|
|
@ -37,7 +37,8 @@ int main() {
|
|||
glfwSetScrollCallback(window, scroll_callback);
|
||||
|
||||
// 创建着色器程序
|
||||
Shader shader = Shader("src/tex.vs", "src/tex.fs");
|
||||
Shader shader = Shader("src/cube.vs", "src/cube.fs");
|
||||
Shader lightShader = Shader("src/light.vs", "src/light.fs");
|
||||
|
||||
unsigned int VAO, VBO, EBO;
|
||||
// 创建顶点数组对象
|
||||
|
@ -50,80 +51,35 @@ int main() {
|
|||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
// 顶点属性
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// 纹理坐标属性
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
|
||||
// 法向量属性
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),
|
||||
(void *)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
// 创建纹理
|
||||
unsigned int texture1, texture2;
|
||||
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_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
// 加载并生成纹理
|
||||
int width, height, nrChannels;
|
||||
// 图片翻转y轴
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
unsigned char *data =
|
||||
stbi_load("resources/container.jpg", &width, &height, &nrChannels, 0);
|
||||
if (data) {
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
|
||||
GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
} else {
|
||||
std::cout << "Failed to load texture" << std::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_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
// 加载并生成纹理
|
||||
// 图片翻转y轴
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
data =
|
||||
stbi_load("resources/awesomeface.png", &width, &height, &nrChannels, 0);
|
||||
if (data) {
|
||||
|
||||
// note that the awesomeface.png has transparency and thus an alpha channel,
|
||||
// so make sure to tell OpenGL the data type is of GL_RGBA
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
|
||||
GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
} else {
|
||||
std::cout << "Failed to load texture" << std::endl;
|
||||
}
|
||||
stbi_image_free(data);
|
||||
// 设置光源
|
||||
unsigned int lightVAO;
|
||||
glGenVertexArrays(1, &lightVAO);
|
||||
glBindVertexArray(lightVAO);
|
||||
// 只需要绑定VBO不用再次设置VBO的数据,因为箱子的VBO数据中已经包含了正确的立方体顶点数据
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
// 设置灯立方体的顶点属性(对我们的灯来说仅仅只有位置数据)
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// 激活着色器
|
||||
shader.use();
|
||||
// 绑定纹理
|
||||
shader.setInt("texture1", 0);
|
||||
shader.setInt("texture2", 1);
|
||||
shader.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
|
||||
shader.setVec3("objectColor", 1.0f, 0.5f, 0.31f);
|
||||
|
||||
// create transformations
|
||||
glm::mat4 view =
|
||||
glm::lookAt(glm::vec3(0.0f, 0.0f, 3.0f), glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
glm::mat4 projection = glm::mat4(1.0f);
|
||||
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
|
||||
lightShader.use();
|
||||
lightShader.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
|
||||
lightShader.setVec3("lightPos", lightPos);
|
||||
|
||||
// 开启深度测试
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
@ -140,18 +96,20 @@ int main() {
|
|||
processInput(window);
|
||||
|
||||
// 设置为白色
|
||||
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 0.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);
|
||||
glm::mat4 view = camera.GetViewMatrix();
|
||||
|
||||
glm::mat4 projection =
|
||||
glm::perspective(glm::radians(camera.Zoom),
|
||||
(float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||
|
||||
// 绘制三角形
|
||||
shader.use();
|
||||
|
||||
shader.setMat4("projection", projection);
|
||||
shader.setMat4("view", view);
|
||||
// 设置变换矩阵
|
||||
glm::mat4 trans(1.0f);
|
||||
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0, 0.25, 0.0));
|
||||
|
@ -169,13 +127,18 @@ int main() {
|
|||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
||||
|
||||
float radius = 10.0f;
|
||||
view = camera.GetViewMatrix();
|
||||
shader.setMat4("view", view);
|
||||
lightShader.use();
|
||||
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, lightPos);
|
||||
model = glm::scale(model, glm::vec3(0.2f)); // a smaller cube
|
||||
glBindVertexArray(lightVAO);
|
||||
lightShader.setMat4("projection", projection);
|
||||
lightShader.setMat4("view", view);
|
||||
lightShader.setMat4("model", model);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
|
||||
projection = glm::perspective(
|
||||
glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||
shader.setMat4("projection", projection);
|
||||
// 检查并调用事件,交换缓冲
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec2 TexCoord;
|
||||
|
||||
uniform sampler2D texture1;
|
||||
uniform sampler2D texture2;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
#version 330 core
|
||||
layout(location = 0) in vec3 aPos;
|
||||
layout(location = 1) in vec2 aTexCoord;
|
||||
|
||||
out vec2 TexCoord;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
uniform mat4 trans;
|
||||
|
||||
void main() {
|
||||
gl_Position = projection * view * model * trans * vec4(aPos, 1.0);
|
||||
// gl_Position = trans * vec4(aPos, 1.0);
|
||||
TexCoord = aTexCoord;
|
||||
}
|
Reference in a new issue