谈到气缸成一个球体,而不会挤压在两极 [英] Turning a cylinder into a sphere without pinching at the poles
问题描述
我正在生成一个星球开出了六边形网格。不需要的两极 - 使之成为一个更容易一些。有没有更好的方式来使气瓶成为一个球体,将有统一的六边形/三角形?
I'm working on generating a planet made out of a hexagon grid. The poles aren't needed - making this a bit easier. Is there a better way to turn the cylinder into a sphere that would have uniform hexagons/triangles?
下面是所需的步骤:
- 生成六边形的二维平面(OK)
- 旋转平面成筒(OK)
- 使气瓶成为一个球体/地圈(实物作品)
对于第2步,我只是用正弦和余弦来移动顶点成圆形。 对于第3步,现在我只是用:顶点[我] =顶点[I] .normalized *半径;
For step 2, I'm just using Sin and Cos to move the vertices into a circular shape. For step 3, right now I'm just using: vertices[i] = vertices[i].normalized * radius;
图像可视化的问题,因为它是目前。 http://i.stack.imgur.com/tKlwy.png 注意两极被故意切断。 红色部分表示什么人六边形网状的样子。我将不得不让他们大致相同的大小和方向,因为它们被用于游戏和可视元素。每个十六进制具有邻居列表和工作方式类似的曲线图基本上
Image to visualize the problem as it is currently. http://i.stack.imgur.com/tKlwy.png Note that the poles are cut off on purpose. The red parts show what one hexagon mesh looks like. I would have to keep them roughly the same size and orientation since they are used for gameplay and visual elements. Each hex has a list of neighbors and works like a graph basically.
推荐答案
相反缸到球体映射我会做一个球体三角...
Instead of cylinder to sphere mapping I would do a sphere triangulation...
-
我会先从2六边形
I would first start with 2 hexagons
- 在每次启动时极和结束赤道
- 或做半只和镜像中的其他时候都做...
然后递归细分三角形
- 让分线一半
- 更改中点的坐标,以便与球体表面
- 这将创建三角球体
- 细分为点的有效数字组成的六边形,并有足够的网格点
修改六边形中点坐标回六边形的平面
change the hexagon mid-point coordinates back to plane of hexagon
- 所以采取其他6点
- ,并计算平均坐标
- ,你会得到点为中...
事情是这样的:
有关更多的想法看看这里:
for more ideas look here:
- Location of highest density on a sphere
- Make a sphere with equidistant vertices
三角测量(无六角勘误的内容)
//---------------------------------------------------------------------------
#include <math.h>
#include "list.h"
class mesh
{
public:
class _pnt { public: double p[3]; _pnt(){}; _pnt(_pnt& a){ *this=a; }; ~_pnt(){}; _pnt* operator = (const _pnt *a) { *this=*a; return this; }; /*_pnt* operator = (const _pnt &a) { ...copy... return this; };*/ };
class _fac { public: int i0,i1,i2; _fac(){}; _fac(_fac& a){ *this=a; }; ~_fac(){}; _fac* operator = (const _fac *a) { *this=*a; return this; }; /*_fac* operator = (const _fac &a) { ...copy... return this; };*/ };
List<_pnt> pnt; // mesh points
List<_fac> fac; // mesh triangles
mesh() {}
mesh(mesh& a) { *this=a; }
~mesh() {}
mesh* operator = (const mesh *a) { *this=*a; return this; }
//mesh* operator = (const mesh &a) { ...copy... return this; }
void draw(); // draws the mesh with OpenGL
void sphere(int n); // init mesh with unit sphere from triangles (n recursion layers)
};
//---------------------------------------------------------------------------
void mesh::draw()
{
int i;
_fac *f;
// fill
glColor3f(0.7,0.7,0.7);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glDepthFunc(GL_LEQUAL);
glBegin(GL_TRIANGLES);
for (i=0,f=fac.dat;i<fac.num;i++,f++)
{
glVertex3dv(pnt.dat[f->i0].p);
glVertex3dv(pnt.dat[f->i1].p);
glVertex3dv(pnt.dat[f->i2].p);
}
glEnd();
// wireframe
glColor3f(0.1,0.3,0.7);
glLineWidth(2.0);
for (i=0,f=fac.dat;i<fac.num;i++,f++)
{
glBegin(GL_LINE_LOOP);
glVertex3dv(pnt.dat[f->i0].p);
glVertex3dv(pnt.dat[f->i1].p);
glVertex3dv(pnt.dat[f->i2].p);
glEnd();
}
glLineWidth(1.0);
}
//---------------------------------------------------------------------------
void mesh::sphere(int n)
{
// init 2 hexagons
int i,j,m,i0,i1,i2,j0,j1,j2;
double a,da=M_PI/3.0;
double *p0,*p1;
_pnt p;
_fac f,*g;
p.p[0]= 0.0;
p.p[1]= 0.0;
p.p[2]=+1.0;
pnt.add(p);
p.p[2]=-1.0;
pnt.add(p);
for (i=0,a=0.0;i<6;i++,a+=da)
{
p.p[0]=cos(a);
p.p[1]=sin(a);
p.p[2]= 0.0;
pnt.add(p);
}
// top half
f.i0=0; f.i1=2; f.i2=3; fac.add(f);
f.i0=0; f.i1=3; f.i2=4; fac.add(f);
f.i0=0; f.i1=4; f.i2=5; fac.add(f);
f.i0=0; f.i1=5; f.i2=6; fac.add(f);
f.i0=0; f.i1=6; f.i2=7; fac.add(f);
f.i0=0; f.i1=7; f.i2=2; fac.add(f);
// botom half
f.i0=1; f.i1=3; f.i2=2; fac.add(f);
f.i0=1; f.i1=4; f.i2=3; fac.add(f);
f.i0=1; f.i1=5; f.i2=4; fac.add(f);
f.i0=1; f.i1=6; f.i2=5; fac.add(f);
f.i0=1; f.i1=7; f.i2=6; fac.add(f);
f.i0=1; f.i1=2; f.i2=7; fac.add(f);
// subdivide triangles
for (;n>0;n--) // recursion layers
for (m=fac.num,i=0;i<m;i++)// scan through all original faces
{
g=&fac[i];
// point indexes
i0=g->i0; j0=pnt.num; // i0
i1=g->i1; j1=j0+1; // j0 j2
i2=g->i2; j2=j0+2; // i1 j1 i2
// genere mid points + sphere surface correction distance from (0,0,0) must be 1.0 (radius)
for (j=0;j<3;j++) p.p[j]=0.5*(pnt[i0].p[j]+pnt[i1].p[j]); a=1.0/sqrt((p.p[0]*p.p[0])+(p.p[1]*p.p[1])+(p.p[2]*p.p[2])); for (j=0;j<3;j++) p.p[j]*=a; pnt.add(p);
for (j=0;j<3;j++) p.p[j]=0.5*(pnt[i1].p[j]+pnt[i2].p[j]); a=1.0/sqrt((p.p[0]*p.p[0])+(p.p[1]*p.p[1])+(p.p[2]*p.p[2])); for (j=0;j<3;j++) p.p[j]*=a; pnt.add(p);
for (j=0;j<3;j++) p.p[j]=0.5*(pnt[i2].p[j]+pnt[i0].p[j]); a=1.0/sqrt((p.p[0]*p.p[0])+(p.p[1]*p.p[1])+(p.p[2]*p.p[2])); for (j=0;j<3;j++) p.p[j]*=a; pnt.add(p);
// change original fac
g->i0=j0; g->i1=j1; g->i2=j2;
// add 3 x fac
f.i0=i0; f.i1=j0; f.i2=j2; fac.add(f);
f.i0=j0; f.i1=i1; f.i2=j1; fac.add(f);
f.i0=j2; f.i1=j1; f.i2=i2; fac.add(f);
}
}
//---------------------------------------------------------------------------
- 有点好奇,所以我试图连接code本
-
用法很简单:
- was a bit curious so I tried to encode this
usage is simple:
mesh obj; // somewhere global... obj.sphere(3); // init (call once or on change of n...) obj.draw(); // inside your gl draw scene routine/event...
-
所以这里是结果概述
so here is the result overview
这篇关于谈到气缸成一个球体,而不会挤压在两极的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!