Follow the steps that maybe you can find something wrong in osgText at OSG3.0, 
maybe something wrong in OSG before 3.0 also :
We will modify the osgViewerMFC example to create a very simply HUD on the 
window:
1: down the attachment in this mail and replace that two files in the 
osgViewerMFC folder.
2: press F5 too debug, and open a cow.osg, you can find a HUD at the top of the 
windows.
cook a coffee , and drink it , you will find something ASSERT have interrupted 
the debugging when you back (maybe some minutes),  you will find something 
wrong messages about osgText.dll in call stack window. If you don't add this 
HUD, the example can debugging forever...


Something wrong in the HUD, or in OSG3.0 osgText?
// MFC_OSG.cpp : implementation of the cOSG class
//
#include "stdafx.h"
#include "MFC_OSG.h"
#include "MFC_OSG_MDI.h"


class CPickHandle :
        public osgGA::GUIEventHandler
{
public:
        CPickHandle(osg::Camera* text)
        {
                hud = text;

        }
        ~CPickHandle(void)
        {

        }

public:
                bool handle(const osgGA::GUIEventAdapter& ea, 
osgGA::GUIActionAdapter& aa,osg::Object*, osg::NodeVisitor*)
                {
                        if(hud)
                        {
                                static int i = 0; 
                                i ++;
                                if(i%20 == 0)
                                {
                                        if(i > 5000)
                                                i = 0;

                                        osg::Geode* gnode = 
dynamic_cast<osg::Geode*>(hud->getChild(0));

                                        if(gnode)
                                        {
                                                osgText::Text* text = 
dynamic_cast<osgText::Text*>(gnode->getDrawable(0));
                                                if(text)
                                                {
                                                        char content[512];
                                                        sprintf(content,"Type = 
%d\nNo = %d\n\n\nLong = %d\nLat = %d\nHeight = %d\nSpeed = %d\n",i + 100,i + 
358, i%78, i *2+1,i * 5,i + 2);
                                                        wchar_t wcontent[512];
                                                        
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,content,512,wcontent,512);
                                                        text->setText(wcontent);
                                                }
                                        }
                                }
                        }

                        return false;
                }

private:
        osg::ref_ptr<osg::Camera> hud;
};


cOSG::cOSG(HWND hWnd) :
   m_hWnd(hWnd) 
{
}

cOSG::~cOSG()
{
    mViewer->setDone(true);
    Sleep(1000);
    mViewer->stopThreading();

    delete mViewer;
}

void cOSG::InitOSG(std::string modelname)
{
    // Store the name of the model to load
    m_ModelName = modelname;

    // Init different parts of OSG
    InitManipulators();
    InitSceneGraph();
    InitCameraConfig();
}

void cOSG::InitManipulators(void)
{
    // Create a trackball manipulator
    trackball = new osgGA::TrackballManipulator();

    // Create a Manipulator Switcher
    keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;

    // Add our trackball manipulator to the switcher
    keyswitchManipulator->addMatrixManipulator( '1', "Trackball", 
trackball.get());

    // Init the switcher to the first manipulator (in this case the only 
manipulator)
    keyswitchManipulator->selectMatrixManipulator(0);  // Zero based index Value
}


void cOSG::InitSceneGraph(void)
{
    // Init the main Root Node/Group
    mRoot  = new osg::Group;

    // Load the Model from the model name
    mModel = osgDB::readNodeFile(m_ModelName);

    // Optimize the model
    osgUtil::Optimizer optimizer;
    optimizer.optimize(mModel.get());
    optimizer.reset();

    // Add the model to the scene
    mRoot->addChild(mModel.get());
}

void cOSG::InitCameraConfig(void)
{
    // Local Variable to hold window size data
    RECT rect;

    // Create the viewer for this window
    mViewer = new osgViewer::Viewer();

    // Add a Stats Handler to the viewer
    mViewer->addEventHandler(new osgViewer::StatsHandler);
    
    // Get the current window size
    ::GetWindowRect(m_hWnd, &rect);

    // Init the GraphicsContext Traits
    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new 
osg::GraphicsContext::Traits;

    // Init the Windata Variable that holds the handle for the Window to 
display OSG in.
    osg::ref_ptr<osg::Referenced> windata = new 
osgViewer::GraphicsWindowWin32::WindowData(m_hWnd);

    // Setup the traits parameters
    traits->x = 0;
    traits->y = 0;
    traits->width = rect.right - rect.left;
    traits->height = rect.bottom - rect.top;
    traits->windowDecoration = false;
    traits->doubleBuffer = true;
    traits->sharedContext = 0;
    traits->setInheritedWindowPixelFormat = true;
    traits->inheritedWindowData = windata;

    // Create the Graphics Context
    osg::GraphicsContext* gc = 
osg::GraphicsContext::createGraphicsContext(traits.get());

    // Init a new Camera (Master for this View)
    osg::ref_ptr<osg::Camera> camera = new osg::Camera;

    // Assign Graphics Context to the Camera
    camera->setGraphicsContext(gc);

    // Set the viewport for the Camera
    camera->setViewport(new osg::Viewport(traits->x, traits->y, traits->width, 
traits->height));

    // Set projection matrix and camera attribtues
    camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    camera->setClearColor(osg::Vec4f(0.2f, 0.2f, 0.4f, 1.0f));
    camera->setProjectionMatrixAsPerspective(
        30.0f, 
static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0, 
1000.0);

    // Add the Camera to the Viewer
    //mViewer->addSlave(camera.get());
    mViewer->setCamera(camera.get());

    // Add the Camera Manipulator to the Viewer
    mViewer->setCameraManipulator(keyswitchManipulator.get());

    // Set the Scene Data
    mViewer->setSceneData(mRoot.get());

    // Realize the Viewer
    mViewer->realize();

    // Correct aspect ratio
    /*double fovy,aspectRatio,z1,z2;
    
mViewer->getCamera()->getProjectionMatrixAsPerspective(fovy,aspectRatio,z1,z2);
    aspectRatio=double(traits->width)/double(traits->height);
    
mViewer->getCamera()->setProjectionMatrixAsPerspective(fovy,aspectRatio,z1,z2);*/

        CreateHUD();
        HUDCamera->setGraphicsContext(gc);
    HUDCamera->setViewport(traits->x, traits->y, traits->width, traits->height);
        mViewer->addSlave(HUDCamera, false);
        mViewer->addEventHandler(new CPickHandle(HUDCamera));
}

void cOSG::PreFrameUpdate()
{
    // Due any preframe updates in this routine
}

void cOSG::PostFrameUpdate()
{

}

void cOSG::Render(void* ptr)
{
    cOSG* osg = (cOSG*)ptr;

    osgViewer::Viewer* viewer = osg->getViewer();

    // You have two options for the main viewer loop
    //      viewer->run()   or
    //      while(!viewer->done()) { viewer->frame(); }

    //viewer->run();
    while(!viewer->done())
    {
        osg->PreFrameUpdate();
        viewer->frame();
        osg->PostFrameUpdate();
        //Sleep(10);         // Use this command if you need to allow other 
processes to have cpu time
    }

    // For some reason this has to be here to avoid issue: 
    // if you have multiple OSG windows up 
    // and you exit one then all stop rendering
    AfxMessageBox("Exit Rendering Thread");

    _endthread();
}

void cOSG::CreateHUD()
{
        RECT rect;
        ::GetWindowRect(m_hWnd, &rect);
    HUDCamera = new osg::Camera;
    
HUDCamera->setProjectionMatrix(osg::Matrix::ortho2D(0,::GetSystemMetrics(SM_CXSCREEN),0,::GetSystemMetrics(SM_CYSCREEN)));
    HUDCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    HUDCamera->setViewMatrix(osg::Matrix::identity());
    HUDCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
    HUDCamera->setRenderOrder(osg::Camera::POST_RENDER);
    HUDCamera->setAllowEventFocus(false);
        osg::ref_ptr<osg::Geode> geode = new osg::Geode();
    std::string timesFont("fonts/SIMHEI.TTF");
    osg::ref_ptr<osg::StateSet> stateset = geode->getOrCreateStateSet();
    stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
    osg::Vec3 position(20.0f,::GetSystemMetrics(SM_CYSCREEN)-40,0.0f);
        osg::ref_ptr<osgText::Text> text = new  osgText::Text;
    geode->addDrawable( text );
        text->setCharacterSize(15);
    text->setFont(timesFont);
    text->setPosition(position);
    text->setText("What");
        text->setDataVariance(osg::Object::DYNAMIC);
    HUDCamera->addChild(geode);
}
#pragma once

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/api/win32/GraphicsWindowWin32>
#include <osgGA/TrackballManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/NodeTrackerManipulator>
#include <osgDB/DatabasePager>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osgManipulator/CommandManager>
#include <osgManipulator/ScaleAxisDragger>
#include <osgManipulator/TrackballDragger>
#include <osgManipulator/TranslateAxisDragger>
#include <osg/Camera>


class cOSG
{
public:
    cOSG(HWND hWnd);
    ~cOSG();

    void InitOSG(std::string filename);
    void InitManipulators(void);
    void InitSceneGraph(void);
    void InitCameraConfig(void);
    void SetupWindow(void);
    void SetupCamera(void);
    void PreFrameUpdate(void);
    void PostFrameUpdate(void);
    void Done(bool value) { mDone = value; }
    bool Done(void) { return mDone; }
    static void Render(void* ptr);

    osgViewer::Viewer* getViewer() { return mViewer; }
        void CreateHUD();

private:
    bool mDone;
    std::string m_ModelName;
    HWND m_hWnd;
    osgViewer::Viewer* mViewer;
    osg::ref_ptr<osg::Group> mRoot;
    osg::ref_ptr<osg::Node> mModel;
    osg::ref_ptr<osgGA::TrackballManipulator> trackball;
    osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator;
        osg::ref_ptr<osg::Camera> HUDCamera;


};
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to