/* -*-c++-*- header - Open Producer - Copyright (C) 2002 Don Burns
 * Distributed under the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE (LGPL)
 * as published by the Free Software Foundation.
 */
#ifndef OSG_SCENE_HANDLER_H
#define OSG_SCENE_HANDLER_H

#include <Producer/Export>

#include <iostream>

#include <Producer/Camera>
#include <osgUtil/SceneView>
#include <osg/Matrix>

namespace Producer {

class OsgSceneHandler : public Producer::Camera::SceneHandler, public osgUtil::SceneView
{
    public :
        OsgSceneHandler( osg::DisplaySettings *ds = NULL) : osgUtil::SceneView(ds)
    {
#ifdef OSG_0_9_2
            mm = new osg::Matrix;
            pm = new osg::Matrix;
#else
            mm = new osg::RefMatrix;
            pm = new osg::RefMatrix;
#endif
        }

        void cull(Producer::Camera &cam) 
        {
            pm->set( cam.getProjectionMatrix());
            mm->set(cam.getPositionAndAttitudeMatrix());
            setProjectionMatrix( pm.get() );
            setModelViewMatrix( mm.get() );

            int x, y;
            unsigned int w, h;
            cam.getProjectionRectangle( x, y, w, h );

            setViewport( x, y, w, h );
#ifdef _windows_is_non_standard
            SceneView::cull();
#else
            osgUtil::SceneView::cull();
#endif
        }

        void draw(Producer::Camera &) 
        {
#ifdef _windows_is_non_standard
            SceneView::draw();
#else
            osgUtil::SceneView::draw();
#endif
        }

        void setContextID( int id )
        {
            getState()->setContextID( id );
        }

        private :
#ifdef OSG_0_9_2
            osg::ref_ptr<osg::Matrix> mm;
            osg::ref_ptr<osg::Matrix> pm;
#else
            osg::ref_ptr<osg::RefMatrix> mm; 
            osg::ref_ptr<osg::RefMatrix> pm;
#endif

};

}

#endif
