查看: 1735|回复: 6

读取node和创建node放入同一个group时不能显示

[复制链接]

该用户从未签到

发表于 2010-7-2 15:08:10 | 显示全部楼层 |阅读模式
osg::ref_ptr<osgModeling:oft> geom3 = new osgModeling::Loft( path.get(), section_QM.get() );
//桥面位置
osg::ref_ptr<osg::Geode> geode3 = new osg::Geode;
    geode3->addDrawable( geom3.get() );

osg::ref_ptr<osg:ositionAttitudeTransform> lofts3 = new osg::PositionAttitudeTransform;
    lofts3->setPosition( osg::Vec3(0.0f, 0.0f, 0.0f) );
lofts3->addChild( geode3.get() );
lofts3->setStateSet(stateset2);
//读取桥墩模型文件,设置位置
osg::ref_ptr<osg::Node> bridgeNode=osgDB::readNodeFile("D:\\temp\\converted.osg");

osg::ref_ptr<osg::PositionAttitudeTransform>  bridgePos=new osg::PositionAttitudeTransform;
if(bridgeNode)
{   
  bridgePos->setPosition(osg::Vec3(0.0f, 0.0f, 0.0f));  
  bridgePos->addChild(bridgeNode.get());
  bridgePos->setStateSet(stateset2);
  
}

//各部分节点放入根节点
osg::ref_ptr<osg::Group> root = new osg::Group;

root->addChild(lofts3.get());
root->addChild(bridgePos.get());

   osgViewer::Viewer mViewer;  
   mViewer.setSceneData(root);
    mViewer.run();



如上面的代码所示
我创建了一个lofts3对象,并通过文件读取了bridgePos对象。
当root单独加载lofts3或bridgePos为它的子节点时,两者都能在viewer中显示,但同时把两者设为root的子节点时,则viewer则不能显示出任何一个对象。
这是为什么
如何改才能让viewer同时显示这两个子节点

该用户从未签到

发表于 2010-7-2 15:32:52 | 显示全部楼层
stateset2是个什么状态~~

不应该呀,有啥提示错误没

该用户从未签到

发表于 2010-7-2 16:57:02 | 显示全部楼层
lofts3->setPosition( osg::Vec3(0.0f, 0.0f, 0.0f) );
bridgePos->setPosition(osg::Vec3(0.0f, 0.0f, 0.0f));  
有可能是模型之间嵌套了,试一试放在不同位置

该用户从未签到

 楼主| 发表于 2010-7-5 15:34:56 | 显示全部楼层
2# tianxiao888

无错误提示
stateset2是我自定义的一个状态

应该和这个状态无关,因为单独一个放到root里是能够显示的

该用户从未签到

 楼主| 发表于 2010-7-5 15:36:46 | 显示全部楼层
3# cug_osg


首先两个模型大小差别比较大 就算是同一位置应该都能看到

现在的状况是lofts3和bridgePos放入到root后两个模型都不显示了

该用户从未签到

发表于 2010-7-5 16:57:51 | 显示全部楼层
您没有说明stateset2是什么内容。如果您打算自行测试的话,可以先屏蔽两个setStateSet(),看一看此时能否加载和显示两个模型

该用户从未签到

 楼主| 发表于 2010-7-6 10:44:06 | 显示全部楼层
本帖最后由 tylr 于 2010-7-6 10:45 编辑

6# array

屏蔽掉stateset2后仍然不能显示
我再把完整的代码发一下:

// readDXF.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "dxfDataTypes.h"
#include "codeValue.h"
#include "dxfFile.h"
#include "dxfReader.h"
#include "dxfBlock.h"
#include "dxfEntity.h"
#include "dxfDataTypes.h"
#include "scene.h"
#include "codeValue.h"
#include "pointData.h"

#include <map>
#include <vector>
#include <iostream>
#include <utility>
#include <sstream>
#include <math.h>
#include <iomanip>

