[osg-users] RTT + shaders example
Sebastian Messerschmidt
sebastian.messerschmidt at gmx.de
Wed Jul 12 05:30:40 PDT 2017
Hi Michael,
As a side note:
In order to get your matrices to your shaders simply use the osg_
equivalents to the gl_-Matrices and setup aliasing:
Viewer->getCamera()->getGraphicsContext()->getState()->setUseModelViewAndProjectionUniforms(true);
Viewer->getCamera()->getGraphicsContext()->getState()->setUseVertexAttributeAliasing(true);
In general you need to create the render-target textures and bind them
to the FBO of your RTT-Camera. Basically there is the
osgmultiplerendertargets example which shows you how to achieve this.
Once you have a working example rendering the gbuffer, you can simply
add more absolute cameras to the graph to do the screenspace calculations.
Also take a look at osgPPU, which might offer some insight.
Cheers
Sebastian
> 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
> together.
> 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"
>
> "}\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"
>
> "}\n";
>
> #define Test1
>
> struct ProjectionMatrixCallback: public osg::Uniform::Callback {
>
> ProjectionMatrixCallback(osg::Camera* camera) :
>
> _camera(camera) {
>
> }
>
> virtual void operator()(osg::Uniform* uniform, osg::NodeVisitor* nv) {
>
> uniform->set(_camera->getProjectionMatrix());
>
> }
>
> 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);
>
> uniform->set(normalMatrix);
>
> }
>
> 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;
>
> uniform->set(modelViewMatrix);
>
> }
>
> 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->setName("shader");
>
> 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));
>
> state->addUniform(modelViewMatrix);
>
> osg::Uniform* normalMatrix = new osg::Uniform(osg::Uniform::FLOAT_MAT4, "N");
>
> normalMatrix->setUpdateCallback(new NormalMatrixCallback(camera));
>
> state->addUniform(normalMatrix);
>
> osg::Uniform* projectionMatrix = new osg::Uniform(osg::Uniform::FLOAT_MAT4, "P");
>
> projectionMatrix->setUpdateCallback(new ProjectionMatrixCallback(camera));
>
> state->addUniform(projectionMatrix);
>
> // turn lights off
>
> state->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
>
> // scene state run
>
> root->addChild(model);
>
> viewer.setSceneData(root.get());
>
> viewer.setUpViewOnSingleScreen(0);
>
> return viewer.run();
>
> }
>
> Another working example with RTT camera but without shaders can be found here:https://github.com/xarray/osgRecipes/blob/master/cookbook/chapter6/ch06_04/read_depth.cpp
> Can somebody help me combine these two or give me a minimal working example (which is documented :) ).
>
> Thanks,
> Michael
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
More information about the osg-users
mailing list