Added ray-disk intersection and material definition.
This commit is contained in:
6
Makefile
6
Makefile
@@ -1,6 +1,6 @@
|
|||||||
TARGET = ray
|
TARGET = ray
|
||||||
HEADERS = ray.hpp figure.hpp sphere.hpp plane.hpp light.hpp tracer.hpp
|
HEADERS = ray.hpp figure.hpp sphere.hpp plane.hpp disk.hpp material.hpp light.hpp tracer.hpp
|
||||||
OBJECTS = main.o sphere.o plane.o tracer.o
|
OBJECTS = main.o sphere.o plane.o disk.o tracer.o
|
||||||
CXX = g++
|
CXX = g++
|
||||||
CXXFLAGS = -ansi -pedantic -Wall -g -DGLM_FORCE_RADIANS -fopenmp
|
CXXFLAGS = -ansi -pedantic -Wall -g -DGLM_FORCE_RADIANS -fopenmp
|
||||||
LDLIBS = -lm
|
LDLIBS = -lm
|
||||||
@@ -17,6 +17,8 @@ sphere.o: sphere.cpp $(HEADERS)
|
|||||||
|
|
||||||
plane.o: plane.cpp $(HEADERS)
|
plane.o: plane.cpp $(HEADERS)
|
||||||
|
|
||||||
|
disk.o: disk.cpp $(HEADERS)
|
||||||
|
|
||||||
tracer.o: tracer.cpp $(HEADERS)
|
tracer.o: tracer.cpp $(HEADERS)
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
|
17
disk.cpp
Normal file
17
disk.cpp
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include "disk.hpp"
|
||||||
|
|
||||||
|
using glm::dot;
|
||||||
|
|
||||||
|
bool Disk::intersect(Ray & r, float & t) const {
|
||||||
|
float _t;
|
||||||
|
vec3 i_pos, i_vec;
|
||||||
|
|
||||||
|
if (Plane::intersect(r, _t)) {
|
||||||
|
i_pos = r.m_origin + (_t * r.m_direction);
|
||||||
|
i_vec = i_pos - m_point;
|
||||||
|
t = _t;
|
||||||
|
return dot(i_vec, i_vec) <= (m_radius * m_radius);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
40
disk.hpp
Normal file
40
disk.hpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef DISK_HPP
|
||||||
|
#define DISK_HPP
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
#include "plane.hpp"
|
||||||
|
|
||||||
|
using glm::vec3;
|
||||||
|
using glm::normalize;
|
||||||
|
|
||||||
|
class Disk : public Plane {
|
||||||
|
public:
|
||||||
|
float m_radius;
|
||||||
|
|
||||||
|
Disk(): m_radius(1.0f) {
|
||||||
|
rho = 0.0f;
|
||||||
|
m_point = vec3(0.0f);
|
||||||
|
m_normal = vec3(0.0f, 1.0f, 0.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
Disk(float x, float y, float z, float nx, float ny, float nz, float _r): m_radius(_r) {
|
||||||
|
rho = 0.0f;
|
||||||
|
m_point = vec3(x, y, z);
|
||||||
|
m_normal = normalize(vec3(nx, ny, nz));
|
||||||
|
}
|
||||||
|
|
||||||
|
Disk(vec3 _p, vec3 _n, float _r): m_radius(_r) {
|
||||||
|
rho = 0.0f;
|
||||||
|
m_point = _p;
|
||||||
|
m_normal = normalize(_n);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~Disk() { }
|
||||||
|
|
||||||
|
virtual bool intersect(Ray & r, float & t) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
@@ -5,12 +5,14 @@
|
|||||||
#include <glm/vec3.hpp>
|
#include <glm/vec3.hpp>
|
||||||
|
|
||||||
#include "ray.hpp"
|
#include "ray.hpp"
|
||||||
|
#include "material.hpp"
|
||||||
|
|
||||||
using glm::vec3;
|
using glm::vec3;
|
||||||
|
|
||||||
class Figure {
|
class Figure {
|
||||||
public:
|
public:
|
||||||
vec3 color;
|
vec3 color;
|
||||||
|
Material m_mat;
|
||||||
float rho;
|
float rho;
|
||||||
|
|
||||||
virtual ~Figure() { }
|
virtual ~Figure() { }
|
||||||
|
19
main.cpp
19
main.cpp
@@ -12,6 +12,7 @@
|
|||||||
#include "figure.hpp"
|
#include "figure.hpp"
|
||||||
#include "sphere.hpp"
|
#include "sphere.hpp"
|
||||||
#include "plane.hpp"
|
#include "plane.hpp"
|
||||||
|
#include "disk.hpp"
|
||||||
#include "light.hpp"
|
#include "light.hpp"
|
||||||
#include "tracer.hpp"
|
#include "tracer.hpp"
|
||||||
|
|
||||||
@@ -40,7 +41,7 @@ int main(int argc, char ** argv) {
|
|||||||
int total;
|
int total;
|
||||||
int current = 0;
|
int current = 0;
|
||||||
|
|
||||||
if(argc < 2 || argc == 3 || argc > 7) {
|
if(argc < 2 || argc > 7) {
|
||||||
cerr << "USAGE: " << argv[0] << " IN FILE [OUT FILE [HEIGHT WIDTH [SAMPLES [FIELD OF VIEW]]]]" << endl;
|
cerr << "USAGE: " << argv[0] << " IN FILE [OUT FILE [HEIGHT WIDTH [SAMPLES [FIELD OF VIEW]]]]" << endl;
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
@@ -145,6 +146,7 @@ int main(int argc, char ** argv) {
|
|||||||
static void scene_1(vector<Figure *> & vf, vector<Light *> & vl) {
|
static void scene_1(vector<Figure *> & vf, vector<Light *> & vl) {
|
||||||
Sphere * s;
|
Sphere * s;
|
||||||
Plane * p;
|
Plane * p;
|
||||||
|
Disk * d;
|
||||||
Light * l;
|
Light * l;
|
||||||
|
|
||||||
s = new Sphere(1.0f, 1.0f, -2.0f, 0.5f);
|
s = new Sphere(1.0f, 1.0f, -2.0f, 0.5f);
|
||||||
@@ -171,11 +173,6 @@ static void scene_1(vector<Figure *> & vf, vector<Light *> & vl) {
|
|||||||
p->set_color(1.0f, 0.5f, 0.4f);
|
p->set_color(1.0f, 0.5f, 0.4f);
|
||||||
vf.push_back(static_cast<Figure *>(p));
|
vf.push_back(static_cast<Figure *>(p));
|
||||||
|
|
||||||
s = new Sphere(0.0f, 0.0f, -1.0f, 0.25f);
|
|
||||||
s->set_color(1.0f, 1.0f, 1.0f);
|
|
||||||
s->rho = 0.1f;
|
|
||||||
vf.push_back(static_cast<Figure *>(s));
|
|
||||||
|
|
||||||
s = new Sphere(-1.5f, 0.0f, -2.0f, 0.5f);
|
s = new Sphere(-1.5f, 0.0f, -2.0f, 0.5f);
|
||||||
s->set_color(1.0f, 1.0f, 1.0f);
|
s->set_color(1.0f, 1.0f, 1.0f);
|
||||||
s->rho = 0.3f;
|
s->rho = 0.3f;
|
||||||
@@ -191,6 +188,16 @@ static void scene_1(vector<Figure *> & vf, vector<Light *> & vl) {
|
|||||||
s->rho = 0.5f;
|
s->rho = 0.5f;
|
||||||
vf.push_back(static_cast<Figure *>(s));
|
vf.push_back(static_cast<Figure *>(s));
|
||||||
|
|
||||||
|
s = new Sphere(0.0f, 0.0f, -1.0f, 0.25f);
|
||||||
|
s->set_color(1.0f, 1.0f, 1.0f);
|
||||||
|
s->rho = 0.1f;
|
||||||
|
vf.push_back(static_cast<Figure *>(s));
|
||||||
|
|
||||||
|
d = new Disk(vec3(0.0f, 2.0f, -2.0f), vec3(0.0f, -1.0f, 0.0f), 1.0f);
|
||||||
|
d->set_color(1.0f, 1.0f, 0.0f);
|
||||||
|
d->rho = 0.8f;
|
||||||
|
vf.push_back(static_cast<Figure *>(d));
|
||||||
|
|
||||||
l = new Light();
|
l = new Light();
|
||||||
l->m_position = normalize(vec3(1.0f, 1.0f, 1.0f));
|
l->m_position = normalize(vec3(1.0f, 1.0f, 1.0f));
|
||||||
l->m_diffuse = vec3(0.0f, 1.0f, 1.0f);
|
l->m_diffuse = vec3(0.0f, 1.0f, 1.0f);
|
||||||
|
20
material.hpp
Normal file
20
material.hpp
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef MATERIAL_HPP
|
||||||
|
#define MATERIAL_HPP
|
||||||
|
|
||||||
|
#include <glm/vec3.hpp>
|
||||||
|
|
||||||
|
using glm::vec3;
|
||||||
|
|
||||||
|
class Material {
|
||||||
|
public:
|
||||||
|
vec3 m_diffuse;
|
||||||
|
vec3 m_specular;
|
||||||
|
vec3 m_ambient;
|
||||||
|
float m_rho;
|
||||||
|
float m_shininess;
|
||||||
|
|
||||||
|
Material(): m_diffuse(vec3(1.0f)), m_specular(vec3(1.0f)), m_ambient(vec3(1.0f)), m_rho(0.0f), m_shininess(89.0f) { }
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
4432
output.ppm
4432
output.ppm
File diff suppressed because one or more lines are too long
@@ -74,7 +74,7 @@ vec3 Tracer::trace_ray(Ray & r, vector<Figure *> & vf, vector<Light *> & vl, uns
|
|||||||
i_pos = r.m_origin + (t * r.m_direction);
|
i_pos = r.m_origin + (t * r.m_direction);
|
||||||
n = _f->normal_at_int(r, t);
|
n = _f->normal_at_int(r, t);
|
||||||
|
|
||||||
if (vis && _f->rho > 0.0f && rec_level < MAX_RECURSION) {
|
if (_f->rho > 0.0f && rec_level < MAX_RECURSION) {
|
||||||
rr = Ray(reflect(r.m_direction, n), i_pos + n * BIAS);
|
rr = Ray(reflect(r.m_direction, n), i_pos + n * BIAS);
|
||||||
color = _f->rho * trace_ray(rr, vf, vl, rec_level + 1);
|
color = _f->rho * trace_ray(rr, vf, vl, rec_level + 1);
|
||||||
} else if (rec_level >= MAX_RECURSION)
|
} else if (rec_level >= MAX_RECURSION)
|
||||||
|
Reference in New Issue
Block a user