Skip to content

Instantly share code, notes, and snippets.

@yorung
yorung / draw_cubes.cpp
Created December 3, 2017 05:40
Tilt cubes along the waves.
Vec3 center = pos + CalcGerstnerWaveOffset(immutableCb, pos, terrainFrameCB.g_time);
Vec3 offsetXZ = pos + Vec3(1, 0, 1) + CalcGerstnerWaveOffset(immutableCb, pos + Vec3(1, 0, 1), terrainFrameCB.g_time);
Vec3 offsetNegX = pos + Vec3(-1, 0, 0) + CalcGerstnerWaveOffset(immutableCb, pos + Vec3(-1, 0, 0), terrainFrameCB.g_time);
Vec3 offsetNegZ = pos + Vec3(0, 0, -1) + CalcGerstnerWaveOffset(immutableCb, pos + Vec3(0, 0, -1), terrainFrameCB.g_time);
Vec3 normal = normalize(cross(offsetNegZ - offsetXZ, offsetNegX - offsetXZ));
Vec3 rotAxis = normalize(cross(normal, Vec3(0, 1, 0)));
float rotRad = acos(dot(normal, Vec3(0, 1, 0)));
cube.Draw(MeshXAnimResult(), scale(5, 5, 5) * q2m(Quat(rotAxis, -rotRad)) * translate(center.x, center.y, center.z));
@yorung
yorung / gerstner_wave.cpp
Created November 15, 2017 14:37
Calc gerstner wave in C++.
struct Wave
{
Vec2 dir;
float amplitude;
float waveLength;
};
struct ImmutableCB
{
float waveSteepness;
@yorung
yorung / convert_tangent_space_to_world.hlsl
Created October 9, 2017 10:28
Convert tangent space normal to world space normal
float3 gerstnerWaveN = CalcGerstnerWaveNormal(vertexPosition + ofsByGerstnerWave);
float3 gerstnerWaveB = CalcGerstnerWaveBinormal(vertexPosition + ofsByGerstnerWave);
float3 gerstnerWaveT = CalcGerstnerWaveTangent(vertexPosition + ofsByGerstnerWave);
float3x3 rotator;
rotator[0] = gerstnerWaveB;
rotator[1] = gerstnerWaveN;
rotator[2] = gerstnerWaveT;
output.normal = mul(water_normal.xyz, rotator);
@yorung
yorung / calculate_gerstner_wave_orientation.hlsl
Last active August 9, 2024 01:57
Calculate gerstner wave normal, binormal, and tangent by GPU Gems's method.
float3 CalcGerstnerWaveNormal(float3 P)
{
float3 normal = float3(0, 1, 0);
[unroll]
for (int i = 0; i < numWaves; i++)
{
Wave wave = waves[i];
float wi = 2 / wave.waveLength;
float WA = wi * wave.amplitude;
float phi = speed * wi;
@yorung
yorung / calculate_gerstner_wave_normals.hlsl
Created October 9, 2017 09:39
Calculate gerstner wave normals
float3 ofsByGerstnerWave = CalcGerstnerWaveOffset(vertexPosition);
float3 dx = float3(0.01, 0, 0) + CalcGerstnerWaveOffset(vertexPosition + float3(0.01, 0, 0));
float3 dz = float3(0, 0, 0.01) + CalcGerstnerWaveOffset(vertexPosition + float3(0, 0, 0.01));
float3 N = normalize(cross(dz - ofsByGerstnerWave, dx - ofsByGerstnerWave));
@yorung
yorung / make_waves.cpp
Last active October 6, 2017 14:11
Make gerstner wave parameters
struct Wave
{
Vec2 dir;
float amplitude;
float waveLength;
};
struct ImmutableCB
{
Wave waves[100];
@yorung
yorung / gerstner_wave.hlsl
Last active August 9, 2024 01:57
Gerstner wave implementation in HLSL based on GPU Gems
struct Wave
{
float2 dir;
float amplitude;
float waveLength;
};
cbuffer cb2 : register(b2)
{
Wave waves[100];
@yorung
yorung / correct.hlsl
Created June 27, 2017 17:19
[HLSL] Define a constant variables.
static float3 lightPosition = { 100, 0, 1000 }; // 'static' needed. not 'const'!
float3 CalcLightDir(float3 myPosition)
{
return normalize(lightPosition - myPosition);
}
@yorung
yorung / wrong.hlsl
Last active June 27, 2017 17:18
[HLSL] Variables will not initialized.
float3 lightPosition = { 100, 0, 1000 }; // This makes an implicit constant buffer, but the light position never assigned!
float3 CalcLightDir(float3 myPosition)
{
return normalize(lightPosition - myPosition);
}
@yorung
yorung / SetRenderTarget.cpp
Last active June 10, 2017 17:23
An example of CPU side descriptor heap.
ComPtr<ID3D12DescriptorHeap> rtvHeap, dsvHeap;
ID3D12GraphicsCommandList* commandList;
ID3D12Device* device;
void CreateHeaps()
{
D3D12_DESCRIPTOR_HEAP_DESC rtvDesc = { D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1 };
device->CreateDescriptorHeap(&rtvDesc, IID_PPV_ARGS(&rtvHeap));
D3D12_DESCRIPTOR_HEAP_DESC dsvDesc = { D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1 };
device->CreateDescriptorHeap(&dsvDesc, IID_PPV_ARGS(&rtvHeap));