Java examples for 2D Graphics:OpenGL
OpenGL draw Sphere
import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.util.ArrayList; import java.util.List; import javax.microedition.khronos.opengles.GL10; public class Main{ //from ww w .j a v a 2s .co m public static void drawSphere(GL10 gl, float r, int stack, int slice) { float stackStep = (float) (Math.PI / stack);// float sliceStep = (float) Math.PI / slice;// float r0, r1, y0, y1, x0, x1, z0, z1; float alpha0 = 0, alpha1 = 0; float beta = 0; List<Float> coordsList = new ArrayList<Float>(); // for (int i = 0; i < stack; i++) { alpha0 = (float) (-Math.PI / 2 + (i * stackStep)); alpha1 = (float) (-Math.PI / 2 + ((i + 1) * stackStep)); y0 = (float) (r * Math.sin(alpha0)); r0 = (float) (r * Math.cos(alpha0)); y1 = (float) (r * Math.sin(alpha1)); r1 = (float) (r * Math.cos(alpha1)); //? for (int j = 0; j <= (slice * 2); j++) { beta = j * sliceStep; x0 = (float) (r0 * Math.cos(beta)); z0 = -(float) (r0 * Math.sin(beta)); x1 = (float) (r1 * Math.cos(beta)); z1 = -(float) (r1 * Math.sin(beta)); coordsList.add(x0); coordsList.add(y0); coordsList.add(z0); coordsList.add(x1); coordsList.add(y1); coordsList.add(z1); } } FloatBuffer fbb = BufferUtil.list2FloatBuffer(coordsList); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, fbb); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, coordsList.size() / 3); } public static FloatBuffer list2FloatBuffer(List<Float> arr) { ByteBuffer ibb = ByteBuffer.allocateDirect(arr.size() * 4); ibb.order(ByteOrder.nativeOrder()); FloatBuffer fbb = ibb.asFloatBuffer(); for (float f : arr) { fbb.put(f); } // fbb.position(0); return fbb; } }