summaryrefslogblamecommitdiffstats
path: root/tools/statepropbuilder/apps/spbuilder/code/sp/engine/context.cpp
blob: f994e0d4c04be13492f9385df8d943447d885282 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792























































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                     
/*
 * sp/engine/context.cpp
 */


#include <SP/engine/workspace.hpp>
#include <SP/engine/context.hpp>
#include <SP/interface/context.hpp>


//#include <SP/engine/project.hpp>
//#include <choreo/load.hpp>

#include <p3d/context.hpp>
#include <p3d/utility.hpp>
#include <p3d/view.hpp>
#include <p3d/directionallight.hpp>
#include <p3d/pointcamera.hpp>
#include <p3d/geometry.hpp>
#include <p3d/anim/drawablepose.hpp>
#include <radload/radload.hpp>

#include <radtime.hpp>
#include <radplatform.hpp>

#include <shlwapi.h>

#include <typeinfo>


#ifdef RAD_DEBUG
static const char* pddiDLL = "pddidx8d.dll";
#endif

#ifdef RAD_TUNE
static const char* pddiDLL = "pddidx8t.dll";
#endif

#ifdef RAD_RELEASE
static const char* pddiDLL = "pddidx8r.dll";
#endif


// global SPContext reference
SPContext* g_SPContext = 0;

extern Workspace* g_Workspace;


//---------------------------------------------------------------------------
// class SPContext - implementation
//---------------------------------------------------------------------------

SPContext::SPContext(HWND hwnd):
    m_HWnd(hwnd),

    m_Context(0),
    m_View(0),
    m_Camera(0),
    m_Light(0),
    m_Shader(0),

    m_CurrentTime(-1.0f),
    m_FPSIndex(0),
    m_SimRate(1.0f),

    m_MouseCapture(0),
    m_MouseX(0),
    m_MouseY(0),

    m_IsCameraLocked(false)
{
    memset(m_FPS, 0, sizeof(m_FPS));
}

SPContext::~SPContext()
{
    tRefCounted::Release(m_Shader);
    tRefCounted::Release(m_Light);
    tRefCounted::Release(m_Camera);
    tRefCounted::Release(m_View);
}

bool SPContext::Open()
{
    // p3d context init
    RECT wndRect;
    GetWindowRect(m_HWnd, &wndRect);

    tContextInitData init;
    init.hwnd = m_HWnd;
    init.displayMode = PDDI_DISPLAY_WINDOW;
    init.bufferMask = PDDI_BUFFER_COLOUR | PDDI_BUFFER_DEPTH;
    int wndWidth = wndRect.right - wndRect.left;
    int wndHeight = wndRect.bottom - wndRect.top;
    init.xsize = wndWidth;
    init.ysize = wndHeight;
    init.bpp = 32;
    strcpy(init.PDDIlib, pddiDLL);

    //p3d::platform->SetActiveContext(0);
    m_Context = p3d::platform->CreateContext(&init);
    if (m_Context == 0)
        return false;

    p3d::platform->SetActiveContext(m_Context);

    // Call into PDDI to enable Z-buffering.
    p3d::pddi->EnableZBuffer(true);


    radLoadInitialize();

    // This call installs chunk handlers for all the primary chunk types that
    // Pure3D supports.  This includes textures, materials, geometries, and the
    // like.  
    p3d::InstallDefaultLoaders();

    // install default choreo loaders
    //choreo::InstallDefaultLoaders();

    // p3d view init
    m_View = new tView;
    m_View->AddRef();

    m_View->SetBackgroundColour(tColour(64, 64, 128));
    m_View->SetAmbientLight(tColour(40, 40, 40));

    // p3d light init
    m_Light = new tDirectionalLight;
    m_Light->AddRef();

    m_Light->SetDirection(0.0f, 0.0f, 1.0f);
    m_Light->SetColour(tColour(255, 255, 255));
    m_View->AddLight(m_Light);

    // p3d camera init
    m_Camera = new tPointCamera;
    m_Camera->AddRef();
    m_Camera->SetNearPlane(0.01f);
    m_Camera->SetFarPlane(5000.0f);
    m_Camera->SetFOV(rmt::DegToRadian(90.0f), (float)wndWidth / (float)wndHeight);
    m_View->SetCamera(m_Camera);

    m_CameraTarget.Clear();
    m_CameraRotateX = rmt::DegToRadian(-45.0f);
    m_CameraRotateY = 0.0f;
    m_CameraDistance = 5.0f;

    // pddi shader
    m_Shader = p3d::device->NewShader("simple");
    m_Shader->AddRef();
    m_Shader->SetInt(PDDI_SP_ISLIT, 0);

    // workspace create
    if (g_Workspace == 0)
    {
        g_Workspace = new Workspace;
        g_Workspace->AddRef();
    }

    return true;
}

bool SPContext::Close()
{
    // p3d release stuff
    tRefCounted::Release(m_Shader);
    tRefCounted::Release(m_Light);
    tRefCounted::Release(m_Camera);
    tRefCounted::Release(m_View);

    // p3d context destroy
    if (m_Context != 0)
    {
        p3d::platform->DestroyContext(m_Context);
        m_Context = 0;
    }

    radLoadTerminate();

    return true;
}

bool SPContext::ViewResize(int w, int h)
{
    if (w <= 0)
        w = 1;
    if (h <= 0)
        h = 1;

    RECT wndRect;
    GetWindowRect(m_HWnd, &wndRect);
    int wndWidth = wndRect.right - wndRect.left;
    if (wndWidth <= 0)
        wndWidth = 1;
    int wndHeight = wndRect.bottom - wndRect.top;
    if (wndHeight <= 0)
        wndHeight = 1;

    if (w > wndWidth)
        w = wndWidth;
    if (h > wndHeight)
        h = wndHeight;

    m_View->SetWindow(0.0f, 0.0f, (float)w / (float)wndWidth, (float)h / (float)wndHeight);
    m_Camera->SetFOV(rmt::DegToRadian(90.0f), (float)w / (float)h);

    return true;
}

bool SPContext::Advance()
{
    // do time
    float deltaTime;
    {
        float newTime = (float)radTimeGetMilliseconds() / 1000.0f;

        if (m_CurrentTime < 0.0f)
        {
            deltaTime = 0.0f;
            m_CurrentTime = newTime;
        }
        else
        {
            deltaTime = newTime - m_CurrentTime;

            if (deltaTime < 0.0f)
            {
                deltaTime = 0.0f;
            }
            else
            {
                m_CurrentTime = newTime;
            }

            // for debugging
            if (deltaTime > 1.0f)
            {
                deltaTime = 1.0f / 60.0f;
            }
            else
            {
                // do fps
                m_FPS[m_FPSIndex] = deltaTime;
                m_FPSIndex = (m_FPSIndex + 1) % MAX_FPS_COUNT;
            }
        }
    }

    float simDeltaTime = m_SimRate * deltaTime;
	if ( g_Workspace )
	{
		g_Workspace->Advance(simDeltaTime * 1000.0f);
	}

    // update camera & light
    {
        /*if (m_IsCameraLocked)
        {
            rmt::Vector desiredTarget = m_CVPuppet->GetCameraTarget();

            rmt::Vector delta = desiredTarget;
            delta.Sub(m_CameraTarget);
            float mag = delta.NormalizeSafe();

            static const float cameraSpeed = 1.5f;  // m/s
            float maxMag = cameraSpeed * deltaTime * mag;
            if (mag > maxMag)
            {
                mag = maxMag;
            }

            delta.Scale(mag);
            m_CameraTarget.Add(delta);
            m_CameraTarget.x = desiredTarget.x;
            m_CameraTarget.z = desiredTarget.z;
        }*/

        m_Camera->SetTarget(m_CameraTarget);
        rmt::Matrix mY;
        mY.FillRotateY(m_CameraRotateY);
        rmt::Matrix mX;
        mX.FillRotateX(m_CameraRotateX);
        rmt::Vector cameraPos(0.0f, 0.0f, m_CameraDistance);
        cameraPos.Rotate(mX);
        cameraPos.Rotate(mY);
        cameraPos.Add(m_CameraTarget);
        m_Camera->SetPosition(cameraPos);

        rmt::Vector lightDir;
        lightDir = m_CameraTarget;
        lightDir.Sub(cameraPos);
        m_Light->SetDirection(lightDir);
    }

    //return rc;
	return true;
}

bool SPContext::Display()
{
    m_Context->BeginFrame();
    m_View->BeginRender();

    // render game context
	if ( g_Workspace )
	{
		g_Workspace->Display(m_Context);
	}

    // draw origin
    {
        rmt::Matrix m;
        m.Identity();
        DrawAxes(m, 0.5f);
    }

    m_View->EndRender();
    m_Context->EndFrame();

    //return rc;
	return true;
}

void SPContext::DrawAxes(const rmt::Matrix& m, float radius)
{
   rmt::Vector oldVtx[4] =
   {
      rmt::Vector(0,0,0),
      rmt::Vector(radius,0,0),
      rmt::Vector(0,radius,0),
      rmt::Vector(0,0,radius)
   };
   rmt::Vector newVtx[4];
   m.Transform(4, oldVtx, newVtx);

   pddiPrimStream* stream = p3d::pddi->BeginPrims(m_Shader, PDDI_PRIM_LINES, PDDI_V_C , 6);

   stream->Vertex((pddiVector*)newVtx, pddiColour(255,0,0));
   stream->Vertex((pddiVector*)(newVtx + 1), pddiColour(255,0,0));

   stream->Vertex((pddiVector*)newVtx, pddiColour(0,255,0));
   stream->Vertex((pddiVector*)(newVtx + 2), pddiColour(0,255,0));

   stream->Vertex((pddiVector*)newVtx, pddiColour(0,0,255));
   stream->Vertex((pddiVector*)(newVtx + 3), pddiColour(0,0,255));

   p3d::pddi->EndPrims(stream);
}

void SPContext::DrawLine(const rmt::Vector& a, const rmt::Vector& b, tColour c)
{
	pddiPrimStream* stream = p3d::pddi->BeginPrims(m_Shader, PDDI_PRIM_LINES, PDDI_V_C , 2 );
	stream->Vertex((pddiVector*)(&a), c);
	stream->Vertex((pddiVector*)(&b), c);
	p3d::pddi->EndPrims(stream);
}

void SPContext::RenderPose(tPose* pose)
{
    pose->Evaluate();

	int i;
	int startJoint = 0;
	for (i = startJoint; i < pose->GetNumJoint(); ++i)
	{
        tPose::Joint* joint = pose->GetJoint(i);
        tPose::Joint* parent = joint->parent;

        if (parent != 0)
        {
            DrawLine(parent->worldMatrix.Row(3), joint->worldMatrix.Row(3), tColour(255,255,255));
            DrawAxes(parent->worldMatrix, 0.02f);
        }
	}
}

void SPContext::MouseDown(int button, int shift, int x, int y)
{
    if (m_MouseCapture == 0)
    {
        SetCapture(m_HWnd);
        m_MouseCapture = 1;
    }

    m_MouseX = x;
    m_MouseY = y;
}

void SPContext::MouseMove(int button, int shift, int x, int y)
{
    if (m_MouseCapture == 0)
        return;
    if (GetCapture() != m_HWnd)
        return;

    int dx = x - m_MouseX;
    int dy = y - m_MouseY;
    m_MouseX = x;
    m_MouseY = y;

    if ((button & MK_LBUTTON) != 0)
    {
        if ((button & MK_MBUTTON) != 0)
        {
            CameraZoom(dx * 10);
        }
        else
        {
            CameraRotate(dx, dy);
        }
    }
    else if ((button & MK_MBUTTON) != 0)
    {
        CameraPan(dx, dy);
    }
}

void SPContext::MouseUp(int button, int shift, int x, int y)
{
    MouseMove(button, shift, x, y);

    if (m_MouseCapture == 0)
        return;

    ReleaseCapture();
    m_MouseCapture = 0;
}

void SPContext::MouseWheel(int scroll)
{
    CameraZoom(scroll);
}

