Skip to content

Commit 23e54fa

Browse files
committed
use real sun and add ground plane
now we have a scene with a ground plane so we can see the shadows we also use a real sun object so the shadows can be projected removed bgsky so this can be merged until we figure out hdr's to use the system now works mostly as expected still just the bug with preview window updating all the other shapes with the selected material.
1 parent efd8d09 commit 23e54fa

5 files changed

Lines changed: 125 additions & 194 deletions

File tree

Engine/source/T3D/guiMaterialPreview.cpp

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,28 @@
3535
#include "renderInstance/renderProbeMgr.h"
3636
#include "T3D/lighting/skylight.h"
3737
#include "gfx/gfxDrawUtil.h"
38+
#include "math/mathUtils.h"
39+
#include "T3D/groundPlane.h"
3840

3941
// GuiMaterialPreview
4042
GuiMaterialPreview::GuiMaterialPreview()
4143
: mMouseState(None),
4244
runThread(0),
4345
lastRenderTime(0),
4446
mLastMousePoint(0, 0),
45-
mFakeSun(NULL),
4647
mMaxOrbitDist(5.0f),
4748
mMinOrbitDist(0.0f),
4849
mOrbitDist(5.0f)
4950
{
5051
mActive = true;
5152
mCameraMatrix.identity();
52-
mCameraRot.set( mDegToRad(30.0f), 0, mDegToRad(-30.0f) );
53+
mCameraRot.set(mDegToRad(3.0f), 0, mDegToRad(-30.0f) );
5354
mCameraPos.set(0.0f, 1.75f, 1.25f);
5455
mCameraMatrix.setColumn(3, mCameraPos);
5556
mOrbitPos.set(0.0f, 0.0f, 0.0f);
5657
mTransStep = 0.01f;
5758
mTranMult = 4.0;
58-
mLightTransStep = 0.01f;
59+
mLightTransStep = 0.5f;
5960
mLightTranMult = 4.0;
6061
mOrbitRelPos = Point3F(0,0,0);
6162

@@ -69,19 +70,22 @@ GuiMaterialPreview::GuiMaterialPreview()
6970
mTempScene->setFogData(FogData());
7071

7172
ScopedSceneManager scopeManager(mTempScene);
72-
mBGSky = new SkySphere();
73-
mBGSky->_setMaterial("Prototyping:hdrMaterial");
74-
mBGSky->registerObject();
73+
mRealSun = new Sun();
74+
mRealSun->setField("brightness", "2.0");
75+
mRealSun->setAzimuth(100.0f);
76+
mRealSun->registerObject();
77+
78+
GroundPlane* plane = new GroundPlane();
79+
plane->_setMaterial("ToolsModule:GreyPreview");
80+
plane->registerObject();
7581

7682
mTSShape = new TSStatic();
7783
}
7884

7985
GuiMaterialPreview::~GuiMaterialPreview()
8086
{
81-
SAFE_DELETE(mFakeSun);
82-
83-
mBGSky->unregisterObject();
84-
SAFE_DELETE(mBGSky);
87+
mRealSun->unregisterObject();
88+
SAFE_DELETE(mRealSun);
8589

8690
if(mTSShape != NULL)
8791
deleteModel();
@@ -94,14 +98,8 @@ bool GuiMaterialPreview::onWake()
9498
if( !Parent::onWake() )
9599
return false;
96100

97-
if (!mFakeSun)
98-
mFakeSun = LightManager::createLightInfo();
99-
100-
mFakeSun->setColor( LinearColorF( 1.0f, 1.0f, 1.0f ) );
101-
mFakeSun->setAmbient( LinearColorF( 0.5f, 0.5f, 0.5f ) );
102-
mFakeSun->setDirection( VectorF( 0.0f, 0.707f, -0.707f ) );
103-
mFakeSun->setPosition( mFakeSun->getDirection() * -10000.0f );
104-
mFakeSun->setRange( 2000000.0f );
101+
mRealSun->setColor( LinearColorF( 1.0f, 1.0f, 1.0f ) );
102+
mRealSun->setAmbientColor( LinearColorF( 0.5f, 0.5f, 0.5f ) );
105103

106104
return true;
107105
}
@@ -111,15 +109,15 @@ void GuiMaterialPreview::setAmbientLightColor( F32 r, F32 g, F32 b )
111109
{
112110
LinearColorF temp(r, g, b);
113111
temp.clamp();
114-
GuiMaterialPreview::mFakeSun->setAmbient( temp );
112+
mRealSun->setAmbientColor( temp );
115113
}
116114

