resetting manifest requested domain to floor
[platform/upstream/libbullet.git] / Extras / CDTestFramework / CapsuleMeshQuery.cpp
1 /*
2 CDTestFramework http://codercorner.com
3 Copyright (c) 2007-2008 Pierre Terdiman,  pierre@codercorner.com
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose, 
8 including commercial applications, and to alter it and redistribute it freely, 
9 subject to the following restrictions:
10
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16
17 #include "stdafx.h"
18 #include "IceHelpers.h"
19 #include "RenderingHelpers.h"
20 #include "Terrain.h"
21 #include "CapsuleMeshQuery.h"
22 #include "Camera.h"
23 #include "GLFontRenderer.h"
24
25 CapsuleMeshQuery::CapsuleMeshQuery() :
26         mBar                    (null),
27         mDist                   (0.0f),
28         mValidHit               (false)
29 {
30 }
31
32 CapsuleMeshQuery::~CapsuleMeshQuery()
33 {
34 }
35
36 void CapsuleMeshQuery::Init()
37 {
38         mP0 = Point(0.0f, -4.0f, 0.0f);
39         mP1 = Point(0.0f, 4.0f, 0.0f);
40
41         Matrix3x3 MX,MY;
42         RotX(MX, 45.0f);
43         RotY(MY, 45.0f);
44
45         mWorld = MX * MY;
46         mWorld.SetTrans(0.0f, 4.0f, 0.0f);
47 }
48
49 void CapsuleMeshQuery::Release()
50 {
51 }
52
53 void CapsuleMeshQuery::PerformTest()
54 {
55         RenderTerrain();
56
57         mCapsule.mP0 = mP0 * mWorld;
58         mCapsule.mP1 = mP1 * mWorld;
59         mCapsule.mRadius = 1.0f;
60         DrawCapsule(mWorld, mP0, mP1, 1.0f);
61
62         const Model* TM = GetTerrainModel();
63         if(TM)
64         {
65                 LSSCollider Collider;
66
67                 mProfiler.Start();
68                 bool Status = Collider.Collide(mCache, mCapsule, *TM, null, null);
69                 mProfiler.End();
70                 mProfiler.Accum();
71
72                 if(Status)
73                 {
74                         if(Collider.GetContactStatus())
75                         {
76                                 udword NbTris = Collider.GetNbTouchedPrimitives();
77                                 const udword* Indices = Collider.GetTouchedPrimitives();
78
79                                 RenderTerrainTriangles(NbTris, Indices);
80                         }
81                 }
82         }
83
84         // Raycast hit
85         if(mValidHit)
86         {
87                 Point wp = mLocalHit + (Point)mWorld.GetTrans();
88                 DrawLine(wp, wp + Point(1.0f, 0.0f, 0.0f), Point(1,0,0), 1.0f);
89                 DrawLine(wp, wp + Point(0.0f, 1.0f, 0.0f), Point(0,1,0), 1.0f);
90                 DrawLine(wp, wp + Point(0.0f, 0.0f, 1.0f), Point(0,0,1), 1.0f);
91         }
92
93         char Buffer[4096];
94         sprintf(Buffer, "Capsule-mesh query = %5.1f us (%d cycles)\n", mProfiler.mMsTime, mProfiler.mCycles);
95         GLFontRenderer::print(10.0f, 10.0f, 0.02f, Buffer);
96 }
97
98 void CapsuleMeshQuery::KeyboardCallback(unsigned char key, int x, int y)
99 {
100 }
101
102 void CapsuleMeshQuery::MouseCallback(int button, int state, int x, int y)
103 {
104         mValidHit = false;
105         if(!button && !state)
106         {
107                 Point Dir = ComputeWorldRay(x, y);
108
109                 float s[2];
110                 if(RayCapsuleOverlap(GetCameraPos(), Dir, mCapsule, s))
111                 {
112                         mValidHit = true;
113                         mDist = s[0];
114                         Point hit = GetCameraPos() + Dir * mDist;
115                         mLocalHit = hit - (Point)mWorld.GetTrans();
116                 }
117         }
118 }
119
120 void CapsuleMeshQuery::MotionCallback(int x, int y)
121 {
122         if(mValidHit)
123         {
124                 Point Dir = ComputeWorldRay(x, y);
125                 mWorld.SetTrans(GetCameraPos() + Dir*mDist - mLocalHit);
126         }
127 }