Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8775 was 8775, checked in by ponder, 18 years ago
  • Applied a small change to sounrd_buffer and ogg_player in order to load the buffers correctly on big endian systems.
  • Did a lot of cleanup for the terrain rendering system. Added destructors and constructors.
  • The cameras clip-space has to be set to a very large range to display the skybox. The maximum clip distance for the terrain needs to be smaller.
File size: 3.3 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#include <stdio.h>
20
21
22#define MAX_CLIP_DISTANCE 400.0f
23#define m( _row,_col )  _m[_col*4+_row-5]
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 */
35class Frustum;
36typedef Frustum *pFrustum;
37class 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(
96                                                         m( 3, 1 ) + m( 4, 1 ),
97                                                         m( 3, 2 ) + m( 4, 2 ),
98                                                         m( 3, 3 ) + m( 4, 3 ),
99                                                         m( 3, 4 ) + m( 4, 4 ) );
100                                                       
101                        planes[FAR].setCoefficients( 
102                                                        -m( 3, 1 ) + m( 4, 1 ),
103                                                        -m( 3, 2 ) + m( 4, 2 ),
104                                                        -m( 3, 3 ) + m( 4, 3 ),
105                                                        -m( 3, 4 ) + m( 4, 4 ) );
106                                                       
107                        planes[BOTTOM].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                                                       
113                        planes[TOP].setCoefficients( 
114                                                        -m( 2, 1 ) + m( 4, 1 ),
115                                                        -m( 2, 2 ) + m( 4, 2 ),
116                                                        -m( 2, 3 ) + m( 4, 3 ),
117                                                        -m( 2, 4 ) + m( 4, 4 ) );
118                                                       
119                        planes[LEFT].setCoefficients( 
120                                                         m( 1, 1 ) + m( 4, 1 ),
121                                                         m( 1, 2 ) + m( 4, 2 ),
122                                                         m( 1, 3 ) + m( 4, 3 ),
123                                                         m( 1, 4 ) + m( 4, 4 ) );
124                                                       
125                        planes[RIGHT].setCoefficients(
126                                                        -m( 1, 1 ) + m( 4, 1 ),
127                                                        -m( 1, 2 ) + m( 4, 2 ),
128                                                        -m( 1, 3 ) + m( 4, 3 ),
129                                                        -m( 1, 4 ) + m( 4, 4 ) );
130                                                       
131                        if ( planes[NEAR].d +planes[FAR].> MAX_CLIP_DISTANCE ) {
132                                planes[FAR].d = -planes[NEAR].d+MAX_CLIP_DISTANCE;
133                        }
134                }
135               
136        protected:
137                Plane           *planes;
138};
139
140#endif
Note: See TracBrowser for help on using the repository browser.