Hello everyone.

I am using custom vertex attributes but running into a strange issue. I
do not seem to be able to use:

        BIND_PER_PRIMITIVE

..when using custom vertex attributes. Is this a known limitation, or is
it likely a bug/me doing something wrong? I have attached a simple
example of this failure; if you uncomment "GridCoordinates", nothing is
rendered at all (nor do I receive any information via OSG_NOTIFY).

If you keep this commented out, the grid renders.

Curious...
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/PrimitiveSet>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>

class Grid: public osg::Geometry {
public:
	enum {
		GRID_POSITION   = 6,
		GRID_COORDINATE = 7
	};

	Grid(const osg::Vec2s& size, const osg::Vec2& gridSize = osg::Vec2(1.0f, 1.0f)):
	_size     (size),
	_gridSize (gridSize) {
		setUseDisplayList(false);
	}

	bool allocate() {
		osg::Program* program = new osg::Program();
		osg::Shader*  vert    = osg::Shader::readShaderFile(osg::Shader::VERTEX, "vert.glsl");
		osg::Shader*  frag    = osg::Shader::readShaderFile(osg::Shader::FRAGMENT, "frag.glsl");
		// osg::Uniform* texture = new osg::Uniform(osg::Uniform::SAMPLER_2D, "Texture");
		
		program->addShader(vert);
		program->addShader(frag);
		program->addBindAttribLocation("GridPosition", GRID_POSITION);
		program->addBindAttribLocation("GridCoordinate", GRID_COORDINATE);

		osg::Vec4Array* gridPositions   = new osg::Vec4Array();
		osg::Vec2Array* gridCoordinates = new osg::Vec2Array();

		for(short x = 0; x < _size.x(); x++) {
			for(short y = 0; y < _size.y(); y++) {
				osg::Vec4 bl(x, y, 0.0f, 0.0f);
				osg::Vec4 br(x + _gridSize.x(), y, 0.0f, 1.0f);
				osg::Vec4 ur(x + _gridSize.x(), y + _gridSize.y(), 0.0f, 2.0f);
				osg::Vec4 ul(x, y + _gridSize.y(), 0.0f, 3.0f);

				gridPositions->push_back(bl);
				gridPositions->push_back(br);
				gridPositions->push_back(ur);
				gridPositions->push_back(ul);

				gridCoordinates->push_back(osg::Vec2(x, y));
			}
		}

		// GridPosition
		setVertexAttribArray(GRID_POSITION, gridPositions);
		setVertexAttribBinding(GRID_POSITION, osg::Geometry::BIND_PER_VERTEX);

		/*
		// GridCoordinates
		setVertexAttribArray(GRID_COORDINATE, gridCoordinates);
		setVertexAttribBinding(GRID_COORDINATE, osg::Geometry::BIND_PER_PRIMITIVE);
		u*/

		// Finish seting up the Geometry...
		addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, _size.x() * _size.y() * 4));

		// texture->set(0);

		osg::StateSet* state = getOrCreateStateSet();

		state->setAttributeAndModes(program);
		// state->addUniform(texture);

		return true;
	}

	virtual osg::BoundingBox computeBound() const {
		return osg::BoundingBox(
			osg::Vec3(0.0f, 0.0f, 0.0f),
			osg::Vec3(_size.x() * _gridSize.x(), _size.y() * _gridSize.y(), 0.0f)
		);
	}

private:
	osg::Vec2s _size;
	osg::Vec2  _gridSize;
};

int main(int argc, char** argv) {
	osgViewer::Viewer viewer;

	Grid* grid = new Grid(osg::Vec2s(10, 10));

	grid->allocate();

	osg::Geode* geode = new osg::Geode();

	geode->addDrawable(grid);

	viewer.setSceneData(geode);
	viewer.addEventHandler(new osgViewer::StatsHandler());

	return viewer.run();
}

varying float color;

void main() {
        gl_FragColor = vec4(color, color, color, 1.0);
}

in vec4 GridPosition;
in vec2 GridCoordinate;

varying float color;

void main() {
        color = GridPosition.w / 3.0;

        gl_Position = gl_ModelViewProjectionMatrix * vec4(GridPosition.xyz, 
1.0);
}

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to