Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bih.hpp
Go to the documentation of this file.
1 // bih.h - Bounding interval hierarchy
2 // architecture for high performance ray tracing
3 // and k-dimensional trees
4 
5 #pragma once
6 
7 #include <stdlib.h> // for abs
8 
9 #include "inexor/shared/cube_types.hpp" // for ushort, uchar
10 #include "inexor/shared/cube_vector.hpp" // for vector
11 #include "inexor/shared/geom.hpp" // for svec, ivec, ivec::(anonymou...
12 
13 struct Texture;
14 struct extentity;
15 
16 struct BIH
17 {
18  struct node
19  {
20  short split[2];
22 
23  int axis() const
24  {
25  return child[0]>>14;
26  }
27 
28  int childindex(int which) const
29  {
30  return child[which]&0x3FFF;
31  }
32 
33  bool isleaf(int which) const
34  {
35  return (child[1]&(1<<(14+which)))!=0;
36  }
37  };
38 
39  struct tri
40  {
42  };
43 
44  struct tribb
45  {
47 
48  bool outside(const ivec &bo, const ivec &br) const
49  {
50  return abs(bo.x - center.x) > br.x + radius.x ||
51  abs(bo.y - center.y) > br.y + radius.y ||
52  abs(bo.z - center.z) > br.z + radius.z;
53  }
54  };
55 
56  enum { MESH_NOCLIP = 1<<0, MESH_ALPHA = 1<<1, MESH_CULLFACE = 1<<2 };
57 
58  struct mesh
59  {
62  float scale, invscale;
64  int numnodes;
65  const tri *tris;
66  const tribb *tribbs;
67  int numtris;
68  const uchar *pos, *tc;
71  int flags;
73 
74  mesh() : numnodes(0), numtris(0), tex(nullptr), flags(0) {}
75 
76  vec getpos(int i) const { return *(const vec *)(pos + i*posstride); }
77  vec2 gettc(int i) const { return *(const vec2 *)(tc + i*tcstride); }
78  };
79 
81  int nummeshes;
83  int numnodes;
85  int numtris;
87  float radius, entradius;
88 
89  BIH(vector<mesh> &buildmeshes);
90 
91  ~BIH();
92 
93  void build(mesh &m, ushort *indices, int numindices, const ivec &vmin, const ivec &vmax);
94 
95  bool traverse(const vec &o, const vec &ray, float maxdist, float &dist, int mode);
96  bool traverse(const mesh &m, const vec &o, const vec &ray, const vec &invray, float maxdist, float &dist, int mode, node *curnode, float tmin, float tmax);
97  bool triintersect(const mesh &m, int tidx, const vec &mo, const vec &mray, float maxdist, float &dist, int mode);
98 
99  void preload();
100 };
101 
102 extern bool mmintersect(const extentity &e, const vec &o, const vec &ray, float maxdist, int mode, float &dist);
103 
Definition: geom.hpp:902
Vector template.
Definition: cube_vector.hpp:22
~BIH()
Definition: bih.cpp:311
int nummeshes
Definition: bih.hpp:81
void build(mesh &m, ushort *indices, int numindices, const ivec &vmin, const ivec &vmax)
Definition: bih.cpp:161
mesh()
Definition: bih.hpp:74
BIH(vector< mesh > &buildmeshes)
Definition: bih.cpp:247
int numnodes
Definition: bih.hpp:83
Definition: bih.hpp:39
Definition: bih.hpp:16
matrix3 invxformnorm
Definition: bih.hpp:61
ushort vert[3]
Definition: bih.hpp:41
tribb * tribbs
Definition: bih.hpp:84
const tri * tris
Definition: bih.hpp:65
int tcstride
Definition: bih.hpp:69
Definition: bih.hpp:56
int childindex(int which) const
Definition: bih.hpp:28
2-dimensional float vectors
Definition: geom.hpp:38
int posstride
Definition: bih.hpp:69
vec bbmax
Definition: bih.hpp:86
Definition: geom.hpp:668
GLushort * indices
Definition: explosion.cpp:34
int flags
Definition: bih.hpp:71
vec bbmin
Definition: bih.hpp:72
matrix3 xformnorm
Definition: bih.hpp:61
ushort child[2]
Definition: bih.hpp:21
svec center
Definition: bih.hpp:46
matrix4x3 invxform
Definition: bih.hpp:60
unsigned short ushort
Definition: cube_types.hpp:8
int numindices
Definition: explosion.cpp:35
bool traverse(const vec &o, const vec &ray, float maxdist, float &dist, int mode)
Definition: bih.cpp:138
int numnodes
Definition: bih.hpp:64
Definition: ents.hpp:102
vector with 3 floats and some useful methods.
Definition: geom.hpp:110
int axis() const
Definition: bih.hpp:23
int numtris
Definition: bih.hpp:85
void preload()
node * nodes
Definition: bih.hpp:82
bool mmintersect(const extentity &e, const vec &o, const vec &ray, float maxdist, int mode, float &dist)
Definition: bih.cpp:318
const uchar * pos
Definition: bih.hpp:68
vec bbmax
Definition: bih.hpp:72
bool isleaf(int which) const
Definition: bih.hpp:33
short split[2]
Definition: bih.hpp:20
int br
Definition: octaedit.cpp:1749
unsigned char uchar
Basic type definitions.
Definition: cube_types.hpp:7
Definition: bih.hpp:56
float radius
Definition: bih.hpp:87
bool triintersect(const mesh &m, int tidx, const vec &mo, const vec &mray, float maxdist, float &dist, int mode)
Definition: bih.cpp:19
svec radius
Definition: bih.hpp:46
int numtris
Definition: bih.hpp:67
float entradius
Definition: bih.hpp:87
Texture * tex
Definition: bih.hpp:70
float invscale
Definition: bih.hpp:62
node * nodes
Definition: bih.hpp:63
const tribb * tribbs
Definition: bih.hpp:66
Definition: bih.hpp:18
vec2 gettc(int i) const
Definition: bih.hpp:77
Definition: bih.hpp:58
mathmatics for vectors, matrices, quaterions and more
vec center
Definition: bih.hpp:86
float scale
Definition: bih.hpp:62
vec getpos(int i) const
Definition: bih.hpp:76
3-dimensional INTEGER vectors
Definition: geom.hpp:1226
vec bbmin
Definition: bih.hpp:86
mesh * meshes
Definition: bih.hpp:80
matrix4x3 xform
Definition: bih.hpp:60
Definition: bih.hpp:44
bool outside(const ivec &bo, const ivec &br) const
Definition: bih.hpp:48
Definition: bih.hpp:56
const uchar * tc
Definition: bih.hpp:68
3 dimensional vector containing shorts.
Definition: geom.hpp:1516
A Texture entry, holding info about a texture on the GPU.
Definition: texture.hpp:27