Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 176 lines (150 sloc) 6.751 kb
44146d06 » rcgordon
2010-05-11 Added GPL license and headers.
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
15 See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
b985ed94 » rcgordon
2005-08-05 First file compiles. :)
22 #include "Frustum.h"
0cca0c49 » rcgordon
2005-08-05 Initial import.
23 #include <math.h>
24
8562aa5b » rcgordon
2005-08-05 More work.
25 #include "gamegl.h"
0cca0c49 » rcgordon
2005-08-05 Initial import.
26
27
28 void FRUSTUM::
29 GetFrustum() {
30 static float projmatrix[16];
31 static float mvmatrix[16];
32 static float clip[16];
33 static float t;
34
35 glGetFloatv(GL_PROJECTION_MATRIX, projmatrix);
36 glGetFloatv(GL_MODELVIEW_MATRIX, mvmatrix);
37
38 // Combine the matrices
39 clip[0] = mvmatrix[0] * projmatrix[0] + mvmatrix[1] * projmatrix[4] + mvmatrix[2] * projmatrix[8] + mvmatrix[3] * projmatrix[12];
40 clip[1] = mvmatrix[0] * projmatrix[1] + mvmatrix[1] * projmatrix[5] + mvmatrix[2] * projmatrix[9] + mvmatrix[3] * projmatrix[13];
41 clip[2] = mvmatrix[0] * projmatrix[2] + mvmatrix[1] * projmatrix[6] + mvmatrix[2] * projmatrix[10] + mvmatrix[3] * projmatrix[14];
42 clip[3] = mvmatrix[0] * projmatrix[3] + mvmatrix[1] * projmatrix[7] + mvmatrix[2] * projmatrix[11] + mvmatrix[3] * projmatrix[15];
43
44 clip[4] = mvmatrix[4] * projmatrix[0] + mvmatrix[5] * projmatrix[4] + mvmatrix[6] * projmatrix[8] + mvmatrix[7] * projmatrix[12];
45 clip[5] = mvmatrix[4] * projmatrix[1] + mvmatrix[5] * projmatrix[5] + mvmatrix[6] * projmatrix[9] + mvmatrix[7] * projmatrix[13];
46 clip[6] = mvmatrix[4] * projmatrix[2] + mvmatrix[5] * projmatrix[6] + mvmatrix[6] * projmatrix[10] + mvmatrix[7] * projmatrix[14];
47 clip[7] = mvmatrix[4] * projmatrix[3] + mvmatrix[5] * projmatrix[7] + mvmatrix[6] * projmatrix[11] + mvmatrix[7] * projmatrix[15];
48
49 clip[8] = mvmatrix[8] * projmatrix[0] + mvmatrix[9] * projmatrix[4] + mvmatrix[10] * projmatrix[8] + mvmatrix[11] * projmatrix[12];
50 clip[9] = mvmatrix[8] * projmatrix[1] + mvmatrix[9] * projmatrix[5] + mvmatrix[10] * projmatrix[9] + mvmatrix[11] * projmatrix[13];
51 clip[10] = mvmatrix[8] * projmatrix[2] + mvmatrix[9] * projmatrix[6] + mvmatrix[10] * projmatrix[10] + mvmatrix[11] * projmatrix[14];
52 clip[11] = mvmatrix[8] * projmatrix[3] + mvmatrix[9] * projmatrix[7] + mvmatrix[10] * projmatrix[11] + mvmatrix[11] * projmatrix[15];
53
54 clip[12] = mvmatrix[12] * projmatrix[0] + mvmatrix[13] * projmatrix[4] + mvmatrix[14] * projmatrix[8] + mvmatrix[15] * projmatrix[12];
55 clip[13] = mvmatrix[12] * projmatrix[1] + mvmatrix[13] * projmatrix[5] + mvmatrix[14] * projmatrix[9] + mvmatrix[15] * projmatrix[13];
56 clip[14] = mvmatrix[12] * projmatrix[2] + mvmatrix[13] * projmatrix[6] + mvmatrix[14] * projmatrix[10] + mvmatrix[15] * projmatrix[14];
57 clip[15] = mvmatrix[12] * projmatrix[3] + mvmatrix[13] * projmatrix[7] + mvmatrix[14] * projmatrix[11] + mvmatrix[15] * projmatrix[15];
58
59 // Right plane
60 frustum[0][0] = clip[3] - clip[0];
61 frustum[0][1] = clip[7] - clip[4];
62 frustum[0][2] = clip[11] - clip[8];
63 frustum[0][3] = clip[15] - clip[12];
64
65 // Left plane
66 frustum[1][0] = clip[3] + clip[0];
67 frustum[1][1] = clip[7] + clip[4];
68 frustum[1][2] = clip[11] + clip[8];
69 frustum[1][3] = clip[15] + clip[12];
70
71 // Bottom plane
72 frustum[2][0] = clip[3] + clip[1];
73 frustum[2][1] = clip[7] + clip[5];
74 frustum[2][2] = clip[11] + clip[9];
75 frustum[2][3] = clip[15] + clip[13];
76
77 // Top plane
78 frustum[3][0] = clip[3] - clip[1];
79 frustum[3][1] = clip[7] - clip[5];
80 frustum[3][2] = clip[11] - clip[9];
81 frustum[3][3] = clip[15] - clip[13];
82
83 // Far plane
84 frustum[4][0] = clip[3] - clip[2];
85 frustum[4][1] = clip[7] - clip[6];
86 frustum[4][2] = clip[11] - clip[10];
87 frustum[4][3] = clip[15] - clip[14];
88
89 // Near plane
90 frustum[5][0] = clip[3] + clip[2];
91 frustum[5][1] = clip[7] + clip[6];
92 frustum[5][2] = clip[11] + clip[10];
93 frustum[5][3] = clip[15] + clip[14];
94 }
95
96 int FRUSTUM::
97 CubeInFrustum(float x, float y, float z, float size) {
98 static int c, c2;
99
100 for(int i=0; i<6; i++) {
101 c=0;
102 if(frustum[i][0] * (x-size) + frustum[i][1] * (y-size) + frustum[i][2] * (z-size) + frustum[i][3] > 0)
103 c++;
104 if(frustum[i][0] * (x+size) + frustum[i][1] * (y-size) + frustum[i][2] * (z-size) + frustum[i][3] > 0)
105 c++;
106 if(frustum[i][0] * (x-size) + frustum[i][1] * (y+size) + frustum[i][2] * (z-size) + frustum[i][3] > 0)
107 c++;
108 if(frustum[i][0] * (x+size) + frustum[i][1] * (y+size) + frustum[i][2] * (z-size) + frustum[i][3] > 0)
109 c++;
110 if(frustum[i][0] * (x-size) + frustum[i][1] * (y-size) + frustum[i][2] * (z+size) + frustum[i][3] > 0)
111 c++;
112 if(frustum[i][0] * (x+size) + frustum[i][1] * (y-size) + frustum[i][2] * (z+size) + frustum[i][3] > 0)
113 c++;
114 if(frustum[i][0] * (x-size) + frustum[i][1] * (y+size) + frustum[i][2] * (z+size) + frustum[i][3] > 0)
115 c++;
116 if(frustum[i][0] * (x+size) + frustum[i][1] * (y+size) + frustum[i][2] * (z+size) + frustum[i][3] > 0)
117 c++;
118 if(c==0)
119 return 0;
120 if(c==8)
121 c2++;
122 }
123 if(c2>=6)
124 return 2;
125 else
126 return 1;
127 }
128
129 int FRUSTUM::
130 CubeInFrustum(float x, float y, float z, float size, float height) {
131 static int c, c2;
132
133 for(int i=0; i<6; i++) {
134 c=0;
135 if(frustum[i][0] * (x-size) + frustum[i][1] * (y-height) + frustum[i][2] * (z-size) + frustum[i][3] > 0)
136 c++;
137 if(frustum[i][0] * (x+size) + frustum[i][1] * (y-height) + frustum[i][2] * (z-size) + frustum[i][3] > 0)
138 c++;
139 if(frustum[i][0] * (x-size) + frustum[i][1] * (y+height) + frustum[i][2] * (z-size) + frustum[i][3] > 0)
140 c++;
141 if(frustum[i][0] * (x+size) + frustum[i][1] * (y+height) + frustum[i][2] * (z-size) + frustum[i][3] > 0)
142 c++;
143 if(frustum[i][0] * (x-size) + frustum[i][1] * (y-height) + frustum[i][2] * (z+size) + frustum[i][3] > 0)
144 c++;
145 if(frustum[i][0] * (x+size) + frustum[i][1] * (y-height) + frustum[i][2] * (z+size) + frustum[i][3] > 0)
146 c++;
147 if(frustum[i][0] * (x-size) + frustum[i][1] * (y+height) + frustum[i][2] * (z+size) + frustum[i][3] > 0)
148 c++;
149 if(frustum[i][0] * (x+size) + frustum[i][1] * (y+height) + frustum[i][2] * (z+size) + frustum[i][3] > 0)
150 c++;
151 if(c==0)
152 return 0;
153 if(c==8)
154 c2++;
155 }
156 if(c2>=6)
157 return 2;
158 else
159 return 1;
160 }
161
162 int FRUSTUM::
163 SphereInFrustum(float x, float y, float z, float radius) {
164 static int c2;
165
166 for(int i=0; i<6; i++) {
167 if(frustum[i][0] * x + frustum[i][1] * y + frustum[i][2] * z + frustum[i][3] > -1*radius)
168 c2++;
169 else
170 return 0;
171 }
172 if(c2>=6)
173 return 2;
174 else
175 return 1;
36cc3af3 » rcgordon
2005-08-05 Added newline to all the source/headers in Source.
176 }
Something went wrong with that request. Please try again.