2 CDTestFramework http://codercorner.com
3 Copyright (c) 2007-2008 Pierre Terdiman, pierre@codercorner.com
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:
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.
17 #include "RenderingHelpers.h"
18 #include "IceHelpers.h"
19 #include "SphereMeshQuery.h"
21 #include "Profiling.h"
23 #include "GLFontRenderer.h"
25 SphereMeshQuery::SphereMeshQuery() :
32 SphereMeshQuery::~SphereMeshQuery()
36 void SphereMeshQuery::Init()
38 mSphere.mCenter = Point(0.0f, 0.0f, 0.0f);
39 mSphere.mRadius = 1.0f;
42 void SphereMeshQuery::Release()
47 void SphereMeshQuery::Select()
51 mBar = TwNewBar("SphereMeshQuery");
52 TwAddVarRW(mBar, "Radius", TW_TYPE_FLOAT, &mSphere.mRadius, " min=0.01 max=200.0 step=0.05");
54 mSettings.AddToTweakBar(mBar);
55 // mProfiler.AddToTweakBar(mBar);
59 void SphereMeshQuery::Deselect()
68 void SphereMeshQuery::PerformTest()
75 const Model* TM = GetTerrainModel();
78 SphereCollider Collider;
79 mSettings.SetupCollider(Collider);
82 bool Status = Collider.Collide(mCache, mSphere, *TM, null, null);
88 if(Collider.GetContactStatus())
90 udword NbTris = Collider.GetNbTouchedPrimitives();
91 const udword* Indices = Collider.GetTouchedPrimitives();
93 RenderTerrainTriangles(NbTris, Indices);
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);
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);
112 void SphereMeshQuery::KeyboardCallback(unsigned char key, int x, int y)
116 void SphereMeshQuery::MouseCallback(int button, int state, int x, int y)
119 if(!button && !state)
121 Point Dir = ComputeWorldRay(x, y);
125 if(SegmentSphere(GetCameraPos(), Dir, 10000.0f, mSphere.mCenter, mSphere.mRadius, d, hit))
129 mLocalHit = hit - mSphere.mCenter;
134 void SphereMeshQuery::MotionCallback(int x, int y)
138 Point Dir = ComputeWorldRay(x, y);
139 mSphere.mCenter = GetCameraPos() + Dir*mDist - mLocalHit;