osg中的 Uniforms主要存在两个地方,一个是SceneView中,void SceneView::updateUniforms()
void SceneView::updateUniforms()
{
if (!_localStateSet)
{
_localStateSet = new osg::StateSet;
}
if (!_localStateSet) return;
if ((_activeUniforms & FRAME_NUMBER_UNIFORM) && _frameStamp.valid())
{
osg::Uniform* uniform = _localStateSet->getOrCreateUniform("osg_FrameNumber",osg::Uniform::UNSIGNED_INT);
uniform->set(_frameStamp->getFrameNumber());
}
if ((_activeUniforms & FRAME_TIME_UNIFORM) && _frameStamp.valid())
{
osg::Uniform* uniform = _localStateSet->getOrCreateUniform("osg_FrameTime",osg::Uniform::FLOAT);
uniform->set(static_cast<float>(_frameStamp->getReferenceTime()));
}
if ((_activeUniforms & DELTA_FRAME_TIME_UNIFORM) && _frameStamp.valid())
{
float delta_frame_time = (_previousFrameTime != 0.0) ? static_cast<float>(_frameStamp->getReferenceTime()-_previousFrameTime) : 0.0f;
_previousFrameTime = _frameStamp->getReferenceTime();
osg::Uniform* uniform = _localStateSet->getOrCreateUniform("osg_DeltaFrameTime",osg::Uniform::FLOAT);
uniform->set(delta_frame_time);
}
if ((_activeUniforms & SIMULATION_TIME_UNIFORM) && _frameStamp.valid())
{
osg::Uniform* uniform = _localStateSet->getOrCreateUniform("osg_SimulationTime",osg::Uniform::FLOAT);
uniform->set(static_cast<float>(_frameStamp->getSimulationTime()));
}
if ((_activeUniforms & DELTA_SIMULATION_TIME_UNIFORM) && _frameStamp.valid())
{
float delta_simulation_time = (_previousSimulationTime != 0.0) ? static_cast<float>(_frameStamp->getSimulationTime()-_previousSimulationTime) : 0.0f;
_previousSimulationTime = _frameStamp->getSimulationTime();
osg::Uniform* uniform = _localStateSet->getOrCreateUniform("osg_DeltaSimulationTime",osg::Uniform::FLOAT);
uniform->set(delta_simulation_time);
}
if (_activeUniforms & VIEW_MATRIX_UNIFORM)
{
osg::Uniform* uniform = _localStateSet->getOrCreateUniform("osg_ViewMatrix",osg::Uniform::FLOAT_MAT4);
uniform->set(getViewMatrix());
}
if (_activeUniforms & VIEW_MATRIX_INVERSE_UNIFORM)
{
osg::Uniform* uniform = _localStateSet->getOrCreateUniform("osg_ViewMatrixInverse",osg::Uniform::FLOAT_MAT4);
uniform->set(osg::Matrix::inverse(getViewMatrix()));
}
}
不过默认的是: DEFAULT_UNIFORMS
enum ActiveUniforms
{
FRAME_NUMBER_UNIFORM = 1,
FRAME_TIME_UNIFORM = 2,
DELTA_FRAME_TIME_UNIFORM = 4,
SIMULATION_TIME_UNIFORM = 8,
DELTA_SIMULATION_TIME_UNIFORM = 16,
VIEW_MATRIX_UNIFORM = 32,
VIEW_MATRIX_INVERSE_UNIFORM = 64,
DEFAULT_UNIFORMS = FRAME_NUMBER_UNIFORM |
FRAME_TIME_UNIFORM |
DELTA_FRAME_TIME_UNIFORM |
SIMULATION_TIME_UNIFORM |
DELTA_SIMULATION_TIME_UNIFORM |
VIEW_MATRIX_UNIFORM |
VIEW_MATRIX_INVERSE_UNIFORM,
ALL_UNIFORMS = 0x7FFFFFFF
};
另一个是在osg::State中,
_modelViewMatrixUniform = new Uniform(Uniform::FLOAT_MAT4,"osg_ModelViewMatrix");
_projectionMatrixUniform = new Uniform(Uniform::FLOAT_MAT4,"osg_ProjectionMatrix");
_modelViewProjectionMatrixUniform = new Uniform(Uniform::FLOAT_MAT4,"osg_ModelViewProjectionMatrix");
_normalMatrixUniform = new Uniform(Uniform::FLOAT_MAT3,"osg_NormalMatrix");
要使用这些Uniforms,需要:
viewer->realize();
osg::State* state = viewer->getCamera()->getGraphicsContext()->getState();
state->setUseModelViewAndProjectionUniforms(true);