<commit_before>/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 <NAME>
 *
 * This application is open source and may be redistributed and/or modified
 * freely and without restriction, both in commercial and non commercial applications,
 * as long as this copyright notice is maintained.
 *
 * This application is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#include <osgDB/ReadFile>

#include <osgGA/StateSetManipulator>

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>

#include <iostream>


int main(int argc, char** argv)
{
    // use an ArgumentParser object to manage the program arguments.
    osg::ArgumentParser arguments(&argc,argv);

    osgViewer::Viewer viewer(arguments);

    // add the state manipulator
    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );

    // add the stats handler
    viewer.addEventHandler(new osgViewer::StatsHandler);


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


    bool vertexShadersAssigned = false;
    std::string vertexShaderFilename;
    while(arguments.read("--vert", vertexShaderFilename))
    {
        osg::ref_ptr<osg::Shader> vertexShader = osgDB::readRefShaderFile(vertexShaderFilename);
        if (vertexShader)
        {
            vertexShadersAssigned = true;
            program->addShader(vertexShader);
        }
        else
        {
            OSG_NOTICE<<"Unable to load vertex shader file : "<<vertexShaderFilename<<std::endl;
        }
    }

    if (!vertexShadersAssigned)
    {
        vertexShaderFilename = "shaders/shaderpipeline.vert";
        osg::ref_ptr<osg::Shader> vertexShader = osgDB::readRefShaderFile(vertexShaderFilename);
        if (vertexShader)
        {
            vertexShadersAssigned = true;
            program->addShader(vertexShader);
        }
        else
        {
            OSG_NOTICE<<"Unable to load vertex shader file : "<<vertexShaderFilename<<std::endl;
            return 1;
        }
    }



    bool fragmentShadersAssigned = false;
    std::string fragmentShaderFilename;
    while(arguments.read("--frag", fragmentShaderFilename))
    {
        osg::ref_ptr<osg::Shader> fragmentShader = osgDB::readRefShaderFile(fragmentShaderFilename);
        if (fragmentShader)
        {
            fragmentShadersAssigned = true;
            program->addShader(fragmentShader);
        }
        else
        {
            OSG_NOTICE<<"Unable to load fragment shader file : "<<fragmentShaderFilename<<std::endl;
        }
    }

    if (!fragmentShadersAssigned)
    {
        fragmentShaderFilename = "shaders/shaderpipeline.frag";
        osg::ref_ptr<osg::Shader> fragmentShader = osgDB::readRefShaderFile(fragmentShaderFilename);
        if (fragmentShader)
        {
            fragmentShadersAssigned = true;
            program->addShader(fragmentShader);
        }
        else
        {
            OSG_NOTICE<<"Unable to load fragment shader file : "<<fragmentShaderFilename<<std::endl;
            return 1;
        }
    }

    // assign program to topmost StateSet
    viewer.getCamera()->getOrCreateStateSet()->setAttribute(program);

    // load the data
    osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFiles(arguments);
    if (!loadedModel)
    {
        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
        return 1;
    }

    viewer.setSceneData(loadedModel);

    viewer.realize();

    return viewer.run();
<commit_msg>Moved shader file loading to a help function<commit_after>/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 <NAME>
 *
 * This application is open source and may be redistributed and/or modified
 * freely and without restriction, both in commercial and non commercial applications,
 * as long as this copyright notice is maintained.
 *
 * This application is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#include <osgDB/ReadFile>

#include <osgGA/StateSetManipulator>

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>

#include <iostream>


bool readShaderArguments(osg::ArgumentParser& arguments, const std::string& option, osg::Program* program, const std::string& fallbackShaderFilename)
{
    bool shaderAssigned = false;
    std::string shaderFilename;
    while(arguments.read(option, shaderFilename))
    {
        osg::ref_ptr<osg::Shader> shader = osgDB::readRefShaderFile(shaderFilename);
        if (shader)
        {
            shaderAssigned = true;
            program->addShader(shader);
        }
        else
        {
            OSG_NOTICE<<"Unable to load shader file : "<<shaderFilename<<std::endl;
        }
    }

    if (shaderAssigned) return true;

    osg::ref_ptr<osg::Shader> shader = osgDB::readRefShaderFile(fallbackShaderFilename);
    if (shader)
    {
        shaderAssigned = true;
        program->addShader(shader);
        return true;
    }
    else
    {
        OSG_NOTICE<<"Unable to load shader file : "<<fallbackShaderFilename<<std::endl;
        return false;
    }
}

int main(int argc, char** argv)
{
    // use an ArgumentParser object to manage the program arguments.
    osg::ArgumentParser arguments(&argc,argv);

    osgViewer::Viewer viewer(arguments);

    // add the state manipulator
    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );

    // add the stats handler
    viewer.addEventHandler(new osgViewer::StatsHandler);


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


    if (!readShaderArguments(arguments, "--vert", program, "shaders/shaderpipeline.vert"))
    {
        return 1;
    }

    if (!readShaderArguments(arguments, "--frag", program, "shaders/shaderpipeline.frag"))
    {
        return 1;
    }

    // assign program to topmost StateSet
    viewer.getCamera()->getOrCreateStateSet()->setAttribute(program);

    // load the data
    osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFiles(arguments);
    if (!loadedModel)
    {
        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
        return 1;
    }

    viewer.setSceneData(loadedModel);

    viewer.realize();

    return viewer.run();

{'fraction_non_alphanumeric': 0.10894157756082443, 'fraction_numerical': 0.0034092670075933675, 'mean_word_length': 3.0719242902208204, 'pattern_counts': {'":': 2, '<': 73, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1208789', 'n_tokens_mistral': 1877, 'n_tokens_neox': 1829, 'n_words': 441}