0
\$\begingroup\$

I illumine model through model illumine of Phong. Ambient and diffuse light’s constituent work well, but specular works wrong. Under this message I sent screenshoots: 1) ambient and diffuse

enter image description here

2) ambinet + diffuse + specular.

enter image description here

Code shader's. Vertex's:

    cbuffer ConstantBufferVS 
    { 
    matrix World; 
    matrix View; 
    matrix Perspective; 
    } 

    struct VS_INPUT 
    { 
    float4 Pos: POSITION; 
    float3 Normal: NORMAL; 
    float4 Color: COLOR; 
    }; 

    struct VS_OUTPUT 
    { 
    float4 Pos: SV_POSITION; 
    float3 Normal: TEXCOORD0; 
    float4 Color: COLOR0; 
    float3 WorldPos: TEXCOORD1; 
    }; 

    VS_OUTPUT main(VS_INPUT input) 
    { 
    VS_OUTPUT _out = (VS_OUTPUT)0; 
    _out.Pos = mul(input.Pos, World); 
    _out.Pos = mul(_out.Pos, View); 
    _out.Pos = mul(_out.Pos, Perspective); 
    _out.Normal = (float3)mul(float4(input.Normal, 1), World); 
    _out.Color = input.Color; 
    _out.WorldPos = (float3)mul(input.Pos, World); // position vertex in world 
    return _out; 
    } 

Pixel's:

    cbuffer ConstantBufferPS 
    { 
    float4 Direction; 
    float4 Color; 
    float4 factors; 
    float4 View; // position camera, 0, 2, -3
    } 

    struct VS_INPUT 
    { 
    float4 Pos: SV_POSITION; 
    float3 Normal: TEXCOORD0; 
    float4 Color: COLOR0; 
    float3 WorldPos: TEXCOORD1; 
    }; 

    float4 main(VS_INPUT input): SV_Target 
    { 
    // ambient 
    float4 ambient = (Color * factors.x) * input.Color; 

    // diffuse 
    float4 diffuse = saturate(dot((float3)Direction, input.Normal) * Color); 

    // TODO 
    // specular 
    float4 specular = 0.0; 
    float3 vView = normalize((float3)View - input.WorldPos); // vector from pixel towards eye 
    float3 vReflect = reflect((float3)normalize(Direction), normalize(input.Normal)); // merror vector direction light 
    // нормалью 
    float spec = pow(dot(vReflect, vView), 64); // calculate spec 
    specular = spec * factors.y * Color; // calculate specular light 

    // multiply light's component's 
    float4 out_color = (ambient + diffuse + specular) * input.Color; 
    out_color.a = 1; 

    return out_color; 
    } 

All the moments are commented on.

\$\endgroup\$
3
  • \$\begingroup\$ What is the question? Note you can see an example of some traditional lighting models implemented in HLSL here. \$\endgroup\$ Apr 2, 2018 at 16:21
  • \$\begingroup\$ @ChuckWalbourn The question is that the lighting is not calculated correctly. I can record a video with a demonstration, it will show the problems of lighting. \$\endgroup\$
    – Range
    Apr 2, 2018 at 16:52
  • \$\begingroup\$ @ChuckWalbourn youtu.be/HaoFR-kVVDA \$\endgroup\$
    – Range
    Apr 2, 2018 at 18:41

1 Answer 1

1
\$\begingroup\$

A few things come to mind, and I will be making an assumption or 2. Firstly, your out_colour obviously produces the top image without adding specular? That just adding specular in causes it to be overly dark. The other thing you should also do is call saturate on your out_colour also. Could you confirm that just removing specular from the link

float4 out_color = (ambient + diffuse + specular) * input.Color; 

renders as per top image.

I've added my specular calculation in below.

    float3 reflection = reflect(diffuseLightDirection, input.normal.xyz);

    // Calculate the specular light based on the reflection and the camera position.
    //Increase the specular light by the shininess value.

    // Calculate the specular light based on the reflection and the camera position.
    float specular = dot(normalize(reflection), normalize(input.cameradirection.xyz));

    // Check to make sure the specular was positive so we aren't adding black spots to the water.
    if (specular > 0.0f)
    {

            //  // Increase the specular light by the shininess value.
        specular = pow(specular, specularShininess);

        // Add the specular to the final color.
        pixelColourBlend = saturate(pixelColourBlend + specular);
    }

What you will notice is that specular is done as a float and is added uniformly to the pixel.

\$\endgroup\$
5
  • \$\begingroup\$ Adding saturate to the out_color makes the image dark. float4 out_color = saturate ((ambient + diffuse + specular) * input.Color); \$\endgroup\$
    – Range
    Apr 4, 2018 at 9:51
  • \$\begingroup\$ Yes, if I write here is so: float4 out_color = (ambient + diffuse) * input.Color; then the picture will look like this: i-fotki.info/23/… \$\endgroup\$
    – Range
    Apr 4, 2018 at 9:52
  • \$\begingroup\$ << Firstly, your out_colour obviously produces the top image without adding specular? Yes. \$\endgroup\$
    – Range
    Apr 4, 2018 at 10:10
  • 1
    \$\begingroup\$ Then you specular component is obviously giving a negative value based upon your answers. I will check my own code to see how I did specular. But maybe try to saturate specular so it does not subtract. Will check my code when I get time and confirm how I did it. \$\endgroup\$
    – ErnieDingo
    Apr 4, 2018 at 20:49
  • \$\begingroup\$ I've just added my specular code. Hopefully this helps. \$\endgroup\$
    – ErnieDingo
    Apr 5, 2018 at 22:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .