OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

Problem with output - OSG/Shader


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message
romulogcerqueira
User


Joined: 11 Jun 2015
Posts: 65
Location: Brazil

PostPosted: Sat Aug 04, 2018 6:36 pm    Post subject:
Problem with output - OSG/Shader
Reply with quote

Hi,

I would like to write the output/buffer of fragment shader to texture (RTT) as I wish (e.g. all red), however this result is depending of the model's shapes visible in the scene. How can I proceed with this correctly?

Follow below my source code and the results.

Thanks in advance!

Expected:


Current:


Code:
#include <osg/TextureRectangle>
#include <osg/Group>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/PolygonMode>
#include <osg/Texture>
#include <osg/Camera>
#include <osgText/Text>

static const char *mrtVertSource = {
    "#version 130\n"
    "void main(void)\n"
    "{\n"
    "   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
    "   gl_TexCoord[0] = gl_MultiTexCoord0;\n"
    "}\n"};

static const char *mrtFragSource = {
    "#version 130\n"
    "void main(void)\n"
    "{\n"
    "   gl_FragData[0] = vec4(1, 0, 0, 1);\n"
    "}\n"};

osg::Geode *createScreenQuad(float width, float height, float scale = 1.0f)
{
    osg::Geometry *geom = osg::createTexturedQuadGeometry(
        osg::Vec3(), osg::Vec3(width, 0.0f, 0.0f), osg::Vec3(0.0f, height, 0.0f),
        0.0f, 0.0f, width * scale, height * scale);
    osg::ref_ptr<osg::Geode> quad = new osg::Geode;
    quad->addDrawable(geom);

    int values = osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED;
    quad->getOrCreateStateSet()->setAttribute(
        new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL), values);
    quad->getOrCreateStateSet()->setMode(GL_LIGHTING, values);
    return quad.release();
}

osg::Camera *createRTTCamera(osg::Camera::BufferComponent buffer, osg::Texture *tex)
{
    osg::ref_ptr<osg::Camera> camera = new osg::Camera;
    camera->setClearColor(osg::Vec4());
    camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
    camera->setRenderOrder(osg::Camera::PRE_RENDER);
    camera->setViewport(0, 0, tex->getTextureWidth(), tex->getTextureHeight());
    camera->attach(buffer, tex);

    return camera.release();
}

osg::Camera *createHUDCamera(double left, double right, double bottom, double top)
{
    osg::ref_ptr<osg::Camera> camera = new osg::Camera;
    camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    camera->setClearMask(GL_DEPTH_BUFFER_BIT);
    camera->setRenderOrder(osg::Camera::POST_RENDER);
    camera->setAllowEventFocus(false);
    camera->setProjectionMatrix(osg::Matrix::ortho2D(left, right, bottom, top));
    camera->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
    return camera.release();
}

osg::TextureRectangle *createFloatTexture(uint w, uint h)
{
    osg::ref_ptr<osg::TextureRectangle> tex2D = new osg::TextureRectangle;
    tex2D->setTextureSize(w, h);
    tex2D->setInternalFormat(GL_RGB32F_ARB);
    tex2D->setSourceFormat(GL_RGBA);
    tex2D->setSourceType(GL_FLOAT);
    tex2D->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
    tex2D->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);
    return tex2D.release();
}

int main(int argc, char **argv)
{
    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFile("/home/romulo/Tools/OpenSceneGraph-Data/cow.osg");

    unsigned int w = 800, h = 800;
    osg::Texture *normalTex = createFloatTexture(w, h);

    osg::ref_ptr<osg::Camera> rttCamera = createRTTCamera(osg::Camera::COLOR_BUFFER0, normalTex);
    rttCamera->addChild(scene.get());

    osg::ref_ptr<osg::Program> program = new osg::Program;
    program->addShader(new osg::Shader(osg::Shader::VERTEX, mrtVertSource));
    program->addShader(new osg::Shader(osg::Shader::FRAGMENT, mrtFragSource));
    rttCamera->getOrCreateStateSet()->setAttributeAndModes(program.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
    rttCamera->getOrCreateStateSet()->addUniform(new osg::Uniform("defaultTex", 0));

    osg::ref_ptr<osg::Camera> hudCamera = createHUDCamera(0.0, 1.0, 0.0, 1.0);
    hudCamera->addChild(createScreenQuad(1.f, 1.0f, w));
    hudCamera->getOrCreateStateSet()->setTextureAttributeAndModes(0, normalTex);

    osg::ref_ptr<osg::Group> root = new osg::Group;
    root->addChild(rttCamera.get());
    root->addChild(hudCamera.get());
    root->addChild(scene.get());

    osgViewer::Viewer viewer;
    viewer.setUpViewInWindow(0, 0, w, h);
    viewer.setSceneData(root.get());
    return viewer.run();
}


Rômulo
Back to top
View user's profile Send private message
Steal
User


Joined: 26 Jan 2017
Posts: 78

PostPosted: Sun Aug 05, 2018 11:39 am    Post subject:
Reply with quote

Hi,

It could be that maybe the black portion of the texture you are doing postproc on has the wrong alpha value... just a stab in the dark lol.

And why exactly would you want an all red scene?

Cheers,
Johny
Back to top
View user's profile Send private message
romulogcerqueira
User


Joined: 11 Jun 2015
Posts: 65
Location: Brazil

PostPosted: Sun Aug 05, 2018 7:08 pm    Post subject:
Reply with quote

Hi Johny,

I would like to draw what I want... the red was just an example.

My main goal is to simulate the reverberation phenomena using rasterization (for the first reflection) + ray tracing (for the second reflection). I would like to output the surface of reflected scenes, which are always not visible from the viewpoint.

...

Thank you!

Cheers,
Rômulo
Back to top
View user's profile Send private message
Steal
User


Joined: 26 Jan 2017
Posts: 78

PostPosted: Tue Aug 07, 2018 10:06 am    Post subject:
Reply with quote

Hi,

That makes zero sense to me. Can you show the glsl?

Thank you!

Cheers,
Johny
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts [Help] OSG + ArKit: Problem convertin... Brayner General 4 Mon Oct 08, 2018 7:07 pm View latest post
No new posts problem with picking/intersecting in ... mue General 1 Sat Jun 09, 2018 11:18 am View latest post
No new posts Shader Program stops working after Ch... ceranco General 0 Mon May 21, 2018 3:09 pm View latest post
No new posts osgText: problem with SCREEN_COORDS mode gwaldron General 9 Thu May 17, 2018 3:56 pm View latest post
No new posts Shader composition with multiple func... hartwigw General 1 Sat May 12, 2018 9:59 pm View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP