[osg-users] Draw vertex as Circle with color given by some Vec4 array

568 views
Skip to first unread message

Bruno Oliveira

unread,
Jan 2, 2017, 2:21:18 PM1/2/17
to OpenSceneGraph Users
Hello,


I have an array of 3d points that I want to render in a scene. I want to render them as circles, centered on each point coordinates, with a given radius and a color that is given by a color array I have in memory.

For now, I am creating a node for each point, that consists of a PrimitiveSet (polygon) that I iterate around the point center. (Check code below).


Now I know this can be done with Shaders. I can draw the circle directly in the fragment shader with this example (http://www.geeks3d.com/20130705/shader-library-circle-disc-fake-sphere-in-glsl-opengl-glslhacker/).

But I need to pass the vertex centers and respective colors. Is it possible to pass a osg::Vec3Array* with vertex centers and a osg::Vec4Array* of colors directly to a Shader, so that I can , in a single compiled Shader program, handle all point positions and colors?

Here is teh code for drawing the circle, I use this for each vertex:

osg::Vec3Array* getArray(const float radius, const int points, const osg::Vec3& center) {
    auto array = new osg::Vec3Array();
    const float ratio = (float(2.0*osg::PI) / float(points));
    for (int i = 0; i < points; i++) {
        const float angle = float(i) * ratio;
        array->push_back(osg::Vec3(
                    center.x() + radius*cosf( angle ),
                    center.y() + radius*sinf( angle ),
                    center.z())
                );
    }
    return array;
}

osg::Geometry* getGeometry(const float radius, const int points, const osg::Vec3& center) {  
    osg::Geometry* geom = new osg::Geometry();

    geom->setVertexArray(getArray(radius, points, center));
    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON, 0, points));

    return geom;
}

Trajce Nikolov NICK

unread,
Jan 2, 2017, 3:56:24 PM1/2/17
to OpenSceneGraph Users
Hi Bruno,

yes, you can pass VertexAttributes to the program and look them up in the shader. You can pass the centers as Vec3Array, Vec4Array for colors and probably encode the radius into float array or the vertex.w() from the vertex array.

Do a search through the examples for how to set up your program with VertexAttributes ... Ping me if you if troubles, I might be able to give some snippets 

Cheers,
Nick

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




--
trajce nikolov nick

Bruno Oliveira

unread,
Jan 2, 2017, 5:10:07 PM1/2/17
to OpenSceneGraph Users
Hi Nick, 
thanks for your answer. I read the osgvertexattributes.cpp example and did not quite get it at first. Do you know if there are any further documentation or snippets I could read?

Or maybe if you have quick snippets it could spare me some valuable time. 

2017-01-02 20:56 GMT+00:00 Trajce Nikolov NICK <trajce.ni...@gmail.com>:
Hi Bruno,

yes, you can pass VertexAttributes to the program and look them up in the shader. You can pass the centers as Vec3Array, Vec4Array for colors and probably encode the radius into float array or the vertex.w() from the vertex array.

Do a search through the examples for how to set up your program with VertexAttributes ... Ping me if you if troubles, I might be able to give some snippets 

Cheers,
Nick

Trajce Nikolov NICK

unread,
Jan 3, 2017, 5:10:57 PM1/3/17
to OpenSceneGraph Users
Hi Bruno,

As I am recalling how this is to be done is something like this:

Let say you want to pass colors as vertex attributes:

const size_t vertexAttribLocation = 5;

osg::Vec4Array* colors = new ...
osg::Geometry* geometry = new ...

geometry->setVertexAttribArray(vertexAttribLocation ,colors,osg::Array::BIND_PER_VERTEX);

osg::Program* program = new ...
program->addBindAttribLocation("colors", vertexAttribLocation );

and then in your GLSL you just call it by the name "colors"

#version 120
attribute vec4 colors;
void main(void) ....

you do the same for the radius and you may do it for the vertices itself although based on your GLSL version you can use gl_Vertex too

Hope this helps
Nick




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




--
trajce nikolov nick

Robert Osfield

unread,
Jan 5, 2017, 5:47:58 AM1/5/17
to OpenSceneGraph Users
HI Bruno,

It sounds like point sprites would be sufficient for you purpose, no
need to use shaders. Have a look at the osgpointsprite example.

Robert.

Bruno Oliveira

unread,
Jan 5, 2017, 3:03:40 PM1/5/17
to OpenSceneGraph Users
Hi all,

Tracje: thanks a lot, that's something like this that I was needing.
Robert: I've tried that approach, the problem is that I need to define point's sizes in World coordinates, not in pixel! Is this possible with PointSprites in any way?

2017-01-05 10:47 GMT+00:00 Robert Osfield <robert....@gmail.com>:
HI Bruno,

It sounds like point sprites would be sufficient for you purpose, no
need to use shaders.  Have a look at the osgpointsprite example.

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

Bruno Oliveira

unread,
Jan 5, 2017, 3:03:57 PM1/5/17
to OpenSceneGraph Users
Hi all,

Tracje: thanks a lot, that's something like this that I was needing.


Robert: I've tried that approach, the problem is that I need to define point's sizes in World coordinates, not in pixel! Is this possible with PointSprites in any way?
2017-01-05 20:03 GMT+00:00 Bruno Oliveira <bruno.mana...@gmail.com>:
Hi all,

Tracje: thanks a lot, that's something like this that I was needing.
Robert: I've tried that approach, the problem is that I need to define point's sizes in World coordinates, not in pixel! Is this possible with PointSprites in any way?
2017-01-05 10:47 GMT+00:00 Robert Osfield <robert....@gmail.com>:
HI Bruno,

It sounds like point sprites would be sufficient for you purpose, no
need to use shaders.  Have a look at the osgpointsprite example.

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


Robert Osfield

unread,
Jan 6, 2017, 3:49:24 AM1/6/17
to OpenSceneGraph Users
On 5 January 2017 at 20:03, Bruno Oliveira
<bruno.mana...@gmail.com> wrote:
> Hi all,
> Robert: I've tried that approach, the problem is that I need to define
> point's sizes in World coordinates, not in pixel! Is this possible with
> PointSprites in any way?

You can set up the osg::Point distance attenuation coefficients to
alter it's size in different ways. Have a look at online docs for
glPoint. Note, for point sprites you use osg::Point and
osg::PointSprite together.

An alternative is to manually doing the same thing as point sprites
using geometry sharers. The advantage with this approach is that you
can compute all the sizes in your vertex and geometry shaders which
provides more control.

Robert.
_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Reply all
Reply to author
Forward
0 new messages