DrawGlobe extends GLBase
{
private final float R = 0.5f;
private final int N = 50;
private final int T = 6;
private final int M = 9;
private final int NT = N * (T + M) - 1;
private final float Start = (float)Math.PI / 2;
private final float BASE = 2 * (float)Math.PI / N;
private final float ADD_1 = (float)Math.PI / T;
private final float ADD_2 = (float)Math.PI / (M - 1);
private final int LEN = 3 * NT;
private float an = 1;
private float[] coords;
FloatBuffer coordBuffer;
private void setCoords()
{
coords = new float[LEN];
int i, j, k, p, pos;
float angle = -ADD_1, rad, x1;
for(k = 0, p = 0; k < T; k++)
{
angle += ADD_1;
for (i = 0; i < N; i++, p++)
{
pos = p * 3;
rad = i * BASE + Start;
x1 = R * (float)Math.cos(rad);
coords[pos] = x1 * (float)Math.cos(angle);
coords[pos + 1] = R * (float)Math.sin(rad);
coords[pos + 2] = x1 * (float)Math.sin(angle);
}
}
float alpha = Start, beta = 0;
float y, r;
int ra = N /((M - 1) * 2);
for(k = 1; k < M; k++)
{
beta += ADD_2;
y = R * (float)Math.cos(beta);
for(j = 0; j < ra; j++,p++)
{
pos = p * 3;
rad = alpha - j * BASE;
coords[pos] = R * (float)Math.cos(rad);
coords[pos + 1] = R * (float)Math.sin(rad);
coords[pos + 2] = 0;
}
alpha -= ADD_2;
if(alpha == -Start)
break;
for(i = 0; i < N; i++, p++)
{
pos = p * 3;
rad = i * BASE;
r = R * (float)Math.sin(beta);
coords[pos] = r * (float)Math.cos(rad);
coords[pos + 1] = y;
coords[pos + 2] = r * (float)Math.sin(rad);
}
}
alpha = -Start;
for(i = 0; i < N/2; i++,p++)
{
pos = p * 3;
rad = i * BASE + alpha;
coords[pos] = R * (float)Math.cos(rad);
coords[pos + 1] = R * (float)Math.sin(rad);
coords[pos + 2] = 0;
}
}
public DrawGlobe(Context c)
{
super(c);
setCoords();
coordBuffer = makeFloatBuffer(coords);
}
private void initial(GL10 gl)
{
an += 1.0;
if(an == 360.0)
an = 0;
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
}
private void destroy(GL10 gl)
{
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
@Override
protected void drawFrame(GL10 gl)
{
initial(gl);
draw(gl);
destroy(gl);
}
private void draw(GL10 gl)
{
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glColor4f(0.5f, 0, 1, 1);
gl.glViewport(0, 0, 480, 480);
gl.glRotatef(an, 0, 1f, 0);
gl.glRotatef(an, 1f, 1f, 0);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, coordBuffer);
gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, NT);
}
}
OPENGL 画圆
最新推荐文章于 2023-01-06 22:01:29 发布