117115
// This function allows the light's color to be changed. This is exposed to script below.
118116
void GuiMaterialPreview::setLightColor( F32 r, F32 g, F32 b )
119117
{
120118
LinearColorF temp(r, g, b);
121119
temp.clamp();
122-
GuiMaterialPreview::mFakeSun->setColor( temp );
120+
mRealSun->setColor( temp );
123121
}
124122

125123
// This function is for moving the light in the scene. This needs to be adjusted to keep the light
@@ -129,21 +127,16 @@ void GuiMaterialPreview::setLightTranslate(S32 modifier, F32 xstep, F32 ystep)
129127
{
130128
F32 _lighttransstep = (modifier & SI_SHIFT ? mLightTransStep : (mLightTransStep*mLightTranMult));
131129

132-
Point3F relativeLightDirection = GuiMaterialPreview::mFakeSun->getDirection();
133-
134-
F32 azimuth = mAtan2(relativeLightDirection.y, relativeLightDirection.x);
135-
F32 elevation = mAsin(relativeLightDirection.z);
130+
// Convert current direction to spherical coordinates (azimuth and elevation in radians)
131+
F32 azimuth = mRealSun->getAzimuth();
132+
F32 elevation = mRealSun->getElevation();
136133

137134
// Modify azimuth and elevation based on input
138-
azimuth += xstep * _lighttransstep;
139-
elevation = mClampF(elevation + ystep * _lighttransstep, -M_2PI_F, M_2PI_F);
135+
azimuth -= xstep * _lighttransstep; // Horizontal movement affects azimuth
136+
elevation = elevation - ystep * _lighttransstep;
140137

141-
// Convert back to Cartesian coordinates
142-
relativeLightDirection.x = mCos(elevation) * mCos(azimuth);
143-
relativeLightDirection.y = mCos(elevation) * mSin(azimuth);
144-
relativeLightDirection.z = mSin(elevation);
145-
146-
GuiMaterialPreview::mFakeSun->setDirection(relativeLightDirection);
138+
// Update azimuth and elevation on the sun object
139+
mRealSun->setDirection(azimuth, elevation);
147140
}
148141

149142
// Left Click
@@ -171,9 +164,9 @@ void GuiMaterialPreview::onMouseDragged(const GuiEvent &event)
171164
// If we are MovingLight...
172165
else
173166
{
174-
Point2I delta = event.mousePoint - mLastMousePoint;
175-
mLastMousePoint = event.mousePoint;
176-
setLightTranslate(event.modifier, delta.x, delta.y);
167+
Point2I delta = event.mousePoint - mLastMousePoint;
168+
mLastMousePoint = event.mousePoint;
169+
setLightTranslate(event.modifier, delta.x, delta.y);
177170
}
178171
}
179172

@@ -227,11 +220,13 @@ void GuiMaterialPreview::setObjectModel(const char* modelName)
227220
if (!mTSShape->isProperlyAdded())
228221
{
229222
ScopedSceneManager scopeManager(mTempScene);
223+
mTSShape->setPosition(Point3F(0, 0, 1.5));
230224
mTSShape->registerObject();
231225
}
232226

233227
// Initialize camera values:
234-
mOrbitPos = mTSShape->mShapeInstance->getShape()->center;
228+
mTSShape->setPosition(Point3F(0, 0, mTSShape->mShapeInstance->getShape()->mBounds.len_z() * 0.5f));
229+
mOrbitPos = mTSShape->getPosition();
235230
mMinOrbitDist = mTSShape->mShapeInstance->getShape()->mRadius;
236231

237232
lastRenderTime = Platform::getVirtualMilliseconds();
@@ -329,37 +324,42 @@ void GuiMaterialPreview::renderWorld(const RectI &updateRect)
329324

330325
ScopedSceneManager scopeManager(mTempScene);
331326

332-
LIGHTMGR->unregisterAllLights();
333-
LIGHTMGR->setSpecialLight(LightManager::slSunLightType, mFakeSun);
327+
RenderPassManager* renderPass = mTempScene->getDefaultRenderPass();
328+
SceneRenderState state
329+
(
330+
mTempScene,
331+
SPT_Diffuse,
332+
SceneCameraState::fromGFX(),
333+
renderPass,
334+
true
335+
);
334336

335337
if (Skylight::smSkylightProbe.isValid())
336338
PROBEMGR->submitProbe(Skylight::smSkylightProbe->getProbeInfo());
337339

338-
mTempScene->renderScene(SPT_Diffuse);
340+
mTempScene->renderScene(&state);
339341

340342
if (mMouseState == MovingLight)
341343
{
342344
renderSunDirection();
343345
}
344346

345-
// Make sure to remove our fake sun
346-
LIGHTMGR->unregisterAllLights();
347347
}
348348

