Why my sensor has no color?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
mybiandou
Posts: 32
Joined: Thu Sep 10, 2009 6:20 am

Why my sensor has no color?

Post by mybiandou »

I want to make a sensor, which has a small cylinder and a long pin in the front.

I want to set the color according to the member variable isON is ture/false.

But I found it can not make any effects whatever I change the color.

Because in my scene I have many other bodies which need not lights source.
So in this case, how can I make my sensor can be displayed with certain color.(Wireframe mode or solid mode)

The following is my code. It should can be run with Irrlicht 1.6

Thank you in advance.

Code: Select all


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

using namespace irr;

using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;


#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif

#define RAY_LINE_OFF_COLOUR irr::video::SColor(255, 255, 0, 0)
#define RAY_LINE_ON_COLOUR irr::video::SColor(255, 0, 255, 0)

// needs to know the vehicle's transformation
class CRaySensor :public irr::scene::ISceneNode
{
private:
	irr::scene::ISceneManager *smgr;
	irr::scene::ISceneNode *parent;
	irr::core::line3df ray;
	irr::core::aabbox3d<irr::f32> Box;
	irr::video::SMaterial Material;
	std::string objName;
	float cyR,cyL;  // the cylinder's length and radius
	irr::scene::ISceneNode *child;
public:
	bool isON;	
public:
	CRaySensor(std::string name, irr::core::vector3df pos, irr::core::vector3df dir, float length, float cyR, float cyL, 
		irr::scene::ISceneManager *mgr,irr::scene::ISceneNode* parent, irr::s32 id) ;
	void setObjectName(const char* name){ this->objName=name; }
	std::string getObjectName(void) const {return objName;}
	virtual void OnRegisterSceneNode()
	{
		if(IsVisible)
			SceneManager->registerNodeForRendering(this);
		ISceneNode::OnRegisterSceneNode();
	}
	virtual void render();
	virtual const irr::core::aabbox3d<irr::f32>& getBoundingBox() const {return Box;};
	virtual irr::u32 getMaterialCount() const {return 1;}
	virtual irr::video::SMaterial& getMaterial(irr::u32){return Material;}

};

CRaySensor::CRaySensor(std::string name, irr::core::vector3df pos, irr::core::vector3df dir, float length, float cyR, float cyL, irr::scene::ISceneManager *mgr,irr::scene::ISceneNode* parent, irr::s32 id) 
: irr::scene::ISceneNode(parent,mgr,id)
{
	const float PI=3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628f;	
	this->objName=name;
	this->smgr=mgr;
	this->parent = parent;
	ray.setLine(pos, pos+(dir.normalize())*length);
	this->cyR=cyR;
	this->cyL=cyL;
	isON=false;

	irr::scene::IMesh *submesh;	
	irr::core::matrix4 mat,mat1,mat2;
	submesh=smgr->getGeometryCreator()->createCylinderMesh(cyR,cyL,10);
	submesh->setMaterialFlag(irr::video::EMF_WIREFRAME,true);	

	// Initial rotation quaternion 
	irr::core::quaternion quat ;
	quat.rotationFromTo(irr::core::vector3df(0,1,0),dir);
	mat1=quat.getMatrix();	
	
	//transformation from 0,0,0 to x,y,z
	mat2.setTranslation(pos);	
	//the multiply order  is very important
	mat=mat2*mat1;	
	//transformation
	irr::scene::IMeshManipulator *manipulator = smgr->getMeshManipulator();
	manipulator->transform(submesh,mat);	

	child=smgr->addMeshSceneNode(submesh,parent);	

	child->setMaterialFlag(irr::video::EMF_LIGHTING,true);

	this->Box.reset(0.0f,0.0f,0.0f);
	this->Box.addInternalBox(child->getBoundingBox());
	this->Box.addInternalPoint(ray.start);
	this->Box.addInternalPoint(ray.end);		
}

void CRaySensor::render()
{
	
	child->render();
	if(isON)
		smgr->getVideoDriver()->draw3DLine(ray.start,ray.end,RAY_LINE_ON_COLOUR);
	else
		smgr->getVideoDriver()->draw3DLine(ray.start,ray.end,RAY_LINE_OFF_COLOUR);
}


int main(int argc, char *argv[])
{
    
  IrrlichtDevice *device=createDevice(video::EDT_OPENGL,core::dimension2d<irr::u32>(800, 600),
		16, false, false, false,NULL);
		
  if (device == 0) 
  {
     std::cout<<"Error:Irrlicht device can not be created."<<std::endl;
     system("pause");
     return 1; 
  }
  

  device->setWindowCaption(L"Tester");    
    
  video::IVideoDriver* driver = device->getVideoDriver();
  IGUIEnvironment* env = device->getGUIEnvironment();
  scene::ISceneManager* smgr = device->getSceneManager();
  

  smgr->addCameraSceneNodeMaya();
  
  CRaySensor *sensor1=new CRaySensor("s1",core::vector3df(0.0,0.0,100.0),
                                          core::vector3df(0.0,0.0,-1.0),
                                          200,50,50,
                                          smgr,smgr->getRootSceneNode(),-1);
  
  while(device->run()&&driver)
  {
    driver->beginScene(true, true, video::SColor(0,255,255,255));
	smgr->drawAll();
	env->drawAll();

	driver->endScene();
   
  }

//release the resource
  device->drop();
  


  return 0;    
   
}    
Image
mybiandou
Posts: 32
Joined: Thu Sep 10, 2009 6:20 am

Post by mybiandou »

Sorry, I have found the answer in this forum.

Code: Select all

	child->setMaterialFlag(irr::video::EMF_LIGHTING,true);		
	setMaterialFlag(irr::video::EMF_LIGHTING,false);	
    getMaterial(0).EmissiveColor=RAY_LINE_OFF_COLOUR;
    child->getMaterial(0).EmissiveColor=RAY_LINE_OFF_COLOUR;
One more point should be noticed,
The light of material should be turned off between draw any 2D/3D line

Code: Select all


			//set material light off between draw line
			SMaterial m; m.Lighting = false;
			smgr->getVideoDriver()->setMaterial(m); 
			smgr->getVideoDriver()->draw3DLine(ray.start,detected_point,RAY_LINE_ON_COLOUR);
Post Reply