#pragma once #include "Intersectable.hpp" #include "Occluder.hpp" class Cylinder : public Intersectable { // const Material* material; Vector centre; float radius; float top; float bottom; public: Cylinder(std::istream& isc, Material** materialTable, int nMaterials) :centre(centre),radius(radius), top(top), bottom(bottom) { centre.clear(); radius = 1.0f; material = &Material::DIFFUSEWHITE; top = 100.0f; bottom = -100.0f; char key[200]; do { isc >> key; if(strcmp(key,"radius") == 0) isc >> radius; else if(strcmp(key,"centre") == 0) isc >> centre; else if(strcmp(key,"top") == 0) isc >> top; else if(strcmp(key,"bottom") == 0) isc >> bottom; else if(strcmp(key,"material") == 0) { isc >> key; for(int i=0; i < nMaterials; i++) { material = materialTable[i]; if(strcmp(key, material->getName()) == 0) break; } } }while(strcmp(key,"end") != 0); Vector diagonal(radius, radius, radius); bbox.minPoint = centre - diagonal; bbox.maxPoint = centre + diagonal; bbox.minPoint.y = bottom; bbox.maxPoint.y = top; nOccluders = 1; occluders = new Occluder(centre, radius); } Cylinder(const Vector& centre, const float radius, const float top, const float bottom, const Material* material) :centre(centre),radius(radius), top(top), bottom(bottom) { this->material = material; Vector diagonal(radius, radius, radius); bbox.minPoint = centre - diagonal; bbox.maxPoint = centre + diagonal; bbox.minPoint.y = bottom; bbox.maxPoint.y = top; nOccluders = 1; occluders = new Occluder(centre, radius); } bool intersectBackSide(const Ray& ray, float& depth, float rayMin, float rayMax ); bool intersect(const Ray& ray, float& depth, float rayMin, float rayMax); Vector getShadingNormal(const Vector& point) { if(point.y >= top - 0.000001f) return Vector(0.0f, 1.0f, 0.0f); if(point.y <= bottom + 0.000001f) return Vector(0.0f, -1.0f, 0.0f); Vector normal = point - centre; normal.y = 0.0f; normal *= 1.0f / radius; return normal; } };