Jump to content
• Advertisement

Public Group

# HLSL Problem

This topic is 3782 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

Hi everyone. I'm working on HLSL now, and my problem is that my pixel shader code only calculates ambient light. My pixel shader code is <source> float4 AmbientMtrl; float4 DiffuseMtrl; float4 DiffuseLightIntensity = {0, 1, 1, 1}; float4 AmbientLightIntensity = {0, 0, 0.2, 1}; struct PS_INPUT { float3 light : TEXCOORD0; float3 norm : TEXCOORD1; float3 view : TEXCOORD2; }; struct PS_OUTPUT { float4 color : COLOR; }; PS_OUTPUT Main(PS_INPUT input) { PS_OUTPUT output = (PS_OUTPUT)0; float3 norm = normalize(input.norm); float3 lightDir = normalize(input.light); float3 view = normalize(input.view); float4 diff = saturate(dot(norm, lightDir)); float3 reflect = normalize(2 * diff * norm - lightDir); float4 specular = pow(saturate(dot(reflect, view)), 8); output.color = (AmbientMtrl * AmbientLightIntensity) + (DiffuseMtrl * DiffuseLightIntensity * diff) + specular; return output; } </source> and my vertex shader code: <source> matrix ViewMatrix; matrix ViewProjMatrix; vector LightDirection; vector EyePos; struct VS_INPUT { vector pos : POSITION; vector norm : NORMAL; }; struct VS_OUTPUT { vector pos : POSITION; float3 light : TEXCOORD0; float3 norm : TEXCOORD1; float3 view : TEXCOORD2; }; VS_OUTPUT Main(VS_INPUT input) { VS_OUTPUT output = (VS_OUTPUT)0; output.pos = mul(input.pos, ViewProjMatrix); output.light = LightDirection; float3 posworld = normalize(mul(input.pos, ViewMatrix)); output.view = EyePos - posworld; output.norm = mul(input.norm, ViewMatrix); return output; } </source> also this is my code in Render() function: <source> D3DXHANDLE hViewMatrix, hViewProjMatrix, hAmbientMtrl, hDiffuseMtrl, hLightDir, hEyePos; hViewMatrix = g_pVSConstants->GetConstantByName(0, "ViewMatrix"); hViewProjMatrix = g_pVSConstants->GetConstantByName(0, "ViewProjMatrix"); hLightDir = g_pVSConstants->GetConstantByName(0, "LightDirection"); hEyePos = g_pVSConstants->GetConstantByName(0, "EyePos"); hAmbientMtrl = g_pPSConstants->GetConstantByName(0, "AmbientMtrl"); hDiffuseMtrl = g_pPSConstants->GetConstantByName(0, "DiffuseMtrl"); D3DXVECTOR4 lightDir(-0.57, 0.57, -0.57, 0); D3DXVECTOR4 ambientMtrl(1, 0, 1, 1); D3DXVECTOR4 diffuseMtrl(0, 1, 1, 1); Matrix viewProjection = mtxView * mtxProjection; D3DXVECTOR4 d3dEyePos(eye.X, eye.Y, eye.Z, 1); g_pVSConstants->SetVector(g_pDev, hLightDir, &lightDir); g_pVSConstants->SetMatrix(g_pDev, hViewMatrix, &mtxView); g_pVSConstants->SetMatrix(g_pDev, hViewProjMatrix, &viewProjection); g_pVSConstants->SetVector(g_pDev, hEyePos, &d3dEyePos); g_pPSConstants->SetVector(g_pDev, hAmbientMtrl, &ambientMtrl); g_pPSConstants->SetVector(g_pDev, hDiffuseMtrl, &diffuseMtrl); g_pDev->SetPixelShader(g_pPS); g_pDev->SetVertexShader(g_pVS); g_pTeapot->DrawSubset(0); </source> I think the problem with my pixel shader is that the vertex shader can't pass light, norm and view to pixel shader so the diffuse and specular components of light are zero. Can you help me to fix my code? Yours, KSP

#### Share this post

##### Share on other sites
Advertisement
After taking a brief look at your code I'm not seeing anything wrong. I'm going to take another look but in the meantime...

-Have you run your program with the debug runtimes active? You're not doing any error checking on your function calls, which means they could be silently failing. The debug runtimes will tell you if something is going wrong.

-Have you tried debugging your shader in PIX? If you've never used PIX before, now might be a great time to start to get familiar with it (it's an absolutely invaluable tool). Using it will let you step through your HLSL code, so you can see if you have any bad values anywhere.

-FYI, you can find out how to properly use the "source" and "code" tags here in the forum faq.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
70
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633430
• Total Posts
3011825
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!