|
楼主 |
发表于 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;
} |
|