-1

I have created a rectangel in OpenGl using Arrays. Here are the Arrays:

  float[] vertices = { 
                -0.5f, 0.5f, 0f,
                -0.5f, -0.5f, 0f, 
                0.5f, -0.5f, 0f, 
                0.5f, 0.5f, 0f, 
        };

        int[] indcies = {
            0,1,3,
            3,1,2

        };

        float[] texture_Coord ={

                0,0,
                0,1,
                1,1,
                1,0

        };

How should i modify my array to create a simple circle can you please help ?

user6250837
  • 458
  • 2
  • 21

2 Answers2

2

You can use parametric equation of a circle: (x,y,z)=(rcos(t)+x0,rsin(t)+y0,0) where t samples form 0 to 2Pi and r is the radius and x0 and y0 specify the center of the circle. You would have n+1 vertices if you sample n times the circle plus the center. If you want to triangulate the circle, you can connect all vertices to the center in an order like 0,i,i+1. If you just want to render the boundary using lines, you don't need the center and you can ith vertex to (i+1)th vertex. Here is a code that you may find useful to sample the circle and make the vertices, for the indices depending on the type of rendering you may want to create different arrays:

for(float t=0;t<2*Math.Pi;i+=0.1)
{
   vertices[i]=r*Math.Cos(t)+x0;
   vertices[i+1]=r*Math.Sin(t)+y0;
   vertices[i+2]=0; 
}
eldo
  • 1,327
  • 2
  • 16
  • 27
Good Luck
  • 1,104
  • 5
  • 12
-1

It's more difficult because whilst there's only one sensible way of representing a rectangle in Open GL, there are many ways you could represent a circle. Higher level code uses Bezier representations and then only rasterises the Bezier at the final step, which is the normal high-end way of doing it. Alternatively you can create a polygon of N sides, where N is chosen to be visually indistinguishable from a circle. However that is not robust to zoom. Or you can call a circle drawing algorithm and convert the pixels to a chain of points. That will give you a pixel perfect circle, but again it won't be pixel perfect if you transform it.

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18