Commit 88069bd0 authored by Håkon Berg Borhaug's avatar Håkon Berg Borhaug
Browse files

Started on lighting in shaders

parent 794a9f4f
......@@ -15,7 +15,7 @@
#include "../../glm-master/glm/glm.hpp"
// 4 points with 3D position and 2D texture position
#define LS_POS_ARRAY_SIZE 4 * (3 + 2)
#define LS_POS_ARRAY_SIZE 4 * (3 + 3 + 2)
#define LS_IND_ARRAY_SIZE 4
class Plane : public SceneObject
......@@ -40,10 +40,10 @@ private:
float positions_[LS_POS_ARRAY_SIZE] = {
// Left plane
-100.0f,0.0f, 100.0f, 0.0f, 0.0f,
100.0f, 0.0f, 100.0f, 1.0f, 0.0f,
100.0f, 0.0f, -100.0f, 1.0f, 1.0f,
-100.0f,0.0f, -100.0f, 0.0f, 1.0f,
-100.0f,0.0f, 100.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
100.0f, 0.0f, 100.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
100.0f, 0.0f, -100.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
-100.0f,0.0f, -100.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
};
unsigned int indices_[LS_IND_ARRAY_SIZE] = {
......
......@@ -5,11 +5,38 @@
layout(location = 0) out vec4 color;
in vec2 v_TexCoord;
in vec3 v_Normal;
in vec3 v_LightPosition;
in vec3 v_FragmentPosition;
//uniform vec4 u_Color;
uniform sampler2D u_Texture;
uniform sampler2D u_DiffuseMaterial;
void main() {
vec4 texColor = texture(u_Texture, v_TexCoord);
color = texColor;
//color = texColor;
// Ambient light
vec3 ambientLightPos = vec3(0.2f, 0.2f, 0.2f);
vec3 ambient = ambientLightPos * texture(u_DiffuseMaterial, v_TexCoord).rgb;
// Diffuse light
vec3 norm = normalize(v_Normal);
vec3 lightDir = normalize(v_LightPosition - v_FragmentPosition);
float diff = max(dot(norm, lightDir), 0.0);
vec3 light_diffuse = vec3(0.5f,0.5f,0.5f);
vec3 diffuse = light_diffuse * diff * texture(u_DiffuseMaterial, v_TexCoord).rgb;
// Specular light
vec3 viewDir = normalize( - v_FragmentPosition);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 64.0f);
vec3 material_specular = vec3(0.5f,0.5f,0.5f);
vec3 light_specular = vec3(1.0f,1.0f,1.0f);
vec3 specular = light_specular * (spec * material_specular);
vec3 result = ambient + diffuse + specular;
color = mix(texColor, vec4(result, 1.0f), 0.9);
};
......@@ -3,15 +3,24 @@
#version 330 core
layout(location = 0) in vec4 position;
layout(location = 1) in vec2 texCoord;
out vec2 v_TexCoord;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 texCoord;
uniform mat4 u_ProjectionMatrix;
uniform mat4 u_ViewMatrix;
uniform mat4 u_ModelMatrix;
//uniform vec4 u_LightPosition;
out vec2 v_TexCoord;
out vec3 v_Normal;
out vec3 v_LightPositon;
out vec3 v_FragmentPosition;
void main() {
gl_Position = u_ProjectionMatrix * u_ViewMatrix * u_ModelMatrix * position;
v_Normal = normal;
v_LightPositon = vec3(u_ViewMatrix * vec4(1.0f, 1.0f, 2.0f, 1.0f));
v_FragmentPosition = vec3(u_ViewMatrix * u_ModelMatrix * position);
v_TexCoord = texCoord;
};
......@@ -32,6 +32,8 @@ void Plane::privateInit()
/* LAYOUT */
// 3 floats for world position
layout.Push<float>(3);
// 3 floats for normals
layout.Push<float>(3);
// 2 floats for texture position
layout.Push<float>(2);
......@@ -48,6 +50,8 @@ void Plane::privateInit()
/* TEXTURE */
texture.Bind(textureSlot);
shader.SetUniform1i("u_Texture", textureSlot);
shader.SetUniform1i("u_DiffuseMaterial", 0);
// shader.SetUniform4f("u_LightPosition", 50.0f, 300.0f, 0.0f, 1.0f);
/* UNBINDING */
texture.Unbind();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment