Adding a model breaks lighting in other models using OpenGL

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
drewbacca
Posts: 38
Joined: Tue Jan 30, 2007 6:49 pm

Adding a model breaks lighting in other models using OpenGL

Post by drewbacca »

I am having a problem where whenever I add a particular tree model to my scene, all my lighted nodes in the scene no longer display properly. Could they be losing their EMF_NORMALIZE_NORMALS flag setting? This only occurs in OpenGL, and only occurs with the particular model I linked to further down.

Here is a simplified sample showing two scaled models with lighting in OpenGL without any problems.
Image

Now when I add a third model of the tree, the lighting gets messed up in the first 2 models. It really obvious on the car, but you also tell that the faerie example model also becomes brighter.
Image

Under direct3d, this does not occur, however, the leaf texture is no longer transparent properly.
Image
I downloaded this model, so I don't know if the model is corrupt somehow. Though I'm more concerned with the effect on the other models of the scene, and don't know what could be causing it. The only thing I could think of other than some sorta state confusion is emmisive lighting, which is not present on any of the materials.

Here is the tree model: http://filebox.vt.edu/users/abacha/irr/oaktree.3DS
http://filebox.vt.edu/users/abacha/irr/BARK.PNG
http://filebox.vt.edu/users/abacha/irr/LEAF.PNG

and here is my code:

Code: Select all

#include <irrlicht.h>
#include <iostream>

using namespace irr;

#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif


void setDirectional(scene::ILightSceneNode *light)
{
	video::SLight lightData;
	lightData = light->getLightData();
	lightData.CastShadows=false;
	lightData.Type= video::ELT_DIRECTIONAL;
	light->setLightData(lightData);
}


void addLights(scene::ISceneManager* smgr)
{
	scene::ILightSceneNode * light;
	light = smgr->addLightSceneNode(0, core::vector3df(0,0,0), 
		video::SColorf(0.7f, 0.7f, 0.7f, 1.f), 2000.f, 0);
	setDirectional(light);
	light->setRotation(core::vector3df(40.f, 330.f, 0));
	light = smgr->addLightSceneNode(0, core::vector3df(0,0,0), 
		video::SColorf(0.7f, 0.7f, 0.7f, 1.f), 2000.f, 0);
	setDirectional(light);
	light->setRotation(core::vector3df(20.f, 210.f, 0.f));
	light = smgr->addLightSceneNode(0, core::vector3df(0,0,0), 
		video::SColorf(0.7f, 0.7f, 0.7f, 1.f), 2000.f, 0);
	setDirectional(light);
	light->setRotation(core::vector3df(20.f, 90.f, 0.f));
}


int main()
{
	IrrlichtDevice* device = createDevice( video::EDT_OPENGL, core::dimension2d<s32>(640, 480),
		16, false, false, false);

	if (device == 0)
		return 1; // could not create selected driver.

	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();


	scene::IAnimatedMesh* carMesh = smgr->getMesh("../../media/Honda_Element.obj");
	scene::ISceneNode* carNode = smgr->addMeshSceneNode(carMesh);	

	if (carMesh)
	{
		carNode->setMaterialFlag(video::EMF_LIGHTING, true);
		carNode->setScale(core::vector3df(20.f, 20.f, 20.f));
		carNode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
	}


	/// Adding this block of code seems to undo the normalize normals for other nodes
	scene::IAnimatedMesh* treeMesh = smgr->getMesh("../../media/oaktree.3ds");
	scene::ISceneNode* treeNode = smgr->addMeshSceneNode(treeMesh);	

	if (treeNode)
	{
		treeNode->setPosition(core::vector3df(0,0,20));
		treeNode->setMaterialFlag(video::EMF_LIGHTING, false);
		treeNode->getMaterial(1).MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
		treeNode->setScale(core::vector3df(0.2f, 0.2f, 0.2f));
	}


	scene::IAnimatedMesh* otherMesh = smgr->getMesh("../../media/faerie.md2");
	scene::IAnimatedMeshSceneNode* otherNode = smgr->addAnimatedMeshSceneNode(otherMesh);	

	if (otherNode)
	{
		otherNode->setMaterialTexture(0, driver->getTexture("../../media/faerie2.bmp"));
		otherNode->setPosition(core::vector3df(0,0,10));
		otherNode->setMaterialFlag(video::EMF_LIGHTING, true);
		otherNode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
		otherNode->setScale(core::vector3df(0.1f, 0.1f, 0.1f));
		otherNode->setFrameLoop(160, 183);
	}





	/*
	To be able to look at and move around in this scene, we create a first
	person shooter style camera and make the mouse cursor invisible.
	*/
	scene::ICameraSceneNode * cam = smgr->addCameraSceneNodeFPS(0, 20.0f, 20.0f);
	device->getCursorControl()->setVisible(false);




	addLights(smgr);


	while(device->run())
	{
		driver->beginScene(true, true, video::SColor(255,113,113,133));

		smgr->drawAll(); // draw the 3d scene
		device->getGUIEnvironment()->drawAll(); // draw the gui environment (the logo)

		driver->endScene();

		int fps = driver->getFPS();
	}

	/*
	In the end, delete the Irrlicht device.
	*/
	device->drop();
	
	return 0;
}

drewbacca
Posts: 38
Joined: Tue Jan 30, 2007 6:49 pm

Post by drewbacca »

After more testing, I think it is something not being properly set in the opengl driver. If you change the position of the tree to treeNode->setPosition(core::vector3df(10000,0,10000));
the lighting on the other models goes back to normal.

Also, I get this behavior using the latest version in svn, and it's been happening since at least rev1630, but probably earlier as well.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Is the tree still in view? If changing the position of the tree as you've done above moves it out of the view frustum, then it won't be rendered, and it won't modify the driver render states.

Travis
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

this are the results I get with your code and meshes (of course another car mesh though):

OpenGL:
Image

DX9:
Image

I'm using Irrlicht v1.4.2 with MinGW/GCC compiler...
my system is WinXP-pro (sp2) with a GeForce FX5200 gfx card...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
drewbacca
Posts: 38
Joined: Tue Jan 30, 2007 6:49 pm

Post by drewbacca »

Thanks for checking it out. I also should have clarified, that I don't think the problem was in 1.4.2 or before.

The problem is definitely triggered by the tree entering the camera view. If you change the tree's position to treeNode->setPosition(core::vector3df(0,0,-50));
The lighting starts out correct. When you turn around, the lighting breaks and stays that way, even if the tree leaves view. As you turn around with the fps camera, the effect also seems to happen well before the bounding box of the tree actually enters view.
Post Reply