#include<Windows.h>//要放在前面#include<osgDB/ReadFile>#include<osgViewer/Viewer>#include<osgGA/GUIEventAdapter>#include<osgViewer/ViewerEventHandlers>#include<osg/ShapeDrawable>#include<osg/Geode>#include<osg/StateSet>#include<osg/Image>#include<osg/Texture2D>#include<osg/Material>#include<iostream>osg::ref_ptr<osg::Geode> GreateBox(){ osg::ref_ptr<osg::Geode> geode = new osg::Geode;//Geode是Node的派生类,为了绘制图元的管理类 osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;//设置精度的类 osg::ref_ptr < osg::ShapeDrawable >shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0,0.0,0.0),1.0,10.0,10.0),hints.get()); osg::ref_ptr<osg::Material> material = new osg::Material; osg::ref_ptr<osg::Texture2D>texture2D = new osg::Texture2D; osg::ref_ptr<osg::Image> image; shape->setColor(osg::Vec4(0.5, 0.5, 0.5, 0.5));//颜色 hints->setDetailRatio(0.5);//设置精度 //设置材质 material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4f(1.0, 1.0, 1.0, 0.7));//面向,和光照颜色第四个参数管透明度? material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4f(1.0, 1.0, 1.0, 0.7));//混合 material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4f(1.0, 1.0, 1.0, 0.7));//反射 material->setShininess(osg::Material::FRONT_AND_BACK, 60.0);//反射面积 //设置纹理 image = osgDB::readImageFile("Images/lz.rgp");//不知为啥我就是读取不了jpg和png格式的图片 if (image.valid())//看看能用不? { texture2D->setImage(image.get()); } //把材质放进去,如果有就get状态,如果没有就set geode->getOrCreateStateSet()->setAttributeAndModes(material.get(), osg::StateAttribute::ON); geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);//透明 geode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);//深度测试 geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2D.get(), osg::StateAttribute::ON); geode->addDrawable(shape.get()); return geode;}int main(){ osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer; viewer->setSceneData(GreateBox()); return viewer->run();}
新闻热点
疑难解答