Lecture #19. Lighting

Computer graphics in Game development

Ivan Belyavtsev

03.12.2022

Recap: Lambertian shading

\[L_d = k_dI\max(0, \vec{n}\cdot\vec{l})\]

where \(L_d\) - diffusely reflected light,

\(k_d\) - material’s diffuse coefficient,

\(\vec{n}\) - surface normal,

\(\vec{l}\) - direction toward the light [1]

Recap: Spaces

  • Model space - coordinate system where the object was created
  • World space - where all object located in proper locations
  • View space - a space located around camera
  • Projection space - a projected camera frustrum to cube space
  • Screen space - a space of resulted image [2]

Question: in which space we can make the Lambertian shading?

Shader’ global definitions

cbuffer ConstantBuffer: register(b0)
{
    float4x4 mwpMatrix;
}

Texture2D g_texture : register(t0);
SamplerState g_sampler : register(s0);

struct PSInput
{
    float4 position : SV_POSITION;
    float4 color: COLOR;
    float2 uv : TEXCOORD;
    float3 world_position : POSITION;
    float3 normal : NORMAL;
};

Vertex shader

PSInput VSMain(float4 position : POSITION, float4 normal: NORMAL, float4 ambient : COLOR0, float4 diffuse : COLOR1,  float4 emissive : COLOR2, float4 texcoords : TEXCOORD)
{
    PSInput result;

    result.position = mul(mwpMatrix, position);
    result.color = ambient;
    result.uv = texcoords.xy;
    result.world_position = position.xyz;
    result.normal = normal.xyz;

    return result;
}

Pixel shader


float GetLambertIntensity(PSInput input)
{
    // (1, 1, 1) is light position in world space
    float3 to_light = normalize(float3(1.f, 1.f, 1.f) - input.world_position);
    return saturate(dot(input.normal, to_light));
}

float4 PSMain_texture(PSInput input) : SV_TARGET
{
    return g_texture.Sample(g_sampler, input.uv);
}

References

1.
McGuire M. The graphics codex. 2.14 ed. Casual Effects, 2018.
2.
Serrano H. From model space to screen space- OpenGL space transformations [Electronic resource]. 2015. URL: https://www.haroldserrano.com/blog/from-model-space-to-screen-space-opengl-space-transformations.