source: trunk/test/btphystest.cpp @ 2910

Last change on this file since 2910 was 2910, checked in by sam, 6 years ago

physics: always activate Bullet and remove dead code.

  • Property svn:eol-style set to LF
File size: 23.4 KB
Line 
1//
2// BtPhysTest
3//
4// Copyright: (c) 2009-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
5//            (c) 2012-2013 Sam Hocevar <sam@hocevar.net>
6//
7
8#if defined HAVE_CONFIG_H
9#   include "config.h"
10#endif
11
12#include "core.h"
13#include "loldebug.h"
14
15using namespace lol;
16
17#include "physics/lolphysics.h"
18#include "physics/easyphysics.h"
19
20#define CAT_MODE        1
21#define OBJ_SIZE        2.f
22#define NB_SPRITE       4
23#define PARTICLE_SIZE   4
24#include "physicobject.h"
25
26#include "btphystest.h"
27
28using namespace lol::phys;
29
30#define CUBE_HALF_EXTENTS   .5f
31#define EXTRA_HEIGHT        1.f
32#define BASE_TIME           2.f
33#define ZERO_TIME (BASE_TIME + rand(-BASE_TIME * .4f, BASE_TIME * .4f))
34#define ZERO_SPEED          3.5f
35#define JUMP_HEIGHT         30.f
36#define JUMP_STRAFE         .5f
37#define TARGET_TIMER        10.f + (rand(4.f) - 2.f)
38
39int gNumObjects = 64;
40
41#if CAT_MODE
42#define USE_WALL        1
43#define USE_BODIES      1
44#else
45#define USE_WALL        1
46#define USE_PLATFORM    1
47#define USE_ROPE        0
48#define USE_BODIES      1
49#define USE_ROTATION    0
50#define USE_CHARACTER   0
51#define USE_STAIRS      0
52#endif
53
54LOLFX_RESOURCE_DECLARE(front_camera_sprite);
55
56BtPhysTest::BtPhysTest(bool editor)
57{
58    m_init_status = 0;
59}
60
61void BtPhysTest::InitApp()
62{
63    m_init_status = 1;
64
65    m_loop_value = .0f;
66
67#if CAT_MODE
68    /* cat datas setup */
69    m_cat_texture = Tiler::Register("data/CatsSheet.png", ivec2(0), ivec2(0,1));
70    m_fov_dp = .0f;
71    m_loc_dp = .0f;
72#endif //CAT_MODE
73
74    /* Register an input controller for the keyboard */
75    m_controller = new Controller(KEY_MAX, 0);
76    m_controller->GetKey(KEY_MOVE_FORWARD).Bind("Keyboard", "Up");
77    m_controller->GetKey(KEY_MOVE_BACK).Bind("Keyboard", "Down");
78    m_controller->GetKey(KEY_MOVE_LEFT).Bind("Keyboard", "Left");
79    m_controller->GetKey(KEY_MOVE_RIGHT).Bind("Keyboard", "Right");
80    m_controller->GetKey(KEY_MOVE_JUMP).Bind("Keyboard", "Space");
81    m_controller->GetKey(KEY_MOVE_UP).Bind("Keyboard", "PageUp");
82    m_controller->GetKey(KEY_MOVE_DOWN).Bind("Keyboard", "PageDown");
83    m_controller->GetKey(KEY_QUIT).Bind("Keyboard", "Escape");
84
85    /* Create a camera that matches the settings of XNA BtPhysTest */
86    m_camera = new Camera();
87
88#if CAT_MODE
89    m_camera->SetView(vec3(70.f, 50.f, 0.f),
90                      vec3(0.f, 0.f, 0.f),
91                      vec3(0, 1, 0));
92    m_camera->SetProjection(60.f, .1f, 1000.f, (float)Video::GetSize().x, (float)Video::GetSize().y / (float)Video::GetSize().x);
93    m_target_timer = TARGET_TIMER;
94    m_cam_target = -1;
95#else
96    m_camera->SetView(vec3(50.f, 50.f, 0.f),
97                      vec3(0.f, 0.f, 0.f),
98                      vec3(0, 1, 0));
99    m_camera->SetProjection(45.f, .1f, 1000.f, (float)Video::GetSize().x, (float)Video::GetSize().y / (float)Video::GetSize().x);
100#endif
101    g_scene->PushCamera(m_camera);
102
103    m_ready = false;
104
105    m_simulation = new Simulation();
106    m_simulation->SetWorldLimit(vec3(-1000.0f, -1000.0f, -1000.0f), vec3(1000.0f, 1000.0f, 1000.0f));
107    m_simulation->Init();
108    vec3 NewGravity = vec3(.0f, -10.0f, .0f);
109    m_simulation->SetGravity(NewGravity);
110    m_simulation->SetContinuousDetection(true);
111    m_simulation->SetTimestep(1.f / 120.f);
112    Ticker::Ref(m_simulation);
113
114    /* Add a white directional light */
115    m_light1 = new Light();
116    m_light1->SetPosition(vec4(0.2f, 0.2f, 0.f, 0.f));
117    m_light1->SetColor(vec4(0.5f, 0.5f, 0.5f, 1.f));
118    Ticker::Ref(m_light1);
119
120    /* Add an orangeish point light */
121    m_light2 = new Light();
122    m_light2->SetPosition(vec4(-15.f, 15.f, 15.f, 1.f));
123    m_light2->SetColor(vec4(0.4f, 0.3f, 0.2f, 1.f));
124    Ticker::Ref(m_light2);
125
126    float offset = 29.5f;
127    vec3 pos_offset = vec3(.0f, 30.f, .0f);
128#if USE_STAIRS
129    {
130        vec3 new_offset = vec3(1.0f, .125f, .0f);
131        quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
132        vec3 NewPosition = pos_offset + vec3(5.0f, -29.f, 15.0f);
133        {
134            NewRotation = quat::fromeuler_xyz(0.f, 0.f, 30.f);
135            NewPosition += vec3(4.0f, .0f, -4.0f);
136
137            PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
138            Ticker::Ref(NewPhyobj);
139            m_stairs_list << NewPhyobj;
140        }
141        {
142            NewRotation = quat::fromeuler_xyz(0.f, 0.f, 40.f);
143            NewPosition += vec3(4.0f, .0f, -4.0f);
144
145            PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
146            Ticker::Ref(NewPhyobj);
147            m_stairs_list << NewPhyobj;
148        }
149        NewPosition = pos_offset + vec3(5.0f, -29.5f, 15.0f);
150        NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
151        for (int i=0; i < 15; i++)
152        {
153            NewPosition += new_offset;
154
155            PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
156            Ticker::Ref(NewPhyobj);
157            m_stairs_list << NewPhyobj;
158        }
159    }
160#endif //USE_STAIRS
161
162#if USE_WALL
163    {
164        for (int i=0; i < 6; i++)
165        {
166            vec3 NewPosition = vec3(.0f);
167            quat NewRotation = quat(1.f);
168
169            PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation);
170
171            int idx = i/2;
172            NewPosition = pos_offset;
173            NewPosition[idx] += offset;
174            offset *= -1.f;
175
176            if (idx != 1)
177            {
178                vec3 NewAxis = vec3(.0f);
179                NewAxis[2 - idx] = 1;
180                NewRotation = quat::rotate(90.f, NewAxis);
181            }
182
183            NewPhyobj->SetTransform(NewPosition, NewRotation);
184            Ticker::Ref(NewPhyobj);
185            m_ground_list << NewPhyobj;
186        }
187    }
188#endif //USE_WALL
189
190    PhysicsObject* BasePhyobj = NULL;
191#if USE_PLATFORM
192    {
193        quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
194        vec3 NewPosition = pos_offset + vec3(5.0f, -25.0f, -15.0f);
195
196        PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
197
198        m_platform_list << NewPhyobj;
199        Ticker::Ref(NewPhyobj);
200
201        NewPosition = pos_offset + vec3(-15.0f, -25.0f, 5.0f);
202
203        NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
204        BasePhyobj = NewPhyobj;
205
206        m_platform_list << NewPhyobj;
207        Ticker::Ref(NewPhyobj);
208
209        NewRotation = quat::fromeuler_xyz(0.f, 0.f, 90.f);
210        NewPosition = pos_offset + vec3(-20.0f, -25.0f, 5.0f);
211
212        NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
213
214        NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), true, true);
215        m_platform_list << NewPhyobj;
216        Ticker::Ref(NewPhyobj);
217
218        //NewPosition += vec3(-0.0f, .0f, .0f);
219        //NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
220
221        //NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), true, false);
222        //m_platform_list << NewPhyobj;
223        //Ticker::Ref(NewPhyobj);
224
225        //NewPosition += vec3(-2.0f, .0f, .0f);
226        //NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
227
228        //NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), false, false);
229        //m_platform_list << NewPhyobj;
230        //Ticker::Ref(NewPhyobj);
231    }
232#endif //USE_PLATFORM
233
234#if USE_CHARACTER
235    {
236        quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
237        vec3 NewPosition = pos_offset + vec3(-5.0f, -10.0f, 15.0f);
238
239        PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 2);
240
241        m_character_list << NewPhyobj;
242        Ticker::Ref(NewPhyobj);
243
244        //NewPhyobj->GetCharacter()->AttachTo(BasePhyobj->GetPhysic(), true, true);
245    }
246#endif //USE_CHARACTER
247
248#if USE_BODIES
249    {
250        for (int x=0; x < 6; x++)
251        {
252            for (int y=0; y < 2; y++)
253            {
254                for (int z=0; z < 5; z++)
255                {
256                    PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f,
257                        vec3(-20.f, 15.f, -20.f) +
258#if CAT_MODE
259                        vec3(rand(4.f), rand(2.f), rand(4.f)) -
260                        vec3(2.f       , 1.f      , 2.f) +
261#endif //CAT_MODE
262                        vec3(8.f * (float)x, 8.f * (float)y, 8.f * (float)z));
263                    m_physobj_list.Push(new_physobj, ZERO_TIME);
264                    Ticker::Ref(new_physobj);
265                }
266            }
267        }
268    }
269#endif //USE_BODIES
270
271#if USE_ROPE
272    {
273        Array<PhysicsObject*> RopeElements;
274        for (int i = 0; i < 14; i++)
275        {
276            PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f,
277                vec3(0.f, 15.f, -20.f) +
278                vec3(0.f, 0.f, 2.f * (float)i), 1);
279            RopeElements << new_physobj;
280            m_physobj_list.Push(new_physobj, ZERO_TIME);
281            Ticker::Ref(new_physobj);
282            if (RopeElements.Count() > 1)
283            {
284                EasyConstraint* new_constraint = new EasyConstraint();
285
286                vec3 A2B = .5f * (RopeElements[i]->GetPhysic()->GetTransform().v3.xyz -
287                            RopeElements[i - 1]->GetPhysic()->GetTransform().v3.xyz);
288                new_constraint->SetPhysObjA(RopeElements[i - 1]->GetPhysic(), lol::mat4::translate(A2B));
289                new_constraint->SetPhysObjB(RopeElements[i]->GetPhysic(), lol::mat4::translate(-A2B));
290                new_constraint->InitConstraintToPoint2Point();
291                new_constraint->DisableCollisionBetweenObjs(true);
292                new_constraint->AddToSimulation(m_simulation);
293                m_constraint_list << new_constraint;
294            }
295        }
296    }
297#endif //USE_ROPE
298}
299
300void BtPhysTest::TickGame(float seconds)
301{
302    WorldEntity::TickGame(seconds);
303
304    if (!m_init_status)
305    {
306        if (g_renderer)
307            InitApp();
308        return;
309    }
310    else if (m_init_status == 1)
311    {
312        m_init_status++;
313        return;
314    }
315
316
317    if (m_controller->GetKey(KEY_QUIT).IsReleased())
318        Ticker::Shutdown();
319
320    m_loop_value += seconds;
321    if (m_loop_value > F_PI * 2.0f)
322        m_loop_value -= F_PI * 2.0f;
323
324    vec3 GroundBarycenter = vec3(.0f);
325    vec3 PhysObjBarycenter = vec3(.0f);
326    float factor = .0f;
327#if CAT_MODE
328#if USE_BODIES
329    vec3 cam_center(0.f);
330    float cam_factor = .0f;
331    vec2 screen_min_max[2] = { vec2(FLT_MAX), vec2(-FLT_MAX) };
332    mat4 world_cam = g_scene->GetCamera()->GetView();
333    mat4 cam_screen = g_scene->GetCamera()->GetProjection();
334
335    m_target_timer -= seconds;
336    if (m_target_timer < .0f)
337    {
338        m_target_timer = TARGET_TIMER;
339        if (m_cam_target == -1)
340            m_cam_target = rand(m_physobj_list.Count());
341        else
342            m_cam_target = -1;
343    }
344
345    for (int i = 0; i < m_physobj_list.Count(); i++)
346    {
347        PhysicsObject* PhysObj = m_physobj_list[i].m1;
348        float &Timer = m_physobj_list[i].m2;
349
350        vec3 obj_loc = PhysObj->GetPhysic()->GetTransform().v3.xyz;
351
352        if (m_cam_target == -1 || m_cam_target == i)
353        {
354            cam_center += obj_loc;
355            cam_factor += 1.f;
356
357            mat4 LocalPos = mat4::translate(obj_loc);
358            vec3 vpos;
359
360            LocalPos = world_cam * LocalPos;
361            mat4 LocalPos0 = LocalPos;
362
363            int j = 2;
364            while (j-- > 0)
365            {
366                if (j == 1)
367                    LocalPos = mat4::translate(vec3(-4.f)) * LocalPos0;
368                else
369                    LocalPos = mat4::translate(vec3(4.f)) * LocalPos0;
370
371                LocalPos = cam_screen * LocalPos;
372                vpos = (LocalPos.v3 / LocalPos.v3.w).xyz;
373                screen_min_max[0] = min(vpos.xy, screen_min_max[0]);
374                screen_min_max[1] = max(vpos.xy, screen_min_max[1]);
375            }
376        }
377
378        //Jump handling
379        //if (length(PhysObj->GetPhysic()->GetLinearVelocity()) < ZERO_SPEED)
380        if (lol::abs(PhysObj->GetPhysic()->GetLinearVelocity().y) < ZERO_SPEED)
381            Timer -= seconds;
382
383        if (Timer < .0f)
384        {
385            PhysObj->GetPhysic()->AddImpulse(JUMP_HEIGHT *
386                                             vec3(JUMP_STRAFE, 1.f, JUMP_STRAFE) *
387                                             vec3(rand(-1.f, 1.f), 1.0f, rand(-1.f, 1.f)) *
388                                             PhysObj->GetPhysic()->GetMass());
389            Timer = ZERO_TIME;
390        }
391    }
392
393    float fov_ratio = max(max(lol::abs(screen_min_max[0].x), lol::abs(screen_min_max[0].y)),
394                          max(lol::abs(screen_min_max[1].x), lol::abs(screen_min_max[1].y)));
395
396    vec3 new_target = cam_center / cam_factor;
397    float fov_dp = .0f;
398    float loc_dp = .0f;
399
400    //ideally fov is on the target
401    if (lol::abs(fov_ratio - 1.f) < .2f)
402        fov_dp = ((m_cam_target == -1)?(.7f):(.2f));
403    else
404        fov_dp = ((m_cam_target == -1)?(1.7f):(.9f));
405
406    //ideally loc is on the target
407    if (length(new_target - m_camera->GetTarget()) < 6.f)
408        loc_dp = ((m_cam_target == -1)?(.5f):(.03f));
409    else
410        loc_dp = ((m_cam_target == -1)?(.9f):(.5f));
411
412    m_fov_dp = damp(m_fov_dp, fov_dp, 0.08f, seconds);
413    m_loc_dp = damp(m_loc_dp, loc_dp, 0.08f, seconds);
414    m_camera->SetFov(damp(m_camera->GetFov(), m_camera->GetFov() * fov_ratio * 1.1f, m_fov_dp, seconds));
415    vec3 tmp = damp(m_camera->GetTarget(), new_target, m_loc_dp, seconds);
416    m_camera->SetView((mat4::rotate(10.f * seconds, vec3(.0f, 1.f, .0f)) * vec4(m_camera->GetPosition(), 1.0f)).xyz,
417                      tmp, vec3(0, 1, 0));
418#endif //USE_BODIES
419#endif //CAT_MODE
420
421#if USE_WALL
422    {
423        for (int i = 0; i < m_ground_list.Count(); i++)
424        {
425            PhysicsObject* PhysObj = m_ground_list[i];
426            mat4 GroundMat = PhysObj->GetTransform();
427
428            GroundBarycenter += GroundMat.v3.xyz;
429            factor += 1.f;
430        }
431
432        GroundBarycenter /= factor;
433
434        for (int i = 0; i < m_ground_list.Count(); i++)
435        {
436            PhysicsObject* PhysObj = m_ground_list[i];
437
438            mat4 GroundMat = PhysObj->GetTransform();
439            vec3 CenterToGround = GroundMat.v3.xyz - GroundBarycenter;
440            vec3 CenterToCam = m_camera->GetPosition() - GroundBarycenter;
441
442            if (dot(normalize(CenterToCam - CenterToGround),
443                    normalize(CenterToGround)) > 0.f)
444                PhysObj->SetRender(false);
445            else
446                PhysObj->SetRender(true);
447        }
448    }
449#endif //USE_WALL
450
451#if USE_ROTATION
452    {
453        for (int i = 0; i < m_ground_list.Count(); i++)
454        {
455            PhysicsObject* PhysObj = m_ground_list[i];
456
457            mat4 GroundMat = PhysObj->GetTransform();
458            mat4 CenterMx = mat4::translate(GroundBarycenter);
459            GroundMat = inverse(CenterMx) * GroundMat;
460            GroundMat = CenterMx *
461                        mat4(quat::fromeuler_xyz(vec3(.0f, 20.f, 20.0f) * seconds))
462                        * GroundMat;
463            PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
464        }
465    }
466#endif //USE_ROTATION
467
468#if USE_PLATFORM
469    {
470        for (int i = 0; i < m_platform_list.Count(); i++)
471        {
472            PhysicsObject* PhysObj = m_platform_list[i];
473
474            mat4 GroundMat = PhysObj->GetTransform();
475            if (i == 0)
476            {
477                GroundMat = GroundMat * mat4(quat::fromeuler_xyz(vec3(20.f, .0f, .0f) * seconds));
478                PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
479            }
480            else if (i == 1)
481            {
482                GroundMat =
483                    mat4::translate(vec3(-15.0f, 5.0f, lol::cos(m_loop_value) * 8.f)) *
484                    mat4(quat::fromeuler_xyz(vec3(.0f, lol::cos(m_loop_value) * 20.f, .0f)));
485                PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
486            }
487        }
488    }
489#endif //USE_PLATFORM
490
491#if USE_CHARACTER
492    {
493        for (int i = 0; i < m_character_list.Count(); i++)
494        {
495            PhysicsObject* PhysObj = m_character_list[i];
496            EasyCharacterController* Character = (EasyCharacterController*)PhysObj->GetCharacter();
497            mat4 CtlrMx = Character->GetTransform();
498
499            vec3 movement(0.f);
500            movement.z = (m_controller->GetKey(KEY_MOVE_RIGHT).IsDown() ? 1.f : 0.f)
501                       - (m_controller->GetKey(KEY_MOVE_LEFT).IsDown() ? 1.f : 0.f);
502            movement.x = (m_controller->GetKey(KEY_MOVE_FORWARD).IsDown() ? 1.f : 0.f)
503                       - (m_controller->GetKey(KEY_MOVE_BACK).IsDown() ? 1.f : 0.f);
504            movement.y = (m_controller->GetKey(KEY_MOVE_UP).IsDown() ? 1.f : 0.f)
505                       - (m_controller->GetKey(KEY_MOVE_DOWN).IsDown() ? 1.f : 0.f);
506            vec3 CharMove = movement * seconds * vec3(4.f, 10.f, 4.f);
507
508            if (m_controller->GetKey(KEY_MOVE_JUMP).IsReleased())
509                Character->Jump();
510            Character->SetMovementForFrame(CharMove);
511
512            RayCastResult HitResult;
513            if (m_simulation->RayHits(HitResult, ERT_Closest, Character->GetTransform().v3.xyz, (Character->GetTransform().v3.xyz + vec3(.0f, -1.f, .0f)), Character))
514                Character->AttachTo(HitResult.m_collider_list[0], true, true);
515            else
516                Character->AttachTo(NULL);
517        }
518    }
519#endif //USE_CHARACTER
520
521#if USE_CHARACTER
522    {
523        PhysObjBarycenter = vec3(.0f);
524        factor = .0f;
525
526        for (int i = 0; i < m_character_list.Count(); i++)
527        {
528            PhysicsObject* PhysObj = m_character_list[i];
529            mat4 GroundMat = PhysObj->GetTransform();
530
531            PhysObjBarycenter += GroundMat.v3.xyz;
532            factor += 1.f;
533        }
534
535        PhysObjBarycenter /= factor;
536
537#if 0
538        m_camera->SetTarget(m_camera->GetTarget() + (seconds / (seconds + 0.18f)) * (PhysObjBarycenter - m_camera->GetTarget()));
539        vec3 CamPosCenter = m_camera->GetTarget() + vec3(.0f, 5.0f, .0f);
540        m_camera->SetPosition(CamPosCenter + normalize(m_camera->GetPosition() - CamPosCenter) * 20.0f);
541#endif
542    }
543#else
544    {
545        PhysObjBarycenter = vec3(.0f);
546        for (int i = 0; i < m_physobj_list.Count(); i++)
547        {
548            PhysicsObject* PhysObj = m_physobj_list[i].m1;
549            mat4 GroundMat = PhysObj->GetTransform();
550
551            PhysObjBarycenter += GroundMat.v3.xyz;
552            factor += 1.f;
553        }
554
555        PhysObjBarycenter /= factor;
556
557#if 0
558        m_camera->SetTarget(PhysObjBarycenter);
559        m_camera->SetPosition(GroundBarycenter + normalize(GroundBarycenter - PhysObjBarycenter) * 60.0f);
560#endif
561    }
562#endif //USE_CHARACTER
563}
564
565void BtPhysTest::TickDraw(float seconds)
566{
567    WorldEntity::TickDraw(seconds);
568
569    if (m_init_status != 2)
570        return;
571
572    if (!m_ready)
573    {
574#if CAT_MODE
575        /* cat datas setup */
576        m_cat_shader = Shader::Create(LOLFX_RESOURCE_NAME(front_camera_sprite));
577#if USE_BODIES
578        for (int i = 0; i < m_physobj_list.Count(); i++)
579        {
580            PhysicsObject* PhysObj = m_physobj_list[i].m1;
581            m_cat_sdata = new CatShaderData(((1 << VertexUsage::Position) |
582                                                (1 << VertexUsage::Color) |
583                                                (1 << VertexUsage::TexCoord) |
584                                                (1 << VertexUsage::TexCoordExt)),
585                                                m_cat_shader);
586            m_cat_sdata->m_shader_texture = m_cat_texture->GetTexture();
587            m_cat_sdata->m_sprite_flip = ((rand(2) == 1)?(1.f):(0.f)) / (float)(NB_SPRITE * PARTICLE_SIZE);
588            PhysObj->SetCustomShaderData(m_cat_sdata);
589            m_cat_sdata = NULL;
590        }
591#endif //USE_BODIES
592#endif //CAT_MODE
593
594        /* FIXME: this object never cleans up */
595        m_ready = true;
596    }
597    else
598    {
599#if CAT_MODE
600        for (int i = 0; i < m_physobj_list.Count(); i++)
601        {
602            PhysicsObject* PhysObj = m_physobj_list[i].m1;
603            CatShaderData* ShaderData = (CatShaderData*)PhysObj->GetCustomShaderData();
604
605            ShaderData->m_sprite_orientation = damp(ShaderData->m_sprite_orientation,
606                                                    F_PI_4 * ((ShaderData->m_sprite_flip * 2.f * (float)(NB_SPRITE * PARTICLE_SIZE)) - 1.f) *
607                                                    clamp(PhysObj->GetPhysic()->GetLinearVelocity().y / 20.0f, -1.f, 1.f),
608                                                    0.1f, seconds);
609        }
610#endif //CAT_MODE
611    }
612
613    //Video::SetClearColor(vec4(0.0f, 0.0f, 0.12f, 1.0f));
614
615}
616
617BtPhysTest::~BtPhysTest()
618{
619    g_scene->PopCamera(m_camera);
620    Ticker::Unref(m_light1);
621    Ticker::Unref(m_light2);
622
623#if CAT_MODE
624    /* cat datas setup */
625    Shader::Destroy(m_cat_shader);
626    Tiler::Deregister(m_cat_texture);
627#endif //CAT_MODE
628
629    while (m_constraint_list.Count())
630    {
631        EasyConstraint* CurPop = m_constraint_list.Last();
632        m_constraint_list.Pop();
633        CurPop->RemoveFromSimulation(m_simulation);
634        delete CurPop;
635    }
636    while (m_ground_list.Count())
637    {
638        PhysicsObject* CurPop = m_ground_list.Last();
639        m_ground_list.Pop();
640        CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
641        Ticker::Unref(CurPop);
642    }
643    while (m_stairs_list.Count())
644    {
645        PhysicsObject* CurPop = m_stairs_list.Last();
646        m_stairs_list.Pop();
647        CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
648        Ticker::Unref(CurPop);
649    }
650    while (m_character_list.Count())
651    {
652        PhysicsObject* CurPop = m_character_list.Last();
653        m_character_list.Pop();
654        CurPop->GetCharacter()->RemoveFromSimulation(m_simulation);
655        Ticker::Unref(CurPop);
656    }
657    while (m_platform_list.Count())
658    {
659        PhysicsObject* CurPop = m_platform_list.Last();
660        m_platform_list.Pop();
661        CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
662        Ticker::Unref(CurPop);
663    }
664    while (m_physobj_list.Count())
665    {
666        PhysicsObject* CurPop = m_physobj_list.Last().m1;
667        m_physobj_list.Pop();
668        CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
669        Ticker::Unref(CurPop);
670    }
671    Ticker::Unref(m_simulation);
672
673}
674
675//-----------------------------------------------------------------------------
676// CShaderData
677//-----------------------------------------------------------------------------
678CatShaderData::CatShaderData(uint32_t vert_decl_flags, Shader* shader)
679    : GpuShaderData(vert_decl_flags, shader, DebugRenderMode::Default)
680{
681    m_sprite_orientation = .0f;
682    m_sprite_flip = .0f;
683    SetupDefaultData();
684}
685
686//-----------------------------------------------------------------------------
687void CatShaderData::SetupDefaultData()
688{
689    AddUniform("in_model_view");
690    AddUniform("in_normal_mat");
691    AddUniform("in_proj");
692    AddUniform("in_texture");
693    AddUniform("in_sprite_orientation");
694    AddUniform("in_sprite_flip");
695}
696
697//-----------------------------------------------------------------------------
698void CatShaderData::SetupShaderDatas(mat4 const &model)
699{
700    mat4 proj = g_scene->GetCamera()->GetProjection();
701    mat4 view = g_scene->GetCamera()->GetView();
702
703    mat4 modelview = view * model;
704    mat3 normalmat = transpose(inverse(mat3(view)));
705
706    m_shader->SetUniform(*GetUniform("in_model_view"), modelview);
707    m_shader->SetUniform(*GetUniform("in_normal_mat"), normalmat);
708    m_shader->SetUniform(*GetUniform("in_proj"), proj);
709    m_shader->SetUniform(*GetUniform("in_texture"), m_shader_texture, 0);
710    m_shader->SetUniform(*GetUniform("in_sprite_orientation"), m_sprite_orientation);
711    m_shader->SetUniform(*GetUniform("in_sprite_flip"), m_sprite_flip);
712}
713
714int main(int argc, char **argv)
715{
716    System::Init(argc, argv);
717
718    Application app("BtPhysTest", ivec2(1280, 960), 60.0f);
719
720    new BtPhysTest(argc > 1);
721    app.ShowPointer(false);
722
723    app.Run();
724
725    return EXIT_SUCCESS;
726}
727
Note: See TracBrowser for help on using the repository browser.