diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-11-01 01:07:41 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-11-01 01:07:41 +0800 |
| commit | 2850abdb23a2ba4fde6268ea4191f4d062b9a01f (patch) | |
| tree | 3e4cc70b322f9a8fd60d1896b4204670517d4b21 /raytracing/apps/main.cc | |
| parent | 464313d02b76385d3a2e8809a4595afabca88dae (diff) | |
| parent | 6a3b4ed10f3aa97470f56c39ea74dd903ec5d474 (diff) | |
| download | rcc-project-2850abdb23a2ba4fde6268ea4191f4d062b9a01f.tar.gz rcc-project-2850abdb23a2ba4fde6268ea4191f4d062b9a01f.tar.xz | |
Merge remote-tracking branch 'raytracing-import/master'
Diffstat (limited to 'raytracing/apps/main.cc')
| -rw-r--r-- | raytracing/apps/main.cc | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/raytracing/apps/main.cc b/raytracing/apps/main.cc new file mode 100644 index 0000000..2a73d94 --- /dev/null +++ b/raytracing/apps/main.cc @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2026 Richard + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +#include "color.h" +#include "ray.h" +#include "vec3.h" + +#include <iostream> + +double hit_sphere( const point3& center, double radius, const ray& r ) +{ + vec3 oc = center - r.origin(); + auto a = dot( r.direction(), r.direction() ); + auto b = -2.0 * dot( r.direction(), oc ); + auto c = dot( oc, oc ) - radius * radius; + auto discriminant = b * b - 4 * a * c; + + if ( discriminant < 0 ) { + return -1.0; + } else { + return ( -b - std::sqrt( discriminant ) ) / ( 2.0 * a ); + } +} + +color ray_color( const ray& r ) +{ + auto t = hit_sphere( point3( 0, 0, -1 ), 0.5, r ); + if ( t > 0.0 ) { + vec3 N = unit_vector( r.at( t ) - vec3( 0, 0, -1 ) ); + return 0.5 * color( N.x() + 1, N.y() + 1, N.z() + 1 ); + } + + vec3 unit_direction = unit_vector( r.direction() ); + auto a = 0.5 * ( unit_direction.y() + 1.0 ); + return ( 1.0 - a ) * color( 1.0, 1.0, 1.0 ) + a * color( 0.5, 0.7, 1.0 ); +} + +int main() +{ + // Image + + auto aspect_ratio = 16.0 / 9.0; + const int image_width = 400; + // Calculate the image height, and ensure that it's at least 1. + int image_height = static_cast<int>( image_width / aspect_ratio ); + image_height = ( image_height < 1 ) ? 1 : image_height; + + // Camera + auto focal_length = 1.0; + auto viewport_height = 2.0; + auto viewport_width = viewport_height * ( double( image_width ) / image_height ); + auto camera_center = point3( 0, 0, 0 ); + + // Calculate the vectors across the horizontal and down the vertical viewport edges. + auto viewport_u = vec3( viewport_width, 0, 0 ); + auto viewport_v = vec3( 0, -viewport_height, 0 ); + + // Calculate the horizontal and vertical delta vectors from pixel to pixel. + auto pixel_delta_u = viewport_u / image_width; + auto pixel_delta_v = viewport_v / image_height; + + // Calculate the location of the upper left pixel. + auto viewport_upper_left = camera_center - vec3( 0, 0, focal_length ) - viewport_u / 2 - viewport_v / 2; + auto pixel00_loc = viewport_upper_left + 0.5 * ( pixel_delta_u + pixel_delta_v ); + + // Render + + std::cout << "P3\n" << image_width << " " << image_height << "\n255\n"; + for ( int j = 0; j < image_height; j++ ) { + std::clog << "\rScanlines remaining: " << ( image_height - j ) << ' ' << std::flush; + for ( int i = 0; i < image_width; i++ ) { + auto pixel_center = pixel00_loc + ( i * pixel_delta_u ) + ( j * pixel_delta_v ); + auto ray_direction = pixel_center - camera_center; + ray r( camera_center, ray_direction ); + + const color pixel_color = ray_color( r ); + write_color( std::cout, pixel_color ); + } + } + std::clog << "\rDone. \n"; + return 0; +}
\ No newline at end of file |
