P.Carlino
P.Carlino

Reputation: 681

Index opengl need explanation

I'm working on a simple opengl programs in android. i wont to draw a fill circle and use it and it vertices to do something. The problem is only in the draw part maybe becouse i didn't undertand how to use indices.

To get the coordinates of the circle i use a simple math algoritm that takes coordinates using polar cordinates

for(int i=0;i<360;i++)
        {
            buffer[i]=(float)Math.cos(Math.toRadians((double)i))*0.5f;
            buffer[++i]=(float)Math.sin(Math.toRadians((double)i))*0.5f*coaf;
            buffer[++i]=0f;
            // Log.i("",""+buffer[i-2]+" "+buffer[i-1]+" "+buffer[i]);
        }

And here there isn't problem. The problem is that i want to draw a filled circle and to do that i decided to draw lines that goes from a point to the opposite respect x axis.

So to create a indices i used this code

short count=0;

        for(int i=0;i<360;i++)
        {
            indices[i++]=count;
            indices[i]=(short)(360-count);
            count++;
        }

When i draw everything the result is thisenter image description here

Now can anyone tell to me why the lines go from a point to the center and not from a pointer to the symmetrical??

Upvotes: 0

Views: 53

Answers (1)

Matic Oblak
Matic Oblak

Reputation: 16774

Your values are a complete mess. As already mentioned you only generate 120 vertices and not 360 due to iterator increment but then even if you did you then use 360-count which produces 360 for the first iteration meaning 361st vertex.

You really need to improve your code to track better what is going on. Use some constants to ensure the values are correct:

int tessellation = 360; // will have 360 vertices
int dimensionCount = 3; // x, y, z
double angleFraction = 360.0 / (double)tessellation; // How many degrees between 2 points
for(int i=0; i<tessellation; i++) {
    double angle = Math.toRadians((double)i*angleFraction);
    buffer[i*dimensionCount + 0] = (float)Math.cos(angle)*0.5f;
    buffer[i*dimensionCount + 1] = (float)Math.sin(angle)*0.5f*coaf;
    buffer[i*dimensionCount + 2] = 0f;
}

Then your second part is completely wrong. The reason the lines went to the center is more of a luck that the data you pass is set to zero. The second vertex in the iteration is always zero since it overflows. But even if not, the equation is wrong (or I am not sure what you are trying to do). What your code seems it should produce is 120 parallel lines which might be the expected result. So lets see about that one:

int limit = tessellation/2; // We need only half of the pairs, the other half will just repeat the same lines
for(short i=0; i<limit; i++) {
    indices[2*i + 0] = i;
    indices[2*i + 1] = (short)(tessellation-i-1); // Note there is always "-1" when backwards enumerating
}

Or to use symmetry through the center:

short limit = tessellation/2; // We need only half of the pairs, the other half will just repeat the same lines
for(short i=0; i<limit; i++) {
    indices[2*i + 0] = i;
    indices[2*i + 1] = (short)(limit+i); // basically saying +180 degrees
}

Upvotes: 2

Related Questions