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

Trajce Nikolov NICK trajce.nikolov.nick at gmail.com
Tue Jan 3 14:10:53 PST 2017


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



On Mon, Jan 2, 2017 at 11:10 PM, Bruno Oliveira <
bruno.manata.oliveira at gmail.com> wrote:

> 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.nikolov.nick at 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
>>
>> On Mon, Jan 2, 2017 at 8:21 PM, Bruno Oliveira <
>> bruno.manata.oliveira at gmail.com> wrote:
>>
>>> 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/201307
>>> 05/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;
>>> }
>>>
>>>
>>> _______________________________________________
>>> osg-users mailing list
>>> osg-users at lists.openscenegraph.org
>>> http://lists.openscenegraph.org/listinfo.cgi/osg-users-opens
>>> cenegraph.org
>>>
>>>
>>
>>
>> --
>> trajce nikolov nick
>>
>> _______________________________________________
>> osg-users mailing list
>> osg-users at lists.openscenegraph.org
>> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>>
>>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>


-- 
trajce nikolov nick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20170103/28e44b0e/attachment-0002.htm>


More information about the osg-users mailing list