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