[osg-users] Color in ShapeDrawable

373 views
Skip to first unread message

Anders Backman

unread,
Dec 1, 2011, 10:52:23 AM12/1/11
to OpenSceneGraph Users

Hi.

What is the idea behind having a color in ShapeDrawable?

What this means, is that if you create a ShapeDrawable,
and stitch a material in a node above it, you cannot control the color through a material when lighting is disabled.

So if I would like to do:

material = new osg::Material;
  stateset->setAttributeAndModes(material,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
 stateset->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF|osg::StateAttribute::PROTECTED);
  
  material->setColorMode(osg::Material::OFF); // Diffuse color == glColor(diffuse);

Then control the color of this node by changing the diffuse color:

  material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0,0.0f,0.0f,1.0f));


It would render in white, due to that the ShapeDrawable applies a white color!
Only way of changing this, would be to call ShapeDrawable::setColor()


Is there another way of achieving this?
(Except for skipping the use of ShapeDrawables completely).

/Anders
--
__________________________________________
Anders Backman, HPC2N
90187 Umeå University, Sweden
and...@cs.umu.se http://www.hpc2n.umu.se
Cell: +46-70-392 64 67

Filip Arlet

unread,
Dec 2, 2011, 3:53:46 AM12/2/11
to osg-...@lists.openscenegraph.org
Hi,

in OpenGL if GL_LIGHTING is disabled, the final color of polygon is determined by glColor.

If you see ShapeDrawable::drawImplemenation(), there is glColor call.


Code:

osg::State& state = *renderInfo.getState();
GLBeginEndAdapter& gl = state.getGLBeginEndAdapter();

if (_shape.valid())
{
gl.Color4fv(_color.ptr()); ///// <---------

DrawShapeVisitor dsv(state,_tessellationHints.get());

_shape->accept(dsv);
}


Cheers,
Filip[/code]

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=44181#44181

_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Anders Backman

unread,
Dec 2, 2011, 4:40:00 AM12/2/11
to osg-...@lists.openscenegraph.org
Yes, which is what I wrote:

"What is the idea behind having a color in ShapeDrawable?"

I cant see any reason why there would be a color associated to a shape?
Create a triangle, and by the way, its red. That was my question.

So if you WOULD like to control the color by disabling light and use diffuse, it will not work.
It will be white (due to the call in ShapeDrawable).

Anyway, there are ways around this. I just thought it was very inconsistent to associate a color to a ShapeDrawable.

/Anders

Jason Daly

unread,
Dec 2, 2011, 2:30:38 PM12/2/11
to osg-...@lists.openscenegraph.org



On 12/02/2011 04:40 AM, Anders Backman wrote:
Yes, which is what I wrote:

"What is the idea behind having a color in ShapeDrawable?"

I cant see any reason why there would be a color associated to a shape?
Create a triangle, and by the way, its red. That was my question.

So if you WOULD like to control the color by disabling light and use diffuse, it will not work.
It will be white (due to the call in ShapeDrawable).


I don't understand what you mean here.  If you disable lighting, material colors are irrelevant.  The ShapeDrawable's colors are the ONLY way to set the color.



Anyway, there are ways around this. I just thought it was very inconsistent to associate a color to a ShapeDrawable.



I'm not sure what's inconsistent about it.  It behaves like any other OpenGL geometry.

As Filip indicated, the color only applies if lighting is disabled.  When you disable lighting, no lighting calculations are done, and material colors become irrelevant, just as they do with any OpenGL rendering when lighting is disabled.  Only the geometry color is used.  If you want an unlit red cube, this is how you'd do it.


When lighting is enabled, the material can be set to either ignore the geometry colors:

material->setColorMode(osg::Material::OFF);

or to use the geometry colors as one of the material colors:

material->setColorMode(osg::Material::DIFFUSE);  (for example)

It's your choice how you want to apply the material.

--"J"

Farshid Lashkari

unread,
Dec 2, 2011, 3:18:52 PM12/2/11
to OpenSceneGraph Users
Hi Jason,

On Fri, Dec 2, 2011 at 11:30 AM, Jason Daly <jd...@ist.ucf.edu> wrote:
I don't understand what you mean here.  If you disable lighting, material colors are irrelevant.  The ShapeDrawable's colors are the ONLY way to set the color.

That's actually not the case with OSG. If you look at the source for osg::Material, you will notice it calls glColor with one of the material colors (depending on the color mode). This means you can use osg::Material to control the color of geometry even when lighting is disabled.

If ShapeDrawable did not call glColor, it would be possible to use osg::Material to control the color with lighting disabled. At the very least, I think there should be an option to have ShapeDrawable inherit the color. I don't see anything wrong with having such an option and I'm sure Robert would accept a patch for it.

Cheers,
Farshid




I'm not sure what's inconsistent about it.  It behaves like any other OpenGL geometry.

As Filip indicated, the color only applies if lighting is disabled.  When you disable lighting, no lighting calculations are done, and material colors become irrelevant, just as they do with any OpenGL rendering when lighting is disabled.  Only the geometry color is used.  If you want an unlit red cube, this is how you'd do it.


When lighting is enabled, the material can be set to either ignore the geometry colors:

material->setColorMode(osg::Material::OFF);

or to use the geometry colors as one of the material colors:

material->setColorMode(osg::Material::DIFFUSE);  (for example)

It's your choice how you want to apply the material.

--"J"


Jason Daly

unread,
Dec 2, 2011, 6:18:34 PM12/2/11
to OpenSceneGraph Users
On 12/02/2011 03:18 PM, Farshid Lashkari wrote:
Hi Jason,

On Fri, Dec 2, 2011 at 11:30 AM, Jason Daly <jd...@ist.ucf.edu> wrote:
I don't understand what you mean here.  If you disable lighting, material colors are irrelevant.  The ShapeDrawable's colors are the ONLY way to set the color.

That's actually not the case with OSG. If you look at the source for osg::Material, you will notice it calls glColor with one of the material colors (depending on the color mode). This means you can use osg::Material to control the color of geometry even when lighting is disabled.

My mistake, I didn't realize that.  I guess I've never hit that use case before.

--"J"

Anders Backman

unread,
Dec 4, 2011, 3:08:15 PM12/4/11
to OpenSceneGraph Users
Farshid: head on the nail! :-)

Hence, it makes no sense storing a color in ShapeDrawable.

Actually, Robert, I think you implemented Shape and ShapeDrawable when you were working here at VRlab in Umeå, right? ;-)

Cheers,

/A

_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Jean-Sébastien Guay

unread,
Dec 4, 2011, 8:50:27 PM12/4/11
to OpenSceneGraph Users
Hi Anders,

> Hence, it makes no sense storing a color in ShapeDrawable.

Well, it makes as much sense as having a color array for an
osg::Geometry... :-)

I think it's not having a color in ShapeDrawable that's the problem,
it's having no way of having no color. In other words, in osg::Geometry
you have the ability to either set a color array or not. You should have
the same ability in ShapeDrawable, i.e. set a color or not. Since
osg::Color doesn't really have a "no color" value, you probably need an
additional bool saying "use the color or not". When _useColor == false,
then no glColor call would be made by ShapeDrawable itself, and the one
from osg::Material would take effect.

Or you could just do what Robert always says, ignore ShapeDrawable even
exists (except for simple debug geometry) and use osg::Geometry
directly. Then you can do what you want.

I've wanted to rewrite ShapeDrawable to use osg::Geometry internally for
a while, but I think Paul beat me to it in osgWorks... :-)

J-S
--
______________________________________________________
Jean-Sebastien Guay jean-seba...@cm-labs.com
http://www.cm-labs.com/
http://whitestar02.dyndns-web.com/

Paul Martz

unread,
Dec 4, 2011, 11:15:40 PM12/4/11
to OpenSceneGraph Users
On 12/4/2011 6:50 PM, Jean-Sébastien Guay wrote:
> I've wanted to rewrite ShapeDrawable to use osg::Geometry internally for a
> while, but I think Paul beat me to it in osgWorks... :-)

Well, I've made a start. Occasionally I even find time to add to it. Submissions
are welcome. :-)

In the context of the rest of this discussion, I'll mention that, some time ago,
we all discussed that osg::Material is more than just a wrapper around
glMaterial. It also owns glColorMaterial, and this actually makes it a little
difficult to enable/disable GL_COLOR_MATERIAL using StateSet::setMode() the way
you can with other OpenGL state. In the same vein, it seems that osg::Material
also wants to own the OpenGL primary color state (set with glColor). While it
might be a fun exercise to go back, rethink all this, and maybe implement a
substitute scheme, honestly I'd rather just use shaders...

--
-Paul Martz Skew Matrix Software
http://www.skew-matrix.com/

Reply all
Reply to author
Forward
0 new messages