#include<vector>
#include<iostream>
#include"Vector3.h"
#include"rgb.h"
#include"Image.h"
#include"Shape.h"
#include"Triangle.h"
#include"Sphere.h"
#include <fstream>
using namespace std;
int main()
{
ofstream testFile("test.ppm");
HitRecord rec;
bool is_a_hit;
float tmax;
Vector3 dir(0, 0, -1);
vector<Shape*> shapes;
shapes.push_back( new Sphere(Vector3(250, 250, -1000), 150, rgb(0.2, 0.2, 0.8), rgb(1, 1, 1), 1));
shapes.push_back(new Triangle(Vector3(300.0f, 600.0f, -800.0f), Vector3(0, 100, -1000), Vector3(450, 20, -1000),
rgb(0.8, 0.2, 0.2), rgb(1, 1, 1), 1));
Image im(500, 500);
for (int i = 0; i < 500; i++){
for (int j = 0; j < 500; j++){
tmax = 100000.0f;
is_a_hit = false;
Ray r(Vector3(i, j, 0), dir);
for (int k = 0; k < shapes.size(); k++){
if (shapes[k]->hit(r, 0.000001f, tmax, 0, rec)){
tmax = rec.t;
is_a_hit = true;
}
}
if (is_a_hit){
im.set(i, j, rec.color);
}
else{
im.set(i, j, rgb(0.2f, 0.2f, 0.2f));
}
}
}
//im.writePPM(cout);
im.writePPM(testFile);
}
1. generate a simple image using spheres and triangles with an orthographic 。