HLSL Lighting

Started by
7 comments, last by MJP 15 years, 11 months ago
Hiya guys, What is HLSL exactly? As far as I know it is a shader language which can make the best of effects. Well whether that's the case or not I really can't find a tutorial on it and my muscles are beginning to have a break down(or it could be because I didn't have enough protein). So in short the question is "Can someone please give me a tutorial that works with C++(Win32) DirectX and is for HLSL?". Thanks. [Edited by - Promit on May 26, 2008 6:41:51 PM]
Advertisement
Google
Thanks for that but none of them seem to be HLSL tutorials, without effect files. Do you have any tutorials that teach you how to work with HLSL without effect files?

Thanks.
Quote:Original post by pcbrainbuster
Thanks for that but none of them seem to be HLSL tutorials, without effect files. Do you have any tutorials that teach you how to work with HLSL without effect files?

Thanks.


Well, the effect files don't make the HLSL itself any different, the language stays the same. However, how you'd implement HLSL shaders along with DirectX is another story, one I am not experienced with. Hopefully someone else can help here.
This is a simple Shader for textured polygons.
There are no effect files, just two files containing the vertex shader and the pixel shader. I think it is self explaining.
How to use them with Direct3D is explained in the documentation. Just search for the function "D3DXCreateShaderFromFile()" and you are done :)
To set the shaders use device->SetVertexShader() and device->SetPixelShader().
To set constants like "WorldViewProjection" use device->SetVertexShaderConstantF().
Now you have just to set the vertex declaration with device->SetVertexDeclaration() and the shaders should work!

Vertexshader:
float4x4 WorldViewProjection : register(c0);struct input{float3 position  : POSITION;float2 texcoord0 : TEXCOORD0;};struct output{float4 position  : POSITION;float2 texcoord0 : TEXCOORD0;};void main(in input indata, out output outdata){outdata.position=mul(float4(indata.position.x, indata.position.y, indata.position.z, 1.0f), WorldViewProjection);outdata.texcoord0=indata.texcoord0;}


Pixelshader:
sampler2D tex : register(s0);struct input{float2 texcoord : TEXCOORD0;};struct output{float4 color : COLOR0;};void main(in input indata, out output outdata){outdata.color=tex2D(tex, indata.texcoord);}

www.bug-soft.net
pcbrainbuster,

The best reference for learning HLSLS is the documentation that is included with the DirectX SDK. Inside the documentation there is both a "Programming Guide" as well as a Reference section.

Within the Programming Guide there are articles on "Writing HSLS Shaders in Direct3D 9", Compiling Shaders and using them both with and without Effects, and "Debugging Shaders in Visual Studio".

Within the reference section there are entire sections describing the Language Syntax for HLSL, as well the the differences between shader models and the Intrinsic Functions you can use within an HLSL program.

Cheers and Good luck!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
We wrote these HLSL tutorials at MDXInfo. Have a look, they might be able to help you.

HLSL Tutorials

I hope this helps.
Take care.
I have a similar question but I do not feel the need to make a new post. I have done spotlights in the past, however, how can I do a multi pass spotlight (so I can have about 4 different spot lights)? I do not want to do deferred shading.
Quote:Original post by simotix
I have a similar question but I do not feel the need to make a new post. I have done spotlights in the past, however, how can I do a multi pass spotlight (so I can have about 4 different spot lights)? I do not want to do deferred shading.


You just need to sum the contribution from each light. If you're doing multi-pass, what you need to do is use no blending for the first pass and additive blending for the second, third, and fourth passes (SrcBlend = ONE, DestBlend = ONE). This is the easiest way to have multiple lights, but is also a bit slow since you have to do a whole pass for each light and there's also a state change after the first pass. To avoid this, you can write a shader that does multiple lights in one pass. With effects, you can also create a few techniques that correspond to different amounts of lights.

float4 PixelShader( uniform int iNumLights ) : COLOR0{    float3 vColor = float4(0,0,0);    for (int i = 0; i < iNumLights; i++)    {         vColor += CalcSpotlight(g_vLightPos, g_vLightInnerRadius, ... );    }    return vColor;}technique Render1Light{    pass p0    {        VertexShader = compile vs_2_0 VertexShader();        PixelShader = compile ps_2_0 PixelShader(1);    }}technique Render2Lights{    pass p0    {        VertexShader = compile vs_2_0 VertexShader();        PixelShader = compile ps_2_0 PixelShader(2);    }}...



This topic is closed to new replies.

Advertisement