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
|
// 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.7.0 (2011/08/29)
#include "IntersectInfiniteCylinders.h"
WM5_WINDOW_APPLICATION(IntersectInfiniteCylinders);
//----------------------------------------------------------------------------
IntersectInfiniteCylinders::IntersectInfiniteCylinders ()
:
WindowApplication3("SampleMathematics/IntersectInfiniteCylinders", 0, 0,
640, 480, Float4(0.75f, 0.75f, 0.75f, 1.0f)),
mTextColor(1.0f, 1.0f, 1.0f, 1.0f),
mRadius0(3.0f),
mRadius1(2.0f),
mHeight(100.0f),
mC0(4.0f),
mW1(3.0f/5.0f),
mW2(4.0f/5.0f)
{
mAngle = Mathf::ATan2(mW1, mW2);
}
//----------------------------------------------------------------------------
bool IntersectInfiniteCylinders::OnInitialize ()
{
if (!WindowApplication3::OnInitialize())
{
return false;
}
mCamera->SetFrustum(60.0f, GetAspectRatio(), 1.0f, 1000.0f);
APoint camPosition(0.0f, -16.0f, 0.0f);
AVector camDVector(0.0f, 1.0f, 0.0f);
AVector camUVector(0.0f, 0.0f, 1.0f);
AVector camRVector = camDVector.Cross(camUVector);
mCamera->SetFrame(camPosition, camDVector, camUVector, camRVector);
CreateScene();
mScene->Update();
InitializeCameraMotion(0.01f, 0.001f);
InitializeObjectMotion(mScene);
return true;
}
//----------------------------------------------------------------------------
void IntersectInfiniteCylinders::OnTerminate ()
{
mScene = 0;
mWireState = 0;
mCylinder0 = 0;
mCylinder1 = 0;
WindowApplication3::OnTerminate();
}
//----------------------------------------------------------------------------
void IntersectInfiniteCylinders::OnIdle ()
{
MeasureTime();
MoveCamera();
if (MoveObject())
{
mScene->Update();
}
if (mRenderer->PreDraw())
{
mRenderer->ClearBuffers();
mRenderer->Draw(mVisible);
DrawFrameRate(8, GetHeight()-8, mTextColor);
mRenderer->PostDraw();
mRenderer->DisplayColorBuffer();
}
UpdateFrameCount();
}
//----------------------------------------------------------------------------
bool IntersectInfiniteCylinders::OnKeyDown (unsigned char key, int x, int y)
{
switch (key)
{
case 'w':
case 'W':
{
mWireState->Enabled = !mWireState->Enabled;
return true;
}
}
return WindowApplication3::OnKeyDown(key, x, y);
}
//----------------------------------------------------------------------------
void IntersectInfiniteCylinders::CreateScene ()
{
mScene = new0 Node();
mWireState = new0 WireState();
mRenderer->SetOverrideWireState(mWireState);
VertexFormat* vformat = VertexFormat::Create(2,
VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0);
int vstride = vformat->GetStride();
StandardMesh sm(vformat);
VertexBufferAccessor vba;
int i;
// Create the canonical cylinder.
mCylinder0 = sm.Cylinder(32, 128, mRadius0, mHeight, true);
mScene->AttachChild(mCylinder0);
mVisible.Insert(mCylinder0);
vba.ApplyTo(mCylinder0);
for (i = 0; i < vba.GetNumVertices(); ++i)
{
vba.Color<Float3>(0, i) = Float3(0.5f, 0.0f, 0.0f);
}
mCylinder0->SetEffectInstance(VertexColor3Effect::CreateUniqueInstance());
// Create the other cylinder.
mCylinder1 = sm.Cylinder(32, 128, mRadius1, mHeight, true);
mScene->AttachChild(mCylinder1);
mVisible.Insert(mCylinder1);
vba.ApplyTo(mCylinder1);
for (i = 0; i < vba.GetNumVertices(); ++i)
{
vba.Color<Float3>(0, i) = Float3(0.0f, 0.0f, 0.5f);
}
mCylinder1->SetEffectInstance(VertexColor3Effect::CreateUniqueInstance());
mCylinder1->LocalTransform.SetRotate(HMatrix(AVector::UNIT_X, -mAngle));
mCylinder1->LocalTransform.SetTranslate(APoint(mC0, 0.0f, 0.0f));
// Create the intersection curve.
const float minTheta = 2.0f*Mathf::PI/3.0f;
const float maxTheta = 4.0f*Mathf::PI/3.0f;
float theta, cs, sn, t, tmp, discr;
VertexBuffer* vbuffer = new0 VertexBuffer(1024, vstride);
mCurve0 = new0 Polysegment(vformat, vbuffer, true);
mScene->AttachChild(mCurve0);
vba.ApplyTo(mCurve0);
int numPoints = vba.GetNumVertices();
float multiplier = (maxTheta - minTheta)/(float)(numPoints - 1);
for (i = 0; i < numPoints; ++i)
{
theta = minTheta + multiplier*i;
cs = Mathf::Cos(theta);
sn = Mathf::Sin(theta);
tmp = mC0 + mRadius1*cs;
discr = Mathf::FAbs(mRadius0*mRadius0 - tmp*tmp);
t = (-mRadius1*mW2*sn - Mathf::Sqrt(discr))/mW1;
Float3& position = vba.Position<Float3>(i);
position[0] = mC0 + mRadius1*cs;
position[1] = +mRadius1*sn*mW2 + t*mW1;
position[2] = -mRadius1*sn*mW1 + t*mW2;
vba.Color<Float3>(0, i) = Float3(0.0f, 0.5f, 0.0f);
}
mCurve0->SetEffectInstance(VertexColor3Effect::CreateUniqueInstance());
mVisible.Insert(mCurve0);
vbuffer = new0 VertexBuffer(1024, vstride);
mCurve1 = new0 Polysegment(vformat, vbuffer, true);
mScene->AttachChild(mCurve1);
vba.ApplyTo(mCurve1);
numPoints = vba.GetNumVertices();
multiplier = (maxTheta - minTheta)/(float)(numPoints - 1);
for (i = 0; i < numPoints; ++i)
{
theta = minTheta + multiplier*i;
cs = Mathf::Cos(theta);
sn = Mathf::Sin(theta);
tmp = mC0 + mRadius1*cs;
discr = Mathf::FAbs(mRadius0*mRadius0 - tmp*tmp);
t = (-mRadius1*mW2*sn + Mathf::Sqrt(discr))/mW1;
Float3& position = vba.Position<Float3>(i);
position[0] = mC0 + mRadius1*cs;
position[1] = +mRadius1*sn*mW2 + t*mW1;
position[2] = -mRadius1*sn*mW1 + t*mW2;
vba.Color<Float3>(0, i) = Float3(0.0f, 0.5f, 0.0f);
}
mCurve1->SetEffectInstance(VertexColor3Effect::CreateUniqueInstance());
mVisible.Insert(mCurve1);
}
//----------------------------------------------------------------------------
|