JohnRobPeter
JohnRobPeter

Reputation: 53

Android OpenGL ES generate a circular mesh for texture

I'm new to OpenGL-ES on Android, and I have a question regarding generating a mesh for a texture that represents a circle.

Desired mesh on the left, and my Texture on the right:

Desired mesh on the left, and my Texture on the right

How do i generate the mesh on the left? and then render it in the following way:

triangle1{Centerpoint, WhitePoint, nextpointclockwise(say #1)},
triangle2{Centerpoint, point#1,    nextpointclockwise(say #2)},
triangle3{Centerpoint, point#2,    nextpointclockwise(say #3)}

Upvotes: 3

Views: 1626

Answers (2)

SteveL
SteveL

Reputation: 3389

This will create the vertices and texture coordinates of a 1 radius circle(but i didnt actually tried it so it may not work :) ) Then you can draw them as TRIANGLE_FAN

public void MakeCircle2d(int points)
{
    float[] verts=new float[points*2+2];
    float[] txtcord=new float[points*2+2];


    verts[0]=0;
    verts[1]=0;
    txtcord[0]=0.5f;
    txtcord[1]=0.5f;
    int c=2;
    for (int i = 0; i < points; i++)
    {
        float fi = 2*Math.PI*i/points;
        float x = Math.cos(fi + Math.PI) ;
        float y = Math.sin(fi + Math.PI) ;

        verts[c]=x;
        verts[c+1]=y;
        txtcord[c]=x*0.5f+0.5f;//scale the circle to 0.5f radius and plus 0.5f because we want the center of the circle tex cordinates to be at 0.5f,0.5f
        txtcord[c+1]=y*0.5f+0.5f;
        c+=2;
    }
}

Upvotes: 3

user2255505
user2255505

Reputation: 1


Thanks to your code I made it work on a 2D OGLes1.1 project on iOS.
Works pretty good, A bit messy but might be good for learners.
Thanks.

-(void) MakeCircle2d:(int)points pos:(CGPoint)pos rad:(GLfloat)rad texName:(GLuint)texName
{
float verts[(points*2)+2];
float txtcord[(points*2)+2];
verts[0]=pos.x;
verts[1]=pos.y;
txtcord[0]=0.5f;
txtcord[1]=0.5f;
int c=2;
for (int i = 0; i < points; i++)
{
    float fi = 2.0*M_PI*((float)i/(float)(points-2.0));
    float x = sinf(fi + M_PI) ;
    float y = cosf(fi + M_PI) ;

    verts[c]=pos.x+(x*rad);
    verts[c+1]=pos.y+(y*rad);
    txtcord[c]=x*0.5f+0.5f;//scale the circle to 0.5f radius and plus 0.5f because we want the center of the circle tex cordinates to be at 0.5f,0.5f
    txtcord[c+1]=y*0.5f+0.5f;
    c+=2;
}



glColor4f(1.0,1.0,1.0,1.0);
//glColor4f(0.0,0.0,0.0, 0.0);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glPushMatrix();

glBindTexture(GL_TEXTURE_2D, texName);

glTexCoordPointer(2, GL_FLOAT, 0, txtcord);
glVertexPointer(2, GL_FLOAT, 0, verts);
glDrawArrays(GL_TRIANGLE_FAN, 0, points);

//glBindTexture(GL_TEXTURE_2D, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glPopMatrix();


}

Upvotes: 0

Related Questions