Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Member Since 24 Aug 2001
Offline Last Active Nov 22 2014 10:21 AM

Topics I've Started

Slope-scale depth bias shadow map in HLSL

05 November 2014 - 02:41 PM

I have been searching the web for days for an answer to this, but I apparently cannot find what I need to, or I am overlooking the answer and not knowing it.


I have the issue of projective-aliasing happening with the terrain in my game, and apparently I need to use a slope-scale bias as well to calculate the shadow.


I know that the algorithm is:


\Where m = max( | ∂z/∂x | , | ∂z/∂y | )


But apparently I am uncertain how to implement this inside of an HLSL shader.  I already use a depth bias, I just need to compute the slope-scale bias as well.


Here is my code:


To render the shadow map:

technique Technique0Shadow
pass Pass0
Lighting = False;
CullMode = NONE;

VertexShader = compile vs_2_0 VS_Shadow();
PixelShader  = compile ps_2_0 PS_Shadow();

shadowType VS_Shadow( vertex IN )
shadowType OUT;

OUT.position = mul( worldViewProj, float4(IN.position, 1) );

OUT.fDepth.z = OUT.position.z;

return OUT;
pixel PS_Shadow( shadowType IN )
    pixel OUT;

    OUT.color = float4(IN.fDepth.z,IN.fDepth.z,IN.fDepth.z,1.0f);

    return OUT;

And here is the HLSL code for rendering the shadow onto the terrain ( edited for simplicity )


Vertex Shader:

fragment TerrainVS_Shadow( vertex IN )
    fragment OUT;
    OUT.color = float4(IN.color.r,IN.color.g,IN.color.b,IN.color.a);

    OUT.hposition = mul( worldViewProj, float4(IN.position, 1) );

    OUT.vProjCoord = mul( float4(IN.position, 1), g_matTexture );
    OUT.vProjCoord2 = mul( float4(IN.position, 1), g_matTexture2 );

    float4 posWorld = mul(float4(IN.position, 1), worldMat);

    OUT.distCalc = posWorld;

    return OUT;


pixel shader, the depth bias is 0.0005f;

pixel TerrainPS_Shadow( fragment IN )
    pixel OUT;
if ( IN.vProjCoord.x/IN.vProjCoord.w < 1.0f && IN.vProjCoord.x/IN.vProjCoord.w > 0.0f && IN.vProjCoord.y/IN.vProjCoord.w < 1.0f && IN.vProjCoord.y/IN.vProjCoord.w > 0.0f )
    fShadowTerm = tex2Dproj( ShadowSampler, IN.vProjCoord ) < (IN.vProjCoord.z - 0.005f) ? 0.4f : 1.0f;

OUT.color = float4(0,0,0,fShadowTerm  );

    return OUT;

My question is, how do I implement the slope-scale bias into my code as well?  How do I add the equation: m*SLOPESCALE + DEPTHBIAS as the overall bias so I can eliminate my projective-aliasing?


Thanks for any info you can give!




DirectX Save Render Target as a Separate Texture

15 September 2014 - 08:43 PM

I am having an issue beginning to implement cascading shadow maps.  I already have shadows already rendering, however when I try to render multiple shadow maps with the same code, I am unable to "capture" the current shadow map texture to use for the render loop.


For example I am trying to render 2 shadow maps using two different ortho views:


