|
楼主 |
发表于 2014-10-12 09:39:14
|
显示全部楼层
本帖最后由 osgxch 于 2014-10-12 09:53 编辑
大师这是我的代码,正如大师所说,可能就是我在存储顶点时顺序有问题,但我还是不知道怎么存储,请大师指教。
#include "../Common/Common.h"
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include<osg/StateSet>
#include <osg/Image>
#include <osg/Texture2D>
#include <osg/Texture3D>
#include <osg/LineWidth>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/GUIEventAdapter>
#include <iostream>
#ifdef _DEBUG
#pragma comment(lib,"../Debug/Commond.lib")
#else
#pragma comment(lib,"../Release/Common.lib")
#endif
osg::ref_ptr<osg::Geode>CreateEllipsoid(float a,float b,int n,int m)
{
double PI = osg:I;
float x, y, z, angle_l, angle_v;
int i, j;
angle_l = 0.0;
angle_v = 0.0;
osg::ref_ptr<osg::Geode>geode = new osg::Geode;
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
//申请一些顶点
osg::ref_ptr<osg::Vec3Array>coords = new osg::Vec3Array;
//申请颜色
osg::ref_ptr<osg::Vec4Array>colors = new osg::Vec4Array;
//申请法向量
osg::ref_ptr<osg::Vec3Array>norms = new osg::Vec3Array;
geode->addDrawable(geom);
//设置顶点
geom->setVertexArray(coords.get());
//设置顶点关联方式
geom->addPrimitiveSet(new osg:rawArrays(osg::PrimitiveSet::Mode::TRIANGLE_STRIP, 0, 5));
//设置顶点颜色
geom->setColorArray(colors);
geom->setColorBinding(osg::Geometry::AttributeBinding::BIND_PER_VERTEX);
//设置法向量
geom->setNormalArray(norms);
geom->setNormalBinding(osg::Geometry::AttributeBinding::BIND_PER_VERTEX);
for (j = 0; j <= 1 + (m + 1) / 2;j++)
{
for (i = 0; i <= n / 4 + 1;i++)
{
angle_l = 2 * PI / n*i;
angle_v = PI / m*j;
x = a*sin(angle_v)*cos(angle_l);
y = a*sin(angle_v)*sin(angle_l);
z = b*cos(angle_v);
osg::ref_ptr<osg::ShapeDrawable>point1 = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(x, y, z), 0.2));
osg::ref_ptr<osg::ShapeDrawable>point2 = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(x, -y, z), 0.2));
osg::ref_ptr<osg::ShapeDrawable>point3 = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(-x, y, z), 0.2));
osg::ref_ptr<osg::ShapeDrawable>point4= new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(-x, -y, z), 0.2));
osg::ref_ptr<osg::ShapeDrawable>point5 = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(x, y, -z), 0.2));
osg::ref_ptr<osg::ShapeDrawable>point6 = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(x, -y, -z), 0.2));
osg::ref_ptr<osg::ShapeDrawable>point7 = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(-x, y, -z), 0.2));
osg::ref_ptr<osg::ShapeDrawable>point8 = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(-x, -y, -z), 0.2));
//设置顶点球颜色
point1->setColor(osg::Vec4(x, y, z, 1.0));
point2->setColor(osg::Vec4(x, -y, z, 1.0));
point3->setColor(osg::Vec4(-x, y, z, 1.0));
point4->setColor(osg::Vec4(-x, -y, z, 1.0));
point5->setColor(osg::Vec4(x, y, -z, 1.0));
point6->setColor(osg::Vec4(x, -y, -z, 1.0));
point7->setColor(osg::Vec4(-x, y, -z, 1.0));
point8->setColor(osg::Vec4(-x, -y, -z, 1.0));
//顶点
coords->push_back(osg::Vec3(x, y, z));
coords->push_back(osg::Vec3(x, -y, z));
coords->push_back(osg::Vec3(-x, y, z));
coords->push_back(osg::Vec3(-x, -y, z));
coords->push_back(osg::Vec3(x, y, -z));
coords->push_back(osg::Vec3(x, -y, -z));
coords->push_back(osg::Vec3(-x, y, -z));
coords->push_back(osg::Vec3(-x, -y, -z));
//颜色
colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 0.5));
colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 0.5));
colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 0.5));
colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 0.5));
colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 0.5));
colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 0.5));
colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 0.5));
colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 0.5));
//法向量
norms->push_back(osg::Vec3(x, y, z));
norms->push_back(osg::Vec3(x, -y, z));
norms->push_back(osg::Vec3(-x, y, z));
norms->push_back(osg::Vec3(-x, -y, z));
norms->push_back(osg::Vec3(x, y, -z));
norms->push_back(osg::Vec3(x, -y, -z));
norms->push_back(osg::Vec3(-x, y, -z));
norms->push_back(osg::Vec3(-x, -y, -z));
geode->addDrawable(point1.get());
geode->addDrawable(point2.get());
geode->addDrawable(point3.get());
geode->addDrawable(point4.get());
geode->addDrawable(point5.get());
geode->addDrawable(point6.get());
geode->addDrawable(point7.get());
geode->addDrawable(point8.get());
}
}
return geode;
}
int main()
{
osg::ref_ptr < osgViewer::Viewer >viewer = new osgViewer::Viewer;
osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("glider.osg");
osg::ref_ptr<osg::Group>group = new osg::Group;
group->addChild(CreateEllipsoid(30.0, 50.0, 100, 49));
viewer->setSceneData(group);
return viewer->run();
} |
|