Jump to content
  • Advertisement
Sign in to follow this  
KSP

HLSL Problem

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


Link to 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


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!