349349
void GuiMaterialPreview::renderSunDirection() const
350350
{
351351
// Render four arrows aiming in the direction of the sun's light
352-
ColorI color = LinearColorF(mFakeSun->getColor()).toColorI();
352+
ColorI color = LinearColorF(mRealSun->getLight()->getColor()).toColorI();
353353
F32 length = mTSShape->mShapeInstance->getShape()->mBounds.len() * 0.8f;
354354

355355
// Get the sun's vectors
356-
Point3F fwd = mFakeSun->getTransform().getForwardVector();
357-
Point3F up = mFakeSun->getTransform().getUpVector() * length / 8;
358-
Point3F right = mFakeSun->getTransform().getRightVector() * length / 8;
356+
Point3F fwd = mRealSun->getLight()->getTransform().getForwardVector();
357+
Point3F up = mRealSun->getLight()->getTransform().getUpVector() * length / 8;
358+
Point3F right = mRealSun->getLight()->getTransform().getRightVector() * length / 8;
359359

360360
// Calculate the start and end points of the first arrow (bottom left)
361-
Point3F start = mTSShape->mShapeInstance->getShape()->center - fwd * length - up / 2 - right / 2;
362-
Point3F end = mTSShape->mShapeInstance->getShape()->center - fwd * length / 3 - up / 2 - right / 2;
361+
Point3F start = mTSShape->getPosition() - fwd * length - up / 2 - right / 2;
362+
Point3F end = mTSShape->getPosition() - fwd * length / 3 - up / 2 - right / 2;
363363

364364
GFXStateBlockDesc desc;
365365
desc.setZReadWrite(true, true);
@@ -382,15 +382,14 @@ void GuiMaterialPreview::setOrbitDistance(F32 distance)
382382
void GuiMaterialPreview::resetViewport()
383383
{
384384
// Reset the camera's orientation.
385-
mCameraRot.set( mDegToRad(30.0f), 0, mDegToRad(-30.0f) );
385+
mCameraRot.set(mDegToRad(3.0f), 0, mDegToRad(-30.0f) );
386386
mCameraPos.set(0.0f, 1.75f, 1.25f);
387387
mOrbitDist = 5.0f;
388-
mOrbitPos = mTSShape->mShapeInstance->getShape()->center;
388+
mOrbitPos = mTSShape->getPosition();
389389

390390
// Reset the viewport's lighting.
391-
GuiMaterialPreview::mFakeSun->setColor( LinearColorF( 1.0f, 1.0f, 1.0f ) );
392-
GuiMaterialPreview::mFakeSun->setAmbient( LinearColorF( 0.5f, 0.5f, 0.5f ) );
393-
GuiMaterialPreview::mFakeSun->setDirection( VectorF( 0.0f, 0.707f, -0.707f ) );
391+
GuiMaterialPreview::mRealSun->setColor( LinearColorF( 1.0f, 1.0f, 1.0f ) );
392+
GuiMaterialPreview::mRealSun->setAmbientColor( LinearColorF( 0.5f, 0.5f, 0.5f ) );
394393
}
395394

396395
// Expose the class and functions to the console.

Engine/source/T3D/guiMaterialPreview.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
#include "T3D/tsStatic.h"
5353
#endif
5454

55+
#ifndef _SUN_H_
56+
#include "environment/sun.h"
57+
#endif
58+
5559
class LightInfo;
5660

5761
class GuiMaterialPreview : public GuiTSCtrl
@@ -93,10 +97,9 @@ class GuiMaterialPreview : public GuiTSCtrl
9397

9498
Point2I mLastMousePoint;
9599

96-
LightInfo* mFakeSun;
97100
SceneManager* mTempScene;
98101

99-
SkySphere* mBGSky;
102+
Sun* mRealSun;
100103
TSStatic* mTSShape;
101104

102105
void renderSunDirection() const;

Engine/source/environment/sun.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,21 @@ void Sun::setColor( const LinearColorF &color )
408408
setMaskBits( UpdateMask ); // TODO: Break out the masks to save some space!
409409
}
410410

