Hi all,
As an experiment, I've created a heightfield whose tessellated geometry is
created on the CPU side (i.e. the (x, y) coordinates), but height is determined
by a vertex shader. I use a FloatArray to specify the height of each vertex,
and then call geom->setVertexAttribArray() to pass it to the shader.
Here's the code I'm using:
Code:
geom = new osg::Geometry;
// vertices and indices are pre-computed
geom->setVertexArray(vertices);
geom->setPrimitiveSet(indices);
osg::VertexBufferObject *heightMapVBO = new osg::VertexBufferObject;
heightMapVBO->setUsage(GL_DYNAMIC_DRAW);
heightMap = new osg::FloatArray(vertexCount);
heightMap->setVertexBufferObject(heightMapVBO);
// zero out heightMap
for (int i = 0; i < vertexCount; i++) (*heightMap)[i] = 0;
// attach array to be used on a per-vertex manner
geom->setVertexAttribArray(1, heightMap);
geom->setVertexAttribBinding(1, osg::Geometry::BIND_PER_VERTEX);
// set vertex and fragment shaders
This code works perfectly when the heightmap remains static. However, if I try
changing the values of the heightmap, the rendered geometry doesn't reflect
those changes. Here is the code I use to alter the heightmap:
Code:
void addHeight(int x, int y, float inc) {
(*heightMap)[x + y*width] += inc;
heightMap->dirty();
}
Looking through the forums, I found osggpumorph, which uses the VBOs
automatically managed by geom instead. I altered my code accordingly:
Code:
geom = new osg::Geometry;
geom->setUseDisplayList(false);
geom->setUseVertexBufferObjects(true);
geom->setVertexArray(vertices);
geom->setPrimitiveSet(indices);
osg::VertexBufferObject *heightMapVBO = geom->getOrCreateVertexBufferObject();
heightMapVBO->setUsage(GL_DYNAMIC_DRAW);
heightMap = new osg::FloatArray(vertexCount);
heightMap->setDataVariance(osg::Object::DYNAMIC);
// ...
with the addHeight function changing to:
Code:
void addHeight(int x, int y, float inc) {
(*heightMap)[x + y*width] += inc;
// copy a new array over each time (this seems a little inefficient, but
maybe it's ok?)
geom->setVertexAttribArray(1, heightMap.get());
heightMap->dirty();
}
But I still don't see any changes in the heightmap. I can verify the shaders
are correct, because when I alter the heightmap the first time, everything is
drawn correctly. It just appears the updating the VBOs (CPU -> GPU) isn't
working correctly.
Any suggestions on what I might be doing wrong would be greatly appreciated!
Thanks,
Abe[/code]
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=49517#49517
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org