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 "types.h" |
---|
18 | #include "glincl.h" |
---|
19 | #include <stdio.h> |
---|
20 | #define m( _row,_col ) _m[_col*4+_row-5] |
---|
21 | |
---|
22 | #define CHECK_GL_ERROR( _d ) do { \ |
---|
23 | GLenum __err = glGetError(); \ |
---|
24 | if ( __err != GL_NO_ERROR ) \ |
---|
25 | printf( "check%s: %s\n", _d, (char*)gluErrorString( __err ) );\ |
---|
26 | }\ |
---|
27 | while ( 0 ) |
---|
28 | |
---|
29 | /** |
---|
30 | * Code borrowed from Lighthouse 3D. Its a very good tutorial on culling. |
---|
31 | */ |
---|
32 | class Frustum; |
---|
33 | typedef Frustum *pFrustum; |
---|
34 | class Frustum { |
---|
35 | |
---|
36 | public: |
---|
37 | |
---|
38 | enum { NEAR = 0 , FAR = 1, TOP = 2, BOTTOM = 3, LEFT = 4, RIGHT = 5 }; |
---|
39 | enum { OUTSIDE, INTERSECT, INSIDE }; |
---|
40 | |
---|
41 | Frustum() |
---|
42 | { |
---|
43 | planes = new Plane[6]; |
---|
44 | } |
---|
45 | ~Frustum() |
---|
46 | { |
---|
47 | if ( planes ) |
---|
48 | delete[] planes; |
---|
49 | } |
---|
50 | /** |
---|
51 | * Extracts the frustum planes from the GL_PROJECTION and GL_MODELVIEW |
---|
52 | * matrices... |
---|
53 | */ |
---|
54 | inline void extractPlanes() |
---|
55 | { |
---|
56 | float proj[16], view[16], combined[16]; |
---|
57 | glGetFloatv( GL_MODELVIEW_MATRIX, view ); |
---|
58 | glGetFloatv( GL_PROJECTION_MATRIX, proj ); |
---|
59 | multMat( combined, view, proj ); |
---|
60 | setFrustum( combined ); |
---|
61 | } |
---|
62 | |
---|
63 | inline int boxInFrustum( const ABox& _box ) |
---|
64 | { |
---|
65 | int result = INSIDE; |
---|
66 | //for each plane do ... |
---|
67 | for( int i = 0; i < 6; ++i ) { |
---|
68 | |
---|
69 | // is the positive vertex outside? |
---|
70 | if ( planes[i].distance( _box.vertexP( planes[i].n ) ) < 0 ) |
---|
71 | return OUTSIDE; |
---|
72 | // is the negative vertex outside? |
---|
73 | else if ( planes[i].distance( _box.vertexN( planes[i].n ) ) < 0 ) |
---|
74 | result = INTERSECT; |
---|
75 | } |
---|
76 | return result; |
---|
77 | } |
---|
78 | |
---|
79 | inline int pointInFrustum( const Triple& _point) |
---|
80 | { |
---|
81 | int result = INSIDE; |
---|
82 | for(int i=0; i < 6; i++) { |
---|
83 | if (planes[i].distance( _point ) < 0) |
---|
84 | return OUTSIDE; |
---|
85 | } |
---|
86 | return result; |
---|
87 | } |
---|
88 | |
---|
89 | inline Plane getPlane( int _plane ) { return planes[_plane]; } |
---|
90 | inline void setFrustum( float *_m ) |
---|
91 | { |
---|
92 | planes[NEAR].setCoefficients( |
---|
93 | m( 3, 1 ) + m( 4, 1 ), |
---|
94 | m( 3, 2 ) + m( 4, 2 ), |
---|
95 | m( 3, 3 ) + m( 4, 3 ), |
---|
96 | m( 3, 4 ) + m( 4, 4 ) ); |
---|
97 | planes[FAR].setCoefficients( |
---|
98 | -m( 3, 1 ) + m( 4, 1 ), |
---|
99 | -m( 3, 2 ) + m( 4, 2 ), |
---|
100 | -m( 3, 3 ) + m( 4, 3 ), |
---|
101 | -m( 3, 4 ) + m( 4, 4 ) ); |
---|
102 | planes[BOTTOM].setCoefficients( |
---|
103 | m( 2, 1 ) + m( 4, 1 ), |
---|
104 | m( 2, 2 ) + m( 4, 2 ), |
---|
105 | m( 2, 3 ) + m( 4, 3 ), |
---|
106 | m( 2, 4 ) + m( 4, 4 ) ); |
---|
107 | planes[TOP].setCoefficients( |
---|
108 | -m( 2, 1 ) + m( 4, 1 ), |
---|
109 | -m( 2, 2 ) + m( 4, 2 ), |
---|
110 | -m( 2, 3 ) + m( 4, 3 ), |
---|
111 | -m( 2, 4 ) + m( 4, 4 ) ); |
---|
112 | planes[LEFT].setCoefficients( |
---|
113 | m( 1, 1 ) + m( 4, 1 ), |
---|
114 | m( 1, 2 ) + m( 4, 2 ), |
---|
115 | m( 1, 3 ) + m( 4, 3 ), |
---|
116 | m( 1, 4 ) + m( 4, 4 ) ); |
---|
117 | planes[RIGHT].setCoefficients( |
---|
118 | -m( 1, 1 ) + m( 4, 1 ), |
---|
119 | -m( 1, 2 ) + m( 4, 2 ), |
---|
120 | -m( 1, 3 ) + m( 4, 3 ), |
---|
121 | -m( 1, 4 ) + m( 4, 4 ) ); |
---|
122 | } |
---|
123 | |
---|
124 | protected: |
---|
125 | Plane *planes; |
---|
126 | }; |
---|
127 | |
---|
128 | #endif |
---|