-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathguiMaterialPreview.cpp
More file actions
496 lines (420 loc) · 14.5 KB
/
guiMaterialPreview.cpp
File metadata and controls
496 lines (420 loc) · 14.5 KB
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
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
// Original header:
// GuiMaterialPreview Control for Material Editor Written by Travis Vroman of Gaslight Studios
// Updated 2-14-09
// Portions based off Constructor viewport code.
#include "console/engineAPI.h"
#include "T3D/guiMaterialPreview.h"
#include "renderInstance/renderPassManager.h"
#include "lighting/lightManager.h"
#include "lighting/lightInfo.h"
#include "core/resourceManager.h"
#include "scene/sceneManager.h"
#include "scene/sceneRenderState.h"
// GuiMaterialPreview
GuiMaterialPreview::GuiMaterialPreview()
: mMouseState(None),
mModel(NULL),
runThread(0),
lastRenderTime(0),
mLastMousePoint(0, 0),
mFakeSun(NULL),
mMaxOrbitDist(5.0f),
mMinOrbitDist(0.0f),
mOrbitDist(5.0f)
{
mActive = true;
mCameraMatrix.identity();
mCameraRot.set( mDegToRad(30.0f), 0, mDegToRad(-30.0f) );
mCameraPos.set(0.0f, 1.75f, 1.25f);
mCameraMatrix.setColumn(3, mCameraPos);
mOrbitPos.set(0.0f, 0.0f, 0.0f);
mTransStep = 0.01f;
mTranMult = 4.0;
mLightTransStep = 0.01f;
mLightTranMult = 4.0;
mOrbitRelPos = Point3F(0,0,0);
// By default don't do dynamic reflection
// updates for this viewport.
mReflectPriority = 0.0f;
}
GuiMaterialPreview::~GuiMaterialPreview()
{
SAFE_DELETE(mModel);
SAFE_DELETE(mFakeSun);
}
bool GuiMaterialPreview::onWake()
{
if( !Parent::onWake() )
return false;
if (!mFakeSun)
mFakeSun = LightManager::createLightInfo();
mFakeSun->setColor( LinearColorF( 1.0f, 1.0f, 1.0f ) );
mFakeSun->setAmbient( LinearColorF( 0.5f, 0.5f, 0.5f ) );
mFakeSun->setDirection( VectorF( 0.0f, 0.707f, -0.707f ) );
mFakeSun->setPosition( mFakeSun->getDirection() * -10000.0f );
mFakeSun->setRange( 2000000.0f );
return true;
}
// This function allows the viewport's ambient color to be changed. This is exposed to script below.
void GuiMaterialPreview::setAmbientLightColor( F32 r, F32 g, F32 b )
{
LinearColorF temp(r, g, b);
temp.clamp();
GuiMaterialPreview::mFakeSun->setAmbient( temp );
}
// This function allows the light's color to be changed. This is exposed to script below.
void GuiMaterialPreview::setLightColor( F32 r, F32 g, F32 b )
{
LinearColorF temp(r, g, b);
temp.clamp();
GuiMaterialPreview::mFakeSun->setColor( temp );
}
// This function is for moving the light in the scene. This needs to be adjusted to keep the light
// from getting all out of whack. For now, we'll just rely on the reset function if we need it
// fixed.
void GuiMaterialPreview::setLightTranslate(S32 modifier, F32 xstep, F32 ystep)
{
F32 _lighttransstep = (modifier & SI_SHIFT ? mLightTransStep : (mLightTransStep*mLightTranMult));
Point3F relativeLightDirection = GuiMaterialPreview::mFakeSun->getDirection();
// May be able to get rid of this. For now, it helps to fix the position of the light if i gets messed up.
if (modifier & SI_PRIMARY_CTRL)
{
relativeLightDirection.x += ( xstep * _lighttransstep * -1 );//need to invert this for some reason. Otherwise, it's backwards.
relativeLightDirection.y += ( ystep * _lighttransstep );
GuiMaterialPreview::mFakeSun->setDirection(relativeLightDirection);
}
// Default action taken by mouse wheel clicking.
else
{
relativeLightDirection.x += ( xstep * _lighttransstep * -1 ); //need to invert this for some reason. Otherwise, it's backwards.
relativeLightDirection.z += ( ystep * _lighttransstep );
GuiMaterialPreview::mFakeSun->setDirection(relativeLightDirection);
}
}
// This is for panning the viewport camera.
void GuiMaterialPreview::setTranslate(S32 modifier, F32 xstep, F32 ystep)
{
F32 transstep = (modifier & SI_SHIFT ? mTransStep : (mTransStep*mTranMult));
F32 nominalDistance = 20.0;
Point3F vec = mCameraPos;
vec -= mOrbitPos;
transstep *= vec.len() / nominalDistance;
if (modifier & SI_PRIMARY_CTRL)
{
mOrbitRelPos.x += ( xstep * transstep );
mOrbitRelPos.y += ( ystep * transstep );
}
else
{
mOrbitRelPos.x += ( xstep * transstep );
mOrbitRelPos.z += ( ystep * transstep );
}
}
// Left Click
void GuiMaterialPreview::onMouseDown(const GuiEvent &event)
{
mMouseState = MovingLight;
mLastMousePoint = event.mousePoint;
mouseLock();
}
// Left Click Release
void GuiMaterialPreview::onMouseUp(const GuiEvent &event)
{
mouseUnlock();
mMouseState = None;
}
// Left Click Drag
void GuiMaterialPreview::onMouseDragged(const GuiEvent &event)
{
if(mMouseState != MovingLight)
{
return;
}
// If we are MovingLight...
else
{
Point2I delta = event.mousePoint - mLastMousePoint;
mLastMousePoint = event.mousePoint;
setLightTranslate(event.modifier, delta.x, delta.y);
}
}
// Right Click
void GuiMaterialPreview::onRightMouseDown(const GuiEvent &event)
{
mMouseState = Rotating;
mLastMousePoint = event.mousePoint;
mouseLock();
}
// Right Click Release
void GuiMaterialPreview::onRightMouseUp(const GuiEvent &event)
{
mouseUnlock();
mMouseState = None;
}
// Right Click Drag
void GuiMaterialPreview::onRightMouseDragged(const GuiEvent &event)
{
if (mMouseState != Rotating)
{
return;
}
Point2I delta = event.mousePoint - mLastMousePoint;
mLastMousePoint = event.mousePoint;
mCameraRot.x += (delta.y * 0.01f);
mCameraRot.z += (delta.x * 0.01f);
}
// Mouse Wheel Scroll Up
bool GuiMaterialPreview::onMouseWheelUp(const GuiEvent &event)
{
mOrbitDist = (mOrbitDist - 0.10f);
return true;
}
// Mouse Wheel Scroll Down
bool GuiMaterialPreview::onMouseWheelDown(const GuiEvent &event)
{
mOrbitDist = (mOrbitDist + 0.10f);
return true;
}
// Mouse Wheel Click
void GuiMaterialPreview::onMiddleMouseDown(const GuiEvent &event)
{
if (!mActive || !mVisible || !mAwake)
{
return;
}
mMouseState = Panning;
mLastMousePoint = event.mousePoint;
mouseLock();
}
// Mouse Wheel Click Release
void GuiMaterialPreview::onMiddleMouseUp(const GuiEvent &event)
{
mouseUnlock();
mMouseState = None;
}
// Mouse Wheel Click Drag
void GuiMaterialPreview::onMiddleMouseDragged(const GuiEvent &event)
{
if (mMouseState != Panning)
{
return;
}
Point2I delta = event.mousePoint - mLastMousePoint;
mLastMousePoint = event.mousePoint;
setTranslate(event.modifier, delta.x, delta.y);
}
// This is used to set the model we want to view in the control object.
void GuiMaterialPreview::setObjectModel(const char* modelName)
{
deleteModel();
Resource<TSShape> model = ResourceManager::get().load(modelName);
if (! bool(model))
{
Con::warnf(avar("GuiMaterialPreview: Failed to load model %s. Please check your model name and load a valid model.", modelName));
return;
}
mModel = new TSShapeInstance(model, true);
AssertFatal(mModel, avar("GuiMaterialPreview: Failed to load model %s. Please check your model name and load a valid model.", modelName));
// Initialize camera values:
mOrbitPos = mModel->getShape()->center;
mMinOrbitDist = mModel->getShape()->mRadius;
lastRenderTime = Platform::getVirtualMilliseconds();
}
void GuiMaterialPreview::deleteModel()
{
SAFE_DELETE(mModel);
runThread = 0;
}
// This is called whenever there is a change in the camera.
bool GuiMaterialPreview::processCameraQuery(CameraQuery* query)
{
MatrixF xRot, zRot;
Point3F vecf, vecu, vecr;;
xRot.set(EulerF(mCameraRot.x, 0.0f, 0.0f));
zRot.set(EulerF(0.0f, 0.0f, mCameraRot.z));
if(mMouseState != Panning)
{
// Adjust the camera so that we are still facing the model:
Point3F vec;
mCameraMatrix.mul(zRot, xRot);
mCameraMatrix.getColumn(1, &vec);
vec *= mOrbitDist;
mCameraPos = mOrbitPos - vec;
query->farPlane = 2100.0f;
query->nearPlane = query->farPlane / 5000.0f;
query->fov = 45.0f;
mCameraMatrix.setColumn(3, mCameraPos);
query->cameraMatrix = mCameraMatrix;
}
else
{
mCameraMatrix.mul( zRot, xRot );
mCameraMatrix.getColumn( 1, &vecf ); // Forward vector
mCameraMatrix.getColumn( 2, &vecu ); // Up vector
mCameraMatrix.getColumn( 0, &vecr ); // Right vector
Point3F flatVecf(vecf.x, vecf.y, 0.0f);
Point3F modvecf = flatVecf * mOrbitRelPos.y;
Point3F modvecu = vecu * mOrbitRelPos.z;
Point3F modvecr = vecr * mOrbitRelPos.x;
// Change the orbit position
mOrbitPos += modvecu - modvecr + modvecf;
F32 vecfmul = mOrbitDist;
Point3F virtualVecF = vecf * mOrbitDist;
vecf *= vecfmul;
mCameraPos = mOrbitPos - virtualVecF;
// Update the camera's position
mCameraMatrix.setColumn( 3, (mOrbitPos - vecf) );
query->farPlane = 2100.0f;
query->nearPlane = query->farPlane / 5000.0f;
query->fov = 45.0f;
query->cameraMatrix = mCameraMatrix;
// Reset the relative position
mOrbitRelPos = Point3F(0,0,0);
}
return true;
}
void GuiMaterialPreview::onMouseEnter(const GuiEvent & event)
{
Con::executef(this, "onMouseEnter");
}
void GuiMaterialPreview::onMouseLeave(const GuiEvent & event)
{
Con::executef(this, "onMouseLeave");
}
void GuiMaterialPreview::renderWorld(const RectI &updateRect)
{
// nothing to render, punt
if ( !mModel && !mMountedModel )
return;
S32 time = Platform::getVirtualMilliseconds();
//S32 dt = time - lastRenderTime;
lastRenderTime = time;
F32 left, right, top, bottom, nearPlane, farPlane;
bool isOrtho;
GFX->getFrustum( &left, &right, &bottom, &top, &nearPlane, &farPlane, &isOrtho);
Frustum frust( isOrtho, left, right, bottom, top, nearPlane, farPlane, MatrixF::Identity );
FogData savedFogData = gClientSceneGraph->getFogData();
gClientSceneGraph->setFogData( FogData() ); // no fog in preview window
RenderPassManager* renderPass = gClientSceneGraph->getDefaultRenderPass();
SceneRenderState state
(
gClientSceneGraph,
SPT_Diffuse,
SceneCameraState( GFX->getViewport(), frust, GFX->getWorldMatrix(), GFX->getProjectionMatrix() ),
renderPass,
true
);
// Set up our TS render state here.
TSRenderState rdata;
rdata.setSceneState( &state );
// We might have some forward lit materials
// so pass down a query to gather lights.
LightQuery query;
query.init( SphereF( Point3F::Zero, 1.0f ) );
rdata.setLightQuery( &query );
// Set up pass transforms
renderPass->assignSharedXform(RenderPassManager::View, MatrixF::Identity);
renderPass->assignSharedXform(RenderPassManager::Projection, GFX->getProjectionMatrix());
LIGHTMGR->unregisterAllLights();
LIGHTMGR->setSpecialLight( LightManager::slSunLightType, mFakeSun );
if ( mModel )
mModel->render( rdata );
if ( mMountedModel )
{
// render a weapon
/*
MatrixF mat;
GFX->pushWorldMatrix();
GFX->multWorld( mat );
GFX->popWorldMatrix();
*/
}
renderPass->renderPass( &state );
gClientSceneGraph->setFogData( savedFogData ); // restore fog setting
// Make sure to remove our fake sun
LIGHTMGR->unregisterAllLights();
}
// Make sure the orbit distance is within the acceptable range.
void GuiMaterialPreview::setOrbitDistance(F32 distance)
{
mOrbitDist = mClampF(distance, mMinOrbitDist, mMaxOrbitDist);
}
// This function is meant to be used with a button to put everything back to default settings.
void GuiMaterialPreview::resetViewport()
{
// Reset the camera's orientation.
mCameraRot.set( mDegToRad(30.0f), 0, mDegToRad(-30.0f) );
mCameraPos.set(0.0f, 1.75f, 1.25f);
mOrbitDist = 5.0f;
mOrbitPos = mModel->getShape()->center;
// Reset the viewport's lighting.
GuiMaterialPreview::mFakeSun->setColor( LinearColorF( 1.0f, 1.0f, 1.0f ) );
GuiMaterialPreview::mFakeSun->setAmbient( LinearColorF( 0.5f, 0.5f, 0.5f ) );
GuiMaterialPreview::mFakeSun->setDirection( VectorF( 0.0f, 0.707f, -0.707f ) );
}
// Expose the class and functions to the console.
IMPLEMENT_CONOBJECT(GuiMaterialPreview);
ConsoleDocClass( GuiMaterialPreview,
"@brief Visual preview of a specified Material\n\n"
"Editor use only.\n\n"
"@internal"
);
// Set the model.
DefineEngineMethod(GuiMaterialPreview, setModel, void, ( const char* shapeName ),,
"Sets the model to be displayed in this control\n\n"
"@param shapeName Name of the model to display.\n")
{
object->setObjectModel(shapeName);
}
DefineEngineMethod(GuiMaterialPreview, deleteModel, void, (),,
"Deletes the preview model.\n")
{
object->deleteModel();
}
// Set orbit distance around the model.
DefineEngineMethod(GuiMaterialPreview, setOrbitDistance, void, ( F32 distance ),,
"Sets the distance at which the camera orbits the object. Clamped to the "
"acceptable range defined in the class by min and max orbit distances.\n\n"
"@param distance The distance to set the orbit to (will be clamped).")
{
object->setOrbitDistance(distance);
}
// Reset control to default values. Meant to be used with a button.
DefineEngineMethod(GuiMaterialPreview, reset, void, (),,
"Resets the viewport to default zoom, pan, rotate and lighting.")
{
object->resetViewport();
}
// This function allows the user to change the light's color.
DefineEngineMethod(GuiMaterialPreview, setLightColor, void, ( LinearColorF color ),,
"Sets the color of the light in the scene.\n")
{
object->setLightColor( color.red, color.green, color.blue );
}
// This function allows the user to change the viewports's ambient color.
DefineEngineMethod(GuiMaterialPreview, setAmbientLightColor, void, ( LinearColorF color ),,
"Sets the color of the ambient light in the scene.\n")
{
object->setAmbientLightColor( color.red, color.green, color.blue );
}