solid single color materials

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
morris
Posts: 36
Joined: Tue Jul 10, 2007 10:10 am

solid single color materials

Post by morris »

hi,

i've read all the topics on this one, but couldnt find a solution that satisfies my needs. i need a material that is just a plain color (say, red), with lighting enabled. there seem to be two solutions to this:

a) vertex coloring. this is not an option for me, because i don't want to copy a mesh for each color. in particular, i want to have "changing" colors in realtime, so i'd have a seperate mesh for each frame, which sucks.

b) using emissive color. this works to an extent, but if there's no light, then the mesh gets rendered in plain red. i want it to be black/invisible if there's no light.

c) using ambient/diffuse color. this does not work - the mesh is always white. highlights and shadows are as expected.

i've setup a small example to reproduce the issue:

Code: Select all

#include <irrlicht/irrlicht.h>

using namespace irr;
using namespace core;
using namespace video;
using namespace scene;

// color constants
const SColor Black(255,0,0,0);
const SColor White(255,255,255,255);
const SColor Red(255,255,0,0);
const SColor Green(255,0,255,0);

int main(int argc, char** argv) {
  // setup
  IrrlichtDevice* device = createDevice(EDT_OPENGL);
  IVideoDriver* driver = device->getVideoDriver();
  ISceneManager* scene = device->getSceneManager();
  
  // left cube
  ISceneNode* left = scene->addCubeSceneNode(15.0f);
  left->setPosition(vector3df(-20.0f, 0.0f, 0.0f));
  
  SMaterial& m_left = left->getMaterial(0);
  m_left.DiffuseColor = Green;
  m_left.AmbientColor = Green;
  m_left.MaterialType = EMT_SOLID;
  
  // right cube
  ISceneNode* right = scene->addCubeSceneNode(15.0f);
  right->setPosition(vector3df(20.0f, 0.0f, 0.0f));
  
  SMaterial& m_right = right->getMaterial(0);
  m_right.DiffuseColor = Black;
  m_right.AmbientColor = Black;
  m_right.SpecularColor = Black;
  m_right.EmissiveColor = Red;
  m_right.MaterialType = EMT_SOLID;
  
  // light
  ILightSceneNode* light = scene->addLightSceneNode(0, vector3df(0.0f, 50.0f, -50.0f), White, 50);
  
  // camera
  ICameraSceneNode* camera = scene->addCameraSceneNodeFPS();
  camera->setPosition(vector3df(0.0f, 0.0f, -30.0f));
  
  // device loop
  while(device->run()) {
    driver->beginScene(true, true, Black);
    scene->drawAll();
    driver->endScene();
  }
  
  return 0; 
}
the left cube is currently white but shows some "shadows" on itself. how can i make the left cube turn green?

edit: i'm testing with irrlicht 1.6
Brkopac
Posts: 88
Joined: Fri Sep 19, 2008 2:36 am

Post by Brkopac »

I accomplish this using textures. (Make sure you drop the Image's after you're done with them)

Code: Select all

video::IImage *blue = device->getVideoDriver()->createImage(video::ECF_A8R8G8B8, core::dimension2d<u32>(128, 128));
video::IImage *red = device->getVideoDriver()->createImage(video::ECF_A8R8G8B8, core::dimension2d<u32>(128, 128));
assert(blue != nullptr);
assert(red != nullptr);

blue->fill(video::SColor(130, 0, 0, 255));
red->fill(video::SColor(64, 255, 0, 0)

device->getVideoDriver()->addTexture("blue", blue);
device->getVideoDriver()->addTexture("red", red);
Then you can just set the material to whatever you want, in my case its a cube.

Code: Select all

sceneNode->getMaterial(0).setTexture(0, m_device->getVideoDriver()->getTexture("blue"));
sceneNode->getMaterial(0).setTexture(0, m_device->getVideoDriver()->getTexture("red"));
I understand this isn't exactly what you were looking for, but may help you.

Brkopac
Image - The glory days.
Post Reply