...
 
......@@ -122,8 +122,8 @@ void Geometry::computeTangents() {
int index_v1 = m_index[i + 1];
int index_v2 = m_index[i + 2];
glm::vec4 deltaPos1 = m_vertices[index_v1] - m_vertices[index_v0];
glm::vec4 deltaPos2 = m_vertices[index_v2] - m_vertices[index_v0];
glm::vec3 deltaPos1 = m_vertices[index_v1] - m_vertices[index_v0];
glm::vec3 deltaPos2 = m_vertices[index_v2] - m_vertices[index_v0];
/*
glm::vec4 deltaPos1 = m_vertices[index_v0]-m_vertices[index_v1];
glm::vec4 deltaPos2 = m_vertices[index_v2]-m_vertices[index_v1];
......@@ -137,7 +137,7 @@ void Geometry::computeTangents() {
glm::vec2 deltaUV2 = m_uvs[index_v2]-m_uvs[index_v1];
*/
float r = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV1.y * deltaUV2.x);
glm::vec4 tmp = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y) * r;
glm::vec3 tmp = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y) * r;
glm::vec3 tangent = glm::vec3(tmp.x, tmp.y, tmp.z);
// Adding, as multiple triangles share indices
......
#version 330 core
//incoming data
// incoming data
in vec4 passPosition;
in vec3 passNormal;
in vec2 passUVCoord;
uniform struct
{
uniform struct {
vec3 diffuse;
vec3 specular;
float shininess;
......@@ -17,13 +15,12 @@ uniform struct
uniform int useColorTexture;
uniform sampler2D colorTexture;
uniform struct
{
vec4 pos;//pos.w = 0 dir. light, pos.w = 1 point light
uniform struct {
vec4 pos; // pos.w = 0 dir. light, pos.w = 1 point light
vec3 col;
vec3 spot_direction;
float spot_exponent;
float spot_cutoff;// no spotlight if cutoff = 0
float spot_cutoff; // no spotlight if cutoff = 0
} light;
uniform vec3 lightAmbient;
......@@ -34,24 +31,24 @@ uniform mat4 viewMatrix;
out vec4 fragmentColor;
void main(){
void main() {
///////////////////////////////////////////////////////
///////////////// Cube Map Reflection /////////////////
///////////////////////////////////////////////////////
vec3 pos = normalize(vec3(passPosition));
vec3 normal = normalize(passNormal);
vec3 reflected = reflect(pos, normal);
reflected = vec3(inverse(viewMatrix) * vec4(reflected, 0.0));
reflected = inverse(mat3(viewMatrix)) * reflected;
vec3 inNormal = normalize(passNormal);
//Diffuse
// Diffuse
vec3 lightVector;
vec3 light_camcoord = (viewMatrix * light.pos).xyz;
if (light.pos.w > 0.001f)
lightVector = normalize(light_camcoord - passPosition.xyz);
lightVector = normalize(light_camcoord - passPosition.xyz);
else
lightVector = normalize(light_camcoord);
lightVector = normalize(light_camcoord);
float cos_phi = max(dot(inNormal, lightVector), 0.0f);
// Specular
......@@ -59,23 +56,23 @@ void main(){
vec3 reflection = normalize(reflect(-lightVector, inNormal));
float cos_psi_n = pow(max(dot(reflection, eye), 0.0f), mat.shininess);
//Spotlight
// Spotlight
float spot = 1.0;
if (light.spot_cutoff >= 0.001f)
{
float cos_phi_spot = max(dot(-lightVector, mat3(viewMatrix) * light.spot_direction), 0.0f);
if (light.spot_cutoff >= 0.001f) {
float cos_phi_spot = max(
dot(-lightVector, mat3(viewMatrix) * light.spot_direction), 0.0f);
if (cos_phi_spot >= cos(light.spot_cutoff))
spot = pow(cos_phi_spot, light.spot_exponent);
spot = pow(cos_phi_spot, light.spot_exponent);
else
spot = 0.0f;
spot = 0.0f;
}
//Color
// Color
vec3 diffuse_color = mat.diffuse;
if (useColorTexture != 0)
diffuse_color = texture(colorTexture, passUVCoord).rgb;
diffuse_color = texture(colorTexture, passUVCoord).rgb;
//All together
// All together
fragmentColor.rgb = diffuse_color * lightAmbient;
fragmentColor.rgb += spot * diffuse_color * cos_phi * light.col;
fragmentColor.rgb += spot * mat.specular * cos_psi_n * light.col;
......
#version 330 core
layout (location = 0) in vec4 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 uv;
layout (location = 3) in vec3 tangent;
layout(location = 0) in vec4 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 uv;
layout(location = 3) in vec3 tangent;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform struct
{
vec4 pos;// light position in world coordinates
vec3 col;// light color
uniform struct {
vec4 pos; // light position in world coordinates
vec3 col; // light color
} light;
out vec3 passLightVector;
out vec3 passEyeVector;
out vec2 passUV;
void main(){
void main() {
gl_Position = projectionMatrix * viewMatrix * modelMatrix * position;
/*********** Calculate matrix that goes from camera space to tangent space **********/
/*********** Calculate matrix that goes from camera space to tangent space
* **********/
mat4 normalMatrix = transpose(inverse(viewMatrix * modelMatrix));
vec3 normal_cs = normalize((normalMatrix * vec4(normal, 0)).xyz);
// tangent transforms just like points
vec3 tangent_cs = normalize((viewMatrix * modelMatrix * vec4(tangent, 0)).xyz);
vec3 bitangent_cs = cross(tangent_cs, normal_cs);
vec3 tangent_cs =
normalize((viewMatrix * modelMatrix * vec4(tangent, 0)).xyz);
vec3 bitangent_cs = cross(normal_cs, tangent_cs);
mat3 tbn = transpose(mat3(
tangent_cs,
bitangent_cs,
normal_cs
));
mat3 tbn = transpose(mat3(tangent_cs, bitangent_cs, normal_cs));
/*********** Calculate position in camera space *************************************/
/*********** Calculate position in camera space
* *************************************/
vec3 position_cs = (viewMatrix * modelMatrix * position).xyz;
/*********** Calculate light vector (tangent space) *********************************/
/*********** Calculate light vector (tangent space)
* *********************************/
// light position in world coordinates
vec3 lightPosition_cs = (viewMatrix *light.pos).xyz;
vec3 lightPosition_cs = (viewMatrix * light.pos).xyz;
// calcluate vector that goes from the vertex to the light, in tangent space
passLightVector = tbn * normalize(lightPosition_cs - position_cs);
/*********** Calculate eye vector (tangent space) **********************************/
/*********** Calculate eye vector (tangent space)
* **********************************/
// calculate eye vector in camera space
vec3 eye_cs = normalize(-position_cs);
// calculate eye vector in tangent space
passEyeVector = tbn * eye_cs;
/*********** Pass uv of the vertex *************************************************/
/*********** Pass uv of the vertex
* *************************************************/
// no special space for this one
passUV = uv;
}
\ No newline at end of file