adjust light functionality,

bugs:
- background cannot be changed during runtime
This commit is contained in:
Anakin
2017-01-18 17:01:43 +01:00
parent a521dfc292
commit 4c177f2ddc
9 changed files with 75 additions and 35 deletions

View File

@@ -19,5 +19,6 @@
<file>Z.png</file>
<file>screenshot.png</file>
<file>wireframe.png</file>
<file>light.png</file>
</qresource>
</RCC>

View File

@@ -8,6 +8,7 @@ left mouse - rotate
right mouse - move
scroll - zoom
space - reset view
L - set light to current position
esc - close
using the X, Y, Z Button you can activate/deactivate the rotating directions

View File

@@ -15,40 +15,41 @@ uniform struct Light {
uniform bool b_transparent;
uniform bool b_light;
varying vec2 v_texcoord;
varying vec3 v_position;
varying vec3 v_normal;
varying vec2 v_surfaceUV;
varying vec3 v_surfacePosition;
varying vec3 v_surfaceNormal;
void main()
{
// variables
float brightness;
if(b_light == true)
{
// calculate normals in worldspace
vec3 normal = normalize(n_matrix * v_normal);
//get the surface - light vector (cause this is a candel)
vec3 surfaceToLight = light.position - v_position;
// calculate the brightness depending on the angle
brightness = dot(normal, surfaceToLight) / (length(surfaceToLight) * length(normal));
brightness = clamp(brightness, 0, 1);
}
else
{
brightness = 1;
light.intensities = vec3(1,1,1);
}
vec3 diffuse;
// get fragment color from texture
vec4 surfaceColor = vec4(texture2D(texture, v_texcoord));
vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));
// if not transparent, ignore alpha value and set it to 1
if(!b_transparent)
surfaceColor.a = 1.0f;
// pass the data to ogl
gl_FragColor = vec4(brightness * light.intensities * surfaceColor.rgb, surfaceColor.a);
if(b_light)
{
// calculate normals in worldspace
vec3 normalWorld = normalize(n_matrix * v_surfaceNormal);
//get the surface - light vector (cause this is a point light)
vec3 surfaceToLight = normalize(light.position - v_surfacePosition);
// calculate the brightness depending on the angle
float diffuseCoefficient = max(0.0, dot(normalWorld, surfaceToLight));
// result diffuse color
diffuse = diffuseCoefficient * surfaceColor.rgb * light.intensities;
}
else
{
diffuse = surfaceColor.rgb;
}
// put all together
gl_FragColor = vec4(diffuse, surfaceColor.a);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

View File

@@ -12,9 +12,9 @@ attribute vec4 a_position;
attribute vec2 a_texcoord;
attribute vec3 a_normal;
varying vec2 v_texcoord;
varying vec3 v_position;
varying vec3 v_normal;
varying vec2 v_surfaceUV;
varying vec3 v_surfacePosition;
varying vec3 v_surfaceNormal;
void main()
{
@@ -23,7 +23,7 @@ void main()
// Pass data to fragment shader
// Value will be automatically interpolated to fragments inside polygon faces
v_texcoord = a_texcoord;
v_position = vec3(norm_matrix * m_matrix * a_position);
v_normal = a_normal;
v_surfaceUV = a_texcoord;
v_surfacePosition = vec3(norm_matrix * m_matrix * a_position);
v_surfaceNormal = a_normal;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB