Sign in to follow this  

HLSL Lighting

This topic is 3486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);
}


Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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[i], g_vLightInnerRadius[i], ... );
}

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);
}
}

...




Share this post


Link to post
Share on other sites

This topic is 3486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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