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