Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / OBBMeshQuery.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 #include "stdafx.h"
17 #include "OBBMeshQuery.h"
18 #include "Terrain.h"
19 #include "IceHelpers.h"
20 #include "RenderingHelpers.h"
21 #include "Camera.h"
22 #include "GLFontRenderer.h"
23
24 OBBMeshQuery::OBBMeshQuery() :
25         mBar                    (null),
26         mAngleX                 (0.0f),
27         mAngleY                 (0.0f),
28         mAngleZ                 (0.0f),
29         mDist                   (0.0f),
30         mValidHit               (false)
31 {
32 }
33
34 OBBMeshQuery::~OBBMeshQuery()
35 {
36 }
37
38 void OBBMeshQuery::Init()
39 {
40         mBox.mCenter = Point(0.0f, 0.0f, 0.0f);
41         mBox.mExtents = Point(1.0f, 1.0f, 1.0f);
42         mBox.mRot.Identity();
43 }
44
45 void OBBMeshQuery::Release()
46 {
47         Deselect();
48 }
49
50 void OBBMeshQuery::Select()
51 {
52         // Create a tweak bar
53         {
54                 mBar = TwNewBar("OBBMeshQuery");
55                 TwAddVarRW(mBar, "Extents.x", TW_TYPE_FLOAT, &mBox.mExtents.x, " min=0.01 max=200.0 step=0.05 group='Extents' ");
56                 TwAddVarRW(mBar, "Extents.y", TW_TYPE_FLOAT, &mBox.mExtents.y, " min=0.01 max=200.0 step=0.05 group='Extents' ");
57                 TwAddVarRW(mBar, "Extents.z", TW_TYPE_FLOAT, &mBox.mExtents.z, " min=0.01 max=200.0 step=0.05 group='Extents' ");
58                 TwAddVarRW(mBar, "Rot.x", TW_TYPE_FLOAT, &mAngleX, " min=0.0 max=6.28 step=0.01 group='Rotation' ");
59                 TwAddVarRW(mBar, "Rot.y", TW_TYPE_FLOAT, &mAngleY, " min=0.0 max=6.28 step=0.01 group='Rotation' ");
60                 TwAddVarRW(mBar, "Rot.z", TW_TYPE_FLOAT, &mAngleZ, " min=0.0 max=6.28 step=0.01 group='Rotation' ");
61
62                 mSettings.AddToTweakBar(mBar);
63
64 //              mProfiler.AddToTweakBar(mBar);
65         }
66 }
67
68 void OBBMeshQuery::Deselect()
69 {
70         if(mBar)
71         {
72                 TwDeleteBar(mBar);
73                 mBar = null;
74         }
75 }
76
77 void OBBMeshQuery::PerformTest()
78 {
79         RenderTerrain();
80
81         Matrix3x3 MX,MY,MZ;
82         RotX(MX, mAngleX);
83         RotY(MY, mAngleY);
84         RotY(MZ, mAngleZ);
85         mBox.mRot = MX * MY * MZ;
86
87         DrawOBB(mBox);
88
89         const Model* TM = GetTerrainModel();
90         if(TM)
91         {
92                 OBBCollider Collider;
93                 mSettings.SetupCollider(Collider);
94
95                 mProfiler.Start();
96                 bool Status = Collider.Collide(mCache, mBox, *TM, null, null);
97                 mProfiler.End();
98                 mProfiler.Accum();
99
100                 if(Status)
101                 {
102                         if(Collider.GetContactStatus())
103                         {
104                                 udword NbTris = Collider.GetNbTouchedPrimitives();
105                                 const udword* Indices = Collider.GetTouchedPrimitives();
106
107                                 RenderTerrainTriangles(NbTris, Indices);
108                         }
109                 }
110         }
111
112         // Raycast hit
113         if(mValidHit)
114         {
115                 Point wp = mLocalHit + mBox.mCenter;
116                 DrawLine(wp, wp + Point(1.0f, 0.0f, 0.0f), Point(1,0,0), 1.0f);
117                 DrawLine(wp, wp + Point(0.0f, 1.0f, 0.0f), Point(0,1,0), 1.0f);
118                 DrawLine(wp, wp + Point(0.0f, 0.0f, 1.0f), Point(0,0,1), 1.0f);
119         }
120
121         char Buffer[4096];
122         sprintf(Buffer, "OBB-mesh query = %5.1f us (%d cycles)\n", mProfiler.mMsTime, mProfiler.mCycles);
123         GLFontRenderer::print(10.0f, 10.0f, 0.02f, Buffer);
124 }
125
126 void OBBMeshQuery::KeyboardCallback(unsigned char key, int x, int y)
127 {
128 }
129
130 void OBBMeshQuery::MouseCallback(int button, int state, int x, int y)
131 {
132         mValidHit = false;
133         if(!button && !state)
134         {
135                 Point Dir = ComputeWorldRay(x, y);
136
137                 float d;
138                 Point hit;
139                 if(RayOBB(GetCameraPos(), Dir, mBox, d, hit))
140                 {
141                         mValidHit = true;
142                         mDist = d;
143                         mLocalHit = hit - mBox.mCenter;
144                 }
145         }
146 }
147
148 void OBBMeshQuery::MotionCallback(int x, int y)
149 {
150         if(mValidHit)
151         {
152                 Point Dir = ComputeWorldRay(x, y);
153                 mBox.mCenter = GetCameraPos() + Dir*mDist - mLocalHit;
154         }
155 }
156
157