简体   繁体   English

生成多边形的顶点

[英]Generate vertices for a polygon

I'm trying to make a useful/generic 2D polygon class for an OpenGL ES renderer. 我正在尝试为OpenGL ES渲染器创建一个有用的/通用的2D多边形类。 When I create a polygon, I give it several parameters: 当我创建一个多边形时,我给它几个参数:

Polygon(Vector3 centerpoint, int numVertices, float inPolySize)

Then, I try to generate the vertices. 然后,我尝试生成顶点。 This is where i'm having a tough time. 这是我度过艰难时期的地方。 I need to determine the number of vertices, get an angle, find the x/y position of that angle, someone take the size into account, AND offset by the position. 我需要确定顶点的数量,获得一个角度,找到该角度的x / y位置,有人考虑尺寸,并按位置偏移。

OpenGL works with big arrays of data. OpenGL适用于大数据阵列。 Nothing is nice like Lists of Vector3's. 没有什么比Vector3的列表更好了。 Instead it's float[] arrays, with the first index being X1, second being Y1, third being Z1, fourth being X2, etc... 相反,它是float []数组,第一个索引是X1,第二个是Y1,第三个是Z1,第四个是X2等等......

final int XPOS = 0;
final int YPOS = 1;
final int ZPOS = 2;
int mvSize = 3; // (x, y, z);
float[] vertices = new float[mvSize * mNumVertices];
for (int verticeIndex = 0; verticeIndex < mNumVertices; verticeIndex++)
{
    double angle = 2 * verticeIndex * Math.PI / mNumVertices;
    vertices[mvSize * verticeIndex + XPOS] = (((float)Math.cos(angle)) * mPolygonSize) + mPosition.GetX();
    vertices[mvSize * verticeIndex + YPOS] = (((float)Math.sin(angle)) * mPolygonSize) + mPosition.GetY();
    vertices[mvSize * verticeIndex + ZPOS] = mPolygonSize + mPosition.GetZ();
}

Unfortunatley, my triangle is never quite right. 不幸的是,我的三角形永远不对。 It's skewed a lot, the size doesn't seem right... 它歪斜了很多,尺寸似乎不对......

I figure i'm throwing the size into the wrong formula, can anyone help? 我想我把大小扔进了错误的公式,有人可以帮忙吗?

EDIT: Here's some sample data Polygon test = new Polygon( new Vector3(0, 1, 0), 3, .5f); 编辑:这是一些示例数据多边形测试=新的多边形(新的Vector3(0,1,0),3,.5f);

vertices[0] = -0.25 顶点[0] = -0.25

vertices[1] = 1.4330127 顶点[1] = 1.4330127

vertices[2] = 0.0 顶点[2] = 0.0

vertices[3] = -0.25 顶点[3] = -0.25

vertices[4] = 0.5669873 顶点[4] = 0.5669873

vertices[5] = 0.0 顶点[5] = 0.0

vertices[6] = 0.5 顶点[6] = 0.5

vertices[7] = 1.0 顶点[7] = 1.0

vertices[8] = 0.0 顶点[8] = 0.0

vertices[9] = -0.25 顶点[9] = -0.25

vertices[10] = 1.4330127 顶点[10] = 1.4330127

vertices[11] = 0.0 顶点[11] = 0.0

I can't believe I was this stupid. 我简直不敢相信我这是愚蠢的。 Basically, my render window was smaller than my screen. 基本上,我的渲染窗口比我的屏幕小。 If my screen is a rectangle, my render window was a square. 如果我的屏幕是一个矩形,我的渲染窗口是一个正方形。

This being the case, any triangle I draw that was up was clipped by my render window. 在这种情况下,我绘制的任何三角形都被我的渲染窗口剪切了。 To me, it looked like the triangle was skewed. 对我来说,看起来三角形是倾斜的。 Really, it was just clipped! 真的,它被剪掉了!

The Java math library takes radians as input, not degrees. Java数学库将弧度视为输入,而不是度。 I didn't see the angles you were using for your calculation, but if you're not converting to radians from degrees, you will get some skewed looking shapes, and would explain that your calculations are correct, but the expected result is off. 我没有看到你用于计算的角度,但如果你没有从度数转换为弧度,你会得到一些倾斜的形状,并会解释你的计算是正确的,但是预期的结果是关闭的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM