Code clean up and refactoring.
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -28,3 +28,6 @@
|
||||
*.out
|
||||
*.app
|
||||
ray
|
||||
|
||||
# Emacs droppings
|
||||
*~
|
||||
|
8
Makefile
8
Makefile
@@ -1,6 +1,6 @@
|
||||
TARGET = ray
|
||||
HEADERS = ray.hpp sphere.hpp figure.hpp
|
||||
OBJECTS = main.o sphere.o
|
||||
HEADERS = ray.hpp sphere.hpp figure.hpp light.hpp tracer.hpp
|
||||
OBJECTS = main.o sphere.o tracer.o
|
||||
CXX = g++
|
||||
CXXFLAGS = -ansi -pedantic -Wall -g -DGLM_FORCE_RADIANS -fopenmp
|
||||
LDLIBS = -lm
|
||||
@@ -15,6 +15,8 @@ main.o: main.cpp $(HEADERS)
|
||||
|
||||
sphere.o: sphere.cpp $(HEADERS)
|
||||
|
||||
tracer.o: tracer.cpp $(HEADERS)
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
$(RM) $(TARGET) *.o
|
||||
$(RM) $(TARGET) $(OBJECTS)
|
||||
|
@@ -6,13 +6,15 @@
|
||||
|
||||
#include "ray.hpp"
|
||||
|
||||
using glm::vec3;
|
||||
|
||||
class Figure {
|
||||
public:
|
||||
vec3 color;
|
||||
|
||||
virtual ~Figure() { }
|
||||
|
||||
virtual bool intersect(Ray & r, float & t) const = 0;
|
||||
virtual bool intersect(Ray & r, float & t, vec3 & n) const = 0;
|
||||
|
||||
virtual void set_color(float r, float g, float b) {
|
||||
color = vec3(r, g, b);
|
||||
|
18
light.hpp
Normal file
18
light.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#ifndef LIGHT_HPP
|
||||
#define LIGHT_HPP
|
||||
|
||||
#include <glm/vec3.hpp>
|
||||
|
||||
class Light {
|
||||
public:
|
||||
vec3 position;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
vec3 ambient;
|
||||
|
||||
Light(): position(vec3(0.0f)), diffuse(vec3(1.0f)), specular(vec3(1.0f)), ambient(vec3(0.1f)) { }
|
||||
Light(vec3 _p, vec3 _d, vec3 _s, vec3 _a): position(_p), diffuse(_d), specular(_s), ambient(_a) { }
|
||||
};
|
||||
|
||||
#endif
|
69
main.cpp
69
main.cpp
@@ -1,10 +1,8 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
|
||||
#include <omp.h>
|
||||
#include <glm/glm.hpp>
|
||||
@@ -12,11 +10,14 @@
|
||||
#include "ray.hpp"
|
||||
#include "figure.hpp"
|
||||
#include "sphere.hpp"
|
||||
#include "light.hpp"
|
||||
#include "tracer.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace glm;
|
||||
|
||||
static const vec3 BCKG_COLOR = vec3(0.16f, 0.66f, 0.72f);
|
||||
#define MAX_RECURSION 9
|
||||
|
||||
static const char * OUT_FILE = "output.ppm";
|
||||
|
||||
static char * input_file;
|
||||
@@ -24,21 +25,17 @@ static int g_samples = 25;
|
||||
static float g_fov = 90.0f;
|
||||
static int g_w = 640;
|
||||
static int g_h = 480;
|
||||
static float g_aspect_ratio = static_cast<float>(g_w) / g_h;
|
||||
static vec3 ** image;
|
||||
|
||||
float random01();
|
||||
vec2 sample_pixel(int i, int j);
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
FILE * out;
|
||||
float t;
|
||||
float _t;
|
||||
Sphere * s;
|
||||
Figure * _f;
|
||||
Light * l;
|
||||
Ray r;
|
||||
vec2 sample;
|
||||
vector<Figure *> figures;
|
||||
vector<Light *> lights;
|
||||
Tracer tracer;
|
||||
|
||||
if(argc < 2 || argc == 3 || argc > 6) {
|
||||
cerr << "USAGE: " << argv[0] << " IN FILE [OUT FILE [HEIGHT WIDTH [SAMPLES [FIELD OF VIEW]]]]" << endl;
|
||||
@@ -108,38 +105,34 @@ int main(int argc, char ** argv) {
|
||||
s->set_color(1.0f, 1.0f, 0.0f);
|
||||
figures.push_back(static_cast<Figure *>(s));
|
||||
|
||||
#pragma omp parallel for schedule(dynamic, 1) private(r, sample, _f, t, _t)
|
||||
l = new Light();
|
||||
lights.push_back(l);
|
||||
|
||||
tracer = Tracer(g_h, g_w, g_fov);
|
||||
|
||||
#pragma omp parallel for schedule(dynamic, 1) private(r, sample)
|
||||
for (int i = 0; i < g_h; i++) {
|
||||
for (int j = 0; j < g_w; j++) {
|
||||
for (int k = 0; k < g_samples; k++) {
|
||||
sample = sample_pixel(i, j);
|
||||
sample = tracer.sample_pixel(i, j);
|
||||
r = Ray(normalize(vec3(sample, -1.0f) - vec3(0.0f, 0.0f, 0.0f)), vec3(0.0f, 0.0f, 0.0f));
|
||||
t = numeric_limits<float>::max();
|
||||
_f = NULL;
|
||||
|
||||
for (size_t f = 0; f < figures.size(); f++) {
|
||||
if (figures[f]->intersect(r, _t) && _t < t) {
|
||||
t = _t;
|
||||
_f = figures[f];
|
||||
}
|
||||
}
|
||||
|
||||
if (_f != NULL) {
|
||||
image[i][j] += vec3(_f->color);
|
||||
} else {
|
||||
image[i][j] += vec3(BCKG_COLOR);
|
||||
}
|
||||
image[i][j] += tracer.trace_ray(r, figures, lights, MAX_RECURSION);
|
||||
}
|
||||
|
||||
image[i][j] /= g_samples;
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t f = 0; f < figures.size(); f++) {
|
||||
delete static_cast<Sphere *>(figures[f]);
|
||||
for (size_t i = 0; i < figures.size(); i++) {
|
||||
delete static_cast<Sphere *>(figures[i]);
|
||||
}
|
||||
figures.clear();
|
||||
|
||||
for (size_t i = 0; i < figures.size(); i++) {
|
||||
delete lights[i];
|
||||
}
|
||||
lights.clear();
|
||||
|
||||
fprintf(out, "P6 %d %d %d ", g_w, g_h, 255);
|
||||
for (int i = 0; i < g_h; i++) {
|
||||
for (int j = 0; j < g_w; j++) {
|
||||
@@ -156,21 +149,3 @@ int main(int argc, char ** argv) {
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
inline float random01() {
|
||||
return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
|
||||
}
|
||||
|
||||
vec2 sample_pixel(int i, int j) {
|
||||
float pxNDC;
|
||||
float pyNDC;
|
||||
float pxS;
|
||||
float pyS;
|
||||
pyNDC = (static_cast<float>(i) + random01()) / g_h;
|
||||
pyS = (1.0f - (2.0f * pyNDC)) * tan(radians(g_fov) / 2);
|
||||
pxNDC = (static_cast<float>(j) + random01()) / g_w;
|
||||
pxS = (2.0f * pxNDC) - 1.0f;
|
||||
pxS *= g_aspect_ratio * tan(radians(g_fov) / 2);
|
||||
|
||||
return vec2(pxS, pyS);
|
||||
}
|
||||
|
62
output.ppm
62
output.ppm
File diff suppressed because one or more lines are too long
@@ -2,7 +2,10 @@
|
||||
|
||||
#include "sphere.hpp"
|
||||
|
||||
bool Sphere::intersect(Ray & r, float & t) const {
|
||||
using glm::normalize;
|
||||
|
||||
bool Sphere::intersect(Ray & r, float & t, vec3 & n) const {
|
||||
vec3 i;
|
||||
float d;
|
||||
|
||||
float a = (r.m_direction.x * r.m_direction.x) +
|
||||
@@ -23,7 +26,11 @@ bool Sphere::intersect(Ray & r, float & t) const {
|
||||
|
||||
d = (b * b) - (4 * a * c);
|
||||
|
||||
if (d >= 0.0f) {
|
||||
t = (-b - sqrt(d)) / (2 * a);
|
||||
i = vec3(r.m_origin + (t * r.m_direction));
|
||||
n = normalize(vec3((i - m_center) / m_radius));
|
||||
}
|
||||
|
||||
return d >= 0.0f;
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@
|
||||
#ifndef SPHERE_HPP
|
||||
#define SPHERE_HPP
|
||||
|
||||
#include <glm/vec3.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "figure.hpp"
|
||||
|
||||
@@ -21,7 +21,7 @@ public:
|
||||
|
||||
virtual ~Sphere() { }
|
||||
|
||||
virtual bool intersect(Ray & r, float & t) const;
|
||||
virtual bool intersect(Ray & r, float & t, vec3 & n) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
57
tracer.cpp
Normal file
57
tracer.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <limits>
|
||||
#include <cstdlib>
|
||||
#include <cmath>
|
||||
|
||||
#include <omp.h>
|
||||
|
||||
#include "tracer.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
static const vec3 BCKG_COLOR = vec3(0.16f, 0.66f, 0.72f);
|
||||
|
||||
static inline float random01() {
|
||||
return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
|
||||
}
|
||||
|
||||
Tracer::Tracer(): m_h(640), m_w(480), m_fov(90.0f), m_a_ratio(640.0f / 480.0f) {}
|
||||
|
||||
Tracer::Tracer(int h, int w, float fov): m_h(h), m_w(w), m_fov(fov) {
|
||||
m_a_ratio = static_cast<float>(w) / h;
|
||||
}
|
||||
|
||||
vec2 Tracer::sample_pixel(int i, int j) const {
|
||||
float pxNDC;
|
||||
float pyNDC;
|
||||
float pxS;
|
||||
float pyS;
|
||||
pyNDC = (static_cast<float>(i) + random01()) / m_h;
|
||||
pyS = (1.0f - (2.0f * pyNDC)) * tan(radians(m_fov) / 2);
|
||||
pxNDC = (static_cast<float>(j) + random01()) / m_w;
|
||||
pxS = (2.0f * pxNDC) - 1.0f;
|
||||
pxS *= m_a_ratio * tan(radians(m_fov) / 2);
|
||||
|
||||
return vec2(pxS, pyS);
|
||||
}
|
||||
|
||||
vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & vf, vector<Light *> & vl, unsigned int rec_level) const {
|
||||
float t;
|
||||
float _t;
|
||||
Figure * _f;
|
||||
vec3 n;
|
||||
|
||||
t = numeric_limits<float>::max();
|
||||
_f = NULL;
|
||||
|
||||
for (size_t f = 0; f < vf.size(); f++) {
|
||||
if (vf[f]->intersect(r, _t, n) && _t < t) {
|
||||
t = _t;
|
||||
_f = vf[f];
|
||||
}
|
||||
}
|
||||
|
||||
if (_f != NULL)
|
||||
return _f->color;
|
||||
else
|
||||
return vec3(BCKG_COLOR);
|
||||
}
|
32
tracer.hpp
Normal file
32
tracer.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#ifndef TRACER_HPP
|
||||
#define TRACER_HPP
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "figure.hpp"
|
||||
#include "light.hpp"
|
||||
#include "ray.hpp"
|
||||
|
||||
using std::vector;
|
||||
using glm::radians;
|
||||
using glm::vec3;
|
||||
using glm::vec2;
|
||||
|
||||
class Tracer {
|
||||
public:
|
||||
int m_h;
|
||||
int m_w;
|
||||
float m_fov;
|
||||
float m_a_ratio;
|
||||
|
||||
Tracer();
|
||||
Tracer(int w, int h, float fov);
|
||||
|
||||
vec2 sample_pixel(int i, int j) const;
|
||||
vec3 trace_ray(Ray & r, vector<Figure *> & f, vector<Light *> & l, unsigned int rec_level) const;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user