4 * Copyright (C) 2004-2011 Simon Wunderlich <dotslash@packetmixer.de>
5 * code originated from http://www.racer.nl/reference/vfc.htm
6 * which is (C) Ruud van Gaal
8 * This file is part of s3d, a 3d network display server.
9 * See http://s3d.berlios.de/ for more updates.
11 * s3d is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * s3d is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with s3d; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <math.h> /* sqrt() */
29 #include <SDL_opengl.h> /* glGetFloatv */
31 #include <GL/gl.h> /* glGetFloatv */
44 static struct t_plane frustumPlane
[6];
46 void cull_get_planes(void)
48 t_mtrx m
, mproj
, mmodel
;
53 /* get matrices from opengl */
54 glGetFloatv(GL_MODELVIEW_MATRIX
, mmodel
);
55 glGetFloatv(GL_PROJECTION_MATRIX
, mproj
);
59 myGetMatrix(m
); /* multiply and have the result in m */
61 p
= &frustumPlane
[RIGHT
];
64 p
->n
.z
= m
[11] - m
[8];
67 p
= &frustumPlane
[LEFT
];
70 p
->n
.z
= m
[11] + m
[8];
73 p
= &frustumPlane
[BOTTOM
];
76 p
->n
.z
= m
[11] + m
[9];
79 p
= &frustumPlane
[TOP
];
82 p
->n
.z
= m
[11] - m
[9];
85 p
= &frustumPlane
[PFAR
];
88 p
->n
.z
= m
[11] - m
[10];
91 p
= &frustumPlane
[PNEAR
];
94 p
->n
.z
= m
[11] + m
[10];
97 /* Normalize all plane normals */
98 for (i
= 0; i
< 6; i
++) {
100 d
= sqrt(p
->n
.x
* p
->n
.x
+ p
->n
.y
* p
->n
.y
+ p
->n
.z
* p
->n
.z
);
110 int cull_sphere_in_frustum(struct t_vertex
*center
, float radius
)
114 for (i
= 0; i
< 6; i
++) {
115 p
= &frustumPlane
[i
];
116 if (p
->n
.x
* center
->x
+ p
->n
.y
* center
->y
+ p
->n
.z
* center
->z
+ p
->d
<= -radius
) {
117 return 0; /* sorry, no ... */
120 return 1; /* it's inside */