-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallClipperShape.cpp
512 lines (413 loc) · 13.7 KB
/
wallClipperShape.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//-----------------------------------------------------------------------------
// Torque 3D
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
#include "T3D/WallClipperShape.h"
#include "math/mathIO.h"
#include "math/mathUtils.h"
#include "sim/netConnection.h"
//#include "sceneGraph/sceneState.h"
#include "scene/sceneRenderState.h"
#include "console/consoleTypes.h"
#include "core/resourceManager.h"
#include "core/stream/bitStream.h"
#include "gfx/gfxTransformSaver.h"
#include "renderInstance/renderPassManager.h"
#include "environment/meshRoom.h"
#include "console/console.h"
#include "console/sim.h"
#include "environment/scatterSky.h"
#include "T3D/pointLight.h"
#include "T3D/tsStatic.h"
IMPLEMENT_CO_NETOBJECT_V1(WallClipperShape);
//-----------------------------------------------------------------------------
// Object setup and teardown
//-----------------------------------------------------------------------------
WallClipperShape::WallClipperShape()
{
// Flag this object so that it will always
// be sent across the network to clients
mNetFlags.set( Ghostable | ScopeAlways );
// Set it as a "static" object that casts shadows
mTypeMask |= StaticObjectType;
// Make sure to initialize our TSShapeInstance to NULL
mShapeInstance = NULL;
// No segment yet
mSegmentIdx = -1;
mRoomName = "";
// Type not defined yet
// mClipType = Undefined;
// No Light
mLight = NULL;
mLightId = 0;
// Wall
mPosPercent = 0.0f;
}
WallClipperShape::~WallClipperShape()
{
if(Sim::findObject(mLightId))
mLight->deleteObject();
}
/*
//-----------------------------------------------------------------------------
// Object Editing
//-----------------------------------------------------------------------------
void WallClipperShape::initPersistFields()
{
addGroup( "Rendering" );
addField( "shapeFile", TypeStringFilename, Offset( mShapeFile, WallClipperShape ) );
endGroup( "Rendering" );
// SceneObject already handles exposing the transform
Parent::initPersistFields();
}
void WallClipperShape::inspectPostApply()
{
Parent::inspectPostApply();
// Flag the network mask to send the updates
// to the client object
setMaskBits( UpdateMask );
}
*/
bool WallClipperShape::onAdd()
{
if ( !Parent::onAdd() )
return false;
/*
// Set up a 1x1x1 bounding box
mObjBox.set( Point3F( -0.5f, -0.5f, -0.5f ),
Point3F( 0.5f, 0.5f, 0.5f ) );
resetWorldBox();
_createShape();
// Add this object to the scene
addToScene();
*/
if(isServerObject())
{
//fetch the clipper shape, if we can
TSShape *shape = mShapeInstance->getShape();
for (U32 i = 0; i < shape->details.size(); i++)
{
const TSDetail * detail = &shape->details[i];
S32 ss = detail->subShapeNum;
S32 od = detail->objectDetailNum;
S32 start = shape->subShapeFirstObject[ss];
S32 end = shape->subShapeNumObjects[ss] + start;
for (U32 j = start; j < end; j++)
{
// Sometimes it is handy to know the name of the mesh it is accessing
const char *name = shape->names[mShapeInstance->mMeshObjects[j].object->nameIndex];
S32 num;
String shortName = String::GetTrailingNumber( name, num );
if(!dStrcmp(shortName, "Clipper") || !dStrcmp(shortName, "clipper"))
{
TSMesh * mesh = mShapeInstance->mMeshObjects[j].getMesh(od);
for (U32 k = 0; k < mesh->mNumVerts; k++)
{
Point2F pos = Point2F(mesh->smVertsList[k]->x, mesh->smVertsList[k]->z);
mClipVerts.push_back(pos);
}
//we got what we need, now bail.
return true;
}
}
}
//if that fails, grab the bounds
{
Point2F pos;
F32 halfWidth = mObjBox.len_x() / 2.0f;
F32 height = mObjBox.len_z();
// Vertex 1
pos.set(-halfWidth, 0.0f);
mClipVerts.push_back(pos);
// Vertex 2
pos.set(halfWidth, 0.0f);
mClipVerts.push_back(pos);
// Vertex 3
pos.set(halfWidth, height);
mClipVerts.push_back(pos);
// Vertex 4
pos.set(-halfWidth, height);
mClipVerts.push_back(pos);
}
}
// Create clip verts
// Right now it is hardcoded to support 4 verts to form a clipping
// rectangle, but that could change to support any shape later
/*if(isServerObject())
{
Point2F pos;
F32 halfWidth = mObjBox.len_x() / 2.0f;
F32 height = mObjBox.len_z();
// Vertex 1
pos.set(-halfWidth, 0.0f);
mClipVerts.push_back(pos);
// Vertex 2
pos.set(halfWidth, 0.0f);
mClipVerts.push_back(pos);
// Vertex 3
pos.set(halfWidth, height);
mClipVerts.push_back(pos);
// Vertex 4
pos.set(-halfWidth, height);
mClipVerts.push_back(pos);
}*/
return true;
}
/*
void WallClipperShape::onRemove()
{
// Remove this object from the scene
removeFromScene();
// Remove our TSShapeInstance
if ( mShapeInstance )
SAFE_DELETE( mShapeInstance );
Parent::onRemove();
}
void WallClipperShape::setTransform(const MatrixF & mat)
{
// Let SceneObject handle all of the matrix manipulation
Parent::setTransform( mat );
// Dirty our network mask so that the new transform gets
// transmitted to the client object
setMaskBits( TransformMask );
}
U32 WallClipperShape::packUpdate( NetConnection *conn, U32 mask, BitStream *stream )
{
// Allow the Parent to get a crack at writing its info
U32 retMask = Parent::packUpdate( conn, mask, stream );
// Write our transform information
if ( stream->writeFlag( mask & TransformMask ) )
{
mathWrite(*stream, getTransform());
mathWrite(*stream, getScale());
}
// Write out any of the updated editable properties
if ( stream->writeFlag( mask & UpdateMask ) )
{
stream->write( mShapeFile );
// Allow the server object a chance to handle a new shape
createShape();
}
return retMask;
}
void WallClipperShape::unpackUpdate(NetConnection *conn, BitStream *stream)
{
// Let the Parent read any info it sent
Parent::unpackUpdate(conn, stream);
if ( stream->readFlag() ) // TransformMask
{
mathRead(*stream, &mObjToWorld);
mathRead(*stream, &mObjScale);
setTransform( mObjToWorld );
}
if ( stream->readFlag() ) // UpdateMask
{
stream->read( &mShapeFile );
if ( isProperlyAdded() )
createShape();
}
}
//-----------------------------------------------------------------------------
// Object Rendering
//-----------------------------------------------------------------------------
void WallClipperShape::createShape()
{
if ( mShapeFile.isEmpty() )
return;
// If this is the same shape then no reason to update it
if ( mShapeInstance && mShapeFile.equal( mShape.getPath().getFullPath(), String::NoCase ) )
return;
// Clean up our previous shape
if ( mShapeInstance )
SAFE_DELETE( mShapeInstance );
mShape = NULL;
// Attempt to get the resource from the ResourceManager
mShape = ResourceManager::get().load( mShapeFile );
if ( !mShape )
{
Con::errorf( "WallClipperShape::createShape() - Unable to load shape: %s", mShapeFile.c_str() );
return;
}
// Attempt to preload the Materials for this shape
if ( isClientObject() &&
!mShape->preloadMaterialList( mShape.getPath() ) &&
NetConnection::filesWereDownloaded() )
{
mShape = NULL;
return;
}
// Update the bounding box
mObjBox = mShape->bounds;
resetWorldBox();
// Create the TSShapeInstance
mShapeInstance = new TSShapeInstance( mShape, isClientObject() );
}
bool WallClipperShape::prepRenderImage( SceneState *state, const U32 stateKey,
const U32 startZone, const bool modifyBaseZoneState)
{
// Make sure we have a TSShapeInstance
if ( !mShapeInstance )
return false;
// Make sure we haven't already been processed by this state
if ( isLastState( state, stateKey ) )
return false;
// Update our state
setLastState(state, stateKey);
// If we are actually rendered then create and submit our RenderInst
if ( state->isObjectRendered( this ) )
{
// Calculate the distance of this object from the camera
Point3F cameraOffset;
getRenderTransform().getColumn( 3, &cameraOffset );
cameraOffset -= state->getDiffuseCameraPosition();
F32 dist = cameraOffset.len();
if ( dist < 0.01f )
dist = 0.01f;
// Set up the LOD for the shape
F32 invScale = ( 1.0f / getMax( getMax( mObjScale.x, mObjScale.y ), mObjScale.z ) );
mShapeInstance->setDetailFromDistance( state, dist * invScale );
// Make sure we have a valid level of detail
if ( mShapeInstance->getCurrentDetail() < 0 )
return false;
// GFXTransformSaver is a handy helper class that restores
// the current GFX matrices to their original values when
// it goes out of scope at the end of the function
GFXTransformSaver saver;
// Set up our TS render state
TSRenderState rdata;
rdata.setSceneState( state );
rdata.setFadeOverride( 1.0f );
// Allow the light manager to set up any lights it needs
LightManager* lm = NULL;
if ( state->getSceneManager() )
{
lm = state->getSceneManager()->getLightManager();
if ( lm && !state->isShadowPass() )
lm->setupLights( this, getWorldSphere() );
}
// Set the world matrix to the objects render transform
MatrixF mat = getRenderTransform();
mat.scale( mObjScale );
GFX->setWorldMatrix( mat );
// Animate the the shape
mShapeInstance->animate();
// Allow the shape to submit the RenderInst(s) for itself
mShapeInstance->render( rdata );
// Give the light manager a chance to reset the lights
if ( lm )
lm->resetLights();
}
return false;
}
*/
//--------------------------------------------------------------------------
// Object Get/Set
//--------------------------------------------------------------------------
Point2F WallClipperShape::getClipVert(U32 idx)
{
if(idx < mClipVerts.size())
{
Point2F clipVert = mClipVerts[idx];
Point2F scale(getScale().x, getScale().z);
clipVert.convolve(scale);
return clipVert;
}
return Point2F(0.0f, 0.0f);
}
void WallClipperShape::updateClipper(MeshRoom *room)
{
const MeshRoomSegment &seg = room->getSegment(mSegmentIdx);
VectorF up(0.0f, 0.0f, 1.0f);
VectorF left = seg.getP01() - seg.getP00();
Point3F pt = seg.getP00() + mPosPercent * left;
pt = MathUtils::mClosestPointOnSegment(seg.slice0->p1, seg.slice1->p1, pt);
pt.z = getPosition().z;
left.normalizeSafe();
VectorF fwd = mCross(up, -left);
MatrixF mat(true);
mat.setColumn(0, -left);
mat.setColumn(1, fwd);
setTransform(mat);
setPosition(pt);
//setScale(getScale());
inspectPostApply();
if(mLightId)
{
// Variables
Point3F start, end;
VectorF toSun;
MatrixF mat;
F32 az, elev;
RayInfo ri;
// Get the sky object
ScatterSky *sky = dynamic_cast<ScatterSky*>(Sim::findObject("Sky"));
// Get clipper's light node
S32 lightNode = getShape()->findNode("light");
// Calculate a vector pointing toward the sun based on its azimuth/elevation
az = mDegToRad(sky->getAzimuth());
elev = mDegToRad(sky->getElevation());
toSun.set(mSin(az)*mCos(elev), mCos(az)*mCos(elev), mSin(elev));
toSun.normalizeSafe();
// Calculate light node's world position; this will be the start of our ray cast
getTransform().getColumn(3,&start); // Clipper world pos
getShape()->getNodeWorldTransform(lightNode, &mat); // Light node object pos (method name is confusing)
start += mat.getPosition(); // Light node world pos
// If sunlight is coming in through the clipper, we cast a ray from the light node
// in the direction of the sunlight. The light is positioned 0.5 meter above the
// hit-point in the direction of the hit-normal. The light is set to a bright white.
// If sunlight is not coming in through the clipper, the light is positioned 0.5
// meters from the light node in the direction of the wall normal and given a less
// bright blueish color.
if(mDot(-toSun, getTransform().getForwardVector()) > 0)
{
// Sunlight is coming in... cast a ray to find bounced light location.
// The ray cast will go from the light node to 20 units in the sunlight direction
end = start - 20.0f * toSun;
disableCollision();
gPolysoupOverride = true;
bool hit = gServerContainer.castRayRendered(start, end, 0xFFFFFFFF, &ri);
gPolysoupOverride = false;
enableCollision();
if(hit)
{
mLight->setPosition(ri.point + 0.5f * ri.normal);
mLight->setColor(1.0f, 1.0f, 1.0f);
mLight->setBrightness(0.2f);
}
}
else
{
// Sunlight is not coming in... simply position light to simulate ambient skylight
mLight->setPosition(start + 0.5f * getTransform().getForwardVector());
mLight->setColor(0.5f, 0.865f, 1.0f);
mLight->setBrightness(0.1f);
}
}
}
void WallClipperShape::initPersistFields()
{
addGroup( "Clipper" );
addField( "segmentId", TypeS32, Offset( mSegmentIdx, WallClipperShape ),
"The Id of the segment we're attached to." );
addField( "roomName", TypeString, Offset( mRoomName, WallClipperShape ),
"The name of the room we're attached to" );
endGroup( "MeshRoom" );
Parent::initPersistFields();
}
U32 WallClipperShape::packUpdate(NetConnection * con, U32 mask, BitStream * stream)
{
U32 retMask = Parent::packUpdate(con, mask, stream);
stream->writeInt(mSegmentIdx, 2);
stream->writeString(mRoomName);
// Were done ...
return retMask;
}
void WallClipperShape::unpackUpdate(NetConnection * con, BitStream * stream)
{
// Unpack Parent.
Parent::unpackUpdate(con, stream);
mSegmentIdx = stream->readInt(2);
char temp[12];
stream->readString(temp);
mRoomName = temp;
}