[osg-users] [HELP] Properly using osg::VertexAttribDivisor for basic geometry instancing

Werner Modenbach Werner.Modenbach at texion.eu
Thu Aug 2 02:20:06 PDT 2018


Hi,

what we do is packing our instance parameters in a uniform array and
access it via gl_InstanceID as an index into the array.

- Werner -

Am 01.08.2018 um 16:32 schrieb Julien Valentin:
> Hi
> perhaps it's because of transaltion array 's default binding (UNDEFINED)
> try
> translation->setArrayBinding(Array::PER_VERTEX)
> Cheers
>
>
> ppsychrite wrote:
>> hello sorry if i did this wrong but i dont know how to use mailing lists
>>
>> so ive been trying to do modern glsl/opengl 3.3+ with osg::Geometry and 
>> osg::Stateset and it's been working OK for basic triangle except for 
>> implementing instanced array data
>> am I doing it the wrong way? here's the .cpp code for it and for me it 
>> seems well enough and fine, position and color works fine
>>
>>
>> 	osg::Vec3 coords[] = {
>> 		{ -0.05f,0.05f,0.f },
>> 		{ 0.05f,-0.05f,0.f },
>> 		{ 0.05f,0.05f,1.f }
>> 	};
>> 	osg::Vec3 colors[] = {
>> 		{1.f,0.f,0.f},
>> 		{0.f,1.f,0.f},
>> 		{0.f,0.f,1.f}
>> 	};
>>
>> 	osg::Vec2 instancetranslations[] = {
>> 		{ 0.0f, 0.0f },
>> 		{ 0.1f, 0.0f },
>> 		{ 0.2f, 0.0f }
>> 	};
>>
>> 	osg::Vec3Array *pos = new osg::Vec3Array(sizeof(coords) / 
>> sizeof(osg::Vec3), coords);
>> 	osg::Vec3Array *color = new osg::Vec3Array(sizeof(colors) / 
>> sizeof(osg::Vec3), colors);
>> 	osg::Vec2Array *translation = new 
>> osg::Vec2Array(sizeof(instancetranslations) / sizeof(osg::Vec2), 
>> instancetranslations);
>>
>> 	osg::Geometry *vao = new osg::Geometry();
>> 	vao->setUseDisplayList(false);
>> 	vao->setUseVertexArrayObject(true);
>> 	vao->setUseVertexBufferObjects(true);
>>
>> 	vao->setVertexAttribArray(0, pos, osg::Array::BIND_PER_VERTEX);
>> 	vao->setVertexAttribArray(1, color, osg::Array::BIND_PER_VERTEX);
>> 	vao->setVertexAttribArray(2, translation);
>>
>> 	vao->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLES, 0, 3, 3)); //3 
>> instances
>>
>> 	osg::ref_ptr<osg::Program> program = new osg::Program;
>>
>> 	program->addShader(osgDB::readShaderFile("shaders/basic.vert"));
>> 	program->addShader(osgDB::readShaderFile("shaders/basic.frag"));
>>
>> 	osg::StateSet *stateset = geom->getOrCreateStateSet();
>> 	stateset->setAttributeAndModes(program.get());
>> 	stateset->setAttribute(new osg::VertexAttribDivisor(2, 1)); //set 3rd 
>> array as per-instance
>>
>> here's the vertex shader code for it:
>>
>>
>> #version 330 core
>> layout (location = 0) in vec3 aPos;
>> layout (location = 1) in vec3 aColor;
>> layout (location = 2) in vec2 aTrans;
>>
>> out vec3 color;
>>
>> void main()
>> {
>> color = aColor;
>> gl_Position = vec4(aPos.x+ aTrans.x,aPos.y+ aTrans.y,aPos.z,1.0);
>> }
>>
>>
>> aTrans doesn't move the triangle at all, it's just one. I know it isn't 
>> the shaders fault because instead of adding aTrans to aPos I've done 
>> (gl_InstanceID * 0.1f) and it's worked perfectly, seeing 3 instanced 
>> triangles
>>
>> what am i doing wrong/how could i get per-instanced array working?
>>
>>
>>
>>
>>
>> _______________________________________________
>> osg-users mailing list
>>
>> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>>
>>  ------------------
>> Post generated by Mail2Forum
>
> ------------------------
> Twirling twirling twirling toward freedom
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=74452#74452
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

-- 
*TEXION Software Solutions, Rotter Bruch 26a, D-52068 Aachen*
Phone: +49 241 475757-0
Fax: +49 241 475757-29
Web: http://texion.eu
eMail: info at texion.eu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20180802/6971b83d/attachment.html>


More information about the osg-users mailing list