00001
00002
00003
00004
00005
00006
00007 #ifndef RC_BVH_H
00008 #define RC_BVH_H
00009
00010
00011
00012 #include "CudaTracer.h"
00013
00014
00015 #define BVH_MAX_CHILDREN_PER_NODE 32
00016
00017 typedef struct {
00018
00019 f32 t;
00020 vec2f uv;
00021
00022 bool hit;
00023
00024
00025 void *triangle;
00026 vec3f pos;
00027 vec3f normal;
00028
00029 u32 aux_idx[3];
00030
00031 } Intersection;
00032
00033 typedef struct {
00034
00035
00036
00037
00038 f32 v0[3];
00039 f32 e1[3];
00040 f32 e2[3];
00041
00042
00043
00044 u32 aux_idx[3];
00045
00046
00047
00048 } Triangle;
00049
00050 typedef struct {
00051
00052 f32 n[2];
00053 u8 norm_dir;
00054 u8 pad[7];
00055
00056
00057
00058
00059
00060
00061 } VertexAux;
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 typedef struct {
00076
00077 AABox box;
00078
00079 u16 leaf;
00080 u16 n;
00081
00082 u32 index;
00083
00084 } BNode;
00085
00086
00087 void BNodeClear();
00088
00089
00090
00091
00092 class BVH {
00093
00094 public:
00095
00096 BVH();
00097
00098 ~BVH();
00099
00100
00101 void build(
00102 VertexArray **va,
00103 mat4f *transform,
00104 u32 n_vas
00105 );
00106
00107 Intersection traceRay(
00108 Ray *ray,
00109 u32 *node_visits
00110 );
00111
00112 bool traceShadowRay(
00113 Ray *ray
00114 );
00115
00116 void traceRaysCuda(
00117 u32 res_x,
00118 u32 res_y
00119 );
00120
00121
00122 void toCudaBVH(
00123 CudaBVH *cbvh
00124 );
00125
00126 private:
00127
00128
00129
00130 void build(
00131 Triangle *tris,
00132 u32 length,
00133 f32 prev_sa,
00134 BNode *parent
00135 );
00136
00137
00138
00139
00140
00141
00142
00143
00144 private:
00145
00146 BNode root;
00147
00148 BNode *nodes;
00149
00150 u32 pool_used;
00151
00152 Triangle *triangles;
00153 u32 n_triangles;
00154
00155 VertexAux *aux;
00156 u32 n_aux;
00157
00158 u32 curr_tri;
00159
00160 Triangle *tmp_tris[3];
00161 AABox *tmp_box;
00162 f32 *tmp_sa;
00163
00164 };
00165
00166 #endif
00167
00168