// Ein "Bildschirm", der das Setzen eines Pixels kapselt // Der Bildschirm hat eine Auflösung (Breite x Höhe) // Kann zur Ausgabe einer PPM-Datei verwendet werden oder // mit SDL2 implementiert werden. void create_image(int image_width, int image_height, const Camera& camera){ std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
color pixel_color = hit_sphere(r);
write_color(std::cout, pixel_color);
}
} std::clog << "\rDone. \n"; }
// Eine "Kamera", die von einem Augenpunkt aus in eine Richtung senkrecht auf ein Rechteck (das Bild) zeigt. // Für das Rechteck muss die Auflösung oder alternativ die Pixelbreite und -höhe bekannt sein. // Für ein Pixel mit Bildkoordinate kann ein Sehstrahl erzeugt werden. void Camera::create_camera(int image_width, int image_height){
// Calculate the vectors across the horizontal and down the vertical viewport edges. this->viewport_u = Vector3df{viewport_width, 0.0f, 0.0f}; this->viewport_v = Vector3df{0.0f, -viewport_height, 0.0f};
// Calculate the horizontal and vertical delta vectors from pixel to pixel. this->pixel_delta_u = viewport_u / static_cast(image_width); this->pixel_delta_v = viewport_v / static_cast(image_height);
// Calculate the location of the upper left pixel. this->viewport_upper_left = camera_center - Vector3df{0, 0, focal_length} - viewport_u/2 - viewport_v/2; this->pixel00_loc = viewport_upper_left + 0.5f * (pixel_delta_u + pixel_delta_v);
}
// Für die "Farbe" benötigt man nicht unbedingt eine eigene Datenstruktur. // Sie kann als Vector3df implementiert werden mit Farbanteil von 0 bis 1. // Vor Setzen eines Pixels auf eine bestimmte Farbe (z.B. 8-Bit-Farbtiefe), // kann der Farbanteil mit 255 multipliziert und der Nachkommaanteil verworfen werden. void write_color(std::ostream& out, const Vector3df& pixel_color) { auto r = pixel_color[0]; auto g = pixel_color[1]; auto b = pixel_color[2];
// Translate the [0,1] component values to the byte range [0,255]. int rbyte = int(255.999 * r); int gbyte = int(255.999 * g); int bbyte = int(255.999 * b);
// Write out the pixel color components. out << rbyte << ' ' << gbyte << ' ' << bbyte << '\n'; }
// Ein "Objekt", z.B. eine Kugel oder ein Dreieck, und dem zugehörigen Material der Oberfläche. // Im Prinzip ein Wrapper-Objekt, das mindestens Material und geometrisches Objekt zusammenfasst. // Kugel und Dreieck finden Sie in geometry.h/tcc color hit_sphere(const Ray3df& r) { Sphere3df sphere(Vector3df{0.0f, 0.0f, -1.0f}, 0.5f); Intersection_Context<float, 3> context; if (sphere.intersects(r, context)) { Vector3df normal_color = 0.5f * (context.normal + Vector3df{1.0f, 1.0f, 1.0f}); return normal_color; }
Vector3df unit_direction = unit_vector(r.direction); float a = 0.5f * (unit_direction[1] + 1.0f); return (1.0f - a) * color{1.0f, 1.0f, 1.0f} + a * color{0.5f, 0.7f, 1.0f}; }
int main() { int image_width = 400; float aspect_ratio = 16.0f / 9.0f; int image_height = static_cast(image_width / aspect_ratio); image_height = (image_height < 1) ? 1 : image_height;
// Kamera erstellen Camera camera{}; camera.create_camera(image_width, image_height);
// Ausgabe in Datei umleiten std::ofstream outfile("result.ppm"); if (!outfile) { std::cerr << "Fehler beim Öffnen der Datei!" << std::endl; return 1; }
Ich werde Ihnen helfen, das Material-System für Ihren Raytracer zu implementieren. Hier ist die Erweiterung mit ambienten, diffusen und reflektiven Farbanteilen:
Änderungen in raytracer.h:
cpp
#ifndef RAYTRACER_H
#define RAYTRACER_H
#include "math.h"
#include "geometry.h"
#include <iostream>
#include <fstream>
#include <limits>
#include <cmath>
#include <memory>
#include <vector>
using vec = Vector<float, 3u>;
using color = Vector<float, 3u>;
using ray = Ray<float, 3u>;
// Material-Klasse für Oberflächeneigenschaften
class Material {
public:
color ambient; // Ambiente Farbe (Grundhelligkeit)
color diffuse; // Diffuse Farbe (matte Reflexion)
color specular; // Spiegelnde Farbe (glänzende Reflexion)
float shininess; // Glanzfaktor (wie stark die Spiegelung ist)
Material(const color& amb = color{0.1f, 0.1f, 0.1f},
const color& diff = color{0.7f, 0.7f, 0.7f},
const color& spec = color{1.0f, 1.0f, 1.0f},
float shine = 32.0f)
: ambient(amb), diffuse(diff), specular(spec), shininess(shine) {}
};
// Lichtquelle
class Light {
public:
vec position;
color intensity;
Light(const vec& pos, const color& intens = color{1.0f, 1.0f, 1.0f})
: position(pos), intensity(intens) {}
};
// Objekt-Klasse, die Geometrie und Material kombiniert
class Object {
public:
std::shared_ptr<Material> material;
Object(std::shared_ptr<Material> mat) : material(mat) {}
virtual ~Object() = default;
virtual bool intersect(const Ray3df& ray, Intersection_Context<float, 3>& context) const = 0;
};
// Kugel-Objekt
class SphereObject : public Object {
private:
Sphere3df sphere;
public:
SphereObject(const vec& center, float radius, std::shared_ptr<Material> mat)
: Object(mat), sphere(center, radius) {}
bool intersect(const Ray3df& ray, Intersection_Context<float, 3>& context) const override {
return sphere.intersects(ray, context);
}
};
class Camera {
public:
Vector<float, 3> camera_center;
Vector<float, 3> viewport_u;
Vector<float, 3> viewport_v;
Vector<float, 3> pixel_delta_u;
Vector<float, 3> pixel_delta_v;
Vector<float, 3> viewport_upper_left;
Vector<float, 3> pixel00_loc;
Camera() = default;
void create_camera(int image_width, int image_height);
};
void create_image(int image_width, int image_height, const Camera& camera);
void write_color(std::ostream& out, const color& pixel_color);
color ray_color(const Ray3df& r);
color trace_ray(const Ray3df& r, const std::vector<std::shared_ptr<Object>>& objects,
const std::vector<Light>& lights);
#endif