add shininess attribute in mesh class

This commit is contained in:
ayachi3 2022-12-16 16:03:53 +08:00
parent d181aefde7
commit 759f4ad688
9 changed files with 88 additions and 52 deletions

View File

@ -1,39 +1,48 @@
#version 430 core #version 430 core
out vec4 FragColor; 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 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 viewPos;
uniform vec3 lightColor; uniform Material material;
uniform Light light;
void main() void main()
{ {
vec3 objectColor = texture(texture_specular1, TexCoords).rgb; // ambient
objectColor = texture(texture_diffuse1, TexCoords).rgb; vec3 ambient = light.ambient * texture(material.diffuse, TexCoords).rgb;
// ambient
float ambientStrength = 0.3;
vec3 ambient = ambientStrength * lightColor;
// diffuse // diffuse
vec3 norm = normalize(Normal); vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos); vec3 lightDir = normalize(light.position - FragPos);
float diff = max(dot(norm, lightDir), 0.0); float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor; vec3 diffuse = light.diffuse * diff * texture(material.diffuse, TexCoords).rgb;
// specular // specular
float specularStrength = 0.5;
vec3 viewDir = normalize(viewPos - FragPos); vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm); vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = specularStrength * spec * lightColor; 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); FragColor = vec4(result, 1.0);
} }

View File

@ -1,2 +1,19 @@
#include"lightCaster.h" #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;
}

View File

@ -1,11 +1,27 @@
#pragma once #pragma once
#include"shader.h" #include"shader.h"
#include<GLM/glm.hpp>
// 发光物不需要纹理所以说需要另外的shader // 发光物不需要纹理所以说需要另外的shader
// 在sceneviewer中被包含 // 在sceneviewer中被包含
class Illuminant { class Illuminant {
private:
glm::vec3 _position;
glm::vec3 _color;
public: public:
Illuminant() {}; Illuminant();
Illuminant(glm::vec3 position, glm::vec3 color);
}; 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;
}

View File

@ -27,6 +27,8 @@ void Mesh::render(const ShaderProgram& shader) const {
// retrieve texture number (the N in diffuse_textureN) // retrieve texture number (the N in diffuse_textureN)
std::string number; std::string number;
std::string name = _textures[i].type() == TextureType::DIFFUSE ? "texture_diffuse" : "texture_specular"; std::string name = _textures[i].type() == TextureType::DIFFUSE ? "texture_diffuse" : "texture_specular";
name = "material." + name;
if (_textures[i].type() == TextureType::DIFFUSE) if (_textures[i].type() == TextureType::DIFFUSE)
number = std::to_string(diffuseNr++); number = std::to_string(diffuseNr++);
else if (_textures[i].type() == TextureType::SPECULAR) else if (_textures[i].type() == TextureType::SPECULAR)

View File

@ -13,6 +13,9 @@ private:
std::vector<unsigned int> _indices; std::vector<unsigned int> _indices;
std::vector<Texture> _textures; std::vector<Texture> _textures;
// we can control shininess in mesh
float _shininess = 64.0f;
VertexArrayObject _vao = VertexArrayObject::empty(); VertexArrayObject _vao = VertexArrayObject::empty();
public: public:
@ -23,6 +26,8 @@ public:
inline std::vector<Vertex> vertices() const { return _vertices; } inline std::vector<Vertex> vertices() const { return _vertices; }
inline std::vector<unsigned int> indices() const { return _indices; } inline std::vector<unsigned int> indices() const { return _indices; }
inline std::vector<Texture> textures() const { return _textures; } inline std::vector<Texture> textures() const { return _textures; }
inline float shininess() { return _shininess; }
inline void setShininess(float shininess) { _shininess = shininess; }
inline VertexArrayObject vao() const { return _vao; } inline VertexArrayObject vao() const { return _vao; }

View File

@ -33,6 +33,10 @@ void Renderable::setScale(float scale) {
void Renderable::render(ShaderProgram shader) { void Renderable::render(ShaderProgram shader) {
// Set model matrix // Set model matrix
shader.setUniform("model", modelMatrix()); shader.setUniform("model", modelMatrix());
// set diffuse
//shader.setUniform("")
// Render // Render
_model->render(shader); _model->render(shader);
} }

View File

@ -6,12 +6,8 @@
#include <qurl.h> #include <qurl.h>
#include <qdir.h> #include <qdir.h>
#include<time.h> #include<time.h>
#include "vbo.h"
#include "vao.h"
#include "shader.h"
#include "logger.h"
#include "model.h"
#include "lightCaster.h"
using std::vector; using std::vector;
void copyFile(std::string name) { void copyFile(std::string name) {
@ -25,6 +21,7 @@ void copyFile(std::string name) {
SceneViewer::SceneViewer(QWidget* parent) SceneViewer::SceneViewer(QWidget* parent)
: QOpenGLWidget(parent) : QOpenGLWidget(parent)
{ {
_illuminants.push_back(Illuminant());
QSurfaceFormat format; QSurfaceFormat format;
format.setProfile(QSurfaceFormat::CoreProfile); format.setProfile(QSurfaceFormat::CoreProfile);
format.setVersion(4, 3); format.setVersion(4, 3);
@ -191,35 +188,18 @@ void SceneViewer::wheelEvent(QWheelEvent* event) {
update(); update();
} }
double rr = 1.0;
void SceneViewer::update_light() { void SceneViewer::update_light() {
// 对于发光体, // 对于发光体,
//model is in Illuminate //model is in Illuminate
// view is from camera // view is from camera
// projection is from caller // 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("lightPos",_illuminants[0].position());
_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("lightColor", _illuminants[0].color());
_shaderProgram.setUniform("viewPos", _camera.position()); _shaderProgram.setUniform("viewPos", _camera.position());
// 要给着色器传递viewPos // 要给着色器传递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();
} }

View File

@ -4,7 +4,6 @@
#include <qevent.h> #include <qevent.h>
#include <qopenglfunctions.h> #include <qopenglfunctions.h>
#include <QtOpenGLWidgets/qopenglwidget.h> #include <QtOpenGLWidgets/qopenglwidget.h>
#include <vector> #include <vector>
#include "camera.h" #include "camera.h"
@ -13,6 +12,10 @@
#include "vao.h" #include "vao.h"
#include "utils.h" #include "utils.h"
#include "lightCaster.h" #include "lightCaster.h"
#include "vbo.h"
#include "logger.h"
#include "model.h"
class SceneViewer : public QOpenGLWidget, protected QOpenGLFunctions class SceneViewer : public QOpenGLWidget, protected QOpenGLFunctions
{ {

View File

@ -3,9 +3,9 @@ layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal; layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords; layout (location = 2) in vec2 aTexCoords;
out vec2 TexCoords;
out vec3 FragPos; out vec3 FragPos;
out vec3 Normal; out vec3 Normal;
out vec2 TexCoords;
uniform mat4 model; uniform mat4 model;
uniform mat4 view; uniform mat4 view;
@ -14,7 +14,7 @@ uniform mat4 projection;
void main() void main()
{ {
FragPos = vec3(model * vec4(aPos, 1.0)); FragPos = vec3(model * vec4(aPos, 1.0));
Normal = aNormal; Normal = mat3(transpose(inverse(model))) * aNormal;
TexCoords = aTexCoords; TexCoords = aTexCoords;
gl_Position = projection * view * vec4(FragPos, 1.0); gl_Position = projection * view * vec4(FragPos, 1.0);