Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS 1
- #include <vector>
- #define STB_IMAGE_WRITE_IMPLEMENTATION
- #include "stb_image_write.h"
- #define STB_IMAGE_IMPLEMENTATION
- #include "stb_image.h"
- class Vector {
- public:
- explicit Vector(double x = 0, double y = 0, double z = 0) {
- data[0] = x;
- data[1] = y;
- data[2] = z;
- }
- double norm2() const {
- return data[0] * data[0] + data[1] * data[1] + data[2] * data[2];
- }
- double norm() const {
- return sqrt(norm2());
- }
- void normalize() {
- double n = norm();
- data[0] /= n;
- data[1] /= n;
- data[2] /= n;
- }
- double operator[](int i) const { return data[i]; };
- double& operator[](int i) { return data[i]; };
- double data[3];
- };
- Vector operator+(const Vector& a, const Vector& b) {
- return Vector(a[0] + b[0], a[1] + b[1], a[2] + b[2]);
- }
- Vector operator-(const Vector& a, const Vector& b) {
- return Vector(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
- }
- Vector operator*(const double a, const Vector& b) {
- return Vector(a*b[0], a*b[1], a*b[2]);
- }
- Vector operator*(const Vector& a, const double b) {
- return Vector(a[0]*b, a[1]*b, a[2]*b);
- }
- Vector operator/(const Vector& a, const double b) {
- return Vector(a[0] / b, a[1] / b, a[2] / b);
- }
- double dot(const Vector& a, const Vector& b) {
- return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
- }
- Vector cross(const Vector& a, const Vector& b) {
- return Vector(a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]);
- }
- class Sphere {
- public:
- // ...
- };
- class Ray {
- public:
- // ...
- };
- int main() {
- int W = 512;
- int H = 512;
- std::vector<unsigned char> image(W * H * 3, 0);
- for (int i = 0; i < H; i++) {
- for (int j = 0; j < W; j++) {
- image[(i * W + j) * 3 + 0] = 255;
- image[(i * W + j) * 3 + 1] = 0;
- image[(i * W + j) * 3 + 2] = 0;
- }
- }
- stbi_write_png("image.png", W, H, 3, &image[0], 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement