参考,这是一位德国人Martin写的文章,年代比较久远了.但是入门还是比较好的
1. 工程的建立
eclipse --> 新建一项目 com.example.gldemo
SDK 都选API 19: android 4.4 (KitKat)
2. MainActivity.java -->跟以前一样
-
package com.example.gldemo;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.view.Menu;
-
import android.view.MenuItem;
-
-
public class MainActivity extends Activity {
-
private VortexView _vortexView;
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
_vortexView = new VortexView(this);
-
setContentView(_vortexView);
-
//setContentView(R.layout.activity_main);
-
}
-
-
@Override
-
public boolean onCreateOptionsMenu(Menu menu) {
-
// Inflate the menu; this adds items to the action bar if it is present.
-
getMenuInflater().inflate(R.menu.main, menu);
-
return true;
-
}
-
-
@Override
-
public boolean onOptionsItemSelected(MenuItem item) {
-
// Handle action bar item clicks here. The action bar will
-
// automatically handle clicks on the Home/Up button, so long
-
// as you specify a parent activity in AndroidManifest.xml.
-
int id = item.getItemId();
-
if (id == R.id.action_settings) {
-
return true;
-
}
-
return super.onOptionsItemSelected(item);
-
}
-
}
VortexView.java
-->跟以前一样
-
package com.example.gldemo;
-
-
import android.content.Context;
-
import android.opengl.GLSurfaceView;
-
import android.view.MotionEvent;
-
-
public class VortexView extends GLSurfaceView{
-
private static final String LOG_TAG="VortexView";
-
private VortexRenderer _renderer;
-
public VortexView (Context context){
-
super(context);
-
_renderer = new VortexRenderer();
-
setRenderer(_renderer);
-
}
-
-
public boolean onTouchEvent(final MotionEvent event){
-
queueEvent(new Runnable(){
-
public void run(){
-
_renderer.setColor(event.getX()/getWidth(),
-
event.getY()/getHeight(), 1.0f);
-
}
-
});
-
return true;
-
}
-
}
VortexRenderer.java
-
package com.example.gldemo;
-
-
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;
-
-
public class VortexRenderer implements GLSurfaceView.Renderer {
-
private static final String LOG_TAG="VortexRenderer";
-
private float _red = 0.9f;
-
private float _green = 0.9f;
-
private float _blue = 0.9f;
-
//use for initTriangle
-
private ShortBuffer _indexBuffer; -->三角形的索引
-
private FloatBuffer _vertexBuffer; -->三角形的坐标
-
private short[] _indicesArray = {0, 1, 2};
-
private int _nrOfVertices = 3; -->三角形的顶点数为3个
-
@Override
-
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
-
// TODO Auto-generated method stub
-
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); -->设置opengl使用vertex数组来画
-
initTriangle(); -->初始化vertex数组
-
}
-
-
@Override
-
public void onSurfaceChanged(GL10 gl, int w, int h){
-
gl.glViewport(0, 0, w, h);
-
}
-
-
@Override
-
public void onDrawFrame(GL10 gl ){
-
gl.glClearColor(_red, _green, _blue, 1.0f); -->用定义的rgb来设设置背景色
-
gl.glClear(GL10.GL_COLOR_BUFFER_BIT); -->清空buffer
-
gl.glColor4f(0.5f, 0f, 0f, 0.5f); -->设置绘制三角形的颜色
-
//设置顶点: 3代表三维,GL_FLOAT代表buffer里面是float,0代表没有offset,_vertexBuffer是存放顶点坐标的buffer
-
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
-
//画:GL_TRIANGLES代表要画三角形,_nrOfVertices代表一个三角形三个顶点,GL_UNSIGNED_SHORT是type,_indexBuffer指向索引存贮位置的指针
-
gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
-
}
-
public void setColor(float r, float g, float b) {
-
_red = r;
-
_green = g;
-
_blue = b;
-
}
-
private void initTriangle(){
-
//ByteBuffer vbb = ByteBuffer.allocate(_nrOfVertices*3*4);
-
ByteBuffer vbb = ByteBuffer.allocateDirect(_nrOfVertices*3*4); -->这儿要用allocateDirect方法
-
vbb.order(ByteOrder.nativeOrder());
-
_vertexBuffer = vbb.asFloatBuffer();
-
-
//ByteBuffer ibb = ByteBuffer.allocate(_nrOfVertices*3*4);
-
ByteBuffer ibb = ByteBuffer.allocateDirect(_nrOfVertices*3*4);
-
ibb.order(ByteOrder.nativeOrder());
-
_indexBuffer = ibb.asShortBuffer();
-
-
float [] coords = {
-
-0.5f, -0.5f, 0f,
-
0.5f, -0.5f, 0f,
-
0.0f, 0.5f, 0f
-
};
-
-
_vertexBuffer.put(coords);
-
_indexBuffer.put(_indicesArray);
-
_vertexBuffer.position(0);
-
_indexBuffer.position(0);
-
}
-
}
注意:原文中使用了ByteBuffer.allocate,但是这个方法现在不能使用了,会报以下错误
E/AndroidRuntime( 6951): java.lang.IllegalArgumentException: Must use a native order direct Buffer
替换为allocateDirect就可以了
3. 按屏幕后出现一个旋转的三角形
glDemo1.rar(下载后不用改名)
在VortexRenderer.java中的onDrawFrame
-
public void onDrawFrame(GL10 gl ){
-
gl.glClearColor(_red, _green, _blue, 1.0f);
-
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
-
gl.glRotatef(_angle, 0f, 1f, 0f);
-
gl.glColor4f(0.5f, 0f, 0f, 0.5f);
-
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
-
gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
-
}
阅读(1945) | 评论(0) | 转发(0) |