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 "OBBMeshQuery.h"
19 #include "IceHelpers.h"
20 #include "RenderingHelpers.h"
22 #include "GLFontRenderer.h"
24 OBBMeshQuery::OBBMeshQuery() :
34 OBBMeshQuery::~OBBMeshQuery()
38 void OBBMeshQuery::Init()
40 mBox.mCenter = Point(0.0f, 0.0f, 0.0f);
41 mBox.mExtents = Point(1.0f, 1.0f, 1.0f);
45 void OBBMeshQuery::Release()
50 void OBBMeshQuery::Select()
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' ");
62 mSettings.AddToTweakBar(mBar);
64 // mProfiler.AddToTweakBar(mBar);
68 void OBBMeshQuery::Deselect()
77 void OBBMeshQuery::PerformTest()
85 mBox.mRot = MX * MY * MZ;
89 const Model* TM = GetTerrainModel();
93 mSettings.SetupCollider(Collider);
96 bool Status = Collider.Collide(mCache, mBox, *TM, null, null);
102 if(Collider.GetContactStatus())
104 udword NbTris = Collider.GetNbTouchedPrimitives();
105 const udword* Indices = Collider.GetTouchedPrimitives();
107 RenderTerrainTriangles(NbTris, Indices);
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);
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);
126 void OBBMeshQuery::KeyboardCallback(unsigned char key, int x, int y)
130 void OBBMeshQuery::MouseCallback(int button, int state, int x, int y)
133 if(!button && !state)
135 Point Dir = ComputeWorldRay(x, y);
139 if(RayOBB(GetCameraPos(), Dir, mBox, d, hit))
143 mLocalHit = hit - mBox.mCenter;
148 void OBBMeshQuery::MotionCallback(int x, int y)
152 Point Dir = ComputeWorldRay(x, y);
153 mBox.mCenter = GetCameraPos() + Dir*mDist - mLocalHit;