How to change colour of manual object in 2.1 Topic is solved

Discussion area about developing with Ogre-Next (2.1, 2.2 and beyond)


Post Reply
Jay721
Halfling
Posts: 62
Joined: Mon Jan 29, 2018 8:19 am

How to change colour of manual object in 2.1

Post by Jay721 »

Hello everyone. (sorry if i've put this in the wrong place)

I’ve just started with Ogre. I'm currently creating a cube with a manual object and attaching it to a scene noded. I need to change the colour of this with code but have no idea how. Any help would be nice as I don’t know where to start!

Is this even possible with 2.1, as all the examples I'm looking at are using "getMaterial" which no longer exists?

Thanks in advance.
Lax
Hobgoblin
Posts: 583
Joined: Mon Aug 06, 2007 12:53 pm
Location: Saarland, Germany
x 50

Re: How to change colour of manual object in 2.1

Post by Lax »

Hi,

you create your manual object and set the color via datablock like this:

Code: Select all

this->translationLine->clear();
this->translationLine->begin("redLine", Ogre::OperationType::OT_LINE_LIST);
		
this->translationLine->position(startPosition);
this->translationLine->position(endPosition);
this->translationLine->line(0, 1);
this->translationLine->end();

YourMaterial.material:
hlms redLine unlit
{
	diffuse 1 0 0 1
}
Or you can set it directly by calling:

Code: Select all

this->translationLine->colour(Ogre::ColourValue(1.0f, 0.0f, 0.0f, 1.0f));
Regards
Lax

http://www.lukas-kalinowski.com/Homepage/?page_id=1631
Please support Second Earth Technic Base built of Lego bricks for Lego ideas: https://ideas.lego.com/projects/81b9bd1 ... b97b79be62

Jay721
Halfling
Posts: 62
Joined: Mon Jan 29, 2018 8:19 am

Re: How to change colour of manual object in 2.1

Post by Jay721 »

Hi, thanks for your reply!

I was able to change the colour of the object with a datablock but I need to be able to change it through code. Whenever I uncomment the call to colour, the cubes kinda break.

Code: Select all

Ogre::ManualObject* cube = test->createManualObject();
	cube->begin("");
	
	//cube->colour(Ogre::ColourValue(1.0f, 0.0f, 0.0f, 1.0f));
	cube->position(0.5, -0.5, 1.0); cube->normal(0.408248, -0.816497, 0.408248); cube->textureCoord(1, 0);
	cube->position(-0.5, -0.5, 0.0); cube->normal(-0.408248, -0.816497, -0.408248); cube->textureCoord(0, 1);
	cube->position(0.5, -0.5, 0.0); cube->normal(0.666667, -0.333333, -0.666667); cube->textureCoord(1, 1);
	cube->position(-0.5, -0.5, 1.0); cube->normal(-0.666667, -0.333333, 0.666667); cube->textureCoord(0, 0);
	cube->position(0.5, 0.5, 1.0); cube->normal(0.666667, 0.333333, 0.666667); cube->textureCoord(1, 0);
	cube->position(-0.5, -0.5, 1.0); cube->normal(-0.666667, -0.333333, 0.666667); cube->textureCoord(0, 1);
	cube->position(0.5, -0.5, 1.0); cube->normal(0.408248, -0.816497, 0.408248); cube->textureCoord(1, 1);
	cube->position(-0.5, 0.5, 1.0); cube->normal(-0.408248, 0.816497, 0.408248); cube->textureCoord(0, 0);
	cube->position(-0.5, 0.5, 0.0); cube->normal(-0.666667, 0.333333, -0.666667); cube->textureCoord(0, 1);
	cube->position(-0.5, -0.5, 0.0); cube->normal(-0.408248, -0.816497, -0.408248); cube->textureCoord(1, 1);
	cube->position(-0.5, -0.5, 1.0); cube->normal(-0.666667, -0.333333, 0.666667); cube->textureCoord(1, 0);
	cube->position(0.5, -0.5, 0.0); cube->normal(0.666667, -0.333333, -0.666667); cube->textureCoord(0, 1);
	cube->position(0.5, 0.5, 0.0); cube->normal(0.408248, 0.816497, -0.408248); cube->textureCoord(1, 1);
	cube->position(0.5, -0.5, 1.0); cube->normal(0.408248, -0.816497, 0.408248); cube->textureCoord(0, 0);
	cube->position(0.5, -0.5, 0.0); cube->normal(0.666667, -0.333333, -0.666667); cube->textureCoord(1, 0);
	cube->position(-0.5, -0.5, 0.0); cube->normal(-0.408248, -0.816497, -0.408248); cube->textureCoord(0, 0);
	cube->position(-0.5, 0.5, 1.0); cube->normal(-0.408248, 0.816497, 0.408248); cube->textureCoord(1, 0);
	cube->position(0.5, 0.5, 0.0); cube->normal(0.408248, 0.816497, -0.408248); cube->textureCoord(0, 1);
	cube->position(-0.5, 0.5, 0.0); cube->normal(-0.666667, 0.333333, -0.666667); cube->textureCoord(1, 1);
	cube->position(0.5, 0.5, 1.0); cube->normal(0.666667, 0.333333, 0.666667); cube->textureCoord(0, 0);

	cube->triangle(0, 1, 2);		cube->triangle(3, 1, 0);
	cube->triangle(4, 5, 6);		cube->triangle(4, 7, 5);
	cube->triangle(8, 9, 10);		cube->triangle(10, 7, 8);
	cube->triangle(4, 11, 12);	cube->triangle(4, 13, 11);
	cube->triangle(14, 8, 12);	cube->triangle(14, 15, 8);
	cube->triangle(16, 17, 18);	cube->triangle(16, 19, 17);
	
	cube->end();
I'm sorry for being so clueless, this is what happens:
Image

Image
al2950
OGRE Expert User
OGRE Expert User
Posts: 1227
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK
x 157

Re: How to change colour of manual object in 2.1

Post by al2950 »

The reason is the way ManualObject declares vertex elements, and then the way you are using them. I would explain how to get it working with ManualObject, HOWEVER, I am going to be cruel to be nice! ManualObject is bad, and is simply there to make life porting from Ogre V1.x to 2.x slightly easier. However if you use it you will likely hit a number of problems that will have you screaming at your computer.

So I highly recommend you look at the Sample_DynamicGeomerty, and more specifically 'DynamicGeometryGameState::createStaticMesh'. It is every so slightly higher learning curve, but at least you know what its doing and how to do more complicated things.
Jay721
Halfling
Posts: 62
Joined: Mon Jan 29, 2018 8:19 am

Re: How to change colour of manual object in 2.1

Post by Jay721 »

Alright, thanks for your advice. I've used the code from that sample to make cubes instead of the manual objects. I still have no clue how to change the colour of them with C++?
al2950
OGRE Expert User
OGRE Expert User
Posts: 1227
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK
x 157

Re: How to change colour of manual object in 2.1

Post by al2950 »

Well it sort of depends, do want to dynamically change the colour, or do you just want to create an object with a colour and never change it?
Jay721
Halfling
Posts: 62
Joined: Mon Jan 29, 2018 8:19 am

Re: How to change colour of manual object in 2.1

Post by Jay721 »

I need to change the colour dynamically.
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5296
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: How to change colour of manual object in 2.1

Post by dark_sylinc »

A couple things:

Per vertex colour will only work with Unlit, not with PBS.
When using per vertex colour, you need to call cube->colour(...) for every vertex, not just once as if it affected the entire object, that's why uncommenting your line glitches everything.

If only you're changing the cube's colour (not per vertex) then you can do that via:

Code: Select all

unlitDatablock->setUseColour( true ); //You only need to call this once
unlitDatablock->setColour( your_colour );
It also works for PBS datablocks:

Code: Select all

pbsDatablock->setDiffuse( your_colour );
//Or alternatively this one, if you're using detail maps
//pbsDatablock->setBackgroundDiffuse( your_colour );
Note that this will affect all the cubes using that same material (aka datablock). If you want to control it per cube, then you will have to clone the datablocks and have each cube have its own clone.

On how to get the datablocks pointers, just see the PbsMaterials sample (Samples/2.0/Showcase/PbsMaterials/PbsMaterialsGameState.cpp). Getting/creating an HlmsUnlitDatablock is the same as HlmsPbsDatablock except the names change (where it says Pbs, you just type Unlit)
Post Reply