查看: 1008|回复: 5

大家看看这个代码

[复制链接]

该用户从未签到

发表于 2010-6-5 20:49:24 | 显示全部楼层 |阅读模式
// testNentw.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


/* OpenSceneGraph example, osganimate.
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy
*  of this software and associated documentation files (the "Software"), to deal
*  in the Software without restriction, including without limitation the rights
*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the Software is
*  furnished to do so, subject to the following conditions:
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*  THE SOFTWARE.
*/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#include <osgUtil/Optimizer>

#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgDB/Registry>

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>

#include <osg/Geode>
#include <osg/Camera>
#include <osg/ShapeDrawable>
#include <osg/Sequence>
#include <osg/PolygonMode>
#include <osg/io_utils>

#include <osgText/Font>
#include <osgText/Text>

//
int  mytest;

bool test = false;
class TextCallBack : public osg::NodeCallback
{
public:

TextCallBack()

{

data_size = 1000;

data_index = 0;

last_update = 0;

int i;

for (i = 0  ; i < data_size ; i++)

{

data[i] = i;

sprintf(str[i],"%d",i);



}

test = true;

printf("\naisdhfjkaaaaaaa\n");

printf("%d\n",i);

//this_text = text;

};


virtual void operator()( osg::Node* node,

osg::NodeVisitor* nv )

{

if (test == true)

{


osg::Geode *geode=dynamic_cast<osg::Geode *>(node);


mytest++;

if(geode)

{


if (data_index < data_size&&(mytest%60==0))

{

//char string[256];


//sprintf(string, "%d", data[data_index++]);

//
sprintf(string, "%d", mytest);


//Sleep(1000);

for (int i = 0 ; i < geode->getNumDrawables() ; i++)

{

osgText::Text* this_text = (osgText::Text *)geode->getDrawable(i);

if (!this_text)

{

continue;

}

this_text->setText(str[data_index++]);


}

}


}


traverse( node, nv );

}



}

//printf("asdfasdg!");

protected:

int data[1000];

char str[1000][1000];

int data_size;

int data_index;

int last_update;

//osgText::Text* this_text;
};

osg::ref_ptr<osg::Node> createText()
{

osg::Group *node = new osg::Group;

osg::Geode *geode = new osg::Geode;

osgText::Text* text = new osgText::Text;

osgText::Font* font = osgText::readFontFile("fonts/arial.ttf");

osg::Vec4 layoutColor(1.0f,1.0f,0.0f,1.0f);

float layoutCharacterSize = 20.0f;

float windowHeight = 1024.0f;

float windowWidth = 1280.0f;

float margin = 50.0f;


node->addChild(geode);

text->setFont(font);

text->setColor(layoutColor);

text->setCharacterSize(layoutCharacterSize);

text->setPosition(osg::Vec3(margin,windowHeight-margin,0.0f));

text->setLayout(osgText::Text:EFT_TO_RIGHT);

text->setText("openscenegraph!");

geode->addDrawable(text);


return node;
}

void main()
{

osgViewer::Viewer viewer;

osg::Group *root = new osg::Group;

osg::Geode *geode = new osg::Geode;

osgText::Text* text = new osgText::Text;

osgText::Font* font = osgText::readFontFile("fonts/arial.ttf");

osg::Vec4 layoutColor(1.0f,1.0f,0.0f,1.0f);

float layoutCharacterSize = 20.0f;

float windowHeight = 1024.0f;

float windowWidth = 1280.0f;

float margin = 50.0f;


root->addChild(geode);


text->setFont(font);

text->setColor(layoutColor);

text->setCharacterSize(layoutCharacterSize);

text->setPosition(osg::Vec3(margin,windowHeight-margin,0.0f));

text->setLayout(osgText::Text::LEFT_TO_RIGHT);

text->setText("wangjunchao!");


geode->addDrawable(text);


geode->setDataVariance(osg::Object:YNAMIC);

geode->setUpdateCallback(new TextCallBack());


viewer.setSceneData(root);

//viewer.setSceneData(osgDB::readNodeFile("cow.osg"));

/*while(!viewer.done())

{


viewer.frame();

}*/

viewer.run();
}

该用户从未签到

 楼主| 发表于 2010-6-5 21:30:53 | 显示全部楼层
人来啊,都来看啊,看这个代码出什么问题了

该用户从未签到

发表于 2010-6-7 08:21:49 | 显示全部楼层
请不要重复发帖,谢谢

该用户从未签到

 楼主| 发表于 2010-6-7 18:28:38 | 显示全部楼层
array,我并没有重复发帖,主要是代码部分有些改动,像你上次说的那些问题我都尝试改过了,可是运行的时候还是会出错,就是突然弹出来一个框,提示“中断”或者“继续”,我很仔细的去对比了代码,可是总是感觉代码方面不会有多少问题,在别的版本上运行同样会出现问题,我想问一下是否是osg在callback中存在bug?

该用户从未签到

发表于 2010-6-8 08:14:45 | 显示全部楼层
OSG的Callback不存在任何问题

在main函数中添加下面一行:
  1. text->setDataVariance(osg::Object::DYNAMIC);
复制代码

程序运行一切正常。

印象中我在QQ群里早已提示了这一问题,不知道为什么您一直拒绝按照这个提示去做~~不过看来您的问题就是出在这里。我之前并没有认真去读您的程序;事实上我也没有足够的时间看代码,而是希望帮助您自行发现问题的所在。

该用户从未签到

 楼主| 发表于 2010-6-8 09:15:12 | 显示全部楼层
谢谢,我知道了,之前对setDataVariance有所了解,不过还是不清楚,最近看了些资料知道了,谢谢array的解答。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表