#include <osg/Group>
#include <osg/AlphaFunc>
#include <osg/Billboard>
#include <osg/BlendFunc>
#include <osg/Depth>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Material>
#include <osg/Math>
#include <osg/MatrixTransform>
#include <osg/PolygonOffset>
#include <osg/Projection>
#include <osg/ShapeDrawable>
#include <osg/StateSet>
#include <osg/Switch>
#include <osg/Texture2D>
#include <osg/TexEnv>
#include <osg/TexGen>
#include <osg/TextureCubeMap>
#include <osg/TexEnvCombine>
#include<osg/positionattitudetransform>
#include <osg/Array>

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/api/win32/GraphicsWindowWin32>
#include <osgGA/TrackballManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgDB/DatabasePager>
#include <osgDB/Registry>
#include <osgDB/Input>
#include <osgDB/ReadFile>
#include <osgDB/FileUtils>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>
#include <osgUtil/IntersectVisitor>
#include <osgUtil/SmoothingVisitor>
#include <osgUtil/ReflectionMapGenerator>
#include <osgUtil/HighlightMapGenerator>
#include <osgText/Text>
#include <iostream>
#include <osgModeling/Helix>
#include <osgModeling/Bezier>
#include <osgModeling/Extrude>
#include <osgModeling/Lathe>
#include <osgModeling/Loft>

using namespace std;
int  _tmain(int argc, _TCHAR* argv[])
{

int i;
osg::ref_ptr<osgModeling::Curve> path = new osgModeling::Curve;//创建路径线
double p_sum=0;
double p_temp[9];

for(i=0;i<5;i++)
{   
       path->addPathPoint(PathVertex);
    if(i>=1)
    {
     p_temp[i-1]=(PathVertex-PathVertex[i-1]).length();
     
     p_sum+=p_temp[i-1];
    }
}
   
//上表面线放样
    osg::ref_ptr<osgModeling::Curve> section_LM = new osgModeling::Curve;
double t_lm[9];
double t_sum=0;
for(i=0;i<10;i++)
{
  section_LM->addPathPoint( LMXvertex );
  if(i>=1)
  {
   t_lm[i-1]=(LMXvertex-LMXvertex[i-1]).length();
            t_sum+=t_lm[i-1];
  }
  
   
}
    osg::ref_ptr<osgModeling:oft> geom1 = new osgModeling::Loft( path.get(), section_LM.get() );
osg::StateSet* stateset1 = new osg::StateSet();
    osg::Image* image1 = osgDB::readImageFile("D:\\temp\\shangbiaomian.jpg");

    if (image1)
    {
  osg::Texture2D* texture = new osg::Texture2D;
        texture->setImage(image1);
  texture->setWrap(osg::Texture::WRAP_S,osg::Texture2D::REPEAT);
  texture->setWrap(osg::Texture::WRAP_T,osg::Texture2D::REPEAT);
  texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture2D::LINEAR);
  texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture2D::LINEAR);
        stateset1->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
  
        
    }
  osg::Vec3Array* v=new osg::Vec3Array(5*10);
  v=(osg::Vec3Array*)(geom1->getVertexArray());
  osg::Vec2Array& t = *(new osg::Vec2Array(5*10));
  
  double deltaTexRow=1.0/(5-1)*10;
  double deltaTexCol=1.0/(10-1)*1;
  osg::Vec2 tex(0.0f,0.0f);
  osg::Vec3 pos(0.0f,0.0f,0.0f);
  int vi=0;
     for(int r=0;r<5;r++)
  {
   tex.x()=0.0f;   
   for(int c=0;c<10;c++)
   {
    t[vi].set(tex.x(),tex.y());
    vi++;
    if(c==9) continue;
    tex.x()+=t_lm[c]/t_sum;
   
   }
   if(r==4) continue;
   tex.y()+=p_temp[r]/p_sum*10;
  }
  osg::Vec4ubArray& color = *(new osg::Vec4ubArray(1));        
  color[0].set(255,255,255,255);
  geom1->setColorArray(&color);
  geom1->setColorBinding(osg::Geometry::BIND_OVERALL);
  geom1->setTexCoordArray(0,&t);
   
    //上表面放样体位置
osg::ref_ptr<osg::Geode> geode1 = new osg::Geode;
    geode1->addDrawable( geom1.get() );
osg::ref_ptr<osg:ositionAttitudeTransform> lofts1 = new osg::PositionAttitudeTransform;
    lofts1->setPosition( osg::Vec3(0.0f, 0.0f, 0.0f) );
lofts1->addChild( geode1.get() );
lofts1->setStateSet(stateset1);
    osg::StateSet* stateset2 = new osg::StateSet();     
osg::Image* image2 = osgDB::readImageFile("D:\\temp\\shuini.jpg");

    if (image2)
    {
  osg::Texture2D* texture = new osg::Texture2D;
        texture->setImage(image2);
  texture->setWrap(osg::Texture::WRAP_S,osg::Texture2D::REPEAT);
  texture->setWrap(osg::Texture::WRAP_T,osg::Texture2D::REPEAT);
  texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture2D::LINEAR);
  texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture2D::LINEAR);
        stateset2->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
    }

//桥梁表面放样
osg::ref_ptr<osgModeling::Curve> section_QL = new osgModeling::Curve;
double t_ql[24];
double t_ql_sum=0;
for(i=0;i<25;i++)
{
  section_QL->addPathPoint( QLXOvertex );
  if(i>=1)
  {
   t_ql[i-1]=(QLXOvertex-QLXOvertex[i-1]).length();
            t_ql_sum+=t_ql[i-1];
  }
}

    osg::ref_ptr<osgModeling::Loft> geom2 = new osgModeling::Loft( path.get(), section_QL.get() );
geom2->setColorArray(&color);
geom2->setColorBinding(osg::Geometry::BIND_OVERALL);

    //桥梁外表面放样位置
osg::ref_ptr<osg::Geode>  geode2=new osg::Geode;
geode2->addDrawable(geom2.get());
osg::ref_ptr<osg::PositionAttitudeTransform>  lofts2=new osg::PositionAttitudeTransform;
lofts2->setPosition(osg::Vec3(0.0f, 0.0f, 0.0f));
lofts2->addChild(geode2.get());
lofts2->setStateSet(stateset2);

//桥面线放样
osg::ref_ptr<osgModeling::Curve> section_QM = new osgModeling::Curve;
double  t_qm[30];
double t_qm_sum=0;
    for(i=0;i<31;i++)
{
  section_QM->addPathPoint( QMXvertex );
  if(i>=1)
  {
   t_qm[i-1]=(QMXvertex-QMXvertex[i-1]).length();
            t_qm_sum+=t_qm[i-1];
  }
}
    osg::ref_ptr<osgModeling::Loft> geom3 = new osgModeling::Loft( path.get(), section_QM.get() );
//桥面位置
osg::ref_ptr<osg::Geode> geode3 = new osg::Geode;
    geode3->addDrawable( geom3.get() );

osg::ref_ptr<osg::PositionAttitudeTransform> lofts3 = new osg::PositionAttitudeTransform;
    lofts3->setPosition( osg::Vec3(0.0f, 0.0f, 0.0f) );
lofts3->addChild( geode3.get() );
lofts3->setStateSet(stateset2);
//读取桥墩模型文件,设置位置
osg::ref_ptr<osg::Node> bridgeNode=osgDB::readNodeFile("D:\\temp\\converted.osg");

osg::ref_ptr<osg::PositionAttitudeTransform>  bridgePos=new osg::PositionAttitudeTransform;
if(bridgeNode)
{   
  bridgePos->setPosition(osg::Vec3(0.0f, 0.0f, 0.0f));  
  bridgePos->addChild(bridgeNode.get());
  bridgePos->setStateSet(stateset2);
  
}

//各部分节点放入根节点
osg::ref_ptr<osg::Group> root = new osg::Group;
    root->addChild( lofts1.get() );
root->addChild(lofts2.get());
root->addChild(lofts3.get());
root->addChild(bridgePos.get());

    bool ws=osgDB::writeNodeFile(*root,"D:\\test.osg");
   
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
    traits->x = 100;
    traits->y = 100;
    traits->width = 1024;
    traits->height = 768;
    traits->windowDecoration = true;
    traits->doubleBuffer = true;
    traits->sharedContext = 0;
    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
    osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get());
    if (!gw)
    {
        osg::notify(osg::NOTICE)<<"Error: unable to create graphics window."<<std::endl;
        return 1;
    }

    osgViewer::Viewer mViewer;  
    mViewer.getCamera()->setGraphicsContext(gc.get());
    mViewer.getCamera()->setViewport(0,0,1024,768);
    // create a tracball manipulator to move the camera around in response to keyboard/mouse events
mViewer.setCameraManipulator(new osgGA::TrackballManipulator);
    osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator(mViewer.getCamera()->getStateSet());
    mViewer.addEventHandler(statesetManipulator.get());
    // add the pick handler
//   mViewer.addEventHandler(new MyMatrixManipulator());
mViewer.setSceneData(root);

mViewer.run();

return 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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