Skip to content
Snippets Groups Projects
Commit a0287c6c authored by Yoel's avatar Yoel
Browse files

Main changes

parent 57e85122
No related branches found
No related tags found
No related merge requests found
......@@ -3,19 +3,30 @@
#include <math.h>
#include <time.h>
#include <cassert>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include "material/BackgroundMaterial.h"
#include "material/DiffuseMaterial.h"
#include "material/ReflectiveMaterial.h"
#include "material/texture/Constant.h"
#include "sampling/Image.h"
#include "sampling/Scene.h"
#include "shape/Background.h"
#include "shape/CirclePlane.h"
#include "shape/Group.h"
#include "shape/LightSingleGroup.h"
#include "shape/RectanglePlane.h"
#include "shape/ShapeSingleGroup.h"
#include "shape/Sphere.h"
#include "shape/TriangleMesh.h"
#include "testing/Testing.h"
#include "tools/Mat4.h"
#include "tools/ObjectLoader.h"
#include "tools/Random.h"
using namespace material;
using namespace util;
......@@ -24,155 +35,82 @@ using namespace cam;
using namespace shapes;
using namespace std;
void writeBmp(const char* filename, Image img) {
int width = img.width; // Width of the image
int height = img.height; // Height of the image
int horizontalBytes; // Horizontal bytes to add, so the pixelarrays width
// is a multiple of 4
horizontalBytes = 4 - ((width * 3) % 4);
if (horizontalBytes == 4) horizontalBytes = 0;
int pixelArraySize; // The size of the pixelArray
pixelArraySize = ((width * 3) + horizontalBytes) * height;
// Headers
int header[12];
// Bitmap file header
char bb = 'B'; // bfType
char mm = 'M';
header[0] = pixelArraySize + 54; // bfSize
header[1] = 0; // bfReserved
header[2] = 54; // bfOffbits
// Bitmap information header
header[3] = 40; // biSize
header[4] = width; // biWidth
header[5] = height; // biHeight
short biPLanes = 1; // biPlanes
short biBitCount = 24; // biBitCount
header[6] = 0; // biCompression
header[7] = pixelArraySize; // biSizeImage
header[8] = 0; // biXPelsPerMeter
header[9] = 0; // biYPelsPerMeter
header[10] = 0; // biClrUsed
header[11] = 0; // biClrImportant
ofstream ofile(filename, std::ios::binary);
// Write the header in the right order
// bfType, ...
ofile.write(&bb, sizeof(bb));
ofile.write(&mm, sizeof(mm));
// ... bfSize, BfReserved, bfOffbits, biSize, biWidth, bitHeight, ...
for (int i = 0; i < 6; i++) {
ofile.write((char*)&header[i], sizeof(header[i]));
}
// ... biPlanes, bitBitCount, ...
ofile.write((char*)&biPLanes, sizeof(biPLanes));
ofile.write((char*)&biBitCount, sizeof(biBitCount));
// ... biCompression, biSizeImage, biXPelsPerMeter, biYPelsPerMeter,
// biClrUsed, biClrImportant
for (int i = 6; i < 12; i++) {
ofile.write((char*)&header[i], sizeof(header[i]));
}
// The colors can only have a value from 0 to 255, so a char is enough to
// store them
char blue, green, red;
// Bmp is written from top to bottom
for (int y = height - 1; y >= 0; y--) {
for (int x = 0; x <= width - 1; x++) {
red = ((img[{x, y}][0]) * 255);
green = ((img[{x, y}][1]) * 255);
blue = ((img[{x, y}][2]) * 255);
// bmp colors are bgr not rgb
char bgr[3] = {blue, green, red};
for (int i = 0; i < 3; i++) {
char c0 = (bgr[i] & 0x00FF);
// char c8 = ((bgr[i] & (unsigned int)0xFF00) >> 8);
ofile.write(&c0, sizeof(c0));
// ofile.write (&c8, sizeof (c8));
}
}
// If needed add extra bytes after each row
if (horizontalBytes != 0) {
char null;
for (int n = 0; n < horizontalBytes; n++) {
ofile.write(&null, sizeof(null));
}
}
}
ofile.close();
}
void printBits(void const* const ptr, size_t const size) {
unsigned char* b = (unsigned char*)ptr;
unsigned char byte;
int i, j;
for (i = size - 1; i >= 0; i--) {
for (j = 7; j >= 0; j--) {
byte = (b[i] >> j) & 1;
printf("%u", byte);
}
printf("\n");
}
puts("");
}
void print_AABB(string x, shared_ptr<Shape> shape) {
cout << x << shape->bounds().minBound() << shape->bounds().maxBound()
<< endl;
}
int main() {
cout << "Start" << endl;
// Image img = Image (100, 100);
Mat4 ident;
Mat4 camm = translate(Vec3(0, 1, 3));
// Mat4 group22Mat = rotate (Vec3 (1, 0, 0), 0);
CamObs obs(camm, M_PI / 2, 500, 500);
auto bg_colo = make_shared<BackgroundMaterial>(Vec3(1, 1, 1));
auto circ_colo = make_shared<DiffuseMaterial>(Vec3(0, 1, 0.5));
auto sphere_colo = make_shared<DiffuseMaterial>(Vec3(1, 0, 0));
Group group(ident);
Group shape_group(ident);
auto background = shapeGroup(ident, make_shared<Background>(bg_colo));
auto circ = shapeGroup(translate(Vec3(0, 2, 0)),
make_shared<CirclePlane>((float)10, circ_colo));
auto sphere = shapeGroup(translate(Vec3(0, 3, 0)),
make_shared<Sphere>((float)0.5, sphere_colo));
shape_group.add(sphere);
shape_group.add(circ);
print_AABB("ShapeGroup: ", make_shared<Group>(shape_group));
print_AABB("Sphere: ", make_shared<Group>(sphere));
group.add(shape_group);
auto sc =
std::make_shared<Scene>(Scene(group, Background(bg_colo), obs, 3));
size_t n = 4;
clock_t clkStart;
clock_t clkFinish;
cout << "Start render" << endl;
clkStart = clock();
Image img = raytrace(4, obs, sc, n * n);
clkFinish = clock();
cout << "Start imaging" << endl;
writeBmp("results/test.bmp", img);
cout << "End" << endl;
std::cout << clkFinish - clkStart;
test::vec3_test();
test::mat4_test();
test::ray_test();
test::shape_test();
if (true) {
cout << "Start" << endl;
// Image img = Image (100, 100);
Mat4 ident;
#if true
Mat4 camm = rotate(Vec3(1, 0, 0), -45) * rotate(Vec3(1, 0, 0), 0) *
translate(Vec3(0, 3, 3));
#else
Mat4 camm = rotate(Vec3(1, 0, 0), -90) * rotate(Vec3(1, 0, 0), 0) *
translate(Vec3(0, 3, 0));
#endif
CamObs obs(camm, M_PI / 2, 500, 500);
auto bg_colo = make_shared<BackgroundMaterial>(Vec3(0.1));
auto light_colo =
make_shared<DiffuseMaterial>(make_shared<Constant>(Vec3(1, 0, 0)),
make_shared<Constant>(Vec3(1, 0, 0)));
auto circ_colo = make_shared<DiffuseMaterial>(Vec3(0.3));
auto sphere_colo = make_shared<DiffuseMaterial>(Vec3(0.5));
auto mirror_colo = make_shared<ReflectiveMaterial>(Vec3(0.3), (float)0);
Group group(ident);
Group shape_group(ident);
auto light = make_shared<CirclePlane>((float)0.5, false, light_colo);
auto light_matrix =
rotate(Vec3(0, 0, 1), -90) * translate(Vec3(-1, 0, 0));
auto lightGroup = make_shared<LightSingleGroup>(light_matrix, light);
auto circ = ShapeSingleGroup(
translate(Vec3(0, -1, 2)),
make_shared<RectanglePlane>((float)4, (float)2, false, circ_colo));
auto sphere =
ShapeSingleGroup(translate(Vec3(0, 0, 0)),
make_shared<Sphere>((float)0.5, sphere_colo));
// shape_group.add(circ);
// shape_group.add(sphere);
shape_group.add(ShapeSingleGroup(light_matrix, light));
std::ifstream is("Tower_Base.obj");
auto mesh = make_shared<TriangleMesh>(is, sphere_colo);
shape_group.add(ShapeSingleGroup(ident, mesh));
/*for (auto pos : mesh->vertices) {
std::cout << pos.position << std::endl;
shape_group.add(
ShapeSingleGroup(translate(pos.position),
make_shared<Sphere>(0.2, sphere_colo)));
}
*/
group.add(shape_group);
group.add(make_shared<Background>(bg_colo));
std::vector<std::shared_ptr<Light>> lights = {lightGroup};
auto sc = std::make_shared<Scene>(Scene(group, lights, obs, 8));
size_t n = 3;
clock_t clkStart;
clock_t clkFinish;
cout << "Start render" << endl;
clkStart = clock();
Image img = raytrace(4, obs, sc, n * n);
clkFinish = clock();
cout << "Start imaging" << endl;
writeBmp("results/aaa.bmp", img);
cout << "End" << endl;
std::cout << clkFinish - clkStart;
} else if (false) {
// test::vec3_test();
// test::mat4_test();
// test::ray_test();
// test::shape_test();
test::axisalignedboundingbox_test();
} else {
}
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment