-
Notifications
You must be signed in to change notification settings - Fork 0
/
Skydome.cs
45 lines (36 loc) · 1.33 KB
/
Skydome.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using ImageMagick; //used for reading hdr image
using System.Numerics;
namespace RayTracer
{
public class Skydome
{
private IPixelCollection<float> pixels;
float exposure;
int width;
int height;
public Skydome(string file, float exposure)
{
this.exposure = exposure;
var image = new MagickImage(file);
pixels = image.GetPixels();
width = image.Width;
height = image.Height;
}
//Return color from Vector pointing to skydome
//Source: https://en.wikipedia.org/wiki/UV_mapping for a detailed explanation
public Vector3 GetColor(Vector3 vec)
{
vec = Vector3.Normalize(vec);
float u = 0.5f + (float)(Math.Atan2(vec.X, vec.Z) / (2 * Math.PI));
float v = 0.5f - (float)(Math.Asin(vec.Y) / Math.PI);
// check if u or v >= 1. If so, then loop around to 0
u = u >= 1 ? 0 : u;
v = v >= 1 ? 0 : v;
int xPixel = (int)(u * width);
int yPixel = (int)(v * height);
IMagickColor<float> pixel = pixels.GetPixel(xPixel, yPixel).ToColor();
return new Vector3(pixel.R / exposure, pixel.G / exposure, pixel.B / exposure);
}
}
}