# 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.cpp`]

    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||GLEW_VERSION_3_0))
        {
          vl::Log::error("OpenGL Shading Language not supported.\n");
          vl::Time::sleep(3000);
          exit(1);
        }

        vl::Log::print(appletInfo());

        rendering()->as<vl::Rendering>()->transform()->addChild(mTransform1.get());
        rendering()->as<vl::Rendering>()->transform()->addChild(mTransform2.get());
        rendering()->as<vl::Rendering>()->transform()->addChild(mTransform3.get());
        rendering()->as<vl::Rendering>()->transform()->addChild(mTransform4.get());
        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);
        model->computeNormals();

        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||GLEW_VERSION_3_0)
        {
          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_Has_Geometry_Shader)
          {
            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(vl::red);
            vl::Log::print("GL_NV_geometry_shader4 not supported.\n");
          }
        }
      }

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

    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 v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:29.
Permission is granted to use this page to write and publish articles regarding Visualization Library.