source: GTP/trunk/App/Demos/Illum/PathMap/TriangleMesh.h @ 896

Revision 896, 2.9 KB checked in by szirmay, 19 years ago (diff)
Line 
1#pragma once
2#include "Intersectable.hpp"
3#include "KDTree.hpp"
4#include <iostream>
5
6class Radion;
7
8#ifndef D3DVERTEX__DEFINED
9struct D3DVERTEX{
10        D3DXVECTOR3 pos;
11        D3DXVECTOR3 normal;
12        D3DXVECTOR2 tex0;
13};
14#define D3DVERTEX__DEFINED
15#endif
16
17/*!
18\brief ray-tracable representaion of a mesh
19TriangleMesh encapsulates a kd-tree containing triangles. It can be constructed using the vertex and index
20buffers of a mesh. Ray-intersection and random surface sampling are supported.
21*/
22class TriangleMesh :
23        public Intersectable
24{
25//      const Material*         material;
26        KDTree* meshTree;
27
28        Vector* meshVertices;
29        Vector* normals;
30        Vector* texCoords;
31        unsigned int nMeshVertices;
32
33        /*!
34        \brief A triangle with ray-intersection.
35        */
36        class Patch : public Intersectable{
37                public:
38                static Vector*  meshVertices;   //!< a temporary reference to the owner TriangleMesh's vertex array
39                static Vector*  meshNormals;    //!< a temporary reference to the owner TriangleMesh's normal array
40                static Vector*  meshTexCoords;  //!< a temporary reference to the owner TriangleMesh's texcoord array
41                Vector                  flatNormal;                                     //!< triangle normal
42                float                   hyperPlaneShiftOffset;          //!< triangle plane offset
43                Vector                  inverseVertexMatrix[3];         //!< Descartes to barycentric matrix
44                unsigned short vertexIndices[3];                        //!< triangle vertex indices
45                bool intersect(const Ray& ray, float& depth, float rayMin, float rayMax);
46                bool intersectBackSide(const Ray& ray, float& depth, float rayMin, float rayMax);
47                void sampleSurface(Radion& radion); //power = brdf
48        } *meshPatches; //!< mesh triangles array
49
50        unsigned int nMeshPatches;                      //!< number of mesh triangles
51        unsigned int nAreaTreeNodes;            //!< number of nodes in area tree (for selection ~ area)
52        double getPatchArea(unsigned int index);
53
54        double surfaceArea;                                     //!< total surface
55        double* areaTree;                                       //!< area tree nodes array
56        void buildAreaTree();                           //!< build area tree from root
57        double buildAreaTree(unsigned int u);   //!< build area tree from given node (recursively)
58public:
59        float getSurfaceArea();                                 //!< return total surface area
60        void sampleSurface(Radion& radion);             //!< return random surface radion
61private:
62        void sampleSurface(unsigned int u, double rnd, Radion& radion); //!< return random surface radion from area subtree under node u (recursive)
63public:
64        TriangleMesh(std::istream& isc, Material** materialTable, int nMaterials);
65        void getTransformedBoundingBox(const Transformation& tf, BoundingBox& bb);
66        bool intersectBackSide(const Ray& ray, float& depth, float rayMin, float rayMax);
67        bool intersect(const Ray& ray, float& depth, float rayMin, float rayMax);
68        ~TriangleMesh(void);
69
70        //! constructor to build a TriangleMesh from mesh buffers. Vertex format has to be per struct D3DVERTEX.
71        TriangleMesh(Material* material, unsigned short* indexBuffer, unsigned int nFaces,
72                                                   D3DVERTEX* vertexBuffer, unsigned int nVertices);
73
74};
Note: See TracBrowser for help on using the repository browser.