游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3734|回复: 0

配合OpenSceneGraph初步效果截图

[复制链接]

2万

主题

2万

帖子

6万

积分

论坛元老

Rank: 8Rank: 8

积分
66484
QQ
发表于 2010-1-2 10:42:00 | 显示全部楼层 |阅读模式


源代码:

#include<hgl/win.h>
#include<hgl/FlowObject.H>
#include<hgl/OpenGL.H>

#include"../osg.h"
#include <osg/Config>
#include <osg/ComputeBoundsVisitor>
#include <osg/ShadeModel>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>
#include <osgShadow/ShadowedScene>
#include <osgShadow/ShadowMap>
#include <osgUtil/Optimizer>

using namespace hgl;

class TestObject:public FlowObject
{
        osg::ref_ptr<osgViewer::Viewer>                        viewer;
        osg::ref_ptr<osgViewer::GraphicsWindow>        window;
        osg::ref_ptr<osgGA::EventQueue>                        event_queue;

        osg::ref_ptr<osg::Node>                                        model;

        osg::ref_ptr<osgShadow::ShadowedScene>        shadow_scene;
        osg::ref_ptr<osgShadow::ShadowMap>                sm;

private:

        bool MouseLeftDownProc        (int x,int y,uint)        {        event_queue->mouseButtonPress        ( x, y, 1);        return(true);        }       
        bool MouseLeftUpProc        (int x,int y,uint)        {        event_queue->mouseButtonRelease        ( x, y, 1);        return(true);        }
        bool MouseMidDownProc        (int x,int y,uint)        {        event_queue->mouseButtonPress        ( x, y, 2);        return(true);        }       
        bool MouseMidUpProc                (int x,int y,uint)        {        event_queue->mouseButtonRelease        ( x, y, 2);        return(true);        }
        bool MouseRightDownProc        (int x,int y,uint)        {        event_queue->mouseButtonPress        ( x, y, 3);        return(true);        }       
        bool MouseRightUpProc        (int x,int y,uint)        {        event_queue->mouseButtonRelease        ( x, y, 3);        return(true);        }
        bool MouseMoveProc                (int x,int y,uint)        {        event_queue->mouseMotion                ( x, y );        return(true);        }

        void SetMouse()
        {
                SetEventCall(OnMouseLeftDown,        this,MouseLeftDownProc        );
                SetEventCall(OnMouseLeftUp,                this,MouseLeftUpProc        );
                SetEventCall(OnMouseMidDown,        this,MouseMidDownProc        );
                SetEventCall(OnMouseMidUp,                this,MouseMidUpProc                );
                SetEventCall(OnMouseRightDown,        this,MouseRightDownProc        );
                SetEventCall(OnMouseRightUp,        this,MouseRightUpProc        );
                SetEventCall(OnMouseMove,                this,MouseMoveProc                );
        }

private:

        osg::ref_ptr<osg::Node> CreateLight(osg::ref_ptr<osg::Node> model)
        {
                osg::ComputeBoundsVisitor cbbv;

                model->accept(cbbv);

                osg::BoundingBox bb=cbbv.getBoundingBox();

                osg::Vec4 lightpos;
                float radius=bb.radius()*2.0f;

                lightpos.set(        bb.xMax()+radius,
                                                bb.yMax()+radius,
                                                bb.zMax()+radius,
                                                1.0f);

                osg::ref_ptr<osg:ightSource> ls=new osg::LightSource();

                osg::Light *l=ls->getLight();

                l->setPosition(lightpos);
                l->setAmbient(osg::Vec4(1.0,1.0,1.0,1.0));
                l->setDiffuse(osg::Vec4(1.0,1.0,1.0,1.0));

                osg::StateSet *state=model->getOrCreateStateSet();

                return ls.get();
        }

        void CreateShadowScene()
        {
                shadow_scene=new osgShadow::ShadowedScene();

                sm=new osgShadow::ShadowMap();

                shadow_scene->setShadowTechnique(sm);

                shadow_scene->addChild(CreateLight(model.get()));
                shadow_scene->addChild(model.get());
        }

        void CreateViewer()
        {
                osgUtil::Optimizer opt;
                opt.optimize(shadow_scene.get());

                viewer=new osgViewer::Viewer;

                window = viewer->setUpViewerAsEmbeddedInWindow(0,0,GetScreenWidth(),GetScreenHeight());

                event_queue=window->getEventQueue();

                viewer->setSceneData(shadow_scene.get());
                viewer->setCameraManipulator(new osgGA::TrackballManipulator);
                viewer->realize();
        }

public:

        TestObject(const char *filename)
        {       
                model = osgDB::readNodeFile(filename);

                if(model)
                {
                        CreateShadowScene();
                        CreateViewer();
                        SetMouse();
                }
        }

        void Draw()
        {
                if(viewer)
                        viewer->frame();
        }
};

void GameMain(int argc,wchar_t **argv)
{
        SystemInitInfo sii;
        GameApplication app;

        sii.info.ProjectName=L"OpenSceneGraph 查看器";
        sii.info.ProjectCode=L"OpenSceneGraph Viewer";

        sii.graphics.gl.MultiSample=4;

        if(!app.Init(&sii))return;

    char filename[1024];

        UnicodeToAnsi(filename,1024,argv[1]);

        app.SetStart(new TestObject(filename));

        app.Run();
}

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2024-5-4 10:33

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表