• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
suliman

fog in hlsl (shader)

11 posts in this topic

Hi

Im using hlsl code from a forum that makes textures splatting on my terrain (i use a directx engine, irrlicht). Now i need fog but im a bit confused on how to add it.

I look at examples like this one:
[url="http://communistgames.blogspot.se/2010/10/fog-in-hlsl.html"]http://communistgame...og-in-hlsl.html[/url]

But im still confused on how to add this to my own shader code I already have. I have figured out i can scale the color of the entire terrain by multiplying the diffuse-color I return in the PS_OUTPUT ps_main(in VS_OUTPUT input) function but dont understand how the shader code can use/know the actual distance from the camera (this is needed to do fog right?). Linear fog is fine for now.

The output function i have now is
[CODE]
PS_OUTPUT ps_main(in VS_OUTPUT input)
{
float texScale = 1.0;
PS_OUTPUT output = (PS_OUTPUT)0;
vector a = tex2D(AlphaMap, input.alphamap);
vector i = tex2D(TextureFour, mul(input.tex, texScale));
vector j = tex2D(TextureFive, mul(input.tex, texScale));
vector k = tex2D(TextureSix, mul(input.tex, texScale));

float4 oneminusx = 1.0 - a.x;
float4 oneminusy = 1.0 - a.y;
float4 oneminusz = 1.0 - a.z;
vector l = a.x * i + oneminusx * i;
vector m = a.y * j + oneminusy * l;
vector n = a.z * k + oneminusz * m;
output.diffuse = n;
return output;
}
[/CODE]

Thankful for any help
Erik Edited by suliman
0

Share this post


Link to post
Share on other sites
Without knowing the contents of the VS_OUTPUT structure it is hard to give precise instructions.
However, probably you can include the camera/view space position to the VS_OUTPUT structure (although clip space position can be used too). The position's z-component can be used as a distance from the view point. The typical fog formulas are available in MSDN or wikipedia.

Cheers!
0

Share this post


Link to post
Share on other sites
Go through the D3D9 samples in the SDK, they show you how to work with shader constants either with or without the effects framework. BasicHLSL and HLSLWithoutEffects should be relevent.
0

Share this post


Link to post
Share on other sites
The structure is simply
[CODE]
struct PS_OUTPUT
{
float4 diffuse : COLOR0;
};
[/CODE]

I can change the diffuse before returning it and this has its desired effect. I just need to know how to know the distance to the camera so i can interpolate the existing color with my fog color.

I know this might come off as lazy but ive skimmed through some hlsl tutorials and they are all very extensive and i just need this to move on with all the other things in the project that has now come to an halt (learning shaders properly must be a later task for me right now).

Thanks for your help!
Erik

The fulll shader code is posted below for reference
[CODE]
float4x4 matViewProjection : ViewProjection;
sampler AlphaMap = sampler_state
{
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};
sampler TextureOne = sampler_state
{
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};
sampler TextureTwo = sampler_state
{
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};
sampler TextureThree = sampler_state
{
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};



struct VS_INPUT
{
float4 Position : POSITION0;
float2 alphamap : TEXCOORD0;
float2 tex : TEXCOORD1;
};
struct VS_OUTPUT
{
float4 Position : POSITION0;
float2 alphamap : TEXCOORD0;
float2 tex : TEXCOORD1;
};
struct PS_OUTPUT
{
float4 diffuse : COLOR0;
};
VS_OUTPUT vs_main( VS_INPUT Input )
{
VS_OUTPUT Output;
Output.Position = mul( Input.Position, matViewProjection );
Output.alphamap = Input.alphamap;
Output.tex = Input.tex;
return( Output );
}
PS_OUTPUT ps_main(in VS_OUTPUT input)
{
float texScale = 1.0;
PS_OUTPUT output = (PS_OUTPUT)0;
vector a = tex2D(AlphaMap, input.alphamap);
vector i = tex2D(TextureOne, mul(input.tex, texScale));
vector j = tex2D(TextureTwo, mul(input.tex, texScale));
vector k = tex2D(TextureThree, mul(input.tex, texScale));

float4 oneminusx = 1.0 - a.x;
float4 oneminusy = 1.0 - a.y;
float4 oneminusz = 1.0 - a.z;
vector l = a.x * i + oneminusx * i;
vector m = a.y * j + oneminusy * l;
vector n = a.z * k + oneminusz * m;
output.diffuse = n;

return output;
}
technique Default_DirectX_Effect
{
pass Pass_0
{
VertexShader = compile vs_2_0 vs_main();
PixelShader = compile ps_2_0 ps_main();
}
}
[/CODE] Edited by suliman
0

Share this post


Link to post
Share on other sites
Need something like this?

in HLSL:
float3 camposition; // put it before 'float4x4 matViewProjection : ViewProjection;' (not in shader code body)

in cpp:
float campos[] = {0,0,-100}; // some random nums for you
pEffect->SetValue("camposition",campos,3*sizeof(float)); // pEffect is a valid ID3DXEffect object

For fog, you need the distance between the pixels and the camrea, vertex shader knows about vertex positions, so just give that to the pixel shader.

struct VS_OUTPUT
{
float4 Position : POSITION0;
float2 alphamap : TEXCOORD0;
float2 tex : TEXCOORD1;
float3 pixelcoord: TEXCOORD2; // thadam!!
};

in VS:

Output.pixelcoord = input.Position.xyz; // world space, i suppose

in PS:

float distance = length(input.pixelcoord - camposition); // dist between pixel and camera

// do some fancy fogging with it....
0

Share this post


Link to post
Share on other sites
Alternatively, you can use shader constants, which is what MJP was referring to. The syntax is as such:

In hlsl at global scope:
float3 camPosition : register( c0 );

In cpp:
float camPosition[] = {0,0,0}
D3DDEVICE->SetVertexShaderConstantF( 0, camPosition, 1);

For reference you can use [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb509704(v=vs.85).aspx"]this[/url] msdn page. Shader constants can be a great performance boost and helpful.
0

Share this post


Link to post
Share on other sites
both D3DDEVICE and pEffect give me undeclared identifier.
Do they need to be created (i guess so)? I dont find how to in the msdn page or its general search.

thanks for your help
0

Share this post


Link to post
Share on other sites
D3DDEVICE is the DX device you created when initializing DX. pEffect is a pointer to your compiled HLSL file.
0

Share this post


Link to post
Share on other sites
Hi again
Ive managed to send the camPos to hlsl now i think.
Is there no way to step through the code to see what values you have/what actually happens like i can do in the c++ code? Its very hard to understand whats going on in the shader part of this...
So i have the correct (i hope) pos in the shader now. This is the PS_OUTPUT
[code]
float3 camPos : register( c0 ); //at top of file
PS_OUTPUT ps_main(in VS_OUTPUT input)
{
float texScale = 1.0;
PS_OUTPUT output = (PS_OUTPUT)0;
vector a = tex2D(AlphaMap, input.alphamap);
vector i = tex2D(TextureOne, mul(input.tex, texScale));
vector j = tex2D(TextureTwo, mul(input.tex, texScale));
vector k = tex2D(TextureThree, mul(input.tex, texScale));

float4 oneminusx = 1.0 - a.x;
float4 oneminusy = 1.0 - a.y;
float4 oneminusz = 1.0 - a.z;
vector l = a.x * i + oneminusx * i;
vector m = a.y * j + oneminusy * l;
vector n = a.z * k + oneminusz * m;
output.diffuse = n;
return output;
} [/code]
This obviously do not take any notice of the camPos (so no fog yet).
But how would i use the camPos to get fog? I try something like
output.diffuse = n*camPos.x;
just to see a reaction but it just stops rendering the texture completely (shows color value only)
thanks for your help
Erik
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0