Commit 507fb7ec authored by cutealien's avatar cutealien

Can now move the light in the meshviewer example. And Shininess set to a...

Can now move the light in the meshviewer example. And Shininess set to a default value that shows off specular lighting.


git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@4953 dfc29bdd-3216-0410-991c-e03cc46cb475
parent 0b677245
...@@ -6,6 +6,10 @@ Only the default non-shader materials are used in here. ...@@ -6,6 +6,10 @@ Only the default non-shader materials are used in here.
You have a node witha mesh, one dynamic light and global ambient light to play around with. You have a node witha mesh, one dynamic light and global ambient light to play around with.
*/ */
// TODO: Should be possible to set all material values by the GUI.
// For now just change the defaultMaterial in CApp::init for the rest.
// TODO: Allow users to switch between a sphere and a box meshĵ
#include <irrlicht.h> #include <irrlicht.h>
#include "driverChoice.h" #include "driverChoice.h"
#include "main.h" #include "main.h"
...@@ -532,6 +536,11 @@ void SMaterialControl::selectTextures(const irr::core::stringw& name) ...@@ -532,6 +536,11 @@ void SMaterialControl::selectTextures(const irr::core::stringw& name)
TextureControls[i]->selectTextureByName(name); TextureControls[i]->selectTextureByName(name);
} }
bool SMaterialControl::isLightingEnabled() const
{
return ButtonLighting && ButtonLighting->isPressed();
}
void SMaterialControl::updateMaterial(video::SMaterial & material) void SMaterialControl::updateMaterial(video::SMaterial & material)
{ {
TypicalColorsControl->updateMaterialColors(material); TypicalColorsControl->updateMaterialColors(material);
...@@ -615,6 +624,10 @@ bool CApp::OnEvent(const SEvent &event) ...@@ -615,6 +624,10 @@ bool CApp::OnEvent(const SEvent &event)
break; break;
} }
} }
else if (event.EventType == EET_KEY_INPUT_EVENT)
{
KeysPressed[event.KeyInput.Key] = event.KeyInput.PressedDown;
}
return false; return false;
} }
...@@ -672,11 +685,16 @@ bool CApp::init(int argc, char *argv[]) ...@@ -672,11 +685,16 @@ bool CApp::init(int argc, char *argv[])
core::vector3df(0, 10, 0), core::vector3df(0, 10, 0),
-1); -1);
// default material
video::SMaterial defaultMaterial;
defaultMaterial.Shininess = 20.f;
// add the nodes which are used to show the materials // add the nodes which are used to show the materials
SceneNode = smgr->addCubeSceneNode (30.0f, 0, -1, SceneNode = smgr->addCubeSceneNode (30.0f, 0, -1,
core::vector3df(0, 0, 0), core::vector3df(0, 0, 0),
core::vector3df(0.f, 45.f, 0.f), core::vector3df(0.f, 45.f, 0.f),
core::vector3df(1.0f, 1.0f, 1.0f)); core::vector3df(1.0f, 1.0f, 1.0f));
SceneNode->getMaterial(0) = defaultMaterial;
MeshMaterialControl.init( SceneNode, Device, core::position2d<s32>(10,controlsTop), L"Material" ); MeshMaterialControl.init( SceneNode, Device, core::position2d<s32>(10,controlsTop), L"Material" );
MeshMaterialControl.selectTextures(core::stringw("CARO_A8R8G8B8")); // set a useful default texture MeshMaterialControl.selectTextures(core::stringw("CARO_A8R8G8B8")); // set a useful default texture
...@@ -692,9 +710,9 @@ bool CApp::init(int argc, char *argv[]) ...@@ -692,9 +710,9 @@ bool CApp::init(int argc, char *argv[])
// add one light // add one light
NodeLight = smgr->addLightSceneNode(0, Camera->getPosition(), NodeLight = smgr->addLightSceneNode(0, core::vector3df(0, 0, -40),
video::SColorf(1.0f, 1.0f, 1.0f), video::SColorf(1.0f, 1.0f, 1.0f),
35.0f); 35.0f);
LightControl.init(NodeLight, guiEnv, core::position2d<s32>(550,controlsTop), L"Dynamic light" ); LightControl.init(NodeLight, guiEnv, core::position2d<s32>(550,controlsTop), L"Dynamic light" );
// one large cube around everything. That's mainly to make the light more obvious. // one large cube around everything. That's mainly to make the light more obvious.
...@@ -732,6 +750,12 @@ bool CApp::update() ...@@ -732,6 +750,12 @@ bool CApp::update()
if ( !Device->run() ) if ( !Device->run() )
return false; return false;
// Figure out delta time since last frame
ITimer * timer = Device->getTimer();
u32 newTick = timer->getRealTime();
f32 deltaTime = RealTimeTick > 0 ? f32(newTick-RealTimeTick)/1000.0 : 0.f; // in seconds
RealTimeTick = newTick;
if ( Device->isWindowActive() || Config.RenderInBackground ) if ( Device->isWindowActive() || Config.RenderInBackground )
{ {
gui::IGUIEnvironment* guiEnv = Device->getGUIEnvironment(); gui::IGUIEnvironment* guiEnv = Device->getGUIEnvironment();
...@@ -758,6 +782,23 @@ bool CApp::update() ...@@ -758,6 +782,23 @@ bool CApp::update()
GlobalAmbient->resetDirty(); GlobalAmbient->resetDirty();
} }
// Let the user move the light around
const float zoomSpeed = 10.f * deltaTime;
const float rotationSpeed = 100.f * deltaTime;
if ( KeysPressed[KEY_PLUS] || KeysPressed[KEY_ADD])
ZoomOut(NodeLight, zoomSpeed);
if ( KeysPressed[KEY_MINUS] || KeysPressed[KEY_SUBTRACT])
ZoomOut(NodeLight, -zoomSpeed);
if ( KeysPressed[KEY_RIGHT])
RotateHorizontal(NodeLight, rotationSpeed);
if ( KeysPressed[KEY_LEFT])
RotateHorizontal(NodeLight, -rotationSpeed);
UpdateRotationAxis(NodeLight, LightRotationAxis);
if ( KeysPressed[KEY_UP])
RotateAroundAxis(NodeLight, rotationSpeed, LightRotationAxis);
if ( KeysPressed[KEY_DOWN])
RotateAroundAxis(NodeLight, -rotationSpeed, LightRotationAxis);
// draw everything // draw everything
video::SColor bkColor( skin->getColor(gui::EGDC_APP_WORKSPACE) ); video::SColor bkColor( skin->getColor(gui::EGDC_APP_WORKSPACE) );
videoDriver->beginScene(true, true, bkColor); videoDriver->beginScene(true, true, bkColor);
...@@ -765,6 +806,16 @@ bool CApp::update() ...@@ -765,6 +806,16 @@ bool CApp::update()
smgr->drawAll(); smgr->drawAll();
guiEnv->drawAll(); guiEnv->drawAll();
if ( MeshMaterialControl.isLightingEnabled() )
{
// draw a line from the light to the target
video::SMaterial lineMaterial;
lineMaterial.Lighting = false;
videoDriver->setMaterial(lineMaterial);
videoDriver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
videoDriver->draw3DLine(NodeLight->getAbsolutePosition(), SceneNode->getAbsolutePosition());
}
videoDriver->endScene(); videoDriver->endScene();
} }
...@@ -871,6 +922,55 @@ void CApp::loadTexture(const io::path &name) ...@@ -871,6 +922,55 @@ void CApp::loadTexture(const io::path &name)
MeshMaterialControl.updateTextures(); MeshMaterialControl.updateTextures();
} }
void CApp::RotateHorizontal(irr::scene::ISceneNode* node, irr::f32 angle)
{
if ( node )
{
core::vector3df pos(node->getPosition());
core::vector2df dir(pos.X, pos.Z);
dir.rotateBy(angle);
pos.X = dir.X;
pos.Z = dir.Y;
node->setPosition(pos);
}
}
void CApp::RotateAroundAxis(irr::scene::ISceneNode* node, irr::f32 angle, const irr::core::vector3df& axis)
{
if ( node )
{
// TOOD: yeah, doesn't rotate around top/bottom yet. Fixes welcome.
core::vector3df pos(node->getPosition());
core::matrix4 mat;
mat.setRotationAxisRadians (core::degToRad(angle), axis);
mat.rotateVect(pos);
node->setPosition(pos);
}
}
void CApp::ZoomOut(irr::scene::ISceneNode* node, irr::f32 units)
{
if ( node )
{
core::vector3df pos(node->getPosition());
irr::f32 len = pos.getLength() + units;
pos.setLength(len);
node->setPosition(pos);
}
}
void CApp::UpdateRotationAxis(irr::scene::ISceneNode* node, irr::core::vector3df& axis)
{
// Find a perpendicular axis to the x,z vector. If none found (vector straight up/down) continue to use the existing one.
core::vector3df pos(node->getPosition());
if ( !core::equals(pos.X, 0.f) || !core::equals(pos.Z, 0.f) )
{
axis.X = -pos.Z;
axis.Z = pos.X;
axis.normalize();
}
}
/* /*
Short main as most is done in classes. Short main as most is done in classes.
*/ */
......
...@@ -164,6 +164,8 @@ struct SMaterialControl ...@@ -164,6 +164,8 @@ struct SMaterialControl
void selectTextures(const irr::core::stringw& name); void selectTextures(const irr::core::stringw& name);
bool isLightingEnabled() const;
protected: protected:
void updateMaterial(irr::video::SMaterial & material); void updateMaterial(irr::video::SMaterial & material);
...@@ -229,13 +231,16 @@ public: ...@@ -229,13 +231,16 @@ public:
// constructor // constructor
CApp() CApp()
: IsRunning(false) : IsRunning(false)
, RealTimeTick(0)
, Device(0) , Device(0)
, MeshManipulator(0) , MeshManipulator(0)
, Camera(0) , Camera(0)
, SceneNode(0), SceneNode2T(0), SceneNodeTangents(0), NodeLight(0) , SceneNode(0), SceneNode2T(0), SceneNodeTangents(0), NodeLight(0)
, LightRotationAxis(irr::core::vector3df(1,0,0))
, ControlVertexColors(0) , ControlVertexColors(0)
, GlobalAmbient(0) , GlobalAmbient(0)
{ {
memset(KeysPressed, 0, sizeof KeysPressed);
} }
// destructor // destructor
...@@ -276,9 +281,17 @@ protected: ...@@ -276,9 +281,17 @@ protected:
// Load a texture and make sure nodes know it when more textures are available. // Load a texture and make sure nodes know it when more textures are available.
void loadTexture(const irr::io::path &name); void loadTexture(const irr::io::path &name);
// Rotate a node around the origin (0,0,0)
void RotateHorizontal(irr::scene::ISceneNode* node, irr::f32 angle);
void RotateAroundAxis(irr::scene::ISceneNode* node, irr::f32 angle, const irr::core::vector3df& axis);
void ZoomOut(irr::scene::ISceneNode* node, irr::f32 units);
void UpdateRotationAxis(irr::scene::ISceneNode* node, irr::core::vector3df& axis);
private: private:
SConfig Config; SConfig Config;
bool IsRunning; bool IsRunning;
irr::u32 RealTimeTick;
irr::IrrlichtDevice * Device; irr::IrrlichtDevice * Device;
irr::scene::IMeshManipulator* MeshManipulator; irr::scene::IMeshManipulator* MeshManipulator;
irr::scene::ICameraSceneNode * Camera; irr::scene::ICameraSceneNode * Camera;
...@@ -286,10 +299,12 @@ private: ...@@ -286,10 +299,12 @@ private:
irr::scene::IMeshSceneNode* SceneNode2T; irr::scene::IMeshSceneNode* SceneNode2T;
irr::scene::IMeshSceneNode* SceneNodeTangents; irr::scene::IMeshSceneNode* SceneNodeTangents;
irr::scene::ILightSceneNode* NodeLight; irr::scene::ILightSceneNode* NodeLight;
irr::core::vector3df LightRotationAxis;
SMaterialControl MeshMaterialControl; SMaterialControl MeshMaterialControl;
SLightNodeControl LightControl; SLightNodeControl LightControl;
CColorControl* ControlVertexColors; CColorControl* ControlVertexColors;
CColorControl* GlobalAmbient; CColorControl* GlobalAmbient;
bool KeysPressed[irr::KEY_KEY_CODES_COUNT];
}; };
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment