OSG use in conjunction with Shader

1 Overview

Prior learning in OpenGL rendering pipeline when data sequentially according to the application, the transfer buffer, the vertex shader, fragment shader programming these steps. OSG is OpenGL some top-level package, using the shader when you do not see these steps, so a little difficult. Here I summarize the two most simple example.

2. Fixed lines coloring

OSG one of the most simple example is to demonstrate that comes with data glider.osg:

#include <iostream>
#include <Windows.h>

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>

using namespace std;

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

    string osgPath = "D:/Work/OSGBuild/OpenSceneGraph-Data/glider.osg";
    osg::Node * node = osgDB::readNodeFile(osgPath);
    root->addChild(node);
    
    osgViewer::Viewer viewer;
    viewer.setSceneData(root);
    viewer.setUpViewInWindow(100, 100, 800, 600);
    return viewer.run();
}

Results shown are a simple glider:

Open glider.osg text data in this manner, which is the vertex information recorded:

This data should be rendered by a fixed line, it can be added to this scenario Shader:

#include <iostream>
#include <Windows.h>

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>

using namespace std;

//设置纹理着色
static void ColorShader(osg::ref_ptr<osg::Node> node)
{
    const char * vertexShader = {
        "void main(void ){\n"
        "   gl_FrontColor = gl_Color;\n"
        "   gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;\n"
        "}\n"
    };

    const char * fragShader = {
        "void main(void){\n"
        "   gl_FragColor = gl_Color;\n"
        "}\n"
    };

    osg::StateSet * ss = node->getOrCreateStateSet();
    osg::ref_ptr<osg::Program> program = new  osg::Program();
    program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragShader));
    program->addShader(new osg::Shader(osg::Shader::VERTEX, vertexShader));
    ss->setAttributeAndModes(program, osg::StateAttribute::ON);
}

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

    string osgPath = "D:/Work/OSGBuild/OpenSceneGraph-Data/glider.osg";
    osg::Node * node = osgDB::readNodeFile(osgPath);
    root->addChild(node);
    
    ColorShader(node);

    osgViewer::Viewer viewer;
    viewer.setSceneData(root);
    viewer.setUpViewInWindow(100, 100, 800, 600);
    return viewer.run();
}

This shader code What does it mean? Is very simple, when used glColor fixed function pipeline, the color value is variable as a built-gl_Color incoming vertex shader, vertex shader calculates positive and negative stored values gl_FontColor and gl_BackColor; fragments continue to pass after shader, gl_Color it will become a FontColor calculated by interpolation and BackColor variables. The final gl_FragColor received is the value of fixed rendering pipeline obtained. Run the following results:

The final results differ from the previous results, which is the default scene is osgViewer of lighting effects, programmable rendering pipeline covering the fixed pipeline effect. In the example of the code may be added before a rendering of the fixed line

root->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);

Removal of lighting effects, both rendering completely in agreement.

3. texture shader

Another example is a loading OSGB model by OSG textured:

#include <iostream>
#include <Windows.h>

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>

using namespace std;

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

    string osgPath = "D:/Data/scene/Dayanta_OSGB/Data/MultiFoderReader.osgb";
    osg::Node * node = osgDB::readNodeFile(osgPath);
    root->addChild(node);
    
    osgViewer::Viewer viewer;
    viewer.setSceneData(root);
    viewer.setUpViewInWindow(100, 100, 800, 600);
    return viewer.run();
}

Operating results will find some dark scenes Perspective, which is also due to the scene of default caused by light:

take the same way, by covering shader rendering pipeline fixed:

//设置纹理着色
static void TextureShader(osg::ref_ptr<osg::Node> node)
{
    const char * vertexShader = {
        "void main(void ){\n"
        "   gl_TexCoord[0] = gl_MultiTexCoord0;\n"
        "   gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;\n"
        "}\n"
    };

    const char * fragShader = {
        "uniform sampler2D baseTexture;\n"
        "void main(void){\n"
        "   vec2 coord = gl_TexCoord[0].xy;\n"
        "   vec4 C = texture2D(baseTexture, coord)\n;"
        "   gl_FragColor = C;\n"
        "}\n"
    };

    osg::StateSet * ss = node->getOrCreateStateSet();
    osg::ref_ptr<osg::Program> program = new  osg::Program();
    program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragShader));
    program->addShader(new osg::Shader(osg::Shader::VERTEX, vertexShader));
    ss->setAttributeAndModes(program, osg::StateAttribute::ON);
}

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

    string osgPath = "D:/Data/scene/Dayanta_OSGB/Data/MultiFoderReader.osgb";
    osg::Node * node = osgDB::readNodeFile(osgPath);
    root->addChild(node);
    
    TextureShader(node);

    osgViewer::Viewer viewer;
    viewer.setSceneData(root);
    viewer.setUpViewInWindow(100, 100, 800, 600);
    return viewer.run();
}

This shader code is relatively simple, the vertex shader, gl_MultiTexCoord0 number 0 represents the texture coordinates of the vertices of the unit when enabled Multitexturing save texture coordinates predefined gl_TexCoord [0] in. gl_TexCoord [0] after interpolation incoming fragment shader, texture unit by self-defined variables sampler2D baseTexture using texture2D function to obtain a pixel value. The final rendering is as follows:

4. Reference

[1]. Under a few simple GLSL Shader
[2]. GLSL texture map

Guess you like

Origin www.cnblogs.com/charlee44/p/11427066.html