diff --git a/FinalProject/fragmentshader.fs b/FinalProject/fragmentshader.fs index 9f4baa5..b2d475d 100644 --- a/FinalProject/fragmentshader.fs +++ b/FinalProject/fragmentshader.fs @@ -1,39 +1,48 @@ #version 430 core out vec4 FragColor; -in vec2 TexCoords; -in vec3 Normal; + +struct Material { + sampler2D texture_diffuse1; + sampler2D texture_specular1; + float shininess; +}; + +struct Light { + vec3 position; + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + + in vec3 FragPos; +in vec3 Normal; +in vec2 TexCoords; -uniform sampler2D texture_diffuse1; -uniform sampler2D texture_specular1; -// above 2 uniform can caculate objectColor -uniform vec3 lightPos; uniform vec3 viewPos; -uniform vec3 lightColor; +uniform Material material; +uniform Light light; + void main() { - vec3 objectColor = texture(texture_specular1, TexCoords).rgb; - objectColor = texture(texture_diffuse1, TexCoords).rgb; - // ambient - float ambientStrength = 0.3; - vec3 ambient = ambientStrength * lightColor; - +// ambient + vec3 ambient = light.ambient * texture(material.diffuse, TexCoords).rgb; + // diffuse vec3 norm = normalize(Normal); - vec3 lightDir = normalize(lightPos - FragPos); + vec3 lightDir = normalize(light.position - FragPos); float diff = max(dot(norm, lightDir), 0.0); - vec3 diffuse = diff * lightColor; - + vec3 diffuse = light.diffuse * diff * texture(material.diffuse, TexCoords).rgb; + // specular - float specularStrength = 0.5; vec3 viewDir = normalize(viewPos - FragPos); vec3 reflectDir = reflect(-lightDir, norm); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32); - vec3 specular = specularStrength * spec * lightColor; + float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); + vec3 specular = light.specular * spec * texture(material.specular, TexCoords).rgb; - vec3 result = (ambient + diffuse + specular) * objectColor; + vec3 result = ambient + diffuse + specular; FragColor = vec4(result, 1.0); } \ No newline at end of file diff --git a/FinalProject/lightCaster.cpp b/FinalProject/lightCaster.cpp index 4e6e08d..08e10e6 100644 --- a/FinalProject/lightCaster.cpp +++ b/FinalProject/lightCaster.cpp @@ -1,2 +1,19 @@ #include"lightCaster.h" +Illuminant::Illuminant() { + _position = glm::vec3(0.0f); + _color = glm::vec3(1.0f); +} +Illuminant::Illuminant(glm::vec3 position, glm::vec3 color) { + _position = position; + _color = color; +} + + + +void Illuminant::setPosition(glm::vec3 position) { + _position = position; +} +void Illuminant::setColor(glm::vec3 color) { + _color = color; +} \ No newline at end of file diff --git a/FinalProject/lightCaster.h b/FinalProject/lightCaster.h index 3343dcd..ca4b7e1 100644 --- a/FinalProject/lightCaster.h +++ b/FinalProject/lightCaster.h @@ -1,11 +1,27 @@ #pragma once #include"shader.h" - +#include // 发光物不需要纹理,所以说需要另外的shader // 在sceneviewer中被包含 class Illuminant { - +private: + glm::vec3 _position; + glm::vec3 _color; public: - Illuminant() {}; + Illuminant(); + Illuminant(glm::vec3 position, glm::vec3 color); -}; \ No newline at end of file + inline glm::vec3 position(); + glm::vec3 color(); + + void setPosition(glm::vec3 position); + void setColor(glm::vec3 color); + +}; + +inline glm::vec3 Illuminant::position() { + return _position; +} +inline glm::vec3 Illuminant::color() { + return _color; +} \ No newline at end of file diff --git a/FinalProject/mesh.cpp b/FinalProject/mesh.cpp index 0f70513..2bdffe8 100644 --- a/FinalProject/mesh.cpp +++ b/FinalProject/mesh.cpp @@ -27,6 +27,8 @@ void Mesh::render(const ShaderProgram& shader) const { // retrieve texture number (the N in diffuse_textureN) std::string number; std::string name = _textures[i].type() == TextureType::DIFFUSE ? "texture_diffuse" : "texture_specular"; + name = "material." + name; + if (_textures[i].type() == TextureType::DIFFUSE) number = std::to_string(diffuseNr++); else if (_textures[i].type() == TextureType::SPECULAR) diff --git a/FinalProject/mesh.h b/FinalProject/mesh.h index 56e07c5..574737a 100644 --- a/FinalProject/mesh.h +++ b/FinalProject/mesh.h @@ -13,6 +13,9 @@ private: std::vector _indices; std::vector _textures; + // we can control shininess in mesh + float _shininess = 64.0f; + VertexArrayObject _vao = VertexArrayObject::empty(); public: @@ -23,6 +26,8 @@ public: inline std::vector vertices() const { return _vertices; } inline std::vector indices() const { return _indices; } inline std::vector textures() const { return _textures; } + inline float shininess() { return _shininess; } + inline void setShininess(float shininess) { _shininess = shininess; } inline VertexArrayObject vao() const { return _vao; } diff --git a/FinalProject/renderable.cpp b/FinalProject/renderable.cpp index 86463a1..45fccab 100644 --- a/FinalProject/renderable.cpp +++ b/FinalProject/renderable.cpp @@ -33,6 +33,10 @@ void Renderable::setScale(float scale) { void Renderable::render(ShaderProgram shader) { // Set model matrix shader.setUniform("model", modelMatrix()); + // set diffuse + //shader.setUniform("") + + // Render _model->render(shader); } diff --git a/FinalProject/sceneviewer.cpp b/FinalProject/sceneviewer.cpp index 7966706..5149f61 100644 --- a/FinalProject/sceneviewer.cpp +++ b/FinalProject/sceneviewer.cpp @@ -6,12 +6,8 @@ #include #include #include -#include "vbo.h" -#include "vao.h" -#include "shader.h" -#include "logger.h" -#include "model.h" +#include "lightCaster.h" using std::vector; void copyFile(std::string name) { @@ -25,6 +21,7 @@ void copyFile(std::string name) { SceneViewer::SceneViewer(QWidget* parent) : QOpenGLWidget(parent) { + _illuminants.push_back(Illuminant()); QSurfaceFormat format; format.setProfile(QSurfaceFormat::CoreProfile); format.setVersion(4, 3); @@ -191,35 +188,18 @@ void SceneViewer::wheelEvent(QWheelEvent* event) { update(); } -double rr = 1.0; + void SceneViewer::update_light() { // 对于发光体, //model is in Illuminate // view is from camera // projection is from caller - rr += time(NULL) / 1000000000.0; - rr = (long long)rr % 10000; - double r = rr / 100; - _shaderProgram.setUniform("lightPos", (float)sin(r), (float)sin(r/4), (float)sin(r/2.1)); - _shaderProgram.setUniform("lightColor", 0.8+0.2*(float)sin(r), 0.8 + 0.2 * (float)sin(r / 3), 0.8 + 0.2 * (float)sin(r / 2.1)); + _shaderProgram.setUniform("lightPos",_illuminants[0].position()); + _shaderProgram.setUniform("lightColor", _illuminants[0].color()); _shaderProgram.setUniform("viewPos", _camera.position()); // 要给着色器传递viewPos - - - //// 下面换成渲染发光体的shaderprogram - //ShaderProgram illuminantShader = ShaderProgram::empty(); - - //illuminantShader.ensureInitialized(); - //Logger::info("Shader Program initialized"); - - //VertexShader vertexShader("./temp/shaders/illuminant.vs"); - //FragmentShader fragmentShader("./temp/shaders/illuminant.fs"); - //_shaderProgram.attachShader(vertexShader); - //_shaderProgram.attachShader(fragmentShader); - //vertexShader.dispose(); - //fragmentShader.dispose(); } diff --git a/FinalProject/sceneviewer.h b/FinalProject/sceneviewer.h index 5f9b9be..d42ad39 100644 --- a/FinalProject/sceneviewer.h +++ b/FinalProject/sceneviewer.h @@ -4,7 +4,6 @@ #include #include #include - #include #include "camera.h" @@ -13,6 +12,10 @@ #include "vao.h" #include "utils.h" #include "lightCaster.h" +#include "vbo.h" +#include "logger.h" +#include "model.h" + class SceneViewer : public QOpenGLWidget, protected QOpenGLFunctions { diff --git a/FinalProject/vertexshader.vs b/FinalProject/vertexshader.vs index fc9d2cf..97c08db 100644 --- a/FinalProject/vertexshader.vs +++ b/FinalProject/vertexshader.vs @@ -3,9 +3,9 @@ layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aNormal; layout (location = 2) in vec2 aTexCoords; -out vec2 TexCoords; out vec3 FragPos; out vec3 Normal; +out vec2 TexCoords; uniform mat4 model; uniform mat4 view; @@ -14,7 +14,7 @@ uniform mat4 projection; void main() { FragPos = vec3(model * vec4(aPos, 1.0)); - Normal = aNormal; + Normal = mat3(transpose(inverse(model))) * aNormal; TexCoords = aTexCoords; gl_Position = projection * view * vec4(FragPos, 1.0);