feat(OpenGL-hello): moving source
This commit is contained in:
parent
6ca0e0315c
commit
b175659ad2
3 changed files with 41 additions and 54 deletions
|
@ -1,25 +1,32 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
in vec3 Normal;
|
in vec3 Normal;
|
||||||
in vec3 FragPos;
|
in vec3 FragPos;
|
||||||
|
|
||||||
uniform vec3 lightPos;
|
uniform vec3 lightPos;
|
||||||
uniform vec3 lightColor;
|
uniform vec3 lightColor;
|
||||||
uniform vec3 objectColor;
|
uniform vec3 objectColor;
|
||||||
|
uniform vec3 viewPos;
|
||||||
|
|
||||||
void main()
|
void main() {
|
||||||
{
|
// 环境
|
||||||
// ambient
|
float ambientStrength = 0.1;
|
||||||
float ambientStrength = 0.1;
|
vec3 ambient = ambientStrength * lightColor;
|
||||||
vec3 ambient = ambientStrength * lightColor;
|
|
||||||
|
// 漫反射
|
||||||
// diffuse
|
vec3 norm = normalize(Normal);
|
||||||
vec3 norm = normalize(Normal);
|
vec3 lightDir = normalize(lightPos - FragPos);
|
||||||
vec3 lightDir = normalize(lightPos - FragPos);
|
float diff = max(dot(norm, lightDir), 0.0);
|
||||||
float diff = max(dot(norm, lightDir), 0.0);
|
vec3 diffuse = diff * lightColor;
|
||||||
vec3 diffuse = diff * lightColor;
|
|
||||||
|
// 镜面
|
||||||
vec3 result = (ambient + diffuse) * objectColor;
|
float specularStrength = 0.5;
|
||||||
FragColor = vec4(result, 1.0);
|
vec3 viewDir = normalize(viewPos - FragPos);
|
||||||
}
|
vec3 reflectDir = reflect(-lightDir, norm);
|
||||||
|
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 64);
|
||||||
|
vec3 specular = specularStrength * spec * lightColor;
|
||||||
|
|
||||||
|
vec3 result = (ambient + diffuse + specular) * objectColor;
|
||||||
|
FragColor = vec4(result, 1.0);
|
||||||
|
}
|
|
@ -1,8 +1,7 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
uniform vec3 lightColor;
|
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
FragColor = vec4(lightColor, 1.0); // set all 4 vector values to 1.0
|
FragColor = vec4(1.0); // set all 4 vector values to 1.0
|
||||||
}
|
}
|
|
@ -69,18 +69,6 @@ int main() {
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
|
||||||
glEnableVertexAttribArray(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);
|
|
||||||
|
|
||||||
lightShader.use();
|
|
||||||
lightShader.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
|
|
||||||
lightShader.setVec3("lightPos", lightPos);
|
|
||||||
|
|
||||||
// 开启深度测试
|
// 开启深度测试
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
|
@ -99,44 +87,37 @@ int main() {
|
||||||
glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
|
glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
glm::mat4 view = camera.GetViewMatrix();
|
lightPos.x = 1.0f + sin(glfwGetTime()) * 2.0f;
|
||||||
|
lightPos.y = sin(glfwGetTime() / 2.0f) * 1.0f;
|
||||||
|
|
||||||
|
glm::mat4 view = camera.GetViewMatrix();
|
||||||
glm::mat4 projection =
|
glm::mat4 projection =
|
||||||
glm::perspective(glm::radians(camera.Zoom),
|
glm::perspective(glm::radians(camera.Zoom),
|
||||||
(float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
(float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||||
|
glm::mat4 model = glm::mat4(1.0f);
|
||||||
|
|
||||||
// 绘制三角形
|
// 绘制三角形
|
||||||
shader.use();
|
shader.use();
|
||||||
|
shader.setVec3("objectColor", 1.0f, 0.5f, 0.31f);
|
||||||
|
shader.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
|
||||||
|
shader.setVec3("lightPos", lightPos);
|
||||||
|
shader.setVec3("viewPos", camera.Position);
|
||||||
|
shader.setMat4("model", model);
|
||||||
shader.setMat4("projection", projection);
|
shader.setMat4("projection", projection);
|
||||||
shader.setMat4("view", view);
|
shader.setMat4("view", view);
|
||||||
// 设置变换矩阵
|
|
||||||
glm::mat4 trans(1.0f);
|
|
||||||
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0, 0.25, 0.0));
|
|
||||||
shader.setMat4("trans", trans);
|
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||||
|
|
||||||
for (unsigned int i = 0; i < 10; i++) {
|
model = glm::mat4(1.0f);
|
||||||
glm::mat4 model(1.0f);
|
|
||||||
model = glm::translate(model, cubePositions[i]);
|
|
||||||
float angle = 20.0f * i + 50.0f;
|
|
||||||
model =
|
|
||||||
glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
|
||||||
shader.setMat4("model", model);
|
|
||||||
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
|
||||||
}
|
|
||||||
|
|
||||||
lightShader.use();
|
|
||||||
|
|
||||||
glm::mat4 model = glm::mat4(1.0f);
|
|
||||||
model = glm::translate(model, lightPos);
|
model = glm::translate(model, lightPos);
|
||||||
model = glm::scale(model, glm::vec3(0.2f)); // a smaller cube
|
model = glm::scale(model, glm::vec3(0.2f)); // a smaller cube
|
||||||
glBindVertexArray(lightVAO);
|
|
||||||
|
lightShader.use();
|
||||||
lightShader.setMat4("projection", projection);
|
lightShader.setMat4("projection", projection);
|
||||||
lightShader.setMat4("view", view);
|
lightShader.setMat4("view", view);
|
||||||
lightShader.setMat4("model", model);
|
lightShader.setMat4("model", model);
|
||||||
|
|
||||||
|
glBindVertexArray(lightVAO);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||||
|
|
||||||
// 检查并调用事件,交换缓冲
|
// 检查并调用事件,交换缓冲
|
||||||
|
|
Reference in a new issue