Skip to content

Commit ff3c434

Browse files
committed
Generate and color a sphere based on surface normal
1 parent 358cd0a commit ff3c434

16 files changed

+100345
-195
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
learning_log.txt

.gitignore~

Whitespace-only changes.

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# Ray Tracer
2-
3-
Building a fully functional ray tracer from scratch based on the "Ray Tracing" series by Peter Shirley.
4-
5-
### In Progress
6-
7-
- Simple brute-force path tracer.
8-
9-
### TODO
10-
11-
- Textures
12-
- Volumes (like fog)
13-
- Rectangles
14-
- Instances
15-
- Lights
1+
# Ray Tracer
2+
3+
Building a fully functional ray tracer from scratch based on the "Ray Tracing" series by Peter Shirley.
4+
5+
### In Progress
6+
7+
- Simple brute-force path tracer.
8+
9+
### TODO
10+
11+
- Textures
12+
- Volumes (like fog)
13+
- Rectangles
14+
- Instances
15+
- Lights
1616
- Bounding Volume Hierarchy (BVH).

gen-background

13.3 KB
Binary file not shown.

gen-background.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
#include "ray.h"
3+
// #include "vec.h"
4+
5+
vec3 color(const ray& r)
6+
{
7+
vec3 unit_direction = unit_vector(r.direction());
8+
float t = 0.5*(unit_direction.y() + 1.0);
9+
return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
10+
}
11+
12+
int main()
13+
{
14+
int nx = 200;
15+
int ny = 100;
16+
std::cout << "P3\n" << nx << " " << ny << "\n255\n";
17+
vec3 lower_left_corner(-2.0, -1.0, -1.0);
18+
vec3 horizontal(4.0, 0.0, 0.0);
19+
vec3 vertical(0.0, 2.0, 0.0);
20+
vec3 origin(0.0, 0.0, 0.0);
21+
for (int j = ny-1; j >= 0; j--)
22+
{
23+
for (int i = 0; i < nx; i++)
24+
{
25+
float u = float(i) / float(nx);
26+
float v = float(j) / float(ny);
27+
ray r(origin, lower_left_corner + u*horizontal + v*vertical);
28+
vec3 col = color(r);
29+
int ir = int(255.99*col[0]);
30+
int ig = int(255.99*col[1]);
31+
int ib = int(255.99*col[2]);
32+
33+
std::cout << ir << " " << ig << " " << ib << "\n";
34+
}
35+
}
36+
}

gen-ppm

12.8 KB
Binary file not shown.

gen-ppm.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
#include "vec3.h"
3+
4+
int main()
5+
{
6+
int nx = 200;
7+
int ny = 100;
8+
std::cout << "P3\n" << nx << " " << ny << "\n255\n";
9+
for (int j = ny-1; j >= 0; j--)
10+
{
11+
for (int i = 0; i < nx; i++)
12+
{
13+
vec3 col(float(i) / float(nx), float(j) / float(ny), 0.2);
14+
int ir = int(255.99*col[0]);
15+
int ig = int(255.99*col[1]);
16+
int ib = int(255.99*col[2]);
17+
std::cout << ir << " " << ig << " " << ib << "\n";
18+
}
19+
}
20+
}

gen-sphere

17.6 KB
Binary file not shown.

gen-sphere.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <iostream>
2+
#include "vec3.h"
3+
#include "ray.h"
4+
5+
// Find where the ray hit the sphere
6+
float hit_sphere(const vec3& center, float radius, const ray& r)
7+
{
8+
// Form a quadratic equation to check if the ray satisfies
9+
// x*x + y*y + z*z = R*R
10+
// which in vector mathematics is
11+
// dot((p(t)-c),(p(t)-c))=R*R
12+
vec3 oc = r.origin() - center;
13+
float a = dot(r.direction(), r.direction());
14+
float b = 2.0 * dot(oc, r.direction());
15+
float c = dot(oc, oc) - radius*radius;
16+
float discriminant = b*b - 4*a*c;
17+
// Return the intersection of the ray and the sphere
18+
if (discriminant < 0)
19+
{
20+
return -1.0;
21+
}
22+
else
23+
{
24+
return (-b - sqrt(discriminant)) / (2.0*a);
25+
}
26+
}
27+
28+
// Determine the color of the pixel
29+
vec3 color(const ray& r)
30+
{
31+
// If the ray hits the sphere, find the normal to the sphere
32+
// and assign a color according to its direction
33+
float t = hit_sphere(vec3(0,0,-1), 0.5, r);
34+
if (t > 0.0)
35+
{
36+
vec3 N = unit_vector(r.point_at_parameter(t) - vec3(0,0,-1));
37+
return 0.5*vec3(N.x()+1, N.y()+1, N.z()+1);
38+
}
39+
// Calculate a blue-white linear interpolation (gradient)
40+
// Color the background based on this lerp
41+
vec3 unit_direction = unit_vector(r.direction());
42+
t = 0.5*(unit_direction.y() + 1.0);
43+
return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
44+
}
45+
46+
// Print PPM to stdout
47+
// Pipe to a .ppm file in the terminal to save and view the output
48+
int main()
49+
{
50+
// Image is 200x100 pixels
51+
int nx = 200;
52+
int ny = 100;
53+
// Standard formatting of a .ppm file
54+
std::cout << "P3\n" << nx << " " << ny << "\n255\n";
55+
// Start calculations from lower_left_corner
56+
vec3 lower_left_corner(-2.0, -1.0, -1.0);
57+
// Directional vectors
58+
vec3 horizontal(4.0, 0.0, 0.0);
59+
vec3 vertical(0.0, 2.0, 0.0);
60+
vec3 origin(0.0, 0.0, 0.0);
61+
// Color x axis from left to right using index i
62+
// Color y axis from top to bottom using index j
63+
for (int j = ny-1; j >= 0; j--)
64+
{
65+
for (int i = 0; i < nx; i++)
66+
{
67+
float u = float(i) / float(nx);
68+
float v = float(j) / float(ny);
69+
ray r(origin, lower_left_corner + u*horizontal + v*vertical);
70+
vec3 col = color(r);
71+
int ir = int(255.99*col[0]);
72+
int ig = int(255.99*col[1]);
73+
int ib = int(255.99*col[2]);
74+
75+
std::cout << ir << " " << ig << " " << ib << "\n";
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)