[osg-users] [HELP] Properly using osg::VertexAttribDivisor for basic geometry instancing
ppsychrite at cock.li
ppsychrite at cock.li
Wed Aug 1 05:16:50 PDT 2018
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?
More information about the osg-users
mailing list