Commit c8bdc67d authored by hybrid's avatar hybrid

Fixed lighting of the models.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@2314 dfc29bdd-3216-0410-991c-e03cc46cb475
parent 316139af
...@@ -30,7 +30,7 @@ enum ...@@ -30,7 +30,7 @@ enum
IDFlag_IsPickable = 1 << 0, IDFlag_IsPickable = 1 << 0,
// I use this flag in ISceneNode IDs to indicate that the // I use this flag in ISceneNode IDs to indicate that the
// scene node can be highlighted. In this example, the // scene node can be highlighted. In this example, the
// homonids can be highlighted, but the level mesh can't. // homonids can be highlighted, but the level mesh can't.
IDFlag_IsHighlightable = 1 << 1 IDFlag_IsHighlightable = 1 << 1
}; };
...@@ -115,7 +115,7 @@ int main() ...@@ -115,7 +115,7 @@ int main()
animator modifies the scene node to which it is attached to in order to animator modifies the scene node to which it is attached to in order to
prevent it moving through walls, and to add gravity to it. The prevent it moving through walls, and to add gravity to it. The
only thing we have to tell the animator is how the world looks like, only thing we have to tell the animator is how the world looks like,
how big the scene node is, how much gravity to apply and so on. After the how big the scene node is, how much gravity to apply and so on. After the
collision response animator is attached to the camera, we do not have to do collision response animator is attached to the camera, we do not have to do
anything more for collision detection, anything is done automatically. anything more for collision detection, anything is done automatically.
The rest of the collision detection code below is for picking. And please The rest of the collision detection code below is for picking. And please
...@@ -132,11 +132,11 @@ int main() ...@@ -132,11 +132,11 @@ int main()
case it is the camera. The third defines how big the object is, it is case it is the camera. The third defines how big the object is, it is
the radius of an ellipsoid. Try it out and change the radius to smaller the radius of an ellipsoid. Try it out and change the radius to smaller
values, the camera will be able to move closer to walls after this. The values, the camera will be able to move closer to walls after this. The
next parameter is the direction and speed of gravity. We'll set it to next parameter is the direction and speed of gravity. We'll set it to
(0, -10, 0), which approximates to realistic gravity, assuming that our (0, -10, 0), which approximates to realistic gravity, assuming that our
units are metres. You could set it to (0,0,0) to disable gravity. And the units are metres. You could set it to (0,0,0) to disable gravity. And the
last value is just a translation: Without this, the ellipsoid with which last value is just a translation: Without this, the ellipsoid with which
collision detection is done would be around the camera, and the camera would collision detection is done would be around the camera, and the camera would
be in the middle of the ellipsoid. But as human beings, we are used to have our be in the middle of the ellipsoid. But as human beings, we are used to have our
eyes on top of the body, with which we collide with our world, not in eyes on top of the body, with which we collide with our world, not in
the middle of it. So we place the scene node 50 units over the center the middle of it. So we place the scene node 50 units over the center
...@@ -155,8 +155,7 @@ int main() ...@@ -155,8 +155,7 @@ int main()
{ {
scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator( scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
selector, camera, core::vector3df(30,50,30), selector, camera, core::vector3df(30,50,30),
core::vector3df(0,-10,0), core::vector3df(0,-10,0), core::vector3df(0,30,0));
core::vector3df(0,30,0));
selector->drop(); // As soon as we're done with the selector, drop it. selector->drop(); // As soon as we're done with the selector, drop it.
camera->addAnimator(anim); camera->addAnimator(anim);
anim->drop(); // And likewise, drop the animator when we're done referring to it. anim->drop(); // And likewise, drop the animator when we're done referring to it.
...@@ -165,7 +164,7 @@ int main() ...@@ -165,7 +164,7 @@ int main()
// Now I create three animated characters which we can pick, a dynamic light for // Now I create three animated characters which we can pick, a dynamic light for
// lighting them, and a billboard for drawing where we found an intersection. // lighting them, and a billboard for drawing where we found an intersection.
// First, let's get rid of the mouse cursor. We'll use a billboard to show // First, let's get rid of the mouse cursor. We'll use a billboard to show
// what we're looking at. // what we're looking at.
device->getCursorControl()->setVisible(false); device->getCursorControl()->setVisible(false);
...@@ -178,22 +177,22 @@ int main() ...@@ -178,22 +177,22 @@ int main()
bill->setSize(core::dimension2d<f32>(20.0f, 20.0f)); bill->setSize(core::dimension2d<f32>(20.0f, 20.0f));
bill->setID(ID_IsNotPickable); // This ensures that we don't accidentally ray-pick it bill->setID(ID_IsNotPickable); // This ensures that we don't accidentally ray-pick it
// Add 3 animated hominids, which we can pick using a ray-triangle intersection. /* Add 3 animated hominids, which we can pick using a ray-triangle intersection.
// They all animate quite slowly, to make it easier to see that accurate triangle They all animate quite slowly, to make it easier to see that accurate triangle
// selection is being performed. selection is being performed. */
scene::IAnimatedMeshSceneNode* node = 0; scene::IAnimatedMeshSceneNode* node = 0;
// Add an MD2 node, which uses vertex-based animation. // Add an MD2 node, which uses vertex-based animation.
node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/faerie.md2"), node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/faerie.md2"),
0, 0, IDFlag_IsPickable | IDFlag_IsHighlightable);
IDFlag_IsPickable | IDFlag_IsHighlightable); node->setPosition(core::vector3df(-70,-15,-120)); // Put its feet on the floor.
node->setPosition(core::vector3df(-70,-25,-120)); // Put its feet on the floor.
node->setScale(core::vector3df(2, 2, 2)); // Make it appear realistically scaled node->setScale(core::vector3df(2, 2, 2)); // Make it appear realistically scaled
node->setMD2Animation(scene::EMAT_RUN); node->setMD2Animation(scene::EMAT_POINT);
node->setAnimationSpeed(20.f); node->setAnimationSpeed(20.f);
video::SMaterial material; video::SMaterial material;
material.setTexture(0, driver->getTexture("../../media/faerie2.bmp")); material.setTexture(0, driver->getTexture("../../media/faerie2.bmp"));
material.Lighting = true; material.Lighting = true;
material.NormalizeNormals = true;
node->getMaterial(0) = material; node->getMaterial(0) = material;
// Now create a triangle selector for it. The selector will know that it // Now create a triangle selector for it. The selector will know that it
...@@ -204,8 +203,7 @@ int main() ...@@ -204,8 +203,7 @@ int main()
// This X files uses skeletal animation, but without skinning. // This X files uses skeletal animation, but without skinning.
node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/dwarf.x"), node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/dwarf.x"),
0, 0, IDFlag_IsPickable | IDFlag_IsHighlightable);
IDFlag_IsPickable | IDFlag_IsHighlightable);
node->setPosition(core::vector3df(-70,-66,0)); // Put its feet on the floor. node->setPosition(core::vector3df(-70,-66,0)); // Put its feet on the floor.
node->setRotation(core::vector3df(0,-90,0)); // And turn it towards the camera. node->setRotation(core::vector3df(0,-90,0)); // And turn it towards the camera.
node->setAnimationSpeed(20.f); node->setAnimationSpeed(20.f);
...@@ -215,12 +213,12 @@ int main() ...@@ -215,12 +213,12 @@ int main()
// And this B3D file uses skinned skeletal animation. // And this B3D file uses skinned skeletal animation.
node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/ninja.b3d"), node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/ninja.b3d"),
0, 0, IDFlag_IsPickable | IDFlag_IsHighlightable);
IDFlag_IsPickable | IDFlag_IsHighlightable);
node->setScale(core::vector3df(10, 10, 10)); node->setScale(core::vector3df(10, 10, 10));
node->setPosition(core::vector3df(-70,-66,-60)); node->setPosition(core::vector3df(-70,-66,-60));
node->setRotation(core::vector3df(0,90,0)); node->setRotation(core::vector3df(0,90,0));
node->setAnimationSpeed(10.f); node->setAnimationSpeed(10.f);
node->getMaterial(0).NormalizeNormals = true;
// Just do the same as we did above. // Just do the same as we did above.
selector = smgr->createTriangleSelector(node); selector = smgr->createTriangleSelector(node);
node->setTriangleSelector(selector); node->setTriangleSelector(selector);
...@@ -231,8 +229,7 @@ int main() ...@@ -231,8 +229,7 @@ int main()
// Add a light, so that the unselected nodes aren't completely dark. // Add a light, so that the unselected nodes aren't completely dark.
scene::ILightSceneNode * light = smgr->addLightSceneNode(0, core::vector3df(-60,100,400), scene::ILightSceneNode * light = smgr->addLightSceneNode(0, core::vector3df(-60,100,400),
video::SColorf(1.0f,1.0f,1.0f,1.0f), video::SColorf(1.0f,1.0f,1.0f,1.0f), 600.0f);
600.0f);
light->setID(ID_IsNotPickable); // Make it an invalid target for selection. light->setID(ID_IsNotPickable); // Make it an invalid target for selection.
// Remember which scene node is highlighted // Remember which scene node is highlighted
...@@ -253,7 +250,7 @@ int main() ...@@ -253,7 +250,7 @@ int main()
highlightedSceneNode = 0; highlightedSceneNode = 0;
} }
// All intersections in this example are done with a ray cast out from the camera to // All intersections in this example are done with a ray cast out from the camera to
// a distance of 1000. You can easily modify this to check (e.g.) a bullet // a distance of 1000. You can easily modify this to check (e.g.) a bullet
// trajectory or a sword's position, or create a ray from a mouse click position using // trajectory or a sword's position, or create a ray from a mouse click position using
// ISceneCollisionManager::getRayFromScreenCoordinates() // ISceneCollisionManager::getRayFromScreenCoordinates()
...@@ -262,42 +259,42 @@ int main() ...@@ -262,42 +259,42 @@ int main()
ray.end = ray.start + (camera->getTarget() - ray.start).normalize() * 1000.0f; ray.end = ray.start + (camera->getTarget() - ray.start).normalize() * 1000.0f;
// Tracks the current intersection point with the level or a mesh // Tracks the current intersection point with the level or a mesh
core::vector3df intersection; core::vector3df intersection;
// Used to show with triangle has been hit // Used to show with triangle has been hit
core::triangle3df hitTriangle; core::triangle3df hitTriangle;
// This call is all you need to perform ray/triangle collision on every scene node // This call is all you need to perform ray/triangle collision on every scene node
// that has a triangle selector, including the Quake level mesh. It finds the nearest // that has a triangle selector, including the Quake level mesh. It finds the nearest
// collision point/triangle, and returns the scene node containing that point. // collision point/triangle, and returns the scene node containing that point.
// Irrlicht provides other types of selection, including ray/triangle selector, // Irrlicht provides other types of selection, including ray/triangle selector,
// ray/box and ellipse/triangle selector, plus associated helpers. // ray/box and ellipse/triangle selector, plus associated helpers.
// See the methods of ISceneCollisionManager // See the methods of ISceneCollisionManager
scene::ISceneNode * selectedSceneNode = collMan->getSceneNodeAndCollisionPointFromRay( scene::ISceneNode * selectedSceneNode =
ray, collMan->getSceneNodeAndCollisionPointFromRay(
intersection, // This will be the position of the collision ray,
hitTriangle, // This will be the triangle hit in the collision intersection, // This will be the position of the collision
IDFlag_IsPickable, // This ensures that only nodes that we have hitTriangle, // This will be the triangle hit in the collision
// set up to be pickable are considered IDFlag_IsPickable, // This ensures that only nodes that we have
0 // Check the entire scene (this is actually the implicit default) // set up to be pickable are considered
); 0); // Check the entire scene (this is actually the implicit default)
// If the ray hit anything, move the billboard to the collision position and draw // If the ray hit anything, move the billboard to the collision position
// the triangle that was hit. // and draw the triangle that was hit.
if(selectedSceneNode) if(selectedSceneNode)
{ {
bill->setPosition(intersection); bill->setPosition(intersection);
// We need to reset the transform before doing our own rendering. // We need to reset the transform before doing our own rendering.
driver->setTransform(video::ETS_WORLD, core::matrix4()); driver->setTransform(video::ETS_WORLD, core::matrix4());
driver->setMaterial(material); driver->setMaterial(material);
driver->draw3DTriangle(hitTriangle, video::SColor(0,255,0,0)); driver->draw3DTriangle(hitTriangle, video::SColor(0,255,0,0));
// We can check the flags for the scene node that was hit to see if it should be // We can check the flags for the scene node that was hit to see if it should be
// highlighted. The animated nodes can be highlighted, but not the Quake level mesh // highlighted. The animated nodes can be highlighted, but not the Quake level mesh
if((selectedSceneNode->getID() & IDFlag_IsHighlightable) == IDFlag_IsHighlightable) if((selectedSceneNode->getID() & IDFlag_IsHighlightable) == IDFlag_IsHighlightable)
{ {
highlightedSceneNode = selectedSceneNode; highlightedSceneNode = selectedSceneNode;
// Highlighting in this case means turning lighting OFF for this node, // Highlighting in this case means turning lighting OFF for this node,
// which means that it will be drawn with full brightness. // which means that it will be drawn with full brightness.
highlightedSceneNode->setMaterialFlag(video::EMF_LIGHTING, false); highlightedSceneNode->setMaterialFlag(video::EMF_LIGHTING, false);
......
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