查看: 1019|回复: 5

大家来看看这个代码有什么不妥

[复制链接]

该用户从未签到

发表于 2010-6-4 17:23:36 | 显示全部楼层 |阅读模式
/* 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>


class TextCallBack : public osg::NodeCallback
{
public:

TextCallBack(osgText::Text *text)

{

data_size = 1000;

data_index = 0;

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

{

data[i] = i;

}

this_text = text;

};


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

osg::NodeVisitor* nv )

{

char string[256];

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

this_text->setText(string);


Sleep(1000);

}

protected:

int data[1000];

int data_size;

int data_index;

osg::ref_ptr<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);


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

root->setUpdateCallback(new TextCallBack(text));


viewer.setSceneData(root);

viewer.run();
}

该用户从未签到

 楼主| 发表于 2010-6-4 17:24:25 | 显示全部楼层
执行的时候总是会报一个错误

该用户从未签到

 楼主| 发表于 2010-6-4 19:34:14 | 显示全部楼层
唉,怎么没人看呢

该用户从未签到

发表于 2010-6-7 08:15:50 | 显示全部楼层
  1. virtual void operator()( osg::Node* node, osg::NodeVisitor* nv )
  2. {
  3. char string[256];
  4. sprintf(string, "%d", data[data_index++]);
  5. this_text->setText(string);
  6. Sleep(1000);
  7. }
复制代码
首先这里的Sleep完全是多余的;其次sprintf一行必然会出错,因为data_index迟早会超出data数组的范围。OSG方面的代码倒是没看出太大的问题

该用户从未签到

 楼主| 发表于 2010-6-7 18:25:55 | 显示全部楼层
array,首先非常感谢你的回答,可是,问题还是存在,不知道为什么,我后来将data_index加了一个if判断,而且sleep也已经去掉了,可是,代码还是有问题的,每次运行的时候都会产生中断,有时候是跳转到线程的异常,有时候是内存的异常,你可以试一下这段代码

该用户从未签到

发表于 2010-6-8 08:25:57 | 显示全部楼层
我已经在另一个帖子里试验并回复了,如果您按照我之前在QQ群中所说的去实践的话,想必早已解决这个所谓的问题了——这只是再常见不过的DataVariance动态变度问题而已
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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