-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
143 lines (127 loc) · 4.6 KB
/
Main.cpp
File metadata and controls
143 lines (127 loc) · 4.6 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <Stdio.h>
#include <iostream>
#include <fstream>
#include <glm/vec3.hpp>
#include "thread"
#include "Ray.h"
#include "Sphere.h"
#include "float.h"
#include "Utilities.h"
#include "Camera.h"
using namespace std;
glm::vec3 GetRayColor(Ray& ray, Hitable *scene, int depth) {
HitObjectRecord *record = new HitObjectRecord();
/*if (scene->CheckObjectHit(ray, EPSILON, FLT_MAX, record)) {
Ray rayScattered;
glm::vec3 attenuation(0.0f, 0.0f, 0.0f);
if (depth < 50 && record->material->Scatter(ray, record, attenuation, rayScattered)) {
return attenuation * GetRayColor(rayScattered, scene, depth + 1);
}
else {
return glm::vec3(0.0f, 0.0f, 0.0f);
}
}
*/
if (scene->CheckObjectHit(ray, EPSILON, FLT_MAX, record)) {
return 0.5f * glm::vec3(record->normal.x + 1.0f, record->normal.y + 1.0f, record->normal.z + 1.0f);
} else {
// set value between 0.0 - 1.0
float t = 0.5f * (ray.direction.y + 1.0f);
// linear blend between white and kind of blue
return (1.0f - t) * glm::vec3(1.0f, 1.0f, 1.0f) + t * glm::vec3(0.5f, 0.7f, 1.0f);
}
delete record;
}
void Render(int minX, int maxX, int minY, int maxY, int numberOfSamplesPerPixel, int tileWidth, int tileHeight, int imageWidth, int imageHeight, Camera &camera, Hitable* scene, std::vector<glm::vec3*> &colorBuffer) {
// X-axis goes from min to max
// Y-axis goes form max to min
for (int y = maxY; y > minY; y--) {
for (int x = minX; x < maxX; x++) {
glm::vec3 color(0.0f, 0.0f, 0.0f);
// This loop is for AA
int depth = 0;
for (int sample = 0; sample < numberOfSamplesPerPixel; sample++) {
// Generate the Ray
float u = float(x + Utilities::GetRandom()) / float(imageWidth);
float v = float(y + Utilities::GetRandom()) / float(imageHeight);
Ray ray = camera.GenerateRay(u, v);
// Generate the Color
color += GetRayColor(ray, scene, depth);
}
// Averaging the color from the samples
color /= numberOfSamplesPerPixel;
color = Utilities::Gamma2(color);
// fill the file with the pixel RGB data
color[0] = int(255.99* color[0]);
color[1] = int(255.99* color[1]);
color[2] = int(255.99* color[2]);
int index = x + (imageHeight - y) * imageWidth;
colorBuffer[index]->x = color.x;
colorBuffer[index]->y = color.y;
colorBuffer[index]->z = color.z;
}
}
}
void main() {
// Image Dimensions
int imageWidth = 600;
int imageHeight = 300;
int colorBufferDimension = imageWidth*imageHeight;
// Final Color Buffer
std::vector<glm::vec3*> colorBuffer(colorBufferDimension);
// Fill in the color buffer
for (int i = 0; i < colorBufferDimension; i++) {
colorBuffer[i] = new glm::vec3(0.0f, 0.0f, 0.0f);
}
// For multi-threading
thread threads[NumberOfThreads];
// For Anti Aliasing
int numberOfSamplesPerPixel = 100;
// Create a camera
Camera camera;
// Create a list of hitable objects in the scene
std::vector<Hitable*> *listOfObjectsInScene = new std::vector<Hitable*>();
Sphere* sphere1 = new Sphere(0.5f, glm::vec3(0.0f, 0.0f, -1.0f));
Sphere* sphere2 = new Sphere(100.0f, glm::vec3(0.0f, -100.5f, -1.0f));
listOfObjectsInScene->push_back(sphere1);
listOfObjectsInScene->push_back(sphere2);
Hitable* scene = new HitableList(listOfObjectsInScene);
// The entire tiling architecture is for multithreading the renderer
// Tile bounds
int minX, maxX, minY, maxY;
// Dividing the image into 3 x 2 tiles.
int rows = 2;
int colums = 3;
int tileWidth = imageWidth / colums;
int tileHeight = imageHeight / rows;
int threadIndex = 0;
for (int yTileDim = 0; yTileDim < rows; yTileDim++) {
for (int xTileDim = 0; xTileDim < colums; xTileDim++) {
// This part renders one tile at a time of the whole image and fills the color buffer
minX = xTileDim * tileWidth;
maxX = ((xTileDim + 1) * tileWidth);
minY = ((1 - yTileDim) * tileHeight);
maxY = ((rows - yTileDim) * (tileHeight));
threads[threadIndex] = thread(Render, minX, maxX, minY, maxY, numberOfSamplesPerPixel, tileWidth, tileHeight, imageWidth, imageHeight, camera, scene, colorBuffer);
threadIndex += 1;
}
}
// Sync Threads
for (int i = 0; i < NumberOfThreads; i++) {
threads[i].join();
}
// Writing the .PPM renderd image file
ofstream myFile;
string filePath = R"(C:\Users\Rudraksha\Documents\Projects\RayTracer\Image.ppm)";
myFile.open(filePath, ios::out | ios::trunc);
if (!myFile) {
std::cout << "Could not open file" << endl;
}
myFile << "P3\n";
myFile << imageWidth << " " << imageHeight << "\n";
myFile << "255\n";
for (int index = 0; index < (imageWidth * imageHeight); index++) {
myFile << colorBuffer[index]->x << " " << colorBuffer[index]->y << " " << colorBuffer[index]->z << "\n";
}
myFile.close();
}