Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain/src/lib/graphics/importer/terrain/frustum.h @ 8321

Last change on this file since 8321 was 8319, checked in by ponder, 18 years ago

I know this is a big commit. I'm sorry about that. Anyway…

  • Added the Terrain / TerrainQuad TerrainPage and Frustum classes to the project. The rendering frustum culling is working.
  • The types.h is here only temporary. It should be removed afterwards.
File size: 2.7 KB
Line 
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
20#define m( _row,_col )  _m[_col*4+_row-5]
21
22/**
23 * Code borrowed from Lighthouse 3D
24 */
25class Frustum;
26typedef Frustum *pFrustum;
27class Frustum {
28
29        public:
30               
31                enum { NEAR = 0 , FAR = 1, TOP = 2, BOTTOM = 3, LEFT = 4, RIGHT = 5 };
32                enum {OUTSIDE, INTERSECT, INSIDE};             
33               
34                Frustum()
35                {
36                        planes = new Plane[6];
37                }
38                ~Frustum()
39                {
40                        if ( planes )
41                                delete[] planes;
42                }
43                /**
44                 * Extracts the frustum planes from the GL_PROJECTION and GL_MODELVIEW
45                 * matrices...
46                 */
47                inline void extractPlanes()
48                {
49                        float proj[16], view[16], combined[16];
50                        glGetFloatv( GL_MODELVIEW_MATRIX, view );
51                        glGetFloatv( GL_PROJECTION_MATRIX, proj );
52                        multMat( combined, view, proj );
53                        setFrustum( combined );
54                }
55               
56                inline int boxInFrustum( const ABox& _box )
57                {
58                        int result = INSIDE;
59                        for(int i=0; i < 6; i++) {
60
61                                if ( planes[i].distance( _box.vertexP( planes[i].n ) ) < 0 )
62                                        return OUTSIDE;
63                                if ( planes[i].distance( _box.vertexN( planes[i].n) ) < 0 )
64                                        result =  INTERSECT;
65                        }
66                        return result;         
67                }
68               
69                inline int pointInFrustum( const Triple& _point)
70                {
71                        int result = INSIDE;
72                        for(int i=0; i < 6; i++) {
73
74                                if (planes[i].distance( _point ) < 0)
75                                        return OUTSIDE;
76                        }
77                        return result;         
78                }
79               
80                inline Plane getPlane( int _plane ) { return planes[_plane]; }
81                inline void setFrustum( float *_m )
82                {
83                        planes[NEAR].setCoefficients(
84                                                         m( 3, 1 ) + m( 4, 1 ),
85                                                         m( 3, 2 ) + m( 4, 2 ),
86                                                         m( 3, 3 ) + m( 4, 3 ),
87                                                         m( 3, 4 ) + m( 4, 4 ) );
88                        planes[FAR].setCoefficients( 
89                                                        -m( 3, 1 ) + m( 4, 1 ),
90                                                        -m( 3, 2 ) + m( 4, 2 ),
91                                                        -m( 3, 3 ) + m( 4, 3 ),
92                                                        -m( 3, 4 ) + m( 4, 4 ) );
93                        planes[BOTTOM].setCoefficients(
94                                                         m( 2, 1 ) + m( 4, 1 ),
95                                                         m( 2, 2 ) + m( 4, 2 ),
96                                                         m( 2, 3 ) + m( 4, 3 ),
97                                                         m( 2, 4 ) + m( 4, 4 ) );
98                        planes[TOP].setCoefficients( 
99                                                        -m( 2, 1 ) + m( 4, 1 ),
100                                                        -m( 2, 2 ) + m( 4, 2 ),
101                                                        -m( 2, 3 ) + m( 4, 3 ),
102                                                        -m( 2, 4 ) + m( 4, 4 ) );
103                        planes[LEFT].setCoefficients( 
104                                                         m( 1, 1 ) + m( 4, 1 ),
105                                                         m( 1, 2 ) + m( 4, 2 ),
106                                                         m( 1, 3 ) + m( 4, 3 ),
107                                                         m( 1, 4 ) + m( 4, 4 ) );
108                        planes[RIGHT].setCoefficients(
109                                                        -m( 1, 1 ) + m( 4, 1 ),
110                                                        -m( 1, 2 ) + m( 4, 2 ),
111                                                        -m( 1, 3 ) + m( 4, 3 ),
112                                                        -m( 1, 4 ) + m( 4, 4 ) );
113                }
114               
115        protected:
116                Plane           *planes;
117};
118
119#endif
Note: See TracBrowser for help on using the repository browser.