将文件的复制与给予权限抽象成一个函数。

添加光照渲染函数
This commit is contained in:
ayachi3 2022-12-15 16:13:52 +08:00
parent df803c8dea
commit 8adf864c39
7 changed files with 50 additions and 10 deletions

View File

@ -2,14 +2,30 @@
out vec4 FragColor; out vec4 FragColor;
in vec2 TexCoords; in vec2 TexCoords;
in vec3 Normal;
in vec3 FragPos;
uniform sampler2D texture_diffuse1; uniform sampler2D texture_diffuse1;
uniform sampler2D texture_specular1; uniform sampler2D texture_specular1;
// above 2 uniform can caculate objectColor
uniform vec3 lightPos;
uniform vec3 lightColor; uniform vec3 lightColor;
void main() void main()
{ {
FragColor = texture(texture_specular1, TexCoords); vec3 objectColor = texture(texture_specular1, TexCoords).rgb;
FragColor = texture(texture_diffuse1, TexCoords); objectColor = texture(texture_diffuse1, TexCoords).rgb;
FragColor = vec4(FragColor * vec4(lightColor,1.0f)); // ambient
float ambientStrength = 0;
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);
} }

View File

View File

@ -0,0 +1,11 @@
#version 430 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);
}

View File

@ -3,8 +3,8 @@
// 发光物不需要纹理所以说需要另外的shader // 发光物不需要纹理所以说需要另外的shader
// 在sceneviewer中被包含 // 在sceneviewer中被包含
class LightCaster { class Illuminant {
public: public:
LightCaster() {}; Illuminant() {};
}; };

View File

@ -5,7 +5,7 @@
#include <qresource.h> #include <qresource.h>
#include <qurl.h> #include <qurl.h>
#include <qdir.h> #include <qdir.h>
#include<time.h>
#include "vbo.h" #include "vbo.h"
#include "vao.h" #include "vao.h"
#include "shader.h" #include "shader.h"
@ -89,8 +89,8 @@ void SceneViewer::paintGL() {
_shaderProgram.setUniform("projection", projection); _shaderProgram.setUniform("projection", projection);
///////////////////////////////////////最终不应该放在这里 ///////////////////////////////////////进行光照处理
_shaderProgram.setUniform("lightColor", 0.0f, 1.0f, 0.0f); update_light();
////////////////////////////////////// //////////////////////////////////////
@ -174,3 +174,10 @@ void SceneViewer::wheelEvent(QWheelEvent* event) {
// Update the view // Update the view
update(); update();
} }
void SceneViewer::update_light() {
auto r = time(NULL);
Logger::debug("1\n");
_shaderProgram.setUniform("lightPos", (float)sin(r), (float)sin(r/4), (float)sin(r/2.1));
_shaderProgram.setUniform("lightColor", 1.0f, 1.0f, 1.0f);
}

View File

@ -41,6 +41,7 @@ private:
public: public:
SceneViewer(QWidget* parent = 0); SceneViewer(QWidget* parent = 0);
~SceneViewer(); ~SceneViewer();
void update_light();
protected: protected:
// OpenGL functions // OpenGL functions

View File

@ -4,6 +4,8 @@ layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords; layout (location = 2) in vec2 aTexCoords;
out vec2 TexCoords; out vec2 TexCoords;
out vec3 FragPos;
out vec3 Normal;
uniform mat4 model; uniform mat4 model;
uniform mat4 view; uniform mat4 view;
@ -11,6 +13,9 @@ uniform mat4 projection;
void main() void main()
{ {
FragPos = vec3(model * vec4(aPos, 1.0));
Normal = aNormal;
TexCoords = aTexCoords; TexCoords = aTexCoords;
gl_Position = projection * view * model * vec4(aPos, 1.0); gl_Position = projection * view * vec4(FragPos, 1.0);
} }