Added ray-plane intersection.
This commit is contained in:
6
Makefile
6
Makefile
@@ -1,6 +1,6 @@
|
||||
TARGET = ray
|
||||
HEADERS = ray.hpp sphere.hpp figure.hpp light.hpp tracer.hpp
|
||||
OBJECTS = main.o sphere.o tracer.o
|
||||
HEADERS = ray.hpp figure.hpp sphere.hpp plane.hpp light.hpp tracer.hpp
|
||||
OBJECTS = main.o sphere.o plane.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)
|
||||
|
||||
plane.o: plane.cpp $(HEADERS)
|
||||
|
||||
tracer.o: tracer.cpp $(HEADERS)
|
||||
|
||||
.PHONY: clean
|
||||
|
10
main.cpp
10
main.cpp
@@ -10,6 +10,7 @@
|
||||
#include "ray.hpp"
|
||||
#include "figure.hpp"
|
||||
#include "sphere.hpp"
|
||||
#include "plane.hpp"
|
||||
#include "light.hpp"
|
||||
#include "tracer.hpp"
|
||||
|
||||
@@ -30,6 +31,7 @@ static vec3 ** image;
|
||||
int main(int argc, char ** argv) {
|
||||
FILE * out;
|
||||
Sphere * s;
|
||||
Plane * p;
|
||||
Light * l;
|
||||
Ray r;
|
||||
vec2 sample;
|
||||
@@ -98,13 +100,17 @@ int main(int argc, char ** argv) {
|
||||
figures.push_back(static_cast<Figure *>(s));
|
||||
|
||||
s = new Sphere(-1.0f, -1.0f, -2.0f, 0.5f);
|
||||
s->set_color(0.5f, 0.5f, 0.5f);
|
||||
s->set_color(1.0f, 0.0f, 1.0f);
|
||||
figures.push_back(static_cast<Figure *>(s));
|
||||
|
||||
s = new Sphere(0.0f, 0.0f, -2.0f, 1.0f);
|
||||
s->set_color(1.0f, 1.0f, 0.0f);
|
||||
figures.push_back(static_cast<Figure *>(s));
|
||||
|
||||
p = new Plane(vec3(0.0f, -1.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f));
|
||||
p->set_color(0.0f, 1.0f, 1.0f);
|
||||
figures.push_back(static_cast<Figure *>(p));
|
||||
|
||||
l = new Light();
|
||||
lights.push_back(l);
|
||||
|
||||
@@ -124,7 +130,7 @@ int main(int argc, char ** argv) {
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < figures.size(); i++) {
|
||||
delete static_cast<Sphere *>(figures[i]);
|
||||
delete figures[i];
|
||||
}
|
||||
figures.clear();
|
||||
|
||||
|
160
output.ppm
160
output.ppm
File diff suppressed because one or more lines are too long
20
plane.cpp
Normal file
20
plane.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <cmath>
|
||||
|
||||
#include "plane.hpp"
|
||||
|
||||
#define TOL 1e-6
|
||||
|
||||
using std::abs;
|
||||
using glm::dot;
|
||||
|
||||
bool Plane::intersect(Ray & r, float & t, vec3 & n) const {
|
||||
float d = dot(r.m_direction, m_normal);
|
||||
|
||||
if (abs(d) > TOL) {
|
||||
t = dot(m_normal, (m_point - r.m_origin)) / d;
|
||||
n = vec3(m_normal);
|
||||
return t >= 0.0f;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
29
plane.hpp
Normal file
29
plane.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#ifndef PLANE_HPP
|
||||
#define PLANE_HPP
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "figure.hpp"
|
||||
|
||||
using glm::vec3;
|
||||
using glm::normalize;
|
||||
|
||||
class Plane : public Figure {
|
||||
public:
|
||||
vec3 m_point;
|
||||
vec3 m_normal;
|
||||
|
||||
Plane(): m_point(vec3(0.0f)), m_normal(0.0f, 0.0f, 1.0f) { }
|
||||
|
||||
Plane(float x, float y, float z, float nx, float ny, float nz): m_point(vec3(x, y, z)), m_normal(normalize(vec3(nx, ny, nz))) { }
|
||||
|
||||
Plane(vec3 _p, vec3 _n): m_point(_p), m_normal(normalize(_n)) { }
|
||||
|
||||
virtual ~Plane() { }
|
||||
|
||||
virtual bool intersect(Ray & r, float & t, vec3 & n) const;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
@@ -30,7 +30,8 @@ bool Sphere::intersect(Ray & r, float & t, vec3 & n) const {
|
||||
t = (-b - sqrt(d)) / (2 * a);
|
||||
i = vec3(r.m_origin + (t * r.m_direction));
|
||||
n = normalize(vec3((i - m_center) / m_radius));
|
||||
}
|
||||
return t >= 0.0f;
|
||||
|
||||
return d >= 0.0f;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user