Lol, thanks Baker. Anyway, I couldn't get it work. Spike advices were very clear but probably my opengl skills are too low to create a working orthographic projection in FTE for project parallel shadows.
I adapted
this GLSL shader that mimic parallel light and I used
this site as reference for bias matrix and for learning how shadows are applied in fragment shader
This is the modification in Sh_GenShadowMap() in gl_shadow.c. I simply delete all the code regarding omni/spot and replaced with this. Just temp code.
Code: Select all
float left = -1;
float bottom = -1;
float znear = -1;
float right = 1;
float top = 1;
float zfar = 1;
Matrix4x4_CM_Orthographic(r_refdef.m_projection_std,left,right,bottom,top,znear,zfar);
memcpy(r_refdef.m_projection_view, r_refdef.m_projection_std, sizeof(r_refdef.m_projection_view));
In CSQC_UpdateView I created a simple light
Code: Select all
ltest = dynamiclight_add(cam.origin,cvar("render_light_radius"),[cvar("render_light_intensity"),cvar("render_light_intensity"),cvar("render_light_intensity")],0,"",0);
This is the GLSL shader created by Hugh Kennedy (MIT license)
Code: Select all
!!permu FOG
!!samps diffuse
!!cvarf render_sun_ground
!!cvarf render_sun_x
!!cvarf render_sun_y
!!cvarf render_sun_z
!!cvarf render_sun_back_x
!!cvarf render_sun_back_y
!!cvarf render_sun_back_z
!!cvarf render_sun_intensity
#include "sys/defs.h"
#include "sys/pcf.h"
#include "sys/fog.h"
#define shdwIntensity 0.1
varying vec2 tc;
varying vec3 eyevector,normalvector,lightvector;
varying vec4 modelvector,viewvector,vtexprojcoord,shadowCoord ;
uniform float cvar_render_sun_x,cvar_render_sun_y,cvar_render_sun_z,cvar_render_sun_ground;
uniform float cvar_render_sun_back_x,cvar_render_sun_back_y,cvar_render_sun_back_z,cvar_render_sun_intensity;
//Copyright (c) 2014 Hugh Kennedy - MIT License - https://github.com/hughsk/glsl-directional-light
//adapted to FTE engine by Antonio "toneddu2000" Latronico - antoniolatronico.com
#ifdef VERTEX_SHADER
void main (void)
{
vec3 eyeminusvertex = e_eyepos - v_position.xyz;
normalvector = -v_normal.xyz;
eyevector.x = dot(eyeminusvertex, v_svector.xyz);
eyevector.y = dot(eyeminusvertex, v_tvector.xyz);
eyevector.z = dot(eyeminusvertex, v_normal.xyz);
lightvector = l_lightposition - v_position.xyz;
const float right = 1.0;
const float bottom = -1.0;
const float left = -1.0;
const float top = 1.0;
const float far = 1.0;
const float near = -1.0;
mat4 shdwmat = mat4(
vec4(2.0 / (right - left), 0, 0, -(right + left) / (right - left)),
vec4(0, 2.0 / (top - bottom), 0, -(top + bottom) / (top - bottom)),
vec4(0, 0, -2.0 / (far - near), -(far + near) / (far - near)),
vec4(0, 0, 0, 1)
);
mat4 biasmat = mat4(
vec4(0.5, 0.0, 0.0, 0.0),
vec4( 0.0, 0.5, 0.0, 0.0),
vec4( 0.0, 0.0, 0.5, 0.0),
vec4(0.5, 0.5, 0.5, 1.0)
);
mat4 depthMVP = m_projection * m_view * m_model;
mat4 depthBiasMVP = biasmat*depthMVP;
vtexprojcoord = (shdwmat*biasmat*vec4(1.0,1.0,1.0, 1.0));
shadowCoord = depthBiasMVP * vec4(v_position.xyz, 1.0);
gl_Position = ftetransform();
}
#endif
#ifdef FRAGMENT_SHADER
vec3 directional_light(vec3 normal,vec3 light,vec3 surface,vec3 lightDirection,mat4 modelMatrix,mat4 viewMatrix,vec3 viewPosition,float shininess,float specularity)
{
vec3 direction = normalize((vec4(lightDirection, 1.0)).xyz);
vec3 halfDirection = normalize(direction + viewPosition);
vec3 tNormal = normalize((modelMatrix * vec4(normal, 1.0)).xyz);
float diffuse = max(dot(tNormal, direction), 0.0);
float halfDot = max(dot(tNormal, halfDirection), 0.0);
float specular = max(pow(halfDot, shininess), 0.0);
return max(light * (diffuse * surface + diffuse * specular * specularity), vec3(0.0));
}
vec3 directional_light(vec3 normal,vec3 light,vec3 surface,vec3 lightDirection,mat4 modelMatrix,mat4 viewMatrix,vec3 viewPosition)
{
vec3 direction = normalize((modelMatrix * vec4(lightDirection, 1.0)).xyz);
float diffuse = max(dot(normal, direction),shdwIntensity);
return max(light * diffuse * surface,vec3(0.0));
}
void main (void)
{
vec3 sky = vec3(0.510,0.247,0.000);
vec3 gnd = vec3(0.002,0.486,0.912);
vec3 direction1 = normalize(vec3(cvar_render_sun_x,cvar_render_sun_y, cvar_render_sun_z));
vec3 direction2 = normalize(vec3(cvar_render_sun_back_x, cvar_render_sun_back_y, cvar_render_sun_back_z));
vec3 surface = vec3(1.0,1.0,1.0);
vec3 color = vec3(0.3,0.3,0.3);
//realtime shadows
float visibility = 1.0;
if ( ShadowmapFilter(s_shadowmap, shadowCoord) < shadowCoord.z){
visibility = 0.2;
}
#ifdef GREENBRIGHT
color = vec3(0.059,0.750,0.000);
#endif
#ifdef GREENDARK
color = vec3(0.000,0.510,0.003);
#endif
#ifdef GREENYELLOW
color = vec3(0.484,0.840,0.032);
#endif
#ifdef BROWNBRIGHT
color = vec3(0.795,0.391,0.027);
#endif
#ifdef BROWNDARK
color = vec3(0.545,0.267,0.001);
#endif
#ifdef GREYBRIGHT
color = vec3(0.645,0.655,0.622);
#endif
#ifdef GREYDARK
color = vec3(0.507,0.515,0.489);
#endif
#ifdef GREYVERYDARK
color = vec3(0.369,0.375,0.356);
#endif
#ifdef RED
color = vec3(0.990,0.188,0.000);
#endif
#ifdef YELLOW
color = vec3(0.990,0.700,0.016);
#endif
vec3 composite = mix(surface,color,max(dot(normalvector, direction1),shdwIntensity));
color *= vec3(cvar_render_sun_intensity);
#ifdef SHDW
color = color + (visibility * l_lightcolour );
#endif
vec3 lightsun = vec3(1.000,0.878,0.000);//light sun (usually bright sun)
vec3 lightambient = vec3(0.081,0.593,0.685);//light gi (usually dark blue)
mat4 matrixModel = m_projection;
mat4 matrixView = m_invviewprojection;
vec3 lighting =
directional_light(normalvector, lightsun, color, direction1, matrixModel, matrixView, eyevector)
+ directional_light(normalvector, lightambient, color, direction2, matrixModel, matrixView, eyevector);
gl_FragColor = fog4(vec4(lighting,1.0));
gl_FragDepth = gl_FragCoord.z;
}
#endif
And this is the material with the BEMODE for rtlights. Note that diffusemap it's useless, since GLSL colors meshes by itself,it's there only to avoid FTE complaining about missing pass
Code: Select all
parallelshdw
{
//first set textures
diffusemap textures/env/green.tga
//second bemode for realtime lights
bemode rtlight
{
program ton_lightparallel#SHDW
}
}
This will make a nice diffuse parallel light with a counter ambient light, which it's cool.Look how round light is.
And this is the shader with the #SHDW flag... practically nothing useful!
As you can see, shadow is just a square sitting in the middle of scene. It's like it was placed at specific coordinates
Weird thing is that, even with FTE release without my modification, rendering is the same as above