Horde3D
http://horde3d.org/forums/

Lightingmaps and shadows [SOLVED]
http://horde3d.org/forums/viewtopic.php?f=2&t=737
Page 1 of 1

Author:  samsaga2 [ 12.05.2009, 16:46 ]
Post subject:  Lightingmaps and shadows [SOLVED]

I'm trying to implement a lightmap shader. But I don't know to add the shadows from the other nodes.

Horde3d it's a great engine but it needs better documentation.

Code:
[[FX]]
<!--
// =================================================================================================
   Lightmap Shader
// =================================================================================================
-->

<Sampler id="albedoMap" />
<Sampler id="lightMap" />

<Context id="AMBIENT">
   <Shaders vertex="VS_GENERAL" fragment="FS_AMBIENT" />
</Context>

[[VS_GENERAL]]
// =================================================================================================

#include "shaders/utilityLib/vertCommon.glsl"

uniform vec3 viewer;
attribute vec2 texCoords0;
varying vec2 texCoords;

void main( void )
{
   // Calculate world space position
   vec4 pos = calcWorldPos( gl_Vertex );
   
   // Calculate texture coordinates and clip space position
   texCoords = texCoords0;
   gl_Position = gl_ModelViewProjectionMatrix * pos;
}

[[FS_AMBIENT]]   
// =================================================================================================

uniform sampler2D albedoMap;
uniform sampler2D lightMap;
varying vec2 texCoords;

void main( void )
{
   vec3 newCoords = vec3( texCoords, 0 );

   // Flip texture vertically to match the GL coordinate system
   newCoords.t *= -1.0;

   vec3 albedo = texture2D( albedoMap, newCoords.st ).rgb;
   vec3 light = texture2D( lightMap, newCoords.st ).rgb;
   
   gl_FragColor.rgb = albedo * light;
}

Author:  marciano [ 12.05.2009, 20:55 ]
Post subject:  Re: Lightingmaps and shadows

Are you sure you mean lightmap? A lightmap is a special precomputed texture which stores the light contribution (and implicitely shadows) for a surface.

Modifying shaders requires some graphics programming knowledge. It is outside the scope of the Horde documentation to cover these basics. There are a lot of good books.
For shadows we use a shadow maps. You can have a look at fragLighting.glsl to see how you can apply the shadow map.

Author:  samsaga2 [ 13.05.2009, 07:44 ]
Post subject:  Re: Lightingmaps and shadows

The problem was that I don't know things like what is ATTRIBPASS.

Anyway I've found the solution. The shader for lightmaps:

Code:
[[FX]]
<!--
// =================================================================================================
   Lightmap Shader
// =================================================================================================
-->

<Sampler id="albedoMap" />
<Sampler id="lightMap" />

<Context id="LIGHTING">
   <Shaders vertex="VS_GENERAL" fragment="FS_LIGHTING" />
   <RenderConfig writeDepth="false" blendMode="MULT" />
</Context>

<Context id="AMBIENT">
   <Shaders vertex="VS_GENERAL" fragment="FS_AMBIENT" />
</Context>

[[VS_GENERAL]]
// =================================================================================================

#include "shaders/utilityLib/vertCommon.glsl"

uniform vec3 viewer;
attribute vec2 texCoords0;
attribute vec3 normal;

varying vec4 pos, vsPos;
varying vec2 texCoords;
varying vec3 tsbNormal;

void main( void )
{
   // Calculate normal
   tsbNormal = calcWorldVec( normal );

   // Calculate world space position
   pos = calcWorldPos( gl_Vertex );

   vsPos = calcViewPos( pos );
   
   // Calculate texture coordinates and clip space position
   texCoords = texCoords0;
   gl_Position = gl_ModelViewProjectionMatrix * pos;
}

[[FS_LIGHTING]]
// =================================================================================================

#include "shaders/utilityLib/fragLighting.glsl" />

uniform sampler2D albedoMap;
uniform sampler2D lightMap;

varying vec4 pos, vsPos;
varying vec2 texCoords;
varying vec3 tsbNormal;

vec3 calcLightmapShadow( const vec3 pos, const vec3 normal, const float viewDist, const float ambientIntensity )
{
   vec3 light = lightPos.xyz - pos;
   
   // Distance
   float lightDist = length( light ) / lightPos.w;
   light = normalize( light );
   
   // Light intensity
   vec3 col = vec3(1,1,1);
   
   // Shadow
   if( dot( normal, light ) > 0.0)
   {   
    vec4 projShadow = shadowMats[3] * vec4( pos, 1.0 );
    if( viewDist < shadowSplitDists.x ) projShadow = shadowMats[0] * vec4( pos, 1.0 );
    else if( viewDist < shadowSplitDists.y ) projShadow = shadowMats[1] * vec4( pos, 1.0 );
    else if( viewDist < shadowSplitDists.z ) projShadow = shadowMats[2] * vec4( pos, 1.0 );
   
    projShadow.z = lightDist;
    projShadow.xy /= projShadow.w;
   
    float shadowFac = PCF( projShadow );
    col *= max( shadowFac, ambientIntensity );
  }
   
   return col;
}

void main( void )
{
   vec3 newCoords = vec3( texCoords, 0 );
   
   // Flip texture vertically to match the GL coordinate system
   newCoords.t *= -1.0;

   vec3 normal = tsbNormal;
   vec3 newPos = pos.xyz;
   
   gl_FragColor.rgb = calcLightmapShadow( newPos, normalize( normal ), -vsPos.z, 0.3 );
}

[[FS_AMBIENT]]   
// =================================================================================================

#include "shaders/utilityLib/fragLighting.glsl" />

uniform sampler2D albedoMap;
uniform sampler2D lightMap;

varying vec4 pos;
varying vec2 texCoords;

void main( void )
{
   vec3 newCoords = vec3( texCoords, 0 );

   // Flip texture vertically to match the GL coordinate system
   newCoords.t *= -1.0;

   vec3 albedo = texture2D( albedoMap, newCoords.st ).rgb;
   vec3 light = texture2D( lightMap, newCoords.st ).rgb;
   
   gl_FragColor.rgb = albedo * light;
}

Author:  marciano [ 13.05.2009, 08:30 ]
Post subject:  Re: Lightingmaps and shadows [SOLVED]

samsaga2 wrote:
The problem was that I don't know things like what is ATTRIBPASS.

I see, that is something related to the pipeline stages. ATTRIBPASS is defined in the deferred pipeline. The general system is described in the pipeline documentation.

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/