Hi everybody!
I am currently trying to write my own GPU Raytracer.
I am using DirectX 11 and Compute Shader.
Here is what I've tried so far:
RayTracer.hlsl
#include "RayTracingHeader.hlsli"
SamplerState gWrapSampler : register(s0);
TextureCube gObjBackground : register(t0); // Not used right now
RWTexture2D<float4> gResult : register(u0);
cbuffer cbCamera : register(b0)
{
Camera gCamera;
};
cbuffer cbGeneralInfo : register(b1)
{
uint gWidth;
uint gHeight;
uint gMaxRecursion; // Not used right now
float gRayMaxLength;
};
[numthreads(32, 32, 1)]
void main( uint3 DTid : SV_DispatchThreadID,
uint3 GroupID : SV_GroupThreadID )
{
gResult[DTid.xy] = float4(0, 0, 0, 1);
float u = (float) DTid.x / (float) gWidth;
float v = (float) DTid.y / (float) gHeight;
float4 currentRayDirection = gCamera.TopLeftCorner + u * (gCamera.TopRightCorner - gCamera.TopLeftCorner)
+ v * (gCamera.BottomLeftCorner - gCamera.TopLeftCorner);
currentRayDirection.w = 0.0f;
currentRayDirection = normalize(currentRayDirection);
Ray r;
r.Origin = gCamera.CamPosition.xyz;
r.Direction = currentRayDirection.xyz;
r.length = gRayMaxLength;
Sphere s1;
s1.Position = float3(0, 0, 10);
s1.Radius = 1;
if (s1.Intersect(r))
{
float4 reflectedColor = float4(1, 0, 0, 1);
gResult[DTid.xy] = reflectedColor;
}
}
RayTracingHeader.hlsli
class Camera
{
float4 CamPosition;
float4 TopLeftCorner;
float4 TopRightCorner;
float4 BottomLeftCorner;
};
class Ray
{
float3 Origin;
float3 Direction;
float length;
};
class Sphere
{
float3 Position;
float Radius;
bool Intersect(Ray r)
{
float3 OriginToSphere = Position - r.Origin;
float projection = dot(OriginToSphere, r.Direction);
float3 distanceVector = OriginToSphere - projection * r.Direction;
float distanceVectorLengthSQ = dot(distanceVector, distanceVector);
float radiusSQ = Radius * Radius;
if (distanceVectorLengthSQ > radiusSQ)
return false;
float newLength = projection - sqrt(radiusSQ - distanceVectorLengthSQ);
if (newLength < 0.0f || newLength > r.length)
return false;
r.length = newLength;
return true;
}
};
But the result is not what I expected.
For example, the sphere is located at (0,0,10) with radius 1, but this is the result when CamPos is 4.5, which I think is wrong.
Also, for some reason, when I rotate the camera, the sphere expands.
Anyone could give me some pieces of advice, please?