~ubuntu-branches/ubuntu/intrepid/blender/intrepid-updates

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-08-08 02:45:40 UTC
  • mfrom: (12.1.14 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080808024540-kkjp7ekfivzhuw3l
Tags: 2.46+dfsg-4
* Fix python syntax warning in import_dxf.py, which led to nasty output
  in installation/upgrade logs during byte-compilation, using a patch
  provided by the script author (Closes: #492280):
   - debian/patches/45_fix_python_syntax_warning

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 14444 2008-04-16 22:40:48Z hos $
3
3
 *
4
 
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 
4
 * ***** BEGIN GPL LICENSE BLOCK *****
5
5
 *
6
6
 * This program is free software; you can redistribute it and/or
7
7
 * modify it under the terms of the GNU General Public License
8
8
 * as published by the Free Software Foundation; either version 2
9
 
 * of the License, or (at your option) any later version. The Blender
10
 
 * Foundation also sells licenses for use in proprietary software under
11
 
 * the Blender License.  See http://www.blender.org/BL/ for information
12
 
 * about this.
 
9
 * of the License, or (at your option) any later version.
13
10
 *
14
11
 * This program is distributed in the hope that it will be useful,
15
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
24
 *
28
25
 * Contributor(s): none yet.
29
26
 *
30
 
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 
27
 * ***** END GPL LICENSE BLOCK *****
31
28
 * Camera in the gameengine. Cameras are also used for views.
32
29
 */
33
30
 
34
31
#include "KX_Camera.h"
35
 
 
 
32
#include "KX_Scene.h"
 
33
#include "KX_PythonInit.h"
36
34
#include "KX_Python.h"
37
35
#include "KX_PyMath.h"
38
36
#ifdef HAVE_CONFIG_H
48
46
                                        KX_GameObject(sgReplicationInfo,callbacks,T),
49
47
                                        m_camdata(camdata),
50
48
                                        m_dirty(true),
51
 
                                        m_normalised(false),
 
49
                                        m_normalized(false),
52
50
                                        m_frustum_culling(frustum_culling && camdata.m_perspective),
53
51
                                        m_set_projection_matrix(false),
54
 
                                        m_set_frustum_centre(false)
 
52
                                        m_set_frustum_center(false)
55
53
{
56
54
        // setting a name would be nice...
57
55
        m_name = "cam";
58
56
        m_projection_matrix.setIdentity();
59
57
        m_modelview_matrix.setIdentity();
60
 
        SetProperty("camera",new CIntValue(1));
 
58
        CValue* val = new CIntValue(1);
 
59
        SetProperty("camera",val);
 
60
        val->Release();
61
61
}
62
62
 
63
63
 
66
66
}       
67
67
 
68
68
 
69
 
        
 
69
CValue* KX_Camera::GetReplica()
 
70
{
 
71
        KX_Camera* replica = new KX_Camera(*this);
 
72
        
 
73
        // this will copy properties and so on...
 
74
        CValue::AddDataToReplica(replica);
 
75
        ProcessReplica(replica);
 
76
        
 
77
        return replica;
 
78
}
 
79
        
 
80
void KX_Camera::ProcessReplica(KX_Camera* replica)
 
81
{
 
82
        KX_GameObject::ProcessReplica(replica);
 
83
}
 
84
 
70
85
MT_Transform KX_Camera::GetWorldToCamera() const
71
86
72
87
        MT_Transform camtrans;
120
135
        m_projection_matrix = mat;
121
136
        m_dirty = true;
122
137
        m_set_projection_matrix = true;
123
 
        m_set_frustum_centre = false;
 
138
        m_set_frustum_center = false;
124
139
}
125
140
 
126
141
 
132
147
{
133
148
        m_modelview_matrix = mat;
134
149
        m_dirty = true;
135
 
        m_set_frustum_centre = false;
 
150
        m_set_frustum_center = false;
136
151
}
137
152
 
138
153
 
216
231
        m_planes[5] = m[3] - m[2];
217
232
        
218
233
        m_dirty = false;
219
 
        m_normalised = false;
 
234
        m_normalized = false;
220
235
}
221
236
 
222
 
void KX_Camera::NormaliseClipPlanes()
 
237
void KX_Camera::NormalizeClipPlanes()
223
238
{
224
 
        if (m_normalised)
 
239
        if (m_normalized)
225
240
                return;
226
241
        
227
242
        for (unsigned int p = 0; p < 6; p++)
231
246
                        m_planes[p] /= factor;
232
247
        }
233
248
        
234
 
        m_normalised = true;
 
249
        m_normalized = true;
235
250
}
236
251
 
237
252
void KX_Camera::ExtractFrustumSphere()
238
253
{
239
 
        if (m_set_frustum_centre)
 
254
        if (m_set_frustum_center)
240
255
                return;
241
256
 
242
 
        // The most extreme points on the near and far plane. (normalised device coords)
 
257
        // The most extreme points on the near and far plane. (normalized device coords)
243
258
        MT_Vector4 hnear(1., 1., 0., 1.), hfar(1., 1., 1., 1.);
244
259
        MT_Matrix4x4 clip_camcs_matrix = m_projection_matrix;
245
260
        clip_camcs_matrix.invert();
252
267
        MT_Point3 nearpoint(hnear[0]/hnear[3], hnear[1]/hnear[3], hnear[2]/hnear[3]);
253
268
        MT_Point3 farpoint(hfar[0]/hfar[3], hfar[1]/hfar[3], hfar[2]/hfar[3]);
254
269
        
255
 
        // Compute centre
256
 
        m_frustum_centre = MT_Point3(0., 0.,
 
270
        // Compute center
 
271
        m_frustum_center = MT_Point3(0., 0.,
257
272
                (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);
 
273
        m_frustum_radius = m_frustum_center.distance(farpoint);
259
274
        
260
275
        // Transform to world space.
261
 
        m_frustum_centre = GetCameraToWorld()(m_frustum_centre);
 
276
        m_frustum_center = GetCameraToWorld()(m_frustum_center);
262
277
        m_frustum_radius /= fabs(NodeGetWorldScaling()[NodeGetWorldScaling().closestAxis()]);
263
278
        
264
 
        m_set_frustum_centre = true;
 
279
        m_set_frustum_center = true;
265
280
}
266
281
 
267
282
bool KX_Camera::PointInsideFrustum(const MT_Point3& x)
308
323
        return INTERSECT;
309
324
}
310
325
 
311
 
int KX_Camera::SphereInsideFrustum(const MT_Point3& centre, const MT_Scalar &radius)
 
326
int KX_Camera::SphereInsideFrustum(const MT_Point3& center, const MT_Scalar &radius)
312
327
{
313
328
        ExtractFrustumSphere();
314
 
        if (centre.distance2(m_frustum_centre) > (radius + m_frustum_radius)*(radius + m_frustum_radius))
 
329
        if (center.distance2(m_frustum_center) > (radius + m_frustum_radius)*(radius + m_frustum_radius))
315
330
                return OUTSIDE;
316
331
 
317
332
        unsigned int p;
318
333
        ExtractClipPlanes();
319
 
        NormaliseClipPlanes();
 
334
        NormalizeClipPlanes();
320
335
                
321
336
        MT_Scalar distance;
322
337
        int intersect = INSIDE;
324
339
        //                                -radius                                      radius
325
340
        for (p = 0; p < 6; p++)
326
341
        {
327
 
                distance = m_planes[p][0]*centre[0] + m_planes[p][1]*centre[1] + m_planes[p][2]*centre[2] + m_planes[p][3];
 
342
                distance = m_planes[p][0]*center[0] + m_planes[p][1]*center[1] + m_planes[p][2]*center[2] + m_planes[p][3];
328
343
                if (fabs(distance) <= radius)
329
344
                        intersect = INTERSECT;
330
345
                else if (distance < -radius)
391
406
        KX_PYMETHODTABLE(KX_Camera, setProjectionMatrix),
392
407
        KX_PYMETHODTABLE(KX_Camera, enableViewport),
393
408
        KX_PYMETHODTABLE(KX_Camera, setViewport),
 
409
        KX_PYMETHODTABLE(KX_Camera, setOnTop),
394
410
        
395
411
        {NULL,NULL} //Sentinel
396
412
};
537
553
}
538
554
 
539
555
KX_PYMETHODDEF_DOC(KX_Camera, sphereInsideFrustum,
540
 
"sphereInsideFrustum(centre, radius) -> Integer\n"
 
556
"sphereInsideFrustum(center, radius) -> Integer\n"
541
557
"\treturns INSIDE, OUTSIDE or INTERSECT if the given sphere is\n"
542
558
"\tinside/outside/intersects this camera's viewing frustum.\n\n"
543
 
"\tcentre = the centre of the sphere (in world coordinates.)\n"
 
559
"\tcenter = the center of the sphere (in world coordinates.)\n"
544
560
"\tradius = the radius of the sphere\n\n"
545
561
"\tExample:\n"
546
562
"\timport GameLogic\n\n"
554
570
"\t\t# Sphere is outside frustum\n"
555
571
)
556
572
{
557
 
        PyObject *pycentre;
 
573
        PyObject *pycenter;
558
574
        float radius;
559
 
        if (PyArg_ParseTuple(args, "Of", &pycentre, &radius))
 
575
        if (PyArg_ParseTuple(args, "Of", &pycenter, &radius))
560
576
        {
561
 
                MT_Point3 centre;
562
 
                if (PyVecTo(pycentre, centre))
 
577
                MT_Point3 center;
 
578
                if (PyVecTo(pycenter, center))
563
579
                {
564
 
                        return PyInt_FromLong(SphereInsideFrustum(centre, radius)); /* new ref */
 
580
                        return PyInt_FromLong(SphereInsideFrustum(center, radius)); /* new ref */
565
581
                }
566
582
        }
567
583
 
568
 
        PyErr_SetString(PyExc_TypeError, "sphereInsideFrustum: Expected arguments: (centre, radius)");
 
584
        PyErr_SetString(PyExc_TypeError, "sphereInsideFrustum: Expected arguments: (center, radius)");
569
585
        
570
586
        Py_Return;
571
587
}
759
775
        }
760
776
        Py_Return;
761
777
}
 
778
 
 
779
KX_PYMETHODDEF_DOC(KX_Camera, setOnTop,
 
780
"setOnTop()\n"
 
781
"Sets this camera's viewport on top\n")
 
782
{
 
783
        class KX_Scene* scene;
 
784
        
 
785
        scene = PHY_GetActiveScene();
 
786
        MT_assert(scene);
 
787
        scene->SetCameraOnTop(this);
 
788
        Py_Return;
 
789
}