Android OpenGL ES为纹理生成圆形网格

时间:2012-09-27 17:23:44

标签: android opengl-es

我是Android上的OpenGL-ES的新手,我有一个关于为表示圆圈的纹理生成网格的问题。

左侧是所需的网格,右侧是我的纹理:

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

如何在左侧生成网格?然后按以下方式呈现它:

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

2 个答案:

答案 0 :(得分:2)

这将创建一个半径圆的顶点和纹理坐标(但我实际上没有尝试过,所以它可能无法工作:)) 然后你可以把它们画成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]=x*0.5f+0.5f;
        c+=2;
    }
}

答案 1 :(得分:0)


感谢您的代码,我在iOS上的2D OGLes1.1项目上工作。
工作得很好,有点乱,但可能对学习者有好处 感谢。

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


}