411+
void Sun::setAmbientColor(const LinearColorF& color)
412+
{
413+
mLightAmbient = color;
414+
_conformLights();
415+
setMaskBits(UpdateMask); // TODO: Break out the masks to save some space!
416+
}
417+
418+
void Sun::setDirection(F32 azimuth, F32 elevation)
419+
{
420+
mSunAzimuth = azimuth;
421+
mSunElevation = elevation;
422+
_conformLights();
423+
setMaskBits(UpdateMask); // TODO: Break out the masks to save some space!
424+
}
425+
411426
void Sun::animate( F32 duration, F32 startAzimuth, F32 endAzimuth, F32 startElevation, F32 endElevation )
412427
{
413428
mAnimateSun = true;
@@ -424,8 +439,8 @@ void Sun::animate( F32 duration, F32 startAzimuth, F32 endAzimuth, F32 startElev
424439
void Sun::_conformLights()
425440
{
426441
// Build the light direction from the azimuth and elevation.
427-
F32 yaw = mDegToRad(mClampF(mSunAzimuth,0,359));
428-
F32 pitch = mDegToRad(mClampF(mSunElevation,-360,+360));
442+
F32 yaw = mDegToRad(mWrapF(mSunAzimuth,0,359));
443+
F32 pitch = mDegToRad(mWrapF(mSunElevation,-180,+180));
429444
VectorF lightDirection;
430445
MathUtils::getVectorFromAngles(lightDirection, yaw, pitch);
431446
lightDirection.normalize();

Engine/source/environment/sun.h

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Sun : public SceneObject, public ISceneLight
4949
protected:
5050

5151
F32 mSunAzimuth;
52-
52+
5353
F32 mSunElevation;
5454

5555
LinearColorF mLightColor;
@@ -70,9 +70,9 @@ class Sun : public SceneObject, public ISceneLight
7070
S32 mStaticRefreshFreq;
7171
S32 mDynamicRefreshFreq;
7272

73-
LightInfo *mLight;
73+
LightInfo* mLight;
7474

75-
LightFlareData *mFlareData;
75+
LightFlareData* mFlareData;
7676
LightFlareState mFlareState;
7777
F32 mFlareScale;
7878

@@ -81,8 +81,8 @@ class Sun : public SceneObject, public ISceneLight
8181
DECLARE_MATERIALASSET(Sun, CoronaMaterial);
8282
DECLARE_ASSET_NET_SETGET(Sun, CoronaMaterial, UpdateMask);
8383

84-
BaseMatInstance *mCoronaMatInst;
85-
MatrixSet *mMatrixSet;
84+
BaseMatInstance* mCoronaMatInst;
85+
MatrixSet* mMatrixSet;
8686
F32 mCoronaScale;
8787
LinearColorF mCoronaTint;
8888
bool mCoronaUseLightColor;
@@ -94,14 +94,14 @@ class Sun : public SceneObject, public ISceneLight
9494

9595
void _conformLights();
9696
void _initCorona();
97-
void _renderCorona( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
98-
void _updateTimeOfDay( TimeOfDay *timeOfDay, F32 time );
97+
void _renderCorona(ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* overrideMat);
98+
void _updateTimeOfDay(TimeOfDay* timeOfDay, F32 time);
9999

100100
// SimObject.
101101
void _onSelected() override;
102102
void _onUnselected() override;
103103

104-
enum NetMaskBits
104+
enum NetMaskBits
105105
{
106106
UpdateMask = BIT(0)
107107
};
@@ -122,28 +122,36 @@ class Sun : public SceneObject, public ISceneLight
122122
void inspectPostApply() override;
123123

124124
// NetObject
125-
U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream ) override;
126-
void unpackUpdate( NetConnection *conn, BitStream *stream ) override;
125+
U32 packUpdate(NetConnection* conn, U32 mask, BitStream* stream) override;
126+
void unpackUpdate(NetConnection* conn, BitStream* stream) override;
127127

128128
// ISceneLight
129-
void submitLights( LightManager *lm, bool staticLighting ) override;
130-
LightInfo* getLight() override { return mLight; }
129+
void submitLights(LightManager* lm, bool staticLighting) override;
130+
LightInfo* getLight() override { return mLight; }
131131

132132
// SceneObject
133-
void prepRenderImage( SceneRenderState* state ) override;
133+
void prepRenderImage(SceneRenderState* state) override;
134134

135135
// ProcessObject
136-
void advanceTime( F32 dt ) override;
136+
void advanceTime(F32 dt) override;
137137

138138
///
139-
void setAzimuth( F32 azimuth );
139+
void setAzimuth(F32 azimuth);
140+
F32 getAzimuth() { return mSunAzimuth; }
140141

141142
///
142-
void setElevation( F32 elevation );
143+
void setElevation(F32 elevation);
144+
F32 getElevation() { return mSunElevation; }
143145

144146
///
145147
void setColor( const LinearColorF &color );
146148

149+
///
150+
void setAmbientColor(const LinearColorF& color);
151+
152+
///
153+
void setDirection(F32 azimuth, F32 elevation);
154+
147155
///
148156
void animate( F32 duration, F32 startAzimuth, F32 endAzimuth, F32 startElevation, F32 endElevation );
149157
};

0 commit comments

Comments
 (0)