Skip to content
Snippets Groups Projects
Commit 617ca3ff authored by BATON Theau's avatar BATON Theau
Browse files

Add phong shading

parent 9085184f
No related branches found
No related tags found
No related merge requests found
...@@ -11,7 +11,6 @@ uniform mat4 uView; ...@@ -11,7 +11,6 @@ uniform mat4 uView;
uniform mat4 uProj; uniform mat4 uProj;
out vec2 zTex; out vec2 zTex;
flat out uint zInstanceId;
void main() { void main() {
vec3 cameraRight = vec3(uView[0][0], uView[1][0], uView[2][0]); vec3 cameraRight = vec3(uView[0][0], uView[1][0], uView[2][0]);
...@@ -23,5 +22,4 @@ void main() { ...@@ -23,5 +22,4 @@ void main() {
gl_Position = uProj * uView * models[gl_InstanceID] * vec4(vertexPosition, 1.0); gl_Position = uProj * uView * models[gl_InstanceID] * vec4(vertexPosition, 1.0);
zTex = aTex; zTex = aTex;
zInstanceId = gl_InstanceID;
} }
\ No newline at end of file
#version 330 core
out vec4 FragColor;
in vec3 zFramPosition;
in vec3 zNormal;
in vec2 zTexture;
uniform vec3 uLightPosition;
uniform vec3 uViewPosition;
uniform vec3 uColor;
uniform sampler2D uSlot;
void main() {
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * uColor;
vec3 norm = normalize(zNormal);
vec3 lightDir = normalize(uLightPosition - zFramPosition);
vec3 diffuse = max(dot(norm, lightDir), 0.0) * uColor;
float specularStrength = 0.5;
vec3 viewDir = normalize(uViewPosition - zFramPosition);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * uColor;
vec3 result = (ambient + diffuse + specular);
FragColor = vec4(result, 1.0) * texture(uSlot, zTexture);
}
\ No newline at end of file
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexture;
out vec3 zFramPosition;
out vec3 zNormal;
out vec2 zTexture;
layout(std140) uniform uModels_t {
mat4 models[1024];
};
uniform mat4 uView;
uniform mat4 uProj;
void main() {
zFramPosition = vec3(models[gl_InstanceID] * vec4(aPos, 1.0));
zNormal = mat3(transpose(inverse(models[gl_InstanceID]))) * aNormal;
zTexture = aTexture;
gl_Position = uProj* uView * vec4(zFramPosition, 1.0);
}
\ No newline at end of file
...@@ -32,8 +32,8 @@ namespace pg::scene ...@@ -32,8 +32,8 @@ namespace pg::scene
} }
if(!this->_textureShader.usable()) { if(!this->_textureShader.usable()) {
Source vertexShaderSource(WORKSPACE_PATH + "res/shaders/scene/Texture-Fat.vert", Source::Categorie::VERTEX); Source vertexShaderSource(WORKSPACE_PATH + "res/shaders/scene/Phong.vert", Source::Categorie::VERTEX);
Source fragmentShaderSource(WORKSPACE_PATH + "res/shaders/scene/Texture.frag", Source::Categorie::FRAGMENT); Source fragmentShaderSource(WORKSPACE_PATH + "res/shaders/scene/Phong.frag", Source::Categorie::FRAGMENT);
this->_textureShader << vertexShaderSource; this->_textureShader << vertexShaderSource;
this->_textureShader << fragmentShaderSource; this->_textureShader << fragmentShaderSource;
...@@ -158,10 +158,16 @@ namespace pg::scene ...@@ -158,10 +158,16 @@ namespace pg::scene
program->setUniform("uView", camera.getViewMatrix()); program->setUniform("uView", camera.getViewMatrix());
program->setUniform("uProj", camera.getViewFrustum().getProjectionMatrix()); program->setUniform("uProj", camera.getViewFrustum().getProjectionMatrix());
program->setUniform("uColor", this->_color);
if(this->_useTexture) { if(this->_useTexture) {
program->setUniform("uSlot", 0); program->setUniform("uSlot", 0);
program->setUniform("uLightPosition", glm::vec3(0.f , 10.f, 0.f));
program->setUniform("uViewPosition", camera.getPosition());
program->setUniform("uColor", glm::vec3(this->_color));
}
else {
program->setUniform("uColor", this->_color);
} }
if(this->_wireframe) { if(this->_wireframe) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment