~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to source/gameengine/Ketsji/KX_Camera.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2007-05-17 11:47:59 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20070517114759-yp4ybrnhp2u7pk66
Tags: 2.44-1
* New upstream release.
* Drop debian/patches/01_64bits_stupidity, not needed anymore: as of this
  version blender is 64 bits safe again. Adjust README.Debian accordingly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Id: KX_Camera.cpp,v 1.16 2006/01/06 03:46:52 erwin Exp $
 
2
 * $Id: KX_Camera.cpp,v 1.17 2007/04/04 13:18:39 campbellbarton Exp $
3
3
 *
4
4
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5
5
 *
48
48
                                        KX_GameObject(sgReplicationInfo,callbacks,T),
49
49
                                        m_camdata(camdata),
50
50
                                        m_dirty(true),
51
 
                                        m_normalised(false),
 
51
                                        m_normalized(false),
52
52
                                        m_frustum_culling(frustum_culling && camdata.m_perspective),
53
53
                                        m_set_projection_matrix(false),
54
 
                                        m_set_frustum_centre(false)
 
54
                                        m_set_frustum_center(false)
55
55
{
56
56
        // setting a name would be nice...
57
57
        m_name = "cam";
120
120
        m_projection_matrix = mat;
121
121
        m_dirty = true;
122
122
        m_set_projection_matrix = true;
123
 
        m_set_frustum_centre = false;
 
123
        m_set_frustum_center = false;
124
124
}
125
125
 
126
126
 
132
132
{
133
133
        m_modelview_matrix = mat;
134
134
        m_dirty = true;
135
 
        m_set_frustum_centre = false;
 
135
        m_set_frustum_center = false;
136
136
}
137
137
 
138
138
 
216
216
        m_planes[5] = m[3] - m[2];
217
217
        
218
218
        m_dirty = false;
219
 
        m_normalised = false;
 
219
        m_normalized = false;
220
220
}
221
221
 
222
 
void KX_Camera::NormaliseClipPlanes()
 
222
void KX_Camera::NormalizeClipPlanes()
223
223
{
224
 
        if (m_normalised)
 
224
        if (m_normalized)
225
225
                return;
226
226
        
227
227
        for (unsigned int p = 0; p < 6; p++)
231
231
                        m_planes[p] /= factor;
232
232
        }
233
233
        
234
 
        m_normalised = true;
 
234
        m_normalized = true;
235
235
}
236
236
 
237
237
void KX_Camera::ExtractFrustumSphere()
238
238
{
239
 
        if (m_set_frustum_centre)
 
239
        if (m_set_frustum_center)
240
240
                return;
241
241
 
242
 
        // The most extreme points on the near and far plane. (normalised device coords)
 
242
        // The most extreme points on the near and far plane. (normalized device coords)
243
243
        MT_Vector4 hnear(1., 1., 0., 1.), hfar(1., 1., 1., 1.);
244
244
        MT_Matrix4x4 clip_camcs_matrix = m_projection_matrix;
245
245
        clip_camcs_matrix.invert();
252
252
        MT_Point3 nearpoint(hnear[0]/hnear[3], hnear[1]/hnear[3], hnear[2]/hnear[3]);
253
253
        MT_Point3 farpoint(hfar[0]/hfar[3], hfar[1]/hfar[3], hfar[2]/hfar[3]);
254
254
        
255
 
        // Compute centre
256
 
        m_frustum_centre = MT_Point3(0., 0.,
 
255
        // Compute center
 
256
        m_frustum_center = MT_Point3(0., 0.,
257
257
                (nearpoint.dot(nearpoint) - farpoint.dot(farpoint))/(2.0*(m_camdata.m_clipend - m_camdata.m_clipstart)));
258
 
        m_frustum_radius = m_frustum_centre.distance(farpoint);
 
258
        m_frustum_radius = m_frustum_center.distance(farpoint);
259
259
        
260
260
        // Transform to world space.
261
 
        m_frustum_centre = GetCameraToWorld()(m_frustum_centre);
 
261
        m_frustum_center = GetCameraToWorld()(m_frustum_center);
262
262
        m_frustum_radius /= fabs(NodeGetWorldScaling()[NodeGetWorldScaling().closestAxis()]);
263
263
        
264
 
        m_set_frustum_centre = true;
 
264
        m_set_frustum_center = true;
265
265
}
266
266
 
267
267
bool KX_Camera::PointInsideFrustum(const MT_Point3& x)
308
308
        return INTERSECT;
309
309
}
310
310
 
311
 
int KX_Camera::SphereInsideFrustum(const MT_Point3& centre, const MT_Scalar &radius)
 
311
int KX_Camera::SphereInsideFrustum(const MT_Point3& center, const MT_Scalar &radius)
312
312
{
313
313
        ExtractFrustumSphere();
314
 
        if (centre.distance2(m_frustum_centre) > (radius + m_frustum_radius)*(radius + m_frustum_radius))
 
314
        if (center.distance2(m_frustum_center) > (radius + m_frustum_radius)*(radius + m_frustum_radius))
315
315
                return OUTSIDE;
316
316
 
317
317
        unsigned int p;
318
318
        ExtractClipPlanes();
319
 
        NormaliseClipPlanes();
 
319
        NormalizeClipPlanes();
320
320
                
321
321
        MT_Scalar distance;
322
322
        int intersect = INSIDE;
324
324
        //                                -radius                                      radius
325
325
        for (p = 0; p < 6; p++)
326
326
        {
327
 
                distance = m_planes[p][0]*centre[0] + m_planes[p][1]*centre[1] + m_planes[p][2]*centre[2] + m_planes[p][3];
 
327
                distance = m_planes[p][0]*center[0] + m_planes[p][1]*center[1] + m_planes[p][2]*center[2] + m_planes[p][3];
328
328
                if (fabs(distance) <= radius)
329
329
                        intersect = INTERSECT;
330
330
                else if (distance < -radius)
537
537
}
538
538
 
539
539
KX_PYMETHODDEF_DOC(KX_Camera, sphereInsideFrustum,
540
 
"sphereInsideFrustum(centre, radius) -> Integer\n"
 
540
"sphereInsideFrustum(center, radius) -> Integer\n"
541
541
"\treturns INSIDE, OUTSIDE or INTERSECT if the given sphere is\n"
542
542
"\tinside/outside/intersects this camera's viewing frustum.\n\n"
543
 
"\tcentre = the centre of the sphere (in world coordinates.)\n"
 
543
"\tcenter = the center of the sphere (in world coordinates.)\n"
544
544
"\tradius = the radius of the sphere\n\n"
545
545
"\tExample:\n"
546
546
"\timport GameLogic\n\n"
554
554
"\t\t# Sphere is outside frustum\n"
555
555
)
556
556
{
557
 
        PyObject *pycentre;
 
557
        PyObject *pycenter;
558
558
        float radius;
559
 
        if (PyArg_ParseTuple(args, "Of", &pycentre, &radius))
 
559
        if (PyArg_ParseTuple(args, "Of", &pycenter, &radius))
560
560
        {
561
 
                MT_Point3 centre;
562
 
                if (PyVecTo(pycentre, centre))
 
561
                MT_Point3 center;
 
562
                if (PyVecTo(pycenter, center))
563
563
                {
564
 
                        return PyInt_FromLong(SphereInsideFrustum(centre, radius)); /* new ref */
 
564
                        return PyInt_FromLong(SphereInsideFrustum(center, radius)); /* new ref */
565
565
                }
566
566
        }
567
567
 
568
 
        PyErr_SetString(PyExc_TypeError, "sphereInsideFrustum: Expected arguments: (centre, radius)");
 
568
        PyErr_SetString(PyExc_TypeError, "sphereInsideFrustum: Expected arguments: (center, radius)");
569
569
        
570
570
        Py_Return;
571
571
}