for( int lpShadow = 0; lpShadow < 2; lpShadow++ )

    if( lpShadow == 0 )
        D3DXMatrixOrthoRH( &shadowProj, (float)(d3ddm.Width)/10,(float)(d3ddm.Height)/10, -60.0f, 60.0f );
    else if( lpShadow == 1 )
        D3DXMatrixOrthoRH( &shadowProj, (float)(d3ddm.Width)/40,(float)(d3ddm.Height)/40, -10.0f, 10.0f );

    D3DXMATRIX matLightViewProj = shadowWorld * shadowView * shadowProj;
    g_pEffect->SetMatrix( "g_matLightViewProj", &matLightViewProj );

    // Grab the old render target and depth buffer
    g_pd3dDevice->GetRenderTarget( 0, &g_pOldColorRT );
    g_pd3dDevice->GetDepthStencilSurface( &g_pOldDepthRT );
    // Render the scene depth to the shadow map
    g_pd3dDevice->SetRenderTarget( 0, g_pShadowSurf );
    g_pd3dDevice->SetDepthStencilSurface( g_pShadowDepth );
    // Clear the viewport
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xFFFFFFFF, 1.0f, 0 );

    //Main rendering code loop here....

    // Restore the render target and depth buffer
    g_pd3dDevice->SetRenderTarget( 0, g_pOldColorRT );
    g_pd3dDevice->SetDepthStencilSurface( g_pOldDepthRT );
    SAFE_RELEASE( g_pOldColorRT );
    SAFE_RELEASE( g_pOldDepthRT );

    //Then finally put the texture into the shader
    g_pEffect->SetTexture( "tShadowMap2", g_pShadowMap );


I initialize the textures and surfaces like so

if( FAILED( g_pd3dDevice->CreateTexture( VGlobal::SHADOW_MAP_SIZE, VGlobal::SHADOW_MAP_SIZE, 1, D3DUSAGE_RENDERTARGET,
D3DFMT_R32F, D3DPOOL_DEFAULT, &g_pShadowMap, NULL ) ) )
    MessageBox( hWnd, "Unable to create shadow map!", ERROR, MB_OK | MB_ICONERROR );
    return E_FAIL;

// Grab the texture's surface
g_pShadowMap->GetSurfaceLevel( 0, &g_pShadowSurf );

My issue is I am failing to understand how to loop through my Shadow Map rendering loop above, and extract the texture out of g_pShadowMap for each loop that I want to render.  I simply want to make it its own texture resource, use it in the shader for the current loop, then discard it at the end of the render process.  Right now it only uses the last surface as all of its shadow map textures which is quite obvious.


I have tried using StretchRect(), CopySurfaceToSurface(), many other types of copy commands, but I cannot seem to grasp how to simply copy a texture resource out of the shadow map render target, and make it a separate texture out of g_pShadowMap.


I have also tried to use multiple render targets for each loop, however I do not seem to understand or grasp how this is possible.  I also tried to make g_pShadowMap an array, but simply assigning the second element to the render target does not seem to work.


Thanks for any help!


Detecting a shadow z-depth collision under terrain polygons

10 September 2014 - 01:42 AM

I may have been looking in the wrong places for this answer, but I cannot find anything that answers this question, if you have an answer that would be great!


I have just implemented shadow maps into my game.  They seem to work fine, however I have the issue where the z-depth calculation still considered a polygon of my terrain not shadowed when the light vector is positioned below the terrain, and I am looking to see if there is a simple solution to this.


For example, if the light vector in the world is placed underneath the terrain, and light upward towards the terrain, the z-depth buffer still collides with the polygon exactly, thus flagging the location is not shaded, as in normal lighting calculations.


My question is, is there a simply way to calculate that the ray is colliding with the polygon underneath it like in the shader?  Is there a way to tell if the light vector calculation hit a counter clockwise, or clockwise polygon?


One answer I was thinking was to make a large outside rendered box on the edge of the rendered world below the terrain, that way the light vector will collide with a polygon underneath the terrain at all times.  However this is a problem since this does not help with terrain on the edge that just ends, it would be tough to render a vertical wall slice for each polygon terrain just for these calculations.


Another method I was thinking of was re-render the entire terrain but at a slightly lower height, that way it would hit the lower terrain before the upper terrain, but that would be massively un-optimized and not a perfect solution. 


Again thanks for any help!

Vantage: Prehistoric Simulation MMO

10 March 2014 - 02:13 PM

