Hi there!
I come here doubtful..
I recently started off with HLSL shader programming and I made some per-pixel lighting effects with a tutorial (point-lighting and directional-lighting). Let PointLighting.fx be my hlsl shader file, I'm able to activate it and render something with one point-light source.
The point is, i'm actually trying to cast two lights on my object. My current code processes twice the same shader pass, properly changing its parameters to achieve the two different lights effects, but the second light is the only visible.
What may i do? Thanks in advance, and, once again, forgive my bad english...
Multi-Shading .. how to?
When rendering your second pass, disable depth writes, set the depth test to LESSEQUAL, and enable additive blending (SRCBLEND = D3DBLEND_ONE, DESTBLEND = D3DBLEND_ONE). This will cause the result of the second pass to be summed with the result of the first pass, and you'll get the contribution from both lights.
You can also author a shader (or usually a set of shaders), that can sum the contribution from multiple lights in one pass, but that is more advanced.
You can also author a shader (or usually a set of shaders), that can sum the contribution from multiple lights in one pass, but that is more advanced.
ok i wrote this to prepare the device to draw in blending mode, it still only shows the second light. Maybe I forgot something?
Of course I restored the render states to the right values after rendering the second pass..
pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE,FALSE);
pd3dDevice->SetRenderState(D3DRS_ZFUNC,D3DCMP_LESSEQUAL);
pd3dDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_ONE);
pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE);
Of course I restored the render states to the right values after rendering the second pass..
Tnaks! Useful as usual!
It works fine, but.. could you please just explain me why you made me turn the zbuffer off?
It works fine, but.. could you please just explain me why you made me turn the zbuffer off?
Once you've rendered a mesh once with z writes enabled, the z-buffer is already filled with depth values at those pixels. There's no need to write them again, because you would just be writing the same exact depth values. The reason you switch the depth test is because using LESSTHAN will fail if you render something with the same exact depth as what's in the z buffer (which is what happens when you render the same mesh twice). So by using LESSEQUAL, the depth test will still pass.
Ok thank you, i'm understanding a little bit more about directx every day with such little questions.
How useful is this forum!
How useful is this forum!
Hi there!
I come here doubtful..
I recently started off with HLSL shader programming and I made some per-pixel lighting effects with a tutorial (point-lighting and directional-lighting). Let PointLighting.fx be my hlsl shader file, I'm able to activate it and render something with one point-light source.
The point is, i'm actually trying to cast two lights on my object. My current code processes twice the same shader pass, properly changing its parameters to achieve the two different lights effects, but the second light is the only visible.
What may i do? Thanks in advance, and, once again, forgive my bad english...
You can do that without multiple passes. Instead you can use a loop in the pixel shader:
for(int i = 0; i < NUM_DIR_LIGHTS; i++)
{
//compute specular component
float s = max(0.0f, dot(normalW, DLights_dir));
pixColor += s*DLights_color;
}
pixColor += globalAmbient;
It's a very simple example, with directional ligth and only diffuse and ambiental component, but It's easy to extend for other stuff like specular component. As you can see you have an array for light direction, and another one for light color, and yo compute each light contribution in a loop.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement