Hi Tensor,

On Tue, 2004-02-10 at 03:11, Tensor G wrote:
> hello everyone!
> 
> i have some little problem understanding the use of colors in opensg, so 
> i'll put u several stupid questions, just to get compfy with this stuff...
> I want to assign color values per vertex, so i retrive the meshes 
> informations from the scene graph. Then i
> triangleiterate throug the meshes and vertex navigate throug the single 
> trinagles...

So far, so well. ;)

> my code looks like:
> 
> for every mesh
>     for every triangle
>            for every vertex
>                  {
>                          getvertexinfo (position and normal)
>                          calculate_color(color_per_vertex)
>                          push_back_color(color_per_vertex)
>                  }
> 
> 
> and then befeore any rendering take place:
> 
> for every mesh
>        mesh->set_color(color_per_vertex)
> 
> but it doesn't work. Now i am thinking that i am wrong parsing the whole 
> stuff: i put in the set_color function an explicit color array, with an 
> entry for every indexed vertex... now maybe i should simply calculate one 
> color for any NON-indexed vertex...
> 
> in numbers: if i had a stupid cube with 8 vertices i'd have 36 indices (6 
> faces => 12 triangles, 3 vertices per triangle). Now during the color 
> assigning, how many colors will i have to pass? 8, one for every vertex, or 
> 36, one for every INDEXED-vertex????
> I am confused....

You probably won't get 36, because the cube will probably not replicate
the vertices shared by the two triangles of a side, so I'd more expect
24 (6*4). But in general the question is what you need. If you can live
with using the same index for vertex and color, things are pretty
simple.

> next some pratical questions: how can i know how many triangles and vertices 
> are there intoa  geoNode?? is there a geoptr->getTriangleNumber()  - 
> geoptr->getVertexNumber() method???

There is a calcPrimitiveCount() in OSGGeoFunctions.h. But you can also
get the info from the properties.

> If i have to pass a color just for the single color (8 colors for the cube) 
> i'd need to know how many vertices i have to create the right amount of 
> space in my geoColors3f array....

Yup, you should.

> how can i index my color array in the right way? AKA same index for vertex 
> array and color array?

If you're really find with that, it's pretty simple. You just have to
create a Color property with the same size as your vertex property.

The following code snippet does that:

Action::ResultE quitGeo(NodePtr& node)
{   
    SLOG << "entering " << node << endLog;

    GeometryPtr geo = GeometryPtr::dcast(node->getCore());
    
    if(geo!=NullFC)
    {
        // Add a vertex-unique color to the geometry
        
        GeoPositionsPtr pos = geo->getPositions();
        
        // Create a new Colors property and resize it to have as many 
        // elements as the positions property.
        GeoColors4fPtr colors = GeoColors4f::create();
        beginEditCP(colors, GeoColors4f::GeoPropDataFieldMask);
        
        colors->resize(pos->size());
               
        // Traverse the geometry, set the color to the position just for
        // testing.
        
        for(TriangleIterator it = geo->beginTriangles(); 
            it != geo->endTriangles(); ++it)
        {
            colors->getFieldPtr()->setValue(
                Color4f( it.getPosition(0)[0], 
                         it.getPosition(0)[1], 
                         it.getPosition(0)[2], 
                         1.f), 
                it.getPositionIndex(0));

            colors->getFieldPtr()->setValue(
                Color4f( it.getPosition(1)[0], 
                         it.getPosition(1)[1], 
                         it.getPosition(1)[2], 
                         1.f), 
                it.getPositionIndex(1));

            colors->getFieldPtr()->setValue(
                Color4f( it.getPosition(2)[0], 
                         it.getPosition(2)[1], 
                         it.getPosition(2)[2], 
                         1.f), 
                it.getPositionIndex(2));
        }
        
        endEditCP  (colors, GeoColors4f::GeoPropDataFieldMask);
       
       
        // Change the geometry to use the new colors
        beginEditCP(geo, Geometry::ColorsFieldMask);
        
        geo->setColors(colors);
        
        // If multi-indexed, make the colors use the same index as
        // the geometry
        if(geo->getIndexMapping().size() > 0)
        {
            Int16 pind = geo->calcMappingIndex(Geometry::MapPosition);
            
            if(pind < 0)
            {
                FFATAL(("Multi-indexed, but no positions index???\n"));
                return Action::Continue; 
            }
            
            // This makes the colors use the same indices as the
positions
            geo->getIndexMapping()[pind] |= Geometry::MapColor;
        }
        
        endEditCP  (geo, Geometry::ColorsFieldMask);
        
        // Check to make sure that the used material actually uses the 
        // vertex colors        
        MaterialPtr mat = geo->getMaterial();
        SimpleMaterialPtr smat = SimpleMaterialPtr::dcast(mat);
        
        if(smat != NullFC)
        {
            beginEditCP(smat, SimpleMaterial::ColorMaterialFieldMask);
            smat->setColorMaterial(GL_AMBIENT_AND_DIFFUSE);
            endEditCP(smat, SimpleMaterial::ColorMaterialFieldMask);
        }
        else
        {
            FWARNING(("Material is not simple, can't guarantee "
                      "color material!\n"));
        }
    }   
    return Action::Continue; 
}

Hope it helps

        Dirk




-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now.
Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to