[osg-users] RTT + shaders example

Michael Maurus michael.maurus at web.de
Wed Jul 12 04:27:31 PDT 2017

Hi all,

I am trying to figure out RTT in combination with shaders for 
implementing a gbuffer but I can't get the different examples to work 
This is a working example for using my shaders, but without RTT and also 
for setting the StateSet for the model and not the camera.

const char* vertSource =

"#version 400\n"

"uniform mat4 MV;\n"

"uniform mat4 N;\n"

"uniform mat4 P;\n"

"layout(location = 0) in vec4 Vertex;\n"

"layout(location = 1) in vec3 Normal;\n"

"smooth out vec3 p;\n"

"smooth out vec3 n;\n"

"void main(void) {\n"

"p = (MV * Vertex).xyz;\n"

"n = normalize(N * vec4(Normal, 0)).xyz;\n"

"gl_Position = P * vec4(p, 1);\n"


const char* fragSource =

"#version 400\n"

"in vec3 p;\n"

"in vec3 n;\n"

"layout(location = 0) out vec4 position;\n"

"layout(location = 1) out vec4 normal;\n"

"layout(location = 2) out float depth;\n"

"void main(void) {\n"

"position = vec4(p,1);\n"

"normal = vec4(n,0);\n"

"depth = gl_FragCoord.z;\n"

//"gl_FragColor = vec4(n,1);\n"


#define Test1

struct ProjectionMatrixCallback: public osg::Uniform::Callback {

ProjectionMatrixCallback(osg::Camera* camera) :

_camera(camera) {


virtual void operator()(osg::Uniform* uniform, osg::NodeVisitor* nv) {



osg::Camera* _camera;


struct NormalMatrixCallback: public osg::Uniform::Callback {

NormalMatrixCallback(osg::Camera* camera) :

_camera(camera) {


virtual void operator()(osg::Uniform* uniform, osg::NodeVisitor* nv) {

osg::Matrixd viewMatrix = _camera->getViewMatrix();

osg::Matrixd normalMatrix = osg::Matrix::inverse(viewMatrix);



osg::Camera* _camera;


struct ModelViewMatrixCallback: public osg::Uniform::Callback {

ModelViewMatrixCallback(osg::Camera* camera) :

_camera(camera) {


virtual void operator()(osg::Uniform* uniform, osg::NodeVisitor* nv) {

osg::Matrixd viewMatrix = _camera->getViewMatrix();

osg::Matrixd modelMatrix = osg::computeLocalToWorld(nv->getNodePath());

osg::Matrixd modelViewMatrix = modelMatrix * viewMatrix;



osg::Camera* _camera;


int main(int, char**){

osgViewer::Viewer viewer;

osg::Node* model = osgDB::readNodeFile("cow.osgt");

osg::ref_ptr<osg::Group> root = new osg::Group();

//create camera

osg::Camera* camera = viewer.getCamera();

// create shader

osg::ref_ptr<osg::Program> program = new osg::Program;


program->addShader(new osg::Shader(osg::Shader::VERTEX, vertSource));

program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragSource));

osg::StateSet* state = model->getOrCreateStateSet();

state->setAttributeAndModes(program.get(), osg::StateAttribute::ON);

// add uniforms

osg::Uniform* modelViewMatrix = new 
osg::Uniform(osg::Uniform::FLOAT_MAT4, "MV");

modelViewMatrix->setUpdateCallback(new ModelViewMatrixCallback(camera));


osg::Uniform* normalMatrix = new osg::Uniform(osg::Uniform::FLOAT_MAT4, 

normalMatrix->setUpdateCallback(new NormalMatrixCallback(camera));


osg::Uniform* projectionMatrix = new 
osg::Uniform(osg::Uniform::FLOAT_MAT4, "P");

projectionMatrix->setUpdateCallback(new ProjectionMatrixCallback(camera));


// turn lights off

state->setMode(GL_LIGHTING, osg::StateAttribute::OFF);

// scene state run




return viewer.run();

} Another working example with RTT camera but without shaders can be 
found here: 
Can somebody help me combine these two or give me a minimal working 
example (which is documented :) ). Thanks, Michael

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20170712/56689f2f/attachment.htm>

More information about the osg-users mailing list