-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
74 lines (58 loc) · 1.94 KB
/
main.cpp
File metadata and controls
74 lines (58 loc) · 1.94 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <vector>
#include <iostream>
#include "write_ppm.h"
#include <Eigen/Core>
#include "Object.h"
#include "ObjectList.h"
#include "Sphere.h"
#include <limits>
#include <Camera.h>
#include "raycolor.h"
#include "Material.h"
#include "random_scene.h"
#include "Texture.h"
#define STB_IMAGE_IMPLEMENTATION
int main(int argc, char * argv[])
{
int width = 640; // 640
int height = 360; // 360
int numSample = 500;
int recDepth = 50;
std::vector<unsigned char> rgb_image(3*width*height);
Eigen::Vector3d lookfrom = Eigen::Vector3d(-13, 2, 3);
Eigen::Vector3d lookat = Eigen::Vector3d(0, 0, 0);
Eigen::Vector3d vup = Eigen::Vector3d(0, 1, 0);
double vfov = 30;
double aspect = (double) width / height;
double aperture = 0.1;
double focus_dist = (lookfrom - lookat).norm();
Camera cam(lookfrom, lookat, vup, vfov, aspect, aperture, focus_dist);
Object * world = random_scene();
// int nx, ny, nn;
// unsigned char *tex_data = stbi_load("picture.jpg", &nx, &ny, &nn, 0);
// Material * mat = new lambertian(new imageTexture(tex_data, nx, ny));
for(int j=0;j<height;j++)
{
if (j % 10 == 0) {
std::cout << "Rendering height " << (double) j * 100 / height << std::endl;
}
for(int i=0;i<width;i++)
{
Eigen::Vector3d rgb = Eigen::Vector3d(0, 0, 0);
for (int s=0; s<numSample; s++) {
double u = double(i) / double(width);
double v = double(j) / double(height);
Ray r = cam.get_ray(u, v);
Eigen::Vector3d p = r.point_at_t(2.0);
rgb += raycolor(r, world, recDepth);
}
rgb /= numSample;
rgb = Eigen::Vector3d(sqrt(rgb[0]),sqrt(rgb[1]),sqrt(rgb[2]));
auto clamp = [](double s){ return std::max(std::min(s,1.0),0.0);};
rgb_image[0+3*(i+width*j)] = 255.0*clamp(rgb(0));
rgb_image[1+3*(i+width*j)] = 255.0*clamp(rgb(1));
rgb_image[2+3*(i+width*j)] = 255.0*clamp(rgb(2));
}
}
write_ppm("rgb.ppm",rgb_image,width,height,3);
}