# Visualization Library A lightweight C++ OpenGL middleware for 2D/3D graphics | [Home] [Tutorials] [All Classes] [Grouped Classes]

# OpenGL Shading Language Tutorial

This tutorial shows how simple it is to use the OpenGL Shading Language with Visualization Library.

The example below demostrates the use of vertex shaders, fragment shaders and geometry shaders.

[From `App_GLSL.hpp`]

    class App_GLSL: public BaseDemo
    {
    public:
      App_GLSL()
      {
        mTransform1 = new vl::Transform;
        mTransform2 = new vl::Transform;
        mTransform3 = new vl::Transform;
        mTransform4 = new vl::Transform;
        mTransform5 = new vl::Transform;
      }

      void initEvent()
      {
        if (!GLEW_ARB_shading_language_100)
        {
          vl::Log::error("GL_ARB_shading_language_100 not supported.\n");
          vl::Time::sleep(3000);
          openglContext()->quitApplication();
        }

        BaseDemo::initEvent();

        vl::VisualizationLibrary::rendering()->as<vl::Rendering>()->transform()->addChild(mTransform1.get());
        vl::VisualizationLibrary::rendering()->as<vl::Rendering>()->transform()->addChild(mTransform2.get());
        vl::VisualizationLibrary::rendering()->as<vl::Rendering>()->transform()->addChild(mTransform3.get());
        vl::VisualizationLibrary::rendering()->as<vl::Rendering>()->transform()->addChild(mTransform4.get());
        vl::VisualizationLibrary::rendering()->as<vl::Rendering>()->transform()->addChild(mTransform5.get());

        vl::ref<vl::ResourceDatabase> res_db = vl::loadResource("/models/3ds/monkey.3ds");
        vl::ref<vl::Geometry> model = res_db->get<vl::Geometry>(0);

        vl::ref<vl::Light> light = new vl::Light(0);

        vl::ref<vl::Effect> effect1 = new vl::Effect;
        effect1->shader()->setRenderState( light.get() );
        effect1->shader()->enable(vl::EN_LIGHTING);
        effect1->shader()->enable(vl::EN_DEPTH_TEST);
        effect1->shader()->enable(vl::EN_CULL_FACE);
        sceneManager()->tree()->addActor( model.get(), effect1.get(), mTransform1.get() );

        vl::ref<vl::Effect> effect2 = new vl::Effect;
        effect2->shader()->setRenderState( light.get() );
        effect2->shader()->enable(vl::EN_LIGHTING);
        effect2->shader()->enable(vl::EN_DEPTH_TEST);
        effect2->shader()->enable(vl::EN_CULL_FACE);
        sceneManager()->tree()->addActor( model.get(), effect2.get(), mTransform2.get() );

        vl::ref<vl::Effect> effect3 = new vl::Effect;
        effect3->shader()->setRenderState( light.get() );
        effect3->shader()->enable(vl::EN_LIGHTING);
        effect3->shader()->enable(vl::EN_DEPTH_TEST);
        effect3->shader()->enable(vl::EN_CULL_FACE);
        sceneManager()->tree()->addActor( model.get(), effect3.get(), mTransform3.get() );

        vl::ref<vl::Effect> effect4 = new vl::Effect;
        effect4->shader()->setRenderState( light.get() );
        effect4->shader()->enable(vl::EN_LIGHTING);
        effect4->shader()->enable(vl::EN_DEPTH_TEST);
        effect4->shader()->enable(vl::EN_CULL_FACE);
        sceneManager()->tree()->addActor( model.get(), effect4.get(), mTransform4.get() );

        vl::ref<vl::Effect> effect5 = new vl::Effect;
        effect5->shader()->setRenderState( light.get() );
        effect5->shader()->enable(vl::EN_LIGHTING);
        effect5->shader()->enable(vl::EN_DEPTH_TEST);
        effect5->shader()->enable(vl::EN_CULL_FACE);
        sceneManager()->tree()->addActor( model.get(), effect5.get(), mTransform5.get() );

        if (GLEW_ARB_shading_language_100)
        {
          vl::ref<vl::GLSLProgram> glsl;

          glsl = effect1->shader()->gocGLSLProgram();
          glsl->attachShader( new vl::GLSLVertexShader("/glsl/perpixellight.vs") );
          glsl->attachShader( new vl::GLSLFragmentShader("/glsl/perpixellight_cartoon.fs") );

          glsl = effect2->shader()->gocGLSLProgram();
          glsl->attachShader( new vl::GLSLVertexShader("/glsl/perpixellight.vs") );
          glsl->attachShader( new vl::GLSLFragmentShader("/glsl/perpixellight.fs") );

          glsl = effect3->shader()->gocGLSLProgram();
          glsl->attachShader( new vl::GLSLVertexShader("/glsl/perpixellight.vs") );
          glsl->attachShader( new vl::GLSLFragmentShader("/glsl/heat.fs") );

          glsl = effect4->shader()->gocGLSLProgram();
          glsl->attachShader( new vl::GLSLVertexShader("/glsl/perpixellight.vs") );
          glsl->attachShader( new vl::GLSLFragmentShader("/glsl/perpixellight_interlaced.fs") );

          if (GLEW_NV_geometry_shader4)
          {
            glsl = effect5->shader()->gocGLSLProgram();
            // a vertex shader is always needed when using geometry shaders
            glsl->attachShader( new vl::GLSLVertexShader("/glsl/diffuse.vs") );
            glsl->attachShader( new vl::GLSLGeometryShader("/glsl/triangle_fur.gs") );
            glsl->setGeometryInputType(vl::GIT_TRIANGLES);
            glsl->setGeometryOutputType(vl::GOT_TRIANGLE_STRIP);
            glsl->setGeometryVerticesOut( 3*6 );
          }
          else
          {
            effect5->shader()->gocMaterial()->setDiffuse(vlut::red);
            vl::Log::print("GL_NV_geometry_shader4 not supported.\n");
          }
        }
      }

      void run()
      {
        // animate the transforms
        mTransform1->setLocalMatrix( vl::mat4::translation(-2,2,0)  * vl::mat4::rotation( vl::Time::currentTime()*45, 0, 1, 0) );
        mTransform2->setLocalMatrix( vl::mat4::translation(+2,2,0)  * vl::mat4::rotation( vl::Time::currentTime()*45, 0, 1, 0) );
        mTransform3->setLocalMatrix( vl::mat4::translation(-2,-2,0) * vl::mat4::rotation( vl::Time::currentTime()*45, 0, 1, 0) );
        mTransform4->setLocalMatrix( vl::mat4::translation(+2,-2,0) * vl::mat4::rotation( vl::Time::currentTime()*45, 0, 1, 0) );
        mTransform5->setLocalMatrix( vl::mat4::translation(0,0,0)   * vl::mat4::rotation( vl::Time::currentTime()*45, 0, 1, 0) );
      }

      void shutdown() {}

    protected:
      vl::ref<vl::Transform> mTransform1;
      vl::ref<vl::Transform> mTransform2;
      vl::ref<vl::Transform> mTransform3;
      vl::ref<vl::Transform> mTransform4;
      vl::ref<vl::Transform> mTransform5;
    };

    // Have fun!

Visualization Library v2009.08 Reference Documentation
Copyright 2005-2009 Michele Bosi. All rights reserved.
Updated on Sun Nov 8 14:28:52 2009.
Permission is granted to use this page to write and publish articles regarding Visualization Library.