Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated example - deferred rendering #4706

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions examples/shaders/resources/shaders/glsl100/deferred_shading.fs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#version 100
#version 300 es

precision highp float;

precision mediump float;
out vec4 finalColor;

// Input vertex attributes (from vertex shader)
varying vec2 fragTexCoord;
varying vec4 fragColor;
in vec2 texCoord;

// Input uniform values
uniform sampler2D gPosition;
uniform sampler2D gNormal;
uniform sampler2D gAlbedoSpec;
Expand All @@ -26,34 +25,32 @@ uniform vec3 viewPosition;
const float QUADRATIC = 0.032;
const float LINEAR = 0.09;

void main()
{
vec3 fragPosition = texture2D(gPosition, fragTexCoord).rgb;
vec3 normal = texture2D(gNormal, fragTexCoord).rgb;
vec3 albedo = texture2D(gAlbedoSpec, fragTexCoord).rgb;
float specular = texture2D(gAlbedoSpec, fragTexCoord).a;
void main() {
vec3 fragPosition = texture(gPosition, texCoord).rgb;
vec3 normal = texture(gNormal, texCoord).rgb;
vec3 albedo = texture(gAlbedoSpec, texCoord).rgb;
float specular = pow(texture(gAlbedoSpec, texCoord).a,8.0);

vec3 ambient = albedo*vec3(0.1);
vec3 ambient = albedo * vec3(0.03f);
vec3 viewDirection = normalize(viewPosition - fragPosition);

for (int i = 0; i < NR_LIGHTS; ++i)
for(int i = 0; i < NR_LIGHTS; ++i)
{
if(lights[i].enabled == 0) continue;
vec3 lightDirection = lights[i].position - fragPosition;
vec3 diffuse = max(dot(normal, lightDirection), 0.0)*albedo*lights[i].color.xyz;
vec3 diffuse = max(dot(normal, lightDirection), 0.0) * albedo * lights[i].color.xyz;

vec3 halfwayDirection = normalize(lightDirection + viewDirection);
float spec = pow(max(dot(normal, halfwayDirection), 0.0), 32.0);
vec3 specular = specular*spec*lights[i].color.xyz;
float spec = pow(max(dot(normal, halfwayDirection), 0.0), 16.0);
vec3 specular = specular * spec * lights[i].color.xyz;

// Attenuation
float distance = length(lights[i].position - fragPosition);
float attenuation = 1.0/(1.0 + LINEAR * distance + QUADRATIC*distance*distance);
float attenuation = 1.0 / (1.0 + LINEAR * distance + QUADRATIC * distance * distance);
diffuse *= attenuation;
specular *= attenuation;
ambient += diffuse + specular;
}

gl_FragColor = vec4(ambient, 1.0);
finalColor = vec4(ambient, 1.0);
}

19 changes: 8 additions & 11 deletions examples/shaders/resources/shaders/glsl100/deferred_shading.vs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#version 100
#version 300 es

// Input vertex attributes
attribute vec3 vertexPosition;
attribute vec2 vertexTexCoord;
precision highp float;

// Output vertex attributes (to fragment shader)
varying vec2 fragTexCoord;
in vec3 vertexPosition;
in vec2 vertexTexCoord;

void main()
{
fragTexCoord = vertexTexCoord;
out vec2 texCoord;

// Calculate final vertex position
gl_Position = vec4(vertexPosition, 1.0);
void main() {
gl_Position = vec4(vertexPosition, 1.0);
texCoord = vertexTexCoord;
}
52 changes: 22 additions & 30 deletions examples/shaders/resources/shaders/glsl100/gbuffer.fs
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
#version 100
#version 300 es

precision mediump float;
precision highp float;

// Input vertex attributes (from vertex shader)
varying vec3 fragPosition;
varying vec2 fragTexCoord;
varying vec3 fragNormal;
varying vec4 fragColor;
layout (location = 0) out vec4 gPosition;
layout (location = 1) out vec4 gNormal;
layout (location = 2) out vec4 gAlbedoSpec;

// TODO: Is there some alternative for GLSL100
//layout (location = 0) out vec3 gPosition;
//layout (location = 1) out vec3 gNormal;
//layout (location = 2) out vec4 gAlbedoSpec;
//uniform vec3 gPosition;
//uniform vec3 gNormal;
//uniform vec4 gAlbedoSpec;
in vec3 fragPosition;
in vec2 fragTexCoord;
in vec3 fragNormal;
in vec4 fragColor;

// Input uniform values
uniform sampler2D texture0; // Diffuse texture
uniform sampler2D specularTexture;
uniform vec4 colDiffuse;

void main()
{
// Store the fragment position vector in the first gbuffer texture
//gPosition = fragPosition;

// Store the per-fragment normals into the gbuffer
//gNormal = normalize(fragNormal);

// Store the diffuse per-fragment color
gl_FragColor.rgb = texture2D(texture0, fragTexCoord).rgb;

// Store specular intensity in gAlbedoSpec's alpha component
gl_FragColor.a = texture2D(specularTexture, fragTexCoord).r;
uniform sampler2D texture0;
uniform sampler2D texture1;

void main() {
// store the fragment position vector in the first gbuffer texture
gPosition = vec4(fragPosition,1.0);
// also store the per-fragment normals into the gbuffer
gNormal = vec4(normalize(fragNormal),1.0);
// and the diffuse per-fragment color
gAlbedoSpec.rgb = texture(texture0, fragTexCoord).rgb * colDiffuse.rgb;
// store specular intensity in gAlbedoSpec's alpha component
gAlbedoSpec.a = pow(texture(texture1, fragTexCoord).r*1.6,16.0);
}
60 changes: 15 additions & 45 deletions examples/shaders/resources/shaders/glsl100/gbuffer.vs
Original file line number Diff line number Diff line change
@@ -1,60 +1,30 @@
#version 100
#version 300 es

// Input vertex attributes
attribute vec3 vertexPosition;
attribute vec2 vertexTexCoord;
attribute vec3 vertexNormal;
attribute vec4 vertexColor;
precision highp float;

in vec3 vertexPosition;
in vec2 vertexTexCoord;
in vec3 vertexNormal;
in vec4 vertexColor;

out vec3 fragPosition;
out vec2 fragTexCoord;
out vec3 fragNormal;
out vec4 fragColor;

// Input uniform values
uniform mat4 matModel;
uniform mat4 matView;
uniform mat4 matProjection;

// Output vertex attributes (to fragment shader)
varying vec3 fragPosition;
varying vec2 fragTexCoord;
varying vec3 fragNormal;
varying vec4 fragColor;


// https://github.com/glslify/glsl-inverse
mat3 inverse(mat3 m)
{
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];
float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];
float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];

float b01 = a22*a11 - a12*a21;
float b11 = -a22*a10 + a12*a20;
float b21 = a21*a10 - a11*a20;

float det = a00*b01 + a01*b11 + a02*b21;

return mat3(b01, (-a22*a01 + a02*a21), (a12*a01 - a02*a11),
b11, (a22*a00 - a02*a20), (-a12*a00 + a02*a10),
b21, (-a21*a00 + a01*a20), (a11*a00 - a01*a10))/det;
}

// https://github.com/glslify/glsl-transpose
mat3 transpose(mat3 m)
{
return mat3(m[0][0], m[1][0], m[2][0],
m[0][1], m[1][1], m[2][1],
m[0][2], m[1][2], m[2][2]);
}

void main()
{
// Calculate vertex attributes for fragment shader
vec4 worldPos = matModel*vec4(vertexPosition, 1.0);
vec4 worldPos = matModel * vec4(vertexPosition, 1.0);
fragPosition = worldPos.xyz;
fragTexCoord = vertexTexCoord;
fragColor = vertexColor;

mat3 normalMatrix = transpose(inverse(mat3(matModel)));
fragNormal = normalMatrix*vertexNormal;
fragNormal = normalMatrix * vertexNormal;

// Calculate final vertex position
gl_Position = matProjection*matView*worldPos;
gl_Position = matProjection * matView * worldPos;
}
Loading
Loading