1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2006 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main programmer: Marco Biasini |
---|
13 | |
---|
14 | */ |
---|
15 | #ifndef _FRUSTUM_H |
---|
16 | #define _FRUSTUM_H |
---|
17 | #include "plane.h" |
---|
18 | #include "vector.h" |
---|
19 | #include "types.h" |
---|
20 | #include "glincl.h" |
---|
21 | #include <stdio.h> |
---|
22 | |
---|
23 | |
---|
24 | #define MAX_CLIP_DISTANCE 800.0f |
---|
25 | #define m( _col, _row ) _m[_row*4+_col] |
---|
26 | |
---|
27 | #define CHECK_GL_ERROR( _d ) do { \ |
---|
28 | GLenum __err = glGetError(); \ |
---|
29 | if ( __err != GL_NO_ERROR ) \ |
---|
30 | printf( "check%s: %s\n", _d, (char*)gluErrorString( __err ) );\ |
---|
31 | }\ |
---|
32 | while ( 0 ) |
---|
33 | |
---|
34 | /** |
---|
35 | * Code borrowed from Lighthouse 3D. Its a very good tutorial on culling. |
---|
36 | */ |
---|
37 | class Frustum; |
---|
38 | typedef Frustum *pFrustum; |
---|
39 | class Frustum { |
---|
40 | |
---|
41 | public: |
---|
42 | |
---|
43 | enum { NEAR = 0 , FAR = 1, TOP = 2, BOTTOM = 3, LEFT = 4, RIGHT = 5 }; |
---|
44 | enum { OUTSIDE, INTERSECT, INSIDE }; |
---|
45 | |
---|
46 | Frustum() |
---|
47 | { |
---|
48 | planes = new Plane[6]; |
---|
49 | } |
---|
50 | ~Frustum() |
---|
51 | { |
---|
52 | if ( planes ) |
---|
53 | delete[] planes; |
---|
54 | } |
---|
55 | /** |
---|
56 | * Extracts the frustum planes from the GL_PROJECTION and GL_MODELVIEW |
---|
57 | * matrices... |
---|
58 | */ |
---|
59 | inline void extractPlanes() |
---|
60 | { |
---|
61 | float proj[16], view[16], combined[16]; |
---|
62 | glGetFloatv( GL_MODELVIEW_MATRIX, view ); |
---|
63 | glGetFloatv( GL_PROJECTION_MATRIX, proj ); |
---|
64 | multMat( combined, view, proj ); |
---|
65 | setFrustum( combined ); |
---|
66 | } |
---|
67 | |
---|
68 | inline int boxInFrustum( const ABox& _box ) |
---|
69 | { |
---|
70 | int result = INSIDE; |
---|
71 | //for each plane do ... |
---|
72 | for( int i = 0; i < 6; ++i ) { |
---|
73 | |
---|
74 | // is the positive vertex outside? |
---|
75 | if ( planes[i].distancePoint( _box.vertexP( planes[i].n ) ) < 0 ) |
---|
76 | return OUTSIDE; |
---|
77 | // is the negative vertex outside? |
---|
78 | else if ( planes[i].distancePoint( _box.vertexN( planes[i].n ) ) < 0 ) |
---|
79 | result = INTERSECT; |
---|
80 | } |
---|
81 | return result; |
---|
82 | } |
---|
83 | |
---|
84 | inline int pointInFrustum( const Vector& _point) |
---|
85 | { |
---|
86 | int result = INSIDE; |
---|
87 | for(int i=0; i < 6; i++) { |
---|
88 | if (planes[i].distancePoint( _point ) < 0) |
---|
89 | return OUTSIDE; |
---|
90 | } |
---|
91 | return result; |
---|
92 | } |
---|
93 | |
---|
94 | inline Plane getPlane( int _plane ) { return planes[_plane]; } |
---|
95 | |
---|
96 | inline void setFrustum( float *_m ) |
---|
97 | { |
---|
98 | planes[NEAR] = Plane( Vector( |
---|
99 | m(2,0) + m(3,0), |
---|
100 | m(2,1) + m(3,1), |
---|
101 | m(2,2) + m(3,2) ), |
---|
102 | m(2,3) + m(3,3) ); |
---|
103 | |
---|
104 | planes[FAR] = Plane( Vector( |
---|
105 | -m(2,0) + m(3,0), |
---|
106 | -m(2,1) + m(3,1), |
---|
107 | -m(2,2) + m(3,2) ), |
---|
108 | -m(2,3) + m(3,3) ); |
---|
109 | |
---|
110 | planes[BOTTOM] = Plane( Vector( |
---|
111 | m(1,0) + m(3,0), |
---|
112 | m(1,1) + m(3,1), |
---|
113 | m(1,2) + m(3,2) ), |
---|
114 | m(1,3) + m(3,3) ); |
---|
115 | planes[TOP] = Plane( Vector( |
---|
116 | -m(1,0) + m(3,0), |
---|
117 | -m(1,1) + m(3,1), |
---|
118 | -m(1,2) + m(3,2) ), |
---|
119 | -m(1,3) + m(3,3) ); |
---|
120 | planes[LEFT] = Plane( Vector( |
---|
121 | m(0,0) + m(3,0), |
---|
122 | m(0,1) + m(3,1), |
---|
123 | m(0,2) + m(3,2) ), |
---|
124 | m(0,3) + m(3,3) ); |
---|
125 | planes[RIGHT] = Plane( Vector( |
---|
126 | -m(0,0) + m(3,0), |
---|
127 | -m(0,1) + m(3,1), |
---|
128 | -m(0,2) + m(3,2) ), |
---|
129 | -m(0,3) + m(3,3) ); |
---|
130 | |
---|
131 | if ( planes[NEAR].k +planes[FAR].k > MAX_CLIP_DISTANCE ) { |
---|
132 | planes[FAR].k = -planes[NEAR].k+MAX_CLIP_DISTANCE; |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | protected: |
---|
137 | Plane *planes; |
---|
138 | }; |
---|
139 | |
---|
140 | #endif |
---|