paged geometry trees and shadows

adanar

02-01-2009 15:49:55

Hi,
my question is pretty basic, however I failed to find smtgh relevant in the forum, other than a topic related to disabling shadows for grass. However, I am concerned with trees and I want to enable shadows for the paged geometry trees. However, depending on the shadow technique I use, I get some strange results on the trees. Either they become black and white glow on them or their branches have a crude shadow-like frame around them. Has someone else experienced similar problems or is it my bad use of ogre shadows??

Thanx
Harris Papadakis

JohnJ

10-01-2009 07:07:24

I'm not sure, since I haven't extensively used Ogre's shadow system with PagedGeometry. From my experience it can be a little tedious to get Ogre shadows set up just right. What I would do is try adding a few trees without using PagedGeometry and try to get the shadows working, then try PagedGeometry. If you encounter problems with shadows only when using PagedGeometry, let me know and I'll try to help you get them working.

overseb

16-04-2009 11:14:31

I am very interested by this post, I have the same pb, any news ?

nargil

16-04-2009 17:36:38



You need to implement depth shadow mapping. Then you need to override the grass meterial. Easiest way would be to create a material called acording to your grass parameters. Look at the code in GrassLoader.cpp
StringUtil::StrStreamType tmpName;
tmpName << "GrassVS_";
if (animate)
tmpName << "anim_";
if (blend)
tmpName << "blend_";
tmpName << renderTechnique << "_";
tmpName << fadeTechnique << "_";
if (fadeTechnique == FADETECH_GROW || fadeTechnique == FADETECH_ALPHAGROW)
tmpName << maxHeight << "_";
tmpName << "vp";
const String vsName = tmpName.str();


So for example if you use:
base material - grass
render technique - GRASSTECHNIQUE_QUAD (0)
fade technique - FADETECH_ALPHAGROW (2)
grass max height - 0.4 world units

then the overriden material will be called: grass_GrassVS_anim_0_2_0.4_vp

You simply create an ogre material:

material grass_GrassVS_anim_0_2_0.4_vp
{
}


and implement you custom shadows to it. This material will prevent PG from creating an own material with shaders. Remember to create a vertex program that can receive

//Set vertex shader parameters
params->setNamedConstant("time", layer->waveCount);
params->setNamedConstant("frequency", layer->animFreq);

Vector3 direction = windDir * layer->animMag;
params->setNamedConstant("direction", Vector4(direction.x, direction.y, direction.z, 0));

or else it might crash.

I think we are allowed to use this as a base for our shaders ?
String vertexProgSource;
vertexProgSource =
"void main( \n"
" float4 iPosition : POSITION, \n"
" float4 iColor : COLOR, \n"
" float2 iUV : TEXCOORD0, \n"
" out float4 oPosition : POSITION, \n"
" out float4 oColor : COLOR, \n"
" out float2 oUV : TEXCOORD0, \n";

if (animate) vertexProgSource +=
" uniform float time, \n"
" uniform float frequency, \n"
" uniform float4 direction, \n";

if (lightingEnabled) vertexProgSource +=
" uniform float4 objSpaceLight, \n"
" uniform float4 lightDiffuse, \n"
" uniform float4 lightAmbient, \n";

if (fadeTechnique == FADETECH_GROW || fadeTechnique == FADETECH_ALPHAGROW) vertexProgSource +=
" uniform float grassHeight, \n";

if (renderTechnique == GRASSTECH_SPRITE || lightingEnabled) vertexProgSource +=
" float4 iNormal : NORMAL, \n";

vertexProgSource +=
" uniform float4x4 worldViewProj, \n"
" uniform float3 camPos, \n"
" uniform float fadeRange ) \n"
"{ \n"
" float4 position = iPosition; \n"
" float dist = distance(camPos.xz, position.xz); \n";

if (lightingEnabled) vertexProgSource +=
//Perform lighting calculations (no specular)
" float3 light = normalize(objSpaceLight.xyz - (iPosition.xyz * objSpaceLight.w)); \n"
" float diffuseFactor = max(dot(float4(0,1,0,0), light), 0); \n"
" oColor = (lightAmbient + diffuseFactor * lightDiffuse) * iColor; \n";
else vertexProgSource +=
" oColor = iColor; \n";

if (fadeTechnique == FADETECH_ALPHA || fadeTechnique == FADETECH_ALPHAGROW) vertexProgSource +=
//Fade out in the distance
" oColor.a = 2.0f - (2.0f * dist / fadeRange); \n";
else vertexProgSource +=
" oColor.a = 1.0f; \n";

vertexProgSource +=
" float oldposx = position.x; \n";

if (renderTechnique == GRASSTECH_SPRITE) vertexProgSource +=
//Face the camera
" float3 dirVec = (float3)position - (float3)camPos; \n"
" float3 p = normalize(cross(float4(0,1,0,0), dirVec)); \n"
" position += float4(p.x * iNormal.x, iNormal.y, p.z * iNormal.x, 0); \n";

if (animate) vertexProgSource +=
" if (iUV.y == 0.0f){ \n"
//Wave grass in breeze
" float offset = sin(time + oldposx * frequency); \n"
" position += direction * offset; \n"
" } \n";

if (blend && animate) vertexProgSource +=
" else { \n";
else if (blend) vertexProgSource +=
" if (iUV.y != 0.0f){ \n";

if (blend) vertexProgSource +=
//Blend the base of nearby grass into the terrain
" if (oColor.a >= 1.0f) \n"
" oColor.a = 4.0f * ((dist / fadeRange) - 0.1f); \n"
" } \n";

if (fadeTechnique == FADETECH_GROW || fadeTechnique == FADETECH_ALPHAGROW) vertexProgSource +=
" float offset = (2.0f * dist / fadeRange) - 1.0f; \n"
" position.y -= grassHeight * clamp(offset, 0, 1); ";

vertexProgSource +=
" oPosition = mul(worldViewProj, position); \n";

vertexProgSource +=
" oUV = iUV;\n"
"}";

vertexShader = HighLevelGpuProgramManager::getSingleton().createProgram(
vsName,
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
"cg", GPT_VERTEX_PROGRAM);

vertexShader->setSource(vertexProgSource);
vertexShader->setParameter("profiles", "vs_1_1 arbvp1");
vertexShader->setParameter("entry_point", "main");
vertexShader->load();
}

overseb

17-04-2009 09:16:04

thx, and you posted a nice pic
surely a stupid question but when you said that we must implement depth shadow mapping, it just means to activate Ogre shadow mapping with this lines :
m_pSceneMgr->setShadowTechnique(SHADOWTYPE_TEXTURE_ADDITIVE); ?
you are not talking about another technique ?

nargil

17-04-2009 17:23:42

http://www.ogre3d.org/wiki/index.php/Cu ... ow_Mapping

overseb

29-04-2009 10:33:34

ok I have read this article, it describes a way to create and to customize our own shadowmap algorithm. I dit not understand all the implementation but I will have a look at the shadow SDK samples and try to get it.

I understand that the grass can not receive shadow because it uses is own fragment shader that does not manage with shadows (am I right ?)
So I have create a material like you said to avoid the grass fragment shader use :

material grass_GrassVS_anim_1_0_500_vp
{
receive_shadows on
transparency_casts_shadows off

technique
{
pass
{
//lighting off
cull_hardware none
cull_software none
scene_blend alpha_blend
alpha_rejection greater_equal 128
//depth_write off

texture_unit
{
texture grass2.png
}
}
}
}


then I have activated shadow map technique :
m_sceneMgr->setShadowTechnique(SHADOWTYPE_TEXTURE_ADDITIVE);
m_sceneMgr->setShadowTexturePixelFormat(PF_L16);
m_sceneMgr->setShadowTextureCasterMaterial(StringUtil::BLANK);
m_sceneMgr->setShadowTextureReceiverMaterial(StringUtil::BLANK);
m_sceneMgr->setShadowTextureSelfShadow(false);
m_sceneMgr->setShadowTextureSettings(1024, 2);
m_sceneMgr->setShadowColour(ColourValue(0.1, 0.1, 0.1));


What I do not understand is why the grass still do not receive any shadow ?
Could you be more explicit please ? and send some samples ?

And I cannot use stencil buffer shadow algorithm because I have to many transparent surfaces (tree's leaves) ...
thx

overseb

30-04-2009 13:36:26

Well, after many tries I managed to get shadow on my grasses
the correct configuration is to use modulative shadow map
m_sceneMgr->setShadowTechnique(SHADOWTYPE_TEXTURE_MODULATIVE);
with this material
material grass
{
transparency_casts_shadows off
receive_shadows on
technique
{
pass
{
cull_hardware none
cull_software none
scene_blend alpha_blend
alpha_rejection greater_equal 128
...


http://www.flickr.com/photos/22788398@N08/3487950693/
and without adding new material or custom shadow shaders

but it is far away to be perfect, when I look enclosed, I see that the transparency parts of the grass quads receive shadow, which is ugly.
http://www.flickr.com/photos/22788398@N08/3487950739/in/photostream/
I think that is because transparency fragment are not used during the depth shadow map calculation.
adding this simple code in the shadow map calculation shader could do the job :
vec4 fvBaseColor = texture2D(baseMap, Texcoord);
if(fvBaseColor.a < 0.5)
// if texture has an alpha, discard fragment for this primitive
discard;


did you make something similar nargil ??
am I right ?

thx

overseb

05-05-2009 16:09:52

Well, I finally did it on my way

http://www.flickr.com/photos/22788398@N08/3503907623/

I used additive shadow map
m_sceneMgr->setShadowTechnique(SHADOWTYPE_TEXTURE_ADDITIVE);
with this material
material grass_GrassVS_0_0_500_vp
{
transparency_casts_shadows off
receive_shadows on
technique
{
pass
{
cull_hardware none
cull_software none
scene_blend alpha_blend
alpha_rejection greater_equal 128

vertex_program_ref GrassVS
{
}

fragment_program_ref GrassFS
{
}

texture_unit
{
colour_op alpha_blend
texture grass.png
}
}
}
}


the vertex shader :

// GLSL vertex shader
// apply model view transformation

varying vec2 Texcoord;
varying vec4 Color;

void main(void)
{
Texcoord = gl_MultiTexCoord0.xy;
Color = gl_Color;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}


and the fragment shader
// GLSL fragment shader

uniform sampler2D baseMap;

varying vec2 Texcoord;
varying vec4 Color;

void main()
{
vec4 outColor = Color;

// get color from texture
vec4 fvBaseColor = texture2D(baseMap, Texcoord);
if(fvBaseColor.a < 0.5)
// if texture has an alpha, discard fragment for this primitive
discard;
else
// update fragment color with depth value
outColor = vec4(vec3(fvBaseColor), 1.0) * Color;

gl_FragColor = outColor;
}


@nargil
I think this way is more simple than yours !
by the way, I would like you to explain me a what custom shadows are and why must I have to use it for my grass to get a shadow on the grass ?
what have you done exactly ? what are your shadow shaders, your materials ?
Could you please be more explicit than your last "one sentence link answer" ? :roll: