Release s3d 0.2.2
[s3d.git] / server / cull.c
blob448799b8eec87bb91e7f87ab6072609645c76ce8
1 /*
2 * cull.c
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
26 #include "global.h"
27 #include <math.h> /* sqrt() */
28 #if G_SDL
29 #include <SDL_opengl.h> /* glGetFloatv */
30 #else
31 #include <GL/gl.h> /* glGetFloatv */
32 #endif
33 struct t_plane {
34 struct t_vertex n;
35 float d;
37 #define LEFT 0
38 #define RIGHT 1
39 #define TOP 2
40 #define BOTTOM 3
41 #define PNEAR 4
42 #define PFAR 5
44 static struct t_plane frustumPlane[6];
46 void cull_get_planes(void)
48 t_mtrx m, mproj, mmodel;
49 struct t_plane *p;
50 int i;
51 float d;
53 /* get matrices from opengl */
54 glGetFloatv(GL_MODELVIEW_MATRIX, mmodel);
55 glGetFloatv(GL_PROJECTION_MATRIX, mproj);
57 mySetMatrix(mproj);
58 myMultMatrix(mmodel);
59 myGetMatrix(m); /* multiply and have the result in m */
61 p = &frustumPlane[RIGHT];
62 p->n.x = m[3] - m[0];
63 p->n.y = m[7] - m[4];
64 p->n.z = m[11] - m[8];
65 p->d = m[15] - m[12];
67 p = &frustumPlane[LEFT];
68 p->n.x = m[3] + m[0];
69 p->n.y = m[7] + m[4];
70 p->n.z = m[11] + m[8];
71 p->d = m[15] + m[12];
73 p = &frustumPlane[BOTTOM];
74 p->n.x = m[3] + m[1];
75 p->n.y = m[7] + m[5];
76 p->n.z = m[11] + m[9];
77 p->d = m[15] + m[13];
79 p = &frustumPlane[TOP];
80 p->n.x = m[3] - m[1];
81 p->n.y = m[7] - m[5];
82 p->n.z = m[11] - m[9];
83 p->d = m[15] - m[13];
85 p = &frustumPlane[PFAR];
86 p->n.x = m[3] - m[2];
87 p->n.y = m[7] - m[6];
88 p->n.z = m[11] - m[10];
89 p->d = m[15] - m[14];
91 p = &frustumPlane[PNEAR];
92 p->n.x = m[3] + m[2];
93 p->n.y = m[7] + m[6];
94 p->n.z = m[11] + m[10];
95 p->d = m[15] + m[14];
97 /* Normalize all plane normals */
98 for (i = 0; i < 6; i++) {
99 p = &frustumPlane[i];
100 d = sqrt(p->n.x * p->n.x + p->n.y * p->n.y + p->n.z * p->n.z);
101 if (d != 0.0) {
102 p->n.x /= d;
103 p->n.y /= d;
104 p->n.z /= d;
105 p->d /= d;
110 int cull_sphere_in_frustum(struct t_vertex *center, float radius)
112 int i;
113 struct t_plane *p;
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 */