REVIEWED: example: shaders_deferred_render
-WIP-
Not working but shader compiles and example runs... not sure if deferred rendering could be supported in OpenGL ES 2.0.
This commit is contained in:
parent
313067d749
commit
7c7b087efb
6 changed files with 179 additions and 9 deletions
|
@ -1042,10 +1042,10 @@ shaders/shaders_custom_uniform: shaders/shaders_custom_uniform.c
|
||||||
shaders/shaders_deferred_render: shaders/shaders_deferred_render.c
|
shaders/shaders_deferred_render: shaders/shaders_deferred_render.c
|
||||||
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) \
|
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) \
|
||||||
--preload-file shaders/resources/fudesumi.png@resources/fudesumi.png \
|
--preload-file shaders/resources/fudesumi.png@resources/fudesumi.png \
|
||||||
--preload-file shaders/resources/shaders/glsl330/gbuffer.vs@resources/shaders/glsl330/gbuffer.vs \
|
--preload-file shaders/resources/shaders/glsl100/gbuffer.vs@resources/shaders/glsl100/gbuffer.vs \
|
||||||
--preload-file shaders/resources/shaders/glsl330/gbuffer.fs@resources/shaders/glsl330/gbuffer.fs \
|
--preload-file shaders/resources/shaders/glsl100/gbuffer.fs@resources/shaders/glsl100/gbuffer.fs \
|
||||||
--preload-file shaders/resources/shaders/glsl330/deferred_shading.fs@resources/shaders/glsl330/deferred_shading.fs \
|
--preload-file shaders/resources/shaders/glsl100/deferred_shading.vs@resources/shaders/glsl100/deferred_shading.vs \
|
||||||
--preload-file shaders/resources/shaders/glsl330/deferred_shading.fs@resources/shaders/glsl330/deferred_shading.fs
|
--preload-file shaders/resources/shaders/glsl100/deferred_shading.fs@resources/shaders/glsl100/deferred_shading.fs
|
||||||
|
|
||||||
shaders/shaders_eratosthenes: shaders/shaders_eratosthenes.c
|
shaders/shaders_eratosthenes: shaders/shaders_eratosthenes.c
|
||||||
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) \
|
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) \
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
#version 100
|
||||||
|
|
||||||
|
precision mediump float;
|
||||||
|
|
||||||
|
// Input vertex attributes (from vertex shader)
|
||||||
|
varying vec2 fragTexCoord;
|
||||||
|
varying vec4 fragColor;
|
||||||
|
|
||||||
|
// Input uniform values
|
||||||
|
uniform sampler2D gPosition;
|
||||||
|
uniform sampler2D gNormal;
|
||||||
|
uniform sampler2D gAlbedoSpec;
|
||||||
|
|
||||||
|
struct Light {
|
||||||
|
int enabled;
|
||||||
|
int type; // Unused in this demo.
|
||||||
|
vec3 position;
|
||||||
|
vec3 target; // Unused in this demo.
|
||||||
|
vec4 color;
|
||||||
|
};
|
||||||
|
|
||||||
|
const int NR_LIGHTS = 4;
|
||||||
|
uniform Light lights[NR_LIGHTS];
|
||||||
|
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;
|
||||||
|
|
||||||
|
vec3 ambient = albedo*vec3(0.1);
|
||||||
|
vec3 viewDirection = normalize(viewPosition - fragPosition);
|
||||||
|
|
||||||
|
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 halfwayDirection = normalize(lightDirection + viewDirection);
|
||||||
|
float spec = pow(max(dot(normal, halfwayDirection), 0.0), 32.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);
|
||||||
|
diffuse *= attenuation;
|
||||||
|
specular *= attenuation;
|
||||||
|
ambient += diffuse + specular;
|
||||||
|
}
|
||||||
|
|
||||||
|
gl_FragColor = vec4(ambient, 1.0);
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
#version 100
|
||||||
|
|
||||||
|
// Input vertex attributes
|
||||||
|
attribute vec3 vertexPosition;
|
||||||
|
attribute vec2 vertexTexCoord;
|
||||||
|
|
||||||
|
// Output vertex attributes (to fragment shader)
|
||||||
|
varying vec2 fragTexCoord;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
fragTexCoord = vertexTexCoord;
|
||||||
|
|
||||||
|
// Calculate final vertex position
|
||||||
|
gl_Position = vec4(vertexPosition, 1.0);
|
||||||
|
}
|
36
examples/shaders/resources/shaders/glsl100/gbuffer.fs
Normal file
36
examples/shaders/resources/shaders/glsl100/gbuffer.fs
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
#version 100
|
||||||
|
|
||||||
|
precision mediump float;
|
||||||
|
|
||||||
|
// Input vertex attributes (from vertex shader)
|
||||||
|
varying vec3 fragPosition;
|
||||||
|
varying vec2 fragTexCoord;
|
||||||
|
varying vec3 fragNormal;
|
||||||
|
varying vec4 fragColor;
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
// Input uniform values
|
||||||
|
uniform sampler2D texture0; // Diffuse texture
|
||||||
|
uniform sampler2D specularTexture;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
60
examples/shaders/resources/shaders/glsl100/gbuffer.vs
Normal file
60
examples/shaders/resources/shaders/glsl100/gbuffer.vs
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
#version 100
|
||||||
|
|
||||||
|
// Input vertex attributes
|
||||||
|
attribute vec3 vertexPosition;
|
||||||
|
attribute vec2 vertexTexCoord;
|
||||||
|
attribute vec3 vertexNormal;
|
||||||
|
attribute vec4 vertexColor;
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
fragPosition = worldPos.xyz;
|
||||||
|
fragTexCoord = vertexTexCoord;
|
||||||
|
fragColor = vertexColor;
|
||||||
|
|
||||||
|
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
|
||||||
|
fragNormal = normalMatrix*vertexNormal;
|
||||||
|
|
||||||
|
// Calculate final vertex position
|
||||||
|
gl_Position = matProjection*matView*worldPos;
|
||||||
|
}
|
|
@ -76,11 +76,11 @@ int main(void)
|
||||||
Model cube = LoadModelFromMesh(GenMeshCube(2.0f, 2.0f, 2.0f));
|
Model cube = LoadModelFromMesh(GenMeshCube(2.0f, 2.0f, 2.0f));
|
||||||
|
|
||||||
// Load geometry buffer (G-buffer) shader and deferred shader
|
// Load geometry buffer (G-buffer) shader and deferred shader
|
||||||
Shader gbufferShader = LoadShader("resources/shaders/glsl330/gbuffer.vs",
|
Shader gbufferShader = LoadShader(TextFormat("resources/shaders/glsl%i/gbuffer.vs", GLSL_VERSION),
|
||||||
"resources/shaders/glsl330/gbuffer.fs");
|
TextFormat("resources/shaders/glsl%i/gbuffer.fs", GLSL_VERSION));
|
||||||
|
|
||||||
Shader deferredShader = LoadShader("resources/shaders/glsl330/deferred_shading.vs",
|
Shader deferredShader = LoadShader(TextFormat("resources/shaders/glsl%i/deferred_shading.vs", GLSL_VERSION),
|
||||||
"resources/shaders/glsl330/deferred_shading.fs");
|
TextFormat("resources/shaders/glsl%i/deferred_shading.fs", GLSL_VERSION));
|
||||||
deferredShader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(deferredShader, "viewPosition");
|
deferredShader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(deferredShader, "viewPosition");
|
||||||
|
|
||||||
// Initialize the G-buffer
|
// Initialize the G-buffer
|
||||||
|
@ -130,7 +130,6 @@ int main(void)
|
||||||
if (!rlFramebufferComplete(gBuffer.framebuffer))
|
if (!rlFramebufferComplete(gBuffer.framebuffer))
|
||||||
{
|
{
|
||||||
TraceLog(LOG_WARNING, "Framebuffer is not complete");
|
TraceLog(LOG_WARNING, "Framebuffer is not complete");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now we initialize the sampler2D uniform's in the deferred shader.
|
// Now we initialize the sampler2D uniform's in the deferred shader.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue