查看: 3054|回复: 1

代码中cin为何不能使用?

[复制链接]

该用户从未签到

发表于 2008-3-21 16:52:49 | 显示全部楼层 |阅读模式
// drawLine.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <osg/Drawable>
#include <osg/Geode>
#include <osgViewer/Viewer>
#include <iostream>
using namespace std;

typedef struct Point
{
GLint x;
GLint y;
GLint z;
}Pt;
class PolyLine
{
public:
Pt* p;
Pt* q;
int num;
PolyLine(){p=new Pt;q=new Pt;}
void get_vertices(Pt*s)
{
p=s;
}
void get_num_veitices(int s){num=s;}

};
void line(Pt *p1,Pt* p2)
{
glBegin(GL_LINE_LOOP );
glColor3f(0.0,1.0,0.0);
glVertex3d(p1->x,p1->y,p1->z);
glVertex3d(p2->x,p2->y,p2->z);
glEnd();
}
void line2i(PolyLine *s)//使用OpenGL函数进行划线,有六个参数,分别是起点的三个坐标和终点的三个坐标
{
int i=0;
cin>>s->num;
for(i=0;i<s->num;i++)
{
//cout<<(s->p+i)->x<<(s->p+i)->y<<cout<<(s->p+i)->z<<endl;
//cout<<(s->p+i+1)->x<<(s->p+i+1)->y<<cout<<(s->p+i+1)->z<<endl;

cin>>(s->p)->x>>(s->p)->y>>(s->p)->z;
if (i>0)
{line(s->q,s->p);}
(s->q)->x=(s->p)->x;
(s->q)->y=(s->p)->y;
(s->q)->z=(s->p)->z;
}
}


class DrawLine:public osg:rawable
{
public:
  PolyLine *s;
  DrawLine() {}
  DrawLine(PolyLine *p) {s=p;}//构造函数
     DrawLine(const DrawLine& teapot,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY)sg::Drawable(teapot,copyop) {} //复制构造函数
  META_Object(myDrawLineApp,DrawLine); //这个是用来干什么的呢?
  virtual void drawImplementation(osg::RenderInfo&) const
  {
line2i(s);
  }
virtual osg::BoundingBox computeBound() const
        {
            osg::BoundingBox bbox;
bbox.expandBy(osg::Vec3(0,0,0));
bbox.expandBy(osg::Vec3(10,10,10));
            return bbox;
        }
protected:
virtual ~DrawLine() {}
};
int _tmain(int argc, _TCHAR* argv[])
{
PolyLine *s= new PolyLine;
osgViewer::Viewer viewer;
osg::Geode* geode = new osg::Geode();
geode->addDrawable(new DrawLine(s));

    // add model to viewer.
    viewer.setSceneData( geode );
return viewer.run() ;
}

但不调试是正确的,但是一运行就死机,各位高手看看是怎么了

该用户从未签到

发表于 2008-3-21 17:08:55 | 显示全部楼层
请一定理解drawImplementation函数的意义!OSG采用:用户/筛选/渲染分线程来实现实时渲染,而这个函数是在渲染线程中使用的,绝不可以在这个函数中执行等待用户输入等操作,必然会造成系统崩溃。
有很多种更规范的方法来获取用户输入并传递到绘图类中,例如编写名为DrawLine::setPoints的函数,并在进入仿真循环之前(viewer.run)将cin的数据传入setPoints;使用回调Callback,在回调中读取用户从GUI控件的输入(依然不要使用cin,因为回调属于用户线程);使用GUIEventHandler等等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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