-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectTool.cpp
311 lines (267 loc) · 10 KB
/
ObjectTool.cpp
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
#include "ObjectTool.h"
#include "Object3D.h"
#include "ModelImporter.h"
#include "StaticObject.h"
#include "Input.h"
#include "Camera.h"
#include "Runnable.h"
#include "Graphics.h"
#include "d3dUtil.h"
#include <limits>
#include "Vertex.h"
#include "Primitive.h"
#include "Effects.h"
#include "RenderStates.h"
#include "Util.h"
#include "World.h"
#include "Terrain.h"
using namespace GLib;
ObjectTool::ObjectTool(GLib::ModelImporter* pImporter)
{
// nullptr as default.
mMovingObject = nullptr;
// Create the X axis.
mAxisX = new StaticObject(pImporter, "models/arrow.obj");
mAxisX->SetPosition(XMFLOAT3(0, 30, 30));
mAxisX->SetMaterials(Material(Colors::Green));
mAxisX->SetRotation(XMFLOAT3(3.14f/2.0f, 3.14f/2.0f, 0));
mAxisX->SetScale(XMFLOAT3(1.50f, 1.50f, 1.50f));
// Create the Y axis.
mAxisY = new StaticObject(pImporter, "models/arrow.obj");
mAxisY->SetPosition(XMFLOAT3(0, 30, 30));
mAxisY->SetMaterials(Material(Colors::Red));
mAxisY->SetRotation(XMFLOAT3(0, 1, 0));
mAxisY->SetScale(XMFLOAT3(1.50f, 1.50f, 1.50f));
// Create the Z axis.
mAxisZ = new StaticObject(pImporter, "models/arrow.obj");
mAxisZ->SetPosition(XMFLOAT3(0, 30, 30));
mAxisZ->SetMaterials(Material(Colors::Blue));
mAxisZ->SetRotation(XMFLOAT3(0, 3.14f/2.0f, 3.14f/2.0f));
mAxisZ->SetScale(XMFLOAT3(1.50f, 1.50f, 1.50f));
mMovingAxis = NONE;
}
//! Cleanup.
ObjectTool::~ObjectTool()
{
delete mAxisX;
delete mAxisY;
delete mAxisZ;
}
void ObjectTool::InitStartingPosition(GLib::Input* pInput, XMFLOAT3& dir, XMFLOAT3& cameraPos, float& dist)
{
dist = numeric_limits<float>::infinity();
cameraPos = GetCamera()->GetPosition();
dir = pInput->GetWorldPickingRay().direction;
// Store as the last plane pos.
mLastPlanePos = cameraPos + dir * dist;
mLastPlanePos = XMFLOAT3(numeric_limits<float>::infinity(), numeric_limits<float>::infinity(), numeric_limits<float>::infinity());
}
//! Poll for input and perform actions.
void ObjectTool::Update(GLib::Input* pInput, float dt)
{
float dist;
XMFLOAT3 pos, dir;
// Scale the axis arrows.
ScaleAxisArrows();
// LBUTTON pressed and inside 3D screen.
if(pInput->KeyPressed(VK_LBUTTON) && IsIn3DScreen(pInput))
{
InitStartingPosition(pInput, dir, pos, dist);
// Find out which axis arrow was pressed.
if(mAxisX->RayIntersect(XMLoadFloat3(&pos), XMLoadFloat3(&dir), dist))
mMovingAxis = X_AXIS;
else if(mAxisY->RayIntersect(XMLoadFloat3(&pos), XMLoadFloat3(&dir), dist))
mMovingAxis = Y_AXIS;
else if(mAxisZ->RayIntersect(XMLoadFloat3(&pos), XMLoadFloat3(&dir), dist))
mMovingAxis = Z_AXIS;
}
else if(pInput->KeyPressed(VK_RBUTTON) && IsIn3DScreen(pInput)) {
InitStartingPosition(pInput, dir, pos, dist);
if(mMovingObject->RayIntersect(XMLoadFloat3(&pos), XMLoadFloat3(&dir), dist))
mMovingAxis = XZ_AXIS;
}
// Not moving any axis any more.
if(pInput->KeyReleased(VK_LBUTTON) || pInput->KeyReleased(VK_RBUTTON))
mMovingAxis = NONE;
// Update the position.
if(mMovingAxis != NONE)
{
XMFLOAT3 pos = GetCamera()->GetPosition();
XMFLOAT3 dir = pInput->GetWorldPickingRay().direction;
if(mMovingAxis == X_AXIS)
UpdatePosition(MoveAxisX(pos, dir));
else if(mMovingAxis == Y_AXIS)
UpdatePosition(MoveAxisY(pos, dir));
else if(mMovingAxis == Z_AXIS)
UpdatePosition(MoveAxisZ(pos, dir));
}
// Scaling with CTRL + mwheel.
if(pInput->KeyDown(VK_CONTROL) && mMovingObject->GetType() != LIGHT_OBJECT) {
mMovingObject->SetScale(mMovingObject->GetScale() + XMFLOAT3(pInput->MouseDz()/1300.0f, pInput->MouseDz()/1300.0f, pInput->MouseDz()/1300.0f));
onScaleChange(mMovingObject->GetScale());
}
// Move on terrain with RBUTTON.
if(pInput->KeyDown(VK_RBUTTON) && mMovingAxis == XZ_AXIS)
{
XMFLOAT3 pos = GetCamera()->GetPosition();
XMFLOAT3 dir = pInput->GetWorldPickingRay().direction;
UpdatePosition(MoveAxisZ(pos, dir));
UpdatePosition(MoveAxisX(pos, dir));
}
// Stick to the terain?
if(pInput->KeyPressed('C')) {
float height = mMovingObject->GetWorld()->GetTerrain()->GetHeight(mMovingObject->GetPosition().x, mMovingObject->GetPosition().z);
if(height != -numeric_limits<float>::infinity())
UpdatePosition(XMFLOAT3(mMovingObject->GetPosition().x, height, mMovingObject->GetPosition().z) - mMovingObject->GetPosition());
}
}
//! Draws the arrow axis.
void ObjectTool::Draw(GLib::Graphics* pGraphics)
{
// Disable the depth test.
ID3D11DepthStencilState* oldState = nullptr;
pGraphics->GetContext()->OMGetDepthStencilState(&oldState, 0);
pGraphics->GetContext()->OMSetDepthStencilState(RenderStates::EnableAllDSS, 0);
Effects::BasicFX->SetUseLighting(false);
// The axes will be rendered through the object.
mAxisX->Draw(pGraphics);
mAxisY->Draw(pGraphics);
mAxisZ->Draw(pGraphics);
Effects::BasicFX->SetUseLighting(true);
// Restore to standard depth stencil state (enable depth testing).
pGraphics->GetContext()->OMSetDepthStencilState(oldState, 0);
XMMATRIX view = XMLoadFloat4x4(&pGraphics->GetCamera()->GetViewMatrix());
XMMATRIX proj = XMLoadFloat4x4(&pGraphics->GetCamera()->GetProjectionMatrix());
}
//! Updates the moving objects position.
void ObjectTool::UpdatePosition(XMFLOAT3 delta)
{
if(mMovingObject != nullptr) {
mMovingObject->SetPosition(mMovingObject->GetPosition() + delta);
onPositionChange(mMovingObject->GetPosition());
}
mAxisX->SetPosition(mAxisX->GetPosition() + delta);
mAxisY->SetPosition(mAxisY->GetPosition() + delta);
mAxisZ->SetPosition(mAxisZ->GetPosition() + delta);
}
//! Move the object on the X axis.
XMFLOAT3 ObjectTool::MoveAxisX(XMFLOAT3 pos, XMFLOAT3 dir)
{
// Top right triangle.
XMVECTOR v0 = XMLoadFloat3(&XMFLOAT3(-10000, mAxisX->GetPosition().y, -10000));
XMVECTOR v1 = XMLoadFloat3(&XMFLOAT3(-10000, mAxisX->GetPosition().y, 10000));
XMVECTOR v2 = XMLoadFloat3(&XMFLOAT3(10000, mAxisX->GetPosition().y, 10000));
float dist = numeric_limits<float>::infinity();
if(!XNA::IntersectRayTriangle(XMLoadFloat3(&pos), XMLoadFloat3(&dir), v0, v1, v2, &dist))
{
// Bottom left triangle.
v0 = XMLoadFloat3(&XMFLOAT3(10000, mAxisX->GetPosition().y, 10000));
v1 = XMLoadFloat3(&XMFLOAT3(10000, mAxisX->GetPosition().y, -10000));
v2 = XMLoadFloat3(&XMFLOAT3(-10000, mAxisX->GetPosition().y, -10000));
dist = numeric_limits<float>::infinity();
XNA::IntersectRayTriangle(XMLoadFloat3(&pos), XMLoadFloat3(&dir), v0, v1, v2, &dist);
}
float dx;
if(mLastPlanePos.x != numeric_limits<float>::infinity()) {
XMFLOAT3 planePos = pos + dir * dist;
dx = planePos.x - mLastPlanePos.x;
mLastPlanePos.x = planePos.x;
}
else {
mLastPlanePos = pos + dir * dist;
dx = 0.0f;
}
return XMFLOAT3(dx, 0, 0);
}
//! Move the object on the Y axis.
XMFLOAT3 ObjectTool::MoveAxisY(XMFLOAT3 pos, XMFLOAT3 dir)
{
// Top right triangle.
XMFLOAT3 right = GetCamera()->GetRight();
XMFLOAT3 up = XMFLOAT3(0, 1, 0);
XMFLOAT3 objectPos = mAxisY->GetPosition();
float halfWidth = 60.0f;
//mLastPlanePos - 1000*XMLoadFloat3(&right);
XMVECTOR v0 = XMLoadFloat3(&(objectPos + right*halfWidth + up*halfWidth));
XMVECTOR v1 = XMLoadFloat3(&(objectPos - right*halfWidth + up*halfWidth));
XMVECTOR v2 = XMLoadFloat3(&(objectPos - right*halfWidth - up*halfWidth));
float dist = numeric_limits<float>::infinity();
if(!XNA::IntersectRayTriangle(XMLoadFloat3(&pos), XMLoadFloat3(&dir), v0, v1, v2, &dist))
{
// Bottom left triangle.
v0 = XMLoadFloat3(&(objectPos + right*halfWidth + up*halfWidth));
v1 = XMLoadFloat3(&(objectPos - right*halfWidth - up*halfWidth));
v2 = XMLoadFloat3(&(objectPos + right*halfWidth - up*halfWidth));
dist = numeric_limits<float>::infinity();
XNA::IntersectRayTriangle(XMLoadFloat3(&pos), XMLoadFloat3(&dir), v0, v1, v2, &dist);
}
float dy;
if(mLastPlanePos.x != numeric_limits<float>::infinity()) {
XMFLOAT3 planePos = pos + dir * dist;
dy = planePos.y - mLastPlanePos.y;
mLastPlanePos = planePos;
}
else {
mLastPlanePos = pos + dir * dist;
dy = 0.0f;
}
return XMFLOAT3(0, dy, 0);
}
//! Move the object on the Z axis.
XMFLOAT3 ObjectTool::MoveAxisZ(XMFLOAT3 pos, XMFLOAT3 dir)
{
// Top right triangle.
XMVECTOR v0 = XMLoadFloat3(&XMFLOAT3(-10000, mAxisX->GetPosition().y, -10000));
XMVECTOR v1 = XMLoadFloat3(&XMFLOAT3(-10000, mAxisX->GetPosition().y, 10000));
XMVECTOR v2 = XMLoadFloat3(&XMFLOAT3(10000, mAxisX->GetPosition().y, 10000));
float dist = numeric_limits<float>::infinity();
if(!XNA::IntersectRayTriangle(XMLoadFloat3(&pos), XMLoadFloat3(&dir), v0, v1, v2, &dist))
{
// Bottom left triangle.
v0 = XMLoadFloat3(&XMFLOAT3(10000, mAxisX->GetPosition().y, 10000));
v1 = XMLoadFloat3(&XMFLOAT3(10000, mAxisX->GetPosition().y, -10000));
v2 = XMLoadFloat3(&XMFLOAT3(-10000, mAxisX->GetPosition().y, -10000));
dist = numeric_limits<float>::infinity();
XNA::IntersectRayTriangle(XMLoadFloat3(&pos), XMLoadFloat3(&dir), v0, v1, v2, &dist);
}
float dz;
if(mLastPlanePos.z != numeric_limits<float>::infinity()) {
XMFLOAT3 planePos = pos + dir * dist;
dz = planePos.z - mLastPlanePos.z;
mLastPlanePos.z = planePos.z;
}
else {
mLastPlanePos = pos + dir * dist;
dz = 0.0f;
}
return XMFLOAT3(0, 0, dz);
}
//! Scales the axis arrows so they allways have the same size on the screen.
void ObjectTool::ScaleAxisArrows()
{
XMFLOAT3 diff = GLib::GetCamera()->GetPosition() - mMovingObject->GetPosition();
float dist = sqrt(diff.x * diff.x + diff.y * diff.y + diff.z * diff.z);
float scale = dist / 60.0f;
mAxisX->SetScale(XMFLOAT3(scale, scale, scale));
mAxisY->SetScale(XMFLOAT3(scale, scale, scale));
mAxisZ->SetScale(XMFLOAT3(scale, scale, scale));
SetPosition(mMovingObject->GetPosition());
}
//! Set the moving object.
void ObjectTool::SetObject(GLib::Object3D* pObject)
{
mMovingObject = pObject;
mAxisX->SetPosition(pObject->GetPosition());
SetPosition(pObject->GetPosition());
}
//! Sets the axis positions.
void ObjectTool::SetPosition(XMFLOAT3 position)
{
mAxisX->SetPosition(position + XMFLOAT3(mAxisX->GetBoundingBox().Extents.x*0.81, 0, 0));
mAxisY->SetPosition(position + XMFLOAT3(0, mAxisY->GetBoundingBox().Extents.y*0.81, 0));
mAxisZ->SetPosition(position + XMFLOAT3(0, 0, mAxisZ->GetBoundingBox().Extents.z*0.81));
}
bool ObjectTool::IsMovingObject()
{
return mMovingAxis == NONE ? false : true;
}