Creating vertices for a polygon

I am trying to create a useful / general class of two-dimensional polygons for rendering OpenGL ES. When I create a polygon, I give it a few parameters:

Polygon(Vector3 centerpoint, int numVertices, float inPolySize) 

Then I try to generate vertices. This is where I have a hard time. I need to determine the number of vertices, get the angle, find the x / y position of this angle, someone takes into account the size, And moves by position.

OpenGL works with large amounts of data. Nothing is more beautiful than Vector3's Lists. Instead, it floats [] arrays, with the first index being X1, the second being Y1, the third being Z1, the fourth being X2, etc.

 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(); } 

Sure, my triangle is never right. He distorted a lot, the size does not seem right ...

I suppose I throw the size into the wrong formula, can anyone help?

EDIT: Here are some examples of data Polygon test = new polygon (new vector3 (0, 1, 0), 3, .5f);

vertices [0] = -0.25

tops [1] = 1.4330127

peaks [2] = 0.0

vertices [3] = -0.25

peaks [4] = 0.5669873

peaks [5] = 0.0

peaks [6] = 0.5

tops [7] = 1.0

peaks [8] = 0.0

vertices [9] = -0.25

tops [10] = 1.4330127

peaks [11] = 0.0

+4
source share
2 answers

I can’t believe that I was so stupid. Basically, my rendering window was smaller than my screen. If my screen is a rectangle, my rendering window was square.

In this case, any triangle that I draw was closed by my rendering window. For me it looked like a triangle was skewed. Indeed, it was just cropped!

+1
source

The Java math library takes radians as input, not degrees. I did not see the angles that you used for the calculation, but if you do not go to radians from degrees, you will get some distorted shapes and explain that your calculations are correct, but the expected result does not work.

0
source

All Articles