Re: [osg-users] Tearing hair out over simple GLSL instancing
Yup that's what I found I had to do . I created a fragment shader and did the basic lighting. I assume I could do that in the vertex shader as well. Thanks for confirming. -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=69809#69809 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Tearing hair out over simple GLSL instancing
Does your vertex shader code perform any lighting calculations? I remember that in my instancing vertex shader code I had to explicitly perform required lighting calculations for ambient, diffuse, specular components (the usual Blinn-Phong lighting equations) and set the vertex colors accordingly. Christian 2016-12-29 22:23 GMT+01:00 Andrew Cunningham : > Hi guys, > Can you help me out with this one more time:) > > As I said, I am now able to draw my geometry ( a simple square) using > hardware instancing. It works perfectly with excellent performance for > millions of squares- except for one thing. > > Now I want to light the squares. I have turned on lighting, assigned a > normal array to the geometry being instanced and assigned material colors, > but I get unlit 'black squares' while the rest of my scene is lit just > fine. Clearly something to do with hardware instancing using the vertex > shader. > > Do I need to add a fragment shader? I am a bit lost here. > > -- > Read this topic online here: > http://forum.openscenegraph.org/viewtopic.php?p=69801#69801 > > > > > > ___ > osg-users mailing list > osg-users@lists.openscenegraph.org > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org > ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Tearing hair out over simple GLSL instancing
Hi guys, Can you help me out with this one more time:) As I said, I am now able to draw my geometry ( a simple square) using hardware instancing. It works perfectly with excellent performance for millions of squares- except for one thing. Now I want to light the squares. I have turned on lighting, assigned a normal array to the geometry being instanced and assigned material colors, but I get unlit 'black squares' while the rest of my scene is lit just fine. Clearly something to do with hardware instancing using the vertex shader. Do I need to add a fragment shader? I am a bit lost here. -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=69801#69801 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Tearing hair out over simple GLSL instancing
Thanks for the help I got it working by adding this to the StateSet osg::VertexAttribDivisor *divisor = new osg::VertexAttribDivisor(1, 1); ss->setAttribute(divisor); And changing my loop to add one offset per primitive to my vertexArray And thanks again!! I would never have figured that one out! -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=69747#69747 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Tearing hair out over simple GLSL instancing
You can use osg::VertexAttribDivisor (set to 1) to tell the shader to use one attribute index per instance. Glenn Waldron On Thu, Dec 22, 2016 at 12:35 PM, Andrew Cunningham wrote: > On thinking about this issue, I see the problem. I really only have 4 > vertices, so the value d in my vertex shader just is always pulled from > locations 0-3 in my array. What I really need to do is access an array of > offsets in my shader indexed by glInstanceID > > -- > Read this topic online here: > http://forum.openscenegraph.org/viewtopic.php?p=69744#69744 > > > > > > ___ > osg-users mailing list > osg-users@lists.openscenegraph.org > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org > ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Tearing hair out over simple GLSL instancing
you might want to look into glVertexAttribDivisor https://www.opengl.org/sdk/docs/man4/html/glVertexAttribDivisor.xhtml That would let you make a vbo with 100 offsets in it and then each instanced draw call would get a different offset. you can even send down a 4x3 transform matrix by using more attributes. On Thu, Dec 22, 2016 at 12:35 PM, Andrew Cunningham wrote: > On thinking about this issue, I see the problem. I really only have 4 > vertices, so the value d in my vertex shader just is always pulled from > locations 0-3 in my array. What I really need to do is access an array of > offsets in my shader indexed by glInstanceID > > -- > Read this topic online here: > http://forum.openscenegraph.org/viewtopic.php?p=69744#69744 > > > > > > ___ > osg-users mailing list > osg-users@lists.openscenegraph.org > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org > -- DI-Guy Engineering Lead, VT MÄK 150 Cambridge Park Drive, 3rd Floor, Cambridge, MA 02140 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Tearing hair out over simple GLSL instancing
On thinking about this issue, I see the problem. I really only have 4 vertices, so the value d in my vertex shader just is always pulled from locations 0-3 in my array. What I really need to do is access an array of offsets in my shader indexed by glInstanceID -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=69744#69744 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
[osg-users] Tearing hair out over simple GLSL instancing
OSG 3.4.0 I am trying to use "instancing" (starting from the example osgdrawinstanced) to draw simple square geometry at multiple locations. I am getting all of my instances sitting on top of each other at (0,0,0). That is they are not offset to their expected locations. I know my shader is working OK because if I add a hack to my shader to add in an offset that depends on gl_InstanceID then the squares do get "offset" per instance. Step 1 Add "nInstances" of the square to my osg::Geometry geom.addPrimitiveSet( new osg::DrawArrays( GL_QUADS, 0, 4, nInstances ) ); Step 2 Create a simple shader program that uses an attribute array of offsets ("d") to offset the vertex locations std::string vertexSource = "#version 120\n" "#extension GL_EXT_draw_instanced : enable\n" "in vec3 d;\n" "void main() \n" "{ \n" // Use the =coord to translate the position of the vertices. "vec3 pos = vec3(gl_Vertex) + d;\n" "vec4 finalPos = vec4(pos, 1.0);\n" "gl_Position = gl_ModelViewProjectionMatrix * finalPos;\n" "} \n"; osg::ref_ptr< osg::Shader > vertexShader = new osg::Shader(); vertexShader->setType(osg::Shader::VERTEX); vertexShader->setShaderSource(vertexSource); osg::ref_ptr< osg::Program > program = new osg::Program(); program->addShader(vertexShader.get()); ss->setAttribute(program.get(),osg::StateAttribute::ON); program->addBindAttribLocation("d", 1); Step 3 Create an array of offsets 4 * nInstances that contain the offsets for each vertex. Is this where I am going wrong??? I have checked that "d" is not (0,0,0) and is varying for each instance. It seems to be that all the instances are using the same offset. osg::Vec3Array *attrib = new osg::Vec3Array; size_t vecIndex = 0; for (size_t i = 0; i < nInstances ; i++) { osg::Vec3 d=GetOffSetForInstance(i); attrib->push_back(d); attrib->push_back(d); attrib->push_back(d); attrib->push_back(d); } geom->setVertexAttribArray(1, attrib); geom->setVertexAttribBinding(1, osg::Geometry::BIND_PER_VERTEX); Andrew -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=69743#69743 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org