#pragma once #include "Intersectable.hpp" #include "KDTree.hpp" #include class Radion; #ifndef D3DVERTEX__DEFINED struct D3DVERTEX{ D3DXVECTOR3 pos; D3DXVECTOR3 normal; D3DXVECTOR2 tex0; }; #define D3DVERTEX__DEFINED #endif /*! \brief ray-tracable representaion of a mesh TriangleMesh encapsulates a kd-tree containing triangles. It can be constructed using the vertex and index buffers of a mesh. Ray-intersection and random surface sampling are supported. */ class TriangleMesh : public Intersectable { // const Material* material; KDTree* meshTree; Vector* meshVertices; Vector* normals; Vector* texCoords; unsigned int nMeshVertices; /*! \brief A triangle with ray-intersection. */ class Patch : public Intersectable{ public: static Vector* meshVertices; //!< a temporary reference to the owner TriangleMesh's vertex array static Vector* meshNormals; //!< a temporary reference to the owner TriangleMesh's normal array static Vector* meshTexCoords; //!< a temporary reference to the owner TriangleMesh's texcoord array Vector flatNormal; //!< triangle normal float hyperPlaneShiftOffset; //!< triangle plane offset Vector inverseVertexMatrix[3]; //!< Descartes to barycentric matrix unsigned short vertexIndices[3]; //!< triangle vertex indices bool intersect(const Ray& ray, float& depth, float rayMin, float rayMax); bool intersectBackSide(const Ray& ray, float& depth, float rayMin, float rayMax); void sampleSurface(Radion& radion); //power = brdf } *meshPatches; //!< mesh triangles array unsigned int nMeshPatches; //!< number of mesh triangles unsigned int nAreaTreeNodes; //!< number of nodes in area tree (for selection ~ area) double getPatchArea(unsigned int index); double surfaceArea; //!< total surface double* areaTree; //!< area tree nodes array void buildAreaTree(); //!< build area tree from root double buildAreaTree(unsigned int u); //!< build area tree from given node (recursively) public: float getSurfaceArea(); //!< return total surface area void sampleSurface(Radion& radion); //!< return random surface radion private: void sampleSurface(unsigned int u, double rnd, Radion& radion); //!< return random surface radion from area subtree under node u (recursive) public: TriangleMesh(std::istream& isc, Material** materialTable, int nMaterials); void getTransformedBoundingBox(const Transformation& tf, BoundingBox& bb); bool intersectBackSide(const Ray& ray, float& depth, float rayMin, float rayMax); bool intersect(const Ray& ray, float& depth, float rayMin, float rayMax); ~TriangleMesh(void); //! constructor to build a TriangleMesh from mesh buffers. Vertex format has to be per struct D3DVERTEX. TriangleMesh(Material* material, unsigned short* indexBuffer, unsigned int nFaces, D3DVERTEX* vertexBuffer, unsigned int nVertices); };