Horde3D

Next-Generation Graphics Engine
It is currently 23.05.2024, 14:40

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: 12.05.2009, 16:46 
Offline

Joined: 27.04.2009, 11:58
Posts: 13
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;
}


Last edited by samsaga2 on 13.05.2009, 07:44, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: 12.05.2009, 20:55 
Offline
Engine Developer

Joined: 10.09.2006, 15:52
Posts: 1217
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.


Top
 Profile  
Reply with quote  
PostPosted: 13.05.2009, 07:44 
Offline

Joined: 27.04.2009, 11:58
Posts: 13
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;
}


Top
 Profile  
Reply with quote  
PostPosted: 13.05.2009, 08:30 
Offline
Engine Developer

Joined: 10.09.2006, 15:52
Posts: 1217
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group