3

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

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

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

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

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

2 回答 2

3

这将创建一个半径为 1 的圆的顶点和纹理坐标(但我实际上没有尝试过,所以它可能不起作用:))然后你可以将它们绘制为 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;
    }
}
于 2012-09-28T10:15:51.920 回答
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();


}
于 2013-04-07T21:15:41.493 回答