Android OpenGL入门实例:绘制球体
相比曲线而言,曲面的坐标计算要复杂许多。在其他OpenGL平台上,绘制一个球体也就是一个GLU API 的事,但是Android为上GLU被严重削减了,总共不到10个API。不过我们可以很方便的将它们的源码拷贝过来。这里要说的是大家不必纠结于算法的实现(通过修改参数,可以大致了解算法过程),在实际应用中,都是通过3ds max建好模型然后导入到OpenGL中,完全不必计算坐标值。
HYPERLINK "/archives/222" \l "viewSource" \o "view source" view source
HYPERLINK "/archives/222" \l "printSource" \o "print" print HYPERLINK "/archives/222" \l "about" \o "?" ?
001
public class DrawSphere extends Activity implements GLSurfaceView.Renderer{
002
Sphere mSphere;
003
float xrot = 0.0f;
004
float yrot = 0.0f;
005
006
float perx = 0;
007
float pery = 0;
008
@Override
009
public void onCreate(Bundle savedInstanceState) {
010
super.onCreate(savedInstanceState);
011
GLSurfaceView myView = new GLSurfaceView(this);
012
myView.setRenderer(this);
013
setContentView(myView);
014
mSphere = new Sphere(40, 40, 2);
015
// mSphere = new Sphere(6, 5, 2);
016
// mSphere = new Sphere(12, 12, 2);
017
}
018
public void onDrawFrame(GL10 gl) {
019
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
020
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
021
gl.glLoadIdentity();
022
GLU.gluLookAt(gl, 0, 0, 5, 0, 0, 0, 0, 1, 0);
023
gl.glRotatef(xrot, 1, 0, 0);
024
gl.glRotatef(yrot, 0, 1, 0);
025
mSphere.draw(gl);
026
027
}
028
public void onSurfaceChanged(GL10 gl, int width, int height) {
029
gl.glViewport(0, 0, width, height);
030
gl.glMatrixMode(GL10.GL_PROJECTION);
031
gl.glLoadIdentity();
032
GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
033
gl.glMatrixMode(GL10.GL_MODELVIEW);
034
gl.glLoadIdentity();
035
init(gl);
036
}
037
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
038
039
}
040
041
@Override
042
public boolean onTouchEvent(MotionEvent event){
043
int action = event.getAction();
044
float scalesize = 0.3f;
045
046
switch(action){
047
case MotionEvent.ACTION_DOWN:
048
perx = event.getX();
049
pery = event.getY();
050
break;
051
case MotionEvent.ACTION_MOVE:
052
yrot