当前位置:首页 » 电脑基础知识

不用任何软件,让电脑不中木马

2010-05-11 23:40 本站整理 浏览(10)

前面讲了一些Android OpenGl 的概念问题,这一节讲给大家讲一下如何画一个三角现出来.

一、新建一个Android工程命名为:OpenGL_Lesson2.

二、修改OpenGL_Lesson2.java代码如下:

package com.android.tutor;
import android.app.Activity;
import android.os.Bundle;
public class OpenGL_Lesson2 extends Activity {
   
	private OpenGlView mOpenGlView;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
        mOpenGlView = new OpenGlView(this);
        setContentView(mOpenGlView);
              
    }
}
三、新建一个类OpenGlView.java继承与GLSurfaceView,代码如下:

package com.android.tutor;
import android.content.Context;
import android.opengl.GLSurfaceView;
public class OpenGlView extends GLSurfaceView {
	
	private OpenGlRender mOpenGlRender;
	public OpenGlView(Context context) {
		super(context);
		mOpenGlRender = new OpenGlRender();
		setRenderer(mOpenGlRender);
	}
	
	
	
}
四、新建一个OpenGlRender.java类继承与Renderer,代码如下:

package com.android.tutor;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class OpenGlRender implements Renderer {
	
	private float _red = 0f;
	private float _green = 25f;
	private float _blue = 200f;
	
	private ShortBuffer _indexBuffer;
	
	private FloatBuffer _vertexBuffer;
	
	
	private short[] _indicesArray = {0, 1, 2};
    private int _nrOfVertices = 3;
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		initTriangle();
	}
	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		
		gl.glViewport(0, 0, width, height);
	}

	
	@Override
	public void onDrawFrame(GL10 gl) {
		gl.glClearColor(_red, _green, _blue, 1.0f);
		
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		 gl.glColor4f(0.5f, 0f, 0f, 0.5f);
		 gl.glVertexPointer(_nrOfVertices, GL10.GL_FLOAT, 0, _vertexBuffer);
		 gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
	}
	
	private void initTriangle() {
        // float has 4 bytes
        ByteBuffer vbb = ByteBuffer.allocateDirect(_nrOfVertices * 3 * 4);
        vbb.order(ByteOrder.nativeOrder());
        _vertexBuffer = vbb.asFloatBuffer();
        
        // short has 4 bytes
        ByteBuffer ibb = ByteBuffer.allocateDirect(_nrOfVertices * 2);
        ibb.order(ByteOrder.nativeOrder());
        _indexBuffer = ibb.asShortBuffer();
        
        float[] coords = {
            -0.5f, -0.5f, 0f, // (x1, y1, z1)
            0.5f, -0.5f, 0f, // (x2, y2, z2)
            0f, 0.5f, 0f // (x3, y3, z3)
        };
        
        _vertexBuffer.put(coords);
        
        _indexBuffer.put(_indicesArray);
        
        _vertexBuffer.position(0);
        _indexBuffer.position(0);
    }
}
五、运行之,效果如下:

今天晚上就先不做详细解释了!改日在加上!谢谢~