您的位置:首页 > 移动开发 > Android开发

Android应用 :第一个 OpenGL ES 三角形

2013-10-16 16:10 204 查看

1.简介

OpenGL是由SGI公司开发的一套3D图形软件接口标准,OpenGL ES就是众多版本中的一个子集。

3D场景中的3D模型的最基本单位是称为顶点的vertex,它代表三维空间中的一个点。
尽管OpenGL支持多种多边形,但是很不幸的是OpenGL ES目前只支持三角形,这主要是出于性能的原因。

OpenGL ES中有一项功能叫做背面裁剪,含义是打开背面裁剪功能后,视角在一个三角形的背面时不渲染此三角形,即看不到这个三角形,此功能可以大大提高渲染效率。
三角形正反面确定:当面对一个三角形时,若顶点的顺序是逆时针则位于三角形的正面,反之则是反面。

SceneRenderer场景渲染器mSurfaceView.requestFocus()获取焦点
setFocusableInTouchMode(true)设置为可触控

点和线的绘制GL_POINTS,GL_LINES,GL_LINE_START,GL_LINE_LOOP,GL_TRIANGLES,GL_TRIANGLE_STRIP,GL_TRIANGLE_FAN\ 。

正交投影和透视投影
正交投影是平行投影的一种,设置正交投影的语句为:gl.glOrthof(left,right,bottom,top,near,far)
透视投影属于非平行投影,游戏中较多采用,设置透视投影的语句为:gl.glFrustumf(left,right,bottom,top,near,far)

2. 代码

布局 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/main_liner"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>


Mainactivity 代码:

package com.example.android_sample_4_1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
private MySurfaceView mSurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSurfaceView = new MySurfaceView(this);
mSurfaceView.requestFocus();
mSurfaceView.setFocusableInTouchMode(true);
LinearLayout ll = (LinearLayout) this.findViewById(R.id.main_liner);
ll.addView(mSurfaceView);
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mSurfaceView.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mSurfaceView.onResume();
}

@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;
}

}


MySurfaceView 代码:

package com.example.android_sample_4_1;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;

public class MySurfaceView extends GLSurfaceView{
private final float TOUCH_SCALE_FACTOR=180.0f/320;//角度缩放比例,即屏幕宽320,从屏幕的一端滑到另一端,x轴上的差距对应相应的需要旋转的角度
private SceneRenderer myRenderer;//场景渲染器
private float myPreviousY;//上次屏幕上的触控位置的Y坐标
private float myPreviousX;//上次屏幕上的触控位置的X坐标

public MySurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
myRenderer = new SceneRenderer();
this.setRenderer(myRenderer);
this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
float y = event.getY();
float x = event.getX();
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
float dy = y - myPreviousY;
float dx = x - myPreviousX;
myRenderer.tr.yAngle += dx * TOUCH_SCALE_FACTOR;
myRenderer.tr.zAngle += dy * TOUCH_SCALE_FACTOR;
requestRender();
}
myPreviousX = x;
myPreviousY = y;
return true;
}

private class SceneRenderer implements GLSurfaceView.Renderer{
Triangle tr = new Triangle();
public SceneRenderer() {
// TODO Auto-generated constructor stub
}
@Override
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glFrontFace(GL10.GL_CCW);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -2.0f);
tr.drawSelf(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
float ratio = (float)width / height;
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(0, 0, 0, 0);
gl.glEnable(GL10.GL_DEPTH_TEST);
}
}
}


Triangle类 代码:

package com.example.android_sample_4_1;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import javax.microedition.khronos.opengles.GL10;
public class Triangle {
private IntBuffer myVertexBuffer;//顶点坐标数据缓冲
private IntBuffer myColorBuffer;//顶点着色数据缓冲
private ByteBuffer myIndexBuffer;//顶点构建的索引数据缓冲
int vCount=0;//顶点数量
int iCount=0;//索引数量
float yAngle=0;//绕y轴旋转的角度
float zAngle=0;//绕z轴旋转的角度
public Triangle(){
vCount=3;//一个三角形,3个顶点
final int UNIT_SIZE=10000;//缩放比例
int []vertices=new int[]
{
-8*UNIT_SIZE,6*UNIT_SIZE,0,
-8*UNIT_SIZE,-6*UNIT_SIZE,0,
8*UNIT_SIZE,-6*UNIT_SIZE,0
};
//创建顶点坐标数据缓存,由于不同平台字节顺序不同,数据单元不是字节的(上面的事整型的缓存),一定要经过ByteBuffer转换,关键是通过ByteOrder设置nativeOrder()
ByteBuffer vbb=ByteBuffer.allocateDirect(vertices.length*4);//一个整数四个字节,根据最新分配的内存块来创建一个有向的字节缓冲
vbb.order(ByteOrder.nativeOrder());//设置这个字节缓冲的字节顺序为本地平台的字节顺序
myVertexBuffer=vbb.asIntBuffer();//转换为int型缓冲
myVertexBuffer.put(vertices);//向缓冲区中放入顶点坐标数据
myVertexBuffer.position(0);//设置缓冲区的起始位置
final int one=65535;//支持65535色色彩通道
int []colors=new int[]//顶点颜色值数组,每个顶点4个色彩值RGBA
{
one,one,one,0,
one,one,one,0,
one,one,one,0
};
ByteBuffer cbb=ByteBuffer.allocateDirect(colors.length*4);
cbb.order(ByteOrder.nativeOrder());
myColorBuffer=cbb.asIntBuffer();
myColorBuffer.put(colors);
myColorBuffer.position(0);
//为三角形构造索引数据初始化
iCount=3;
byte []indices=new byte[]
{
0,1,2
};
//创建三角形构造索引数据缓冲
myIndexBuffer=ByteBuffer.allocateDirect(indices.length);
myIndexBuffer.put(indices);
myIndexBuffer.position(0);
}
public void drawSelf(GL10 gl)//GL10是实现接口GL的一公共接口,包含了一系列常量和抽象方法
{
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//启用顶点坐标数组
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);//启用顶点颜色数组

gl.glRotatef(yAngle,0,1,0);//根据yAngle的角度值,绕y轴旋转yAngle
gl.glRotatef(zAngle,0,0,1);

gl.glVertexPointer//为画笔指定顶点坐标数据
(
3,					//每个顶点的坐标数量为3
GL10.GL_FIXED,		//顶点坐标值的类型为GL_FIXED,整型
0,					//连续顶点坐标数据之间的间隔
myVertexBuffer		//顶点坐标数量
);
gl.glColorPointer//为画笔指定顶点 颜色数据
(
4,
GL10.GL_FIXED,
0,
myColorBuffer
);
gl.glDrawElements//绘制图形
(
GL10.GL_TRIANGLES,		//填充模式,这里是以三角形方式填充
iCount,					//顶点数量
GL10.GL_UNSIGNED_BYTE,	//索引值的类型
myIndexBuffer			//索引值数据
);
}}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息