1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.2 (2015/11/25)
#include "IntersectingBoxes.h"
WM5_WINDOW_APPLICATION(IntersectingBoxes);
// #define SINGLE_STEP
//----------------------------------------------------------------------------
IntersectingBoxes::IntersectingBoxes ()
:
WindowApplication3("SamplePhysics/IntersectingBoxes", 0, 0, 640, 480,
Float4(0.75f, 0.75f, 0.75f, 1.0f))
{
mManager = 0;
mDoSimulation = true;
mLastIdle = 0.0f;
mSize = 256.0f;
}
//----------------------------------------------------------------------------
bool IntersectingBoxes::OnInitialize ()
{
if (!WindowApplication3::OnInitialize())
{
return false;
}
// Set up the camera.
mCamera->SetFrustum(60.0f, GetAspectRatio(), 1.0f, 1000.0f);
APoint camPosition(0.0f, 0.0f, -mSize);
AVector camDVector(0.0f, 0.0f, 1.0f);
AVector camUVector(0.0f, 1.0f, 0.0f);
AVector camRVector = camDVector.Cross(camUVector);
mCamera->SetFrame(camPosition, camDVector, camUVector, camRVector);
CreateScene();
// Initial update of objects.
mScene->Update();
// Initial culling of scene.
mCuller.SetCamera(mCamera);
mCuller.ComputeVisibleSet(mScene);
InitializeCameraMotion(0.5f, 0.001f);
InitializeObjectMotion(mScene);
return true;
}
//----------------------------------------------------------------------------
void IntersectingBoxes::OnTerminate ()
{
delete0(mManager);
mScene = 0;
mWireState = 0;
mNoIntersectEffect = 0;
mIntersectEffect = 0;
WindowApplication3::OnTerminate();
}
//----------------------------------------------------------------------------
void IntersectingBoxes::OnIdle ()
{
MeasureTime();
MoveCamera();
if (MoveObject())
{
mScene->Update();
}
#ifndef SINGLE_STEP
PhysicsTick();
#endif
GraphicsTick();
UpdateFrameCount();
}
//----------------------------------------------------------------------------
bool IntersectingBoxes::OnKeyDown (unsigned char key, int x, int y)
{
if (WindowApplication3::OnKeyDown(key, x, y))
{
return true;
}
switch (key)
{
case 'w': // toggle wireframe
case 'W':
mWireState->Enabled = !mWireState->Enabled;
return true;
case 's': // toggle simulation
case 'S':
mDoSimulation = !mDoSimulation;
return true;
#ifdef SINGLE_STEP
case 'g':
case 'G':
ModifyBoxes();
return true;
#endif
}
return false;
}
//----------------------------------------------------------------------------
void IntersectingBoxes::CreateScene ()
{
// Create some axis-aligned boxes for intersection testing.
const int imax = 16;
int i;
for (i = 0; i < imax; ++i)
{
float xMin = 0.5f*mSize*Mathf::SymmetricRandom();
float xMax = xMin + Mathf::IntervalRandom(8.0f, 32.0f);
float yMin = 0.5f*mSize*Mathf::SymmetricRandom();
float yMax = yMin + Mathf::IntervalRandom(8.0f, 32.0f);
float zMin = 0.5f*mSize*Mathf::SymmetricRandom();
float zMax = zMin + Mathf::IntervalRandom(8.0f, 32.0f);
mBoxes.push_back(
AxisAlignedBox3f(xMin, xMax, yMin, yMax, zMin, zMax));
}
mManager = new0 BoxManagerf(mBoxes);
// Scene graph for the visual representation of the boxes.
mScene = new0 Node();
mWireState = new0 WireState();
mRenderer->SetOverrideWireState(mWireState);
// Effects for boxes, blue for nonintersecting and red for intersecting.
Float4 black(0.0f, 0.0f, 0.0f, 1.0f);
Float4 white(1.0f, 1.0f, 1.0f, 1.0f);
Material* blueMaterial = new0 Material();
blueMaterial->Emissive = black;
blueMaterial->Ambient = Float4(0.25f, 0.25f, 0.25f, 1.0f);
blueMaterial->Diffuse = Float4(0.0f, 0.0f, 1.0f, 1.0f);
blueMaterial->Specular = black;
Material* redMaterial = new0 Material();
redMaterial->Emissive = black;
redMaterial->Ambient = Float4(0.25f, 0.25f, 0.25f, 1.0f);
redMaterial->Diffuse = Float4(1.0f, 0.0f, 0.0f, 1.0f);
redMaterial->Specular = black;
// A light for the effects.
Light* light = new0 Light(Light::LT_DIRECTIONAL);
light->Ambient = white;
light->Diffuse = white;
light->Specular = black;
light->SetDirection(AVector::UNIT_Z);
LightDirPerVerEffect* effect = new0 LightDirPerVerEffect();
mNoIntersectEffect = effect->CreateInstance(light, blueMaterial);
mIntersectEffect = effect->CreateInstance(light, redMaterial);
// Create visual representations of the boxes. The normals are duplicated
// to texture coordinates to avoid the AMD lighting problems due to use of
// pre-OpenGL2.x extensions.
VertexFormat* vformat = VertexFormat::Create(3,
VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
VertexFormat::AU_NORMAL, VertexFormat::AT_FLOAT3, 0,
VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT3, 1);
for (i = 0; i < imax; ++i)
{
APoint center(
0.5f*(mBoxes[i].Min[0] + mBoxes[i].Max[0]),
0.5f*(mBoxes[i].Min[1] + mBoxes[i].Max[1]),
0.5f*(mBoxes[i].Min[2] + mBoxes[i].Max[2]));
Transform transform;
transform.SetTranslate(center);
float xExtent = 0.5f*(mBoxes[i].Max[0] - mBoxes[i].Min[0]);
float yExtent = 0.5f*(mBoxes[i].Max[1] - mBoxes[i].Min[1]);
float zExtent = 0.5f*(mBoxes[i].Max[2] - mBoxes[i].Min[2]);
StandardMesh sm(vformat, true, false, &transform);
TriMesh* mesh = sm.Box(xExtent, yExtent, zExtent);
mesh->SetEffectInstance(mNoIntersectEffect);
mScene->AttachChild(mesh);
}
}
//----------------------------------------------------------------------------
void IntersectingBoxes::ModifyBoxes ()
{
const int numBoxes = (int)mBoxes.size();
int i;
for (i = 0; i < numBoxes; ++i)
{
AxisAlignedBox3f box = mBoxes[i];
float dx = Mathf::IntervalRandom(-4.0f, 4.0f);
if (-mSize <= box.Min[0] + dx && box.Max[0] + dx <= mSize)
{
box.Min[0] += dx;
box.Max[0] += dx;
}
float dy = Mathf::IntervalRandom(-4.0f, 4.0f);
if (-mSize <= box.Min[1] + dy && box.Max[1] + dy <= mSize)
{
box.Min[1] += dy;
box.Max[1] += dy;
}
float dz = Mathf::IntervalRandom(-4.0f, 4.0f);
if (-mSize <= box.Min[2] + dz && box.Max[2] + dz <= mSize)
{
box.Min[2] += dz;
box.Max[2] += dz;
}
mManager->SetBox(i, box);
ModifyMesh(i);
}
mManager->Update();
mScene->Update();
// Switch material to red for any box that overlaps another.
TriMesh* mesh;
for (i = 0; i < numBoxes; ++i)
{
// Reset all boxes to blue.
mesh = StaticCast<TriMesh>(mScene->GetChild(i));
mesh->SetEffectInstance(mNoIntersectEffect);
}
const std::set<EdgeKey>& overlap = mManager->GetOverlap();
std::set<EdgeKey>::const_iterator iter = overlap.begin();
std::set<EdgeKey>::const_iterator end = overlap.end();
for (/**/; iter != end; ++iter)
{
// Set intersecting boxes to red.
i = iter->V[0];
mesh = StaticCast<TriMesh>(mScene->GetChild(i));
mesh->SetEffectInstance(mIntersectEffect);
i = iter->V[1];
mesh = StaticCast<TriMesh>(mScene->GetChild(i));
mesh->SetEffectInstance(mIntersectEffect);
}
}
//----------------------------------------------------------------------------
void IntersectingBoxes::ModifyMesh (int i)
{
Vector3f center(
0.5f*(mBoxes[i].Min[0] + mBoxes[i].Max[0]),
0.5f*(mBoxes[i].Min[1] + mBoxes[i].Max[1]),
0.5f*(mBoxes[i].Min[2] + mBoxes[i].Max[2]));
float xExtent = 0.5f*(mBoxes[i].Max[0] - mBoxes[i].Min[0]);
float yExtent = 0.5f*(mBoxes[i].Max[1] - mBoxes[i].Min[1]);
float zExtent = 0.5f*(mBoxes[i].Max[2] - mBoxes[i].Min[2]);
Vector3f xTerm = xExtent*Vector3f::UNIT_X;
Vector3f yTerm = yExtent*Vector3f::UNIT_Y;
Vector3f zTerm = zExtent*Vector3f::UNIT_Z;
TriMesh* mesh = StaticCast<TriMesh>(mScene->GetChild(i));
VertexBufferAccessor vba(mesh);
vba.Position<Vector3f>(0) = center - xTerm - yTerm - zTerm;
vba.Position<Vector3f>(1) = center + xTerm - yTerm - zTerm;
vba.Position<Vector3f>(2) = center + xTerm + yTerm - zTerm;
vba.Position<Vector3f>(3) = center - xTerm + yTerm - zTerm;
vba.Position<Vector3f>(4) = center - xTerm - yTerm + zTerm;
vba.Position<Vector3f>(5) = center + xTerm - yTerm + zTerm;
vba.Position<Vector3f>(6) = center + xTerm + yTerm + zTerm;
vba.Position<Vector3f>(7) = center - xTerm + yTerm + zTerm;
mesh->UpdateModelSpace(Visual::GU_NORMALS);
for (int j = 0; j < vba.GetNumVertices(); ++j)
{
vba.TCoord<Float3>(1, j) = vba.Normal<Float3>(j);
}
mRenderer->Update(mesh->GetVertexBuffer());
}
//----------------------------------------------------------------------------
void IntersectingBoxes::PhysicsTick ()
{
if (mDoSimulation)
{
float currIdle = (float)GetTimeInSeconds();
float diff = currIdle - mLastIdle;
if (diff >= 1.0f/30.0f)
{
ModifyBoxes();
mLastIdle = currIdle;
}
}
}
//----------------------------------------------------------------------------
void IntersectingBoxes::GraphicsTick ()
{
mCuller.ComputeVisibleSet(mScene);
if (mRenderer->PreDraw())
{
mRenderer->ClearBuffers();
mRenderer->Draw(mCuller.GetVisibleSet());
mRenderer->PostDraw();
mRenderer->DisplayColorBuffer();
}
}
//----------------------------------------------------------------------------
|