jeudi 14 novembre 2019

Clueless and need help recalculate normals on a tesselated cubed sphere with heightmap applied

I'm currently trying learning about the graphic's pipeline using C++, HLSL and Direct_X 11 for my course. I'm currently tesselating a cubed sphere with an applied height map.

My issuse is that I'm struggling to try to figure out how I go about recalculating the normals after the tesselated height map is applied. I struggle with maths so I apolgise ahead of time for any confusion/frustration caused. I'm trying to recalculate them in the ds

// Tessellation domain shader
// After tessellation the domain shader processes the all the vertices
Texture2D HightMapTexture : register(t0);
SamplerState Sampler  : register(s0);

cbuffer MatrixBuffer : register(b0)
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projectionMatrix;
};

cbuffer TessellationBuffer : register(b1)
{
    float4 TessellationFactor;
    float4 CameraPosition;
    float4 padding;

};

struct ConstantOutputType
{
    float edges[4] : SV_TessFactor;
    float inside[2] : SV_InsideTessFactor;
};

struct InputType
{
    float3 position : POSITION;
    float4 colour : COLOUR; 
    float3 normal : NORMAL;
    float2 tex : TEXCOORD0;
};

struct OutputType
{
    float4 position : SV_POSITION;
    float4 colour : COLOUR;
    float3 normal : NORMAL;
    float2 tex : TEXCOORD0;
};

[domain("quad")]
OutputType main(ConstantOutputType input, float2 uvwCoord : SV_DomainLocation, const OutputPatch<InputType, 4> patch)
{
    float3 vertexPosition;
    float3 normalPosition;
    float2 UV;
    OutputType output;

    float3 v1 = lerp(patch[0].position, patch[1].position, uvwCoord.y); 
    float3 v2 = lerp(patch[3].position, patch[2].position, uvwCoord.y);
    vertexPosition = lerp(v1, v2, uvwCoord.x);

    float3 np1 = lerp(patch[0].normal, patch[1].normal, uvwCoord.y);
    float3 np2 = lerp(patch[3].normal, patch[2].normal, uvwCoord.y);
    normalPosition = lerp(np1, np2, uvwCoord.x);
     // Send the input normalPosition into the pixel shader.
    output.normal = normalPosition;

    float2 uv1 = lerp(patch[0].tex, patch[1].tex, uvwCoord.y);
    float2 uv2 = lerp(patch[3].tex, patch[2].tex, uvwCoord.y);
    UV = lerp(uv1, uv2, uvwCoord.x);    
    output.tex = UV;

    float hightColour = HightMapTexture.SampleLevel(Sampler, output.tex, 0).r;
    vertexPosition += (2 * hightColour) * float4(output.normal, 0);


    //vertexPosition.z =    sin((vertexPosition.y *2) + (padding.x * 2) *2);
    //vertexPosition.z += cos((vertexPosition.x *2) + (padding.x * 2)*2);

    // Calculate the position of the new vertex against the world, view, and projection matrices.
    output.position = mul(float4(vertexPosition, 1.0f), worldMatrix);
    output.position = mul(output.position, viewMatrix);
    output.position = mul(output.position, projectionMatrix);

    //output.worldPosition = float4(vertexPosition, 1.0f);

    // Send the input color into the pixel shader.
    output.colour = patch[0].colour;
    output.normal = mul(output.normal, (float3x3)worldMatrix);
    output.normal = normalize(output.normal);

    return output;
}

`

Aucun commentaire:

Enregistrer un commentaire