I have finally gotten my game to the point where I am beginning to expand the player base beyond a few testers, and I am now starting to advertise and expand the exposure of the game.
Vantage is a Prehistoric Simulation MMO.  Your main objective is simple... to stay alive!
You explore the massive world searching for food, creating primitive tools and weapons to create, hunt, and craft other items to make your life
Attached File  ls1.png   1.06MB   1 downloads
Create fires at night using some tinder and wood so you don't freeze at night.
Attached File  fire.png   938.69KB   1 downloads
Build a hut to live in and call 'home'.
Attached File  placehut.jpg   185.25KB   1 downloads
Find wild plants, take their seeds and plant crops to become more self-sustaining.
Attached File  farming.png   1.68MB   3 downloads
Create some clothes to keep yourself warm and well protected, by skinning dead animals and tanning the hides.
And much, much more!
Vantage is a game I have been working on longer than I care to say.  It is my life's work and I am proud to still be working on it, and I
believe in the concept of the game and I hope that other people do as well, and find the game fun, entertaining, and also educational.
The game is based heavily around a complex crafting mechanic.  You must craft all the items you need to survive.  In addition, a lot of
research is done to make the crafting mechanics for each item as realistic as possible.  For example: to make a fire, you need a wood spindle,
wood hearth, and some tinder (dry grass for example) just like in primitive times.  You attempt to make the fire with those items and you may fail
if your skills are not good enough.
Attached File  crafts.png   330.37KB   3 downloads
The game engine is my own custom engine I call VantEdit.  The engine renders a fully seamless world (no loading screens) of any definable size.
The world is created using a tectonic plate simulation system that simulated millions of years of tectonic shifting, collision, and terrain
erosion, etc.  The final result is completely terraformed using my custom terraforming algorithms, and the final world is streamed to the user
as they play.
You can interact with every single item within the game, from trees to rocks and animals.  Depending on the size of the world, there can be
millions of objects, or more!  You can take an apple from one of thousands of apple trees, or extract the water from a cactus and take it
with you in newly created coconut cup.
My ultimate objective for the game is to make a complete technological skill tree for people to play through, from primitive times all the way
to modern times.  By implementing items, skills and crafts one at a time.  The VantEdit interface has a full editor system to quickly and easily
allow for me to do this.
But of course, the absolute main object of the game is to make it fun!  If the game is not fun, there is no reason to make it!
Currently the game is completely free to play, I would greatly appreciate and suggestions/comments/bugs you can give me, it would be extremely valuable and helpful to me.
A full interactive in-game tutorial is included as you play the game.
Direct Vantage Download link (installer): http://www.crystaldragon.com/VantageInstaller.exe
Vantage Facebook page (currently the main community page): https://www.facebook.com/pages/Vantage-Prehistoric-Simulation-Game/265649860135120
You can also email me with any questions/bugs at carradine@crystaldragon.com
Thanks, and enjoy!

How to allow file create/read/write/modify access in Program Files for a patcher

23 October 2013 - 12:25 PM

I have a auto-patcher for my game which downloads and updates files just like most patchers for games.  I create a dummy file called "temp.xxx", insert the file data into the file as it gets transferred from the server, then once the data is all received, I call rename() to rename the file to the actual name, and the function also moves it to the correct destination directory.


However, since many users have access restriction to creating/deleting/renaming files that need confirmations, file creation fails.


The patcher works fine if I install the game into the Users/Appdata directory where most users have full rights, however some people get the same error there as well.


To create a file I simply use:


std::ofstream recvFile;

recvFile.open( VGlobal::CURRWORKDIR + VGlobal::CurrTempFileDir + "Temp.txt", std::ios_base::out | std::ios_base::binary );


 So my question is, how can I get around this access restriction?  Can I change the folder settings in code?  Should I use something other than recvFile.open(...), and rename() to change the file name and move the file to another location?


Thanks for any help.