void SPContext::CameraPan(int dx, int dy)
{
    RECT wndRect;
    GetWindowRect(m_HWnd, &wndRect);

    rmt::Vector v((float)-dx / (float)(wndRect.right - wndRect.left),
                  (float)dy / (float)(wndRect.bottom - wndRect.top), 0.0f);
    rmt::Vector a, b;
    m_Camera->ViewToWorld(v, &a, &b);
    b.Sub(a);
    b.NormalizeSafe();
    b.Scale(m_CameraDistance - m_Camera->GetNearPlane());
    b.Add(a);
    m_CameraTarget = b;
}

void SPContext::CameraZoom(int dz)
{
	m_CameraDistance += ((float)-dz * 0.001f * m_CameraDistance);

	if (m_CameraDistance < 0.1f)
	{
		m_CameraDistance = 0.1f;
	}
}

void SPContext::CameraRotate(int dx, int dy)
{
    m_CameraRotateX += ((float)-dy * 0.01f);

    if (m_CameraRotateX > rmt::DegToRadian(89.9f))
    {
        m_CameraRotateX = rmt::DegToRadian(89.9f);
    }

    if (m_CameraRotateX < rmt::DegToRadian(-89.9f))
    {
        m_CameraRotateX = rmt::DegToRadian(-89.9f);
    }

    m_CameraRotateY += ((float)dx * 0.01f);
}

float SPContext::SimulationGetFPS() const
{
    float fps = 0.0f;

    for (int i = 0; i < MAX_FPS_COUNT; ++i)
    {
        fps += m_FPS[i];
    }

    fps /= (float)MAX_FPS_COUNT;

    if (!rmt::Epsilon(fps, 0.0f))
    {
        fps = 1.0f / fps;
    }

    return fps;
}

void SPContext::SimulationSetRate(float simRate)
{
    m_SimRate = simRate;

    if (m_SimRate < 0.0f)
    {
        m_SimRate = 0.0f;
    }
}

bool SPContext::GetEntityName(tEntity* entity, char* name, int maxLen)
{
    if (maxLen > 0)
    {
        name[0] = 0;
    }

    if (entity == 0)
        return false;

    if (entity->GetName() != 0)
    {
        _snprintf(name, maxLen, "%s", entity->GetName());
    }
    else
    {
        _snprintf(name, maxLen, "0x%016I64x", entity->GetUID());
    }

    if (maxLen > 0)
    {
        name[maxLen - 1] = 0;
    }

    return true;
}

void SPContext::InventoryClear()
{
    if (m_Context != 0)
    {
        m_Context->GetInventory()->RemoveAllElements();
    }
}

int SPContext::InventoryGetEntityCount() const
{
    if (m_Context == 0)
        return 0;

    int entityCount = 0;
	/*
    for (int s = 0; s < m_Context->GetInventory()->GetSectionCount(); ++s)
    {
        entityCount += m_Context->GetInventory()->GetSectionByIndex(s)->GetElementCount();
    }
	*/

    return entityCount;
}

bool SPContext::InventoryGetEntityName(int index,
                                       char* name, int nameLen,
                                       char* type, int typeLen) const
{
    int entityCount = 0;

	/*
    for (int s = 0; s < m_Context->GetInventory()->GetSectionCount(); ++s)
    {
        tEntityTable::RawIterator itor(m_Context->GetInventory()->GetSectionByIndex(s));

        tEntity* obj = itor.First();
        while (obj)
        {
            if (entityCount == index)
            {
                GetEntityName(obj, name, nameLen);
                _snprintf(type, typeLen, "%s", typeid(*obj).name());
                type[typeLen - 1] = '\0';

                return true;
            }

            ++entityCount;
            obj = itor.Next();
        }
    }
	*/

    return false;
}

void SPContext::SetBackgroundColour(int r , int g , int b )
{
	m_View->SetBackgroundColour( tColour( r , g , b ) );
}


//---------------------------------------------------------------------------
// SPContext - DLL interface
//---------------------------------------------------------------------------

int SP_CALLCONV SPContextOpen(HWND hwnd)
{
    P3DASSERT(g_SPContext == 0);
    if (g_SPContext != 0)
        return -1;

    g_SPContext = new SPContext(hwnd);
    if (!g_SPContext->Open())
    {
        delete g_SPContext;
        g_SPContext = 0;
        return -1;
    }

    return 1;
}

int SP_CALLCONV SPContextClose()
{
    if (g_SPContext == 0)
        return 0;

    if (!g_SPContext->Close())
    {
        delete g_SPContext;
        g_SPContext = 0;
        return -1;
    }

    delete g_SPContext;
    g_SPContext = 0;
    return 1;
}

int SP_CALLCONV SPContextViewResize(int w, int h)
{
    if (g_SPContext == 0)
        return -1;

    g_SPContext->ViewResize(w, h);
    return 1;
}

int SP_CALLCONV SPContextIsPDDIStatsEnabled()
{
    if (p3d::pddi == 0)
        return -1;
    return p3d::pddi->IsStatsOverlayEnabled();
}

int SP_CALLCONV SPContextSetIsPDDIStatsEnabled(int statsEnabled)
{
    if (p3d::pddi == 0)
        return -1;
    p3d::pddi->EnableStatsOverlay(statsEnabled != 0);
    return 1;
}

int SP_CALLCONV SPContextAdvance()
{
    if (g_SPContext == 0)
        return -1;

    return g_SPContext->Advance();
}

int SP_CALLCONV SPContextDisplay()
{
    if (g_SPContext == 0)
        return -1;

    return g_SPContext->Display();
}

int SP_CALLCONV SPContextMouseDown(int button, int shift, int x, int y)
{
    if (g_SPContext == 0)
        return -1;

    g_SPContext->MouseDown(button, shift, x, y);
    return 0;
}

int SP_CALLCONV SPContextMouseMove(int button, int shift, int x, int y)
{
    if (g_SPContext == 0)
        return -1;

    g_SPContext->MouseMove(button, shift, x, y);
    return 0;
}

int SP_CALLCONV SPContextMouseUp(int button, int shift, int x, int y)
{
    if (g_SPContext == 0)
        return -1;

    g_SPContext->MouseUp(button, shift, x, y);
    return 0;
}

int SP_CALLCONV SPContextMouseWheel(int scroll)
{
    if (g_SPContext == 0)
        return -1;

    g_SPContext->MouseWheel(scroll);
    return 0;
}

float SP_CALLCONV SPSimulationGetFPS()
{
    if (g_SPContext == 0)
        return 0.0f;

    return g_SPContext->SimulationGetFPS();
}

float SP_CALLCONV SPSimulationGetRate()
{
    if (g_SPContext == 0)
        return 0.0f;

    return g_SPContext->SimulationGetRate();
}

int SP_CALLCONV SPSimulationSetRate(float rate)
{
    if (g_SPContext == 0)
        return -1;

    g_SPContext->SimulationSetRate(rate);
    return 0;
}

int SP_CALLCONV SPContextGetCameraLock()
{
    if (g_SPContext == 0)
        return -1;

    return g_SPContext->IsCameraLocked();
}

int SP_CALLCONV SPContextSetCameraLock(int cameraLock)
{
    if (g_SPContext == 0)
        return -1;

    g_SPContext->SetIsCameraLocked(cameraLock != 0);
    return 0;
}

int SP_CALLCONV SPInventoryClear()
{
    if (g_SPContext == 0)
        return -1;

    g_SPContext->InventoryClear();
    return 0;
}

int SP_CALLCONV SPInventoryGetEntityCount()
{
    if (g_SPContext == 0)
        return -1;

    return g_SPContext->InventoryGetEntityCount();
}

int SP_CALLCONV SPInventoryGetEntityName(int index, char* name, int nameLen, char* type, int typeLen)
{
    if (g_SPContext == 0)
        return -1;

    if (!g_SPContext->InventoryGetEntityName(index, name, nameLen, type, typeLen))
    {
        return -1;
    }

    return 0;
}

int SP_CALLCONV SPSetBackgroundColour( int r , int g , int b )
{
    if (g_SPContext == 0)
        return 0;

    g_SPContext->SetBackgroundColour( r , g , b );
	return 1;
}

// End of file.