openGL ES 学习第一课(绘制三角形和四边形)
来源:互联网 发布:协同办公软件排名 编辑:程序博客网 时间:2024/04/30 02:17
openGL ES主要是用于嵌入式设备的3D图形的绘制
GLSurfaceView GLSurfaceView.Renderer
效果如下:
直接代码:
1、GLRender.java
package wu.demo.www;import java.nio.IntBuffer;import javax.microedition.khronos.egl.EGLConfig;import javax.microedition.khronos.opengles.GL10;import android.opengl.GLSurfaceView.Renderer;public class GLRender implements Renderer {int one = 0x10000;//三角形的三个顶点private int[] triggerBuffer = {0, one, 0, //上顶点-one, -one, 0, //下左one, -one, 0, //下右};private int[] quaterBuffer = {-one, one, 0,one, one, 0, -one, -one, 0, one, -one, 0};private IntBuffer getVertexBuffer(int capacity, int sum[]) {IntBuffer vertexBuffer = BufferFactory.createIntBuffer(capacity).put(sum);vertexBuffer.position(0);return vertexBuffer;}public void onDrawFrame(GL10 gl) {//清除屏幕和深度缓存gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);gl.glLoadIdentity();//移动顶点,左移1.5,向里6个单位gl.glTranslatef(-1.5f, 0.0f, -6.0f);//移动好了后,设置顶点gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//绘制三角形gl.glVertexPointer(3, GL10.GL_FIXED, 0, getVertexBuffer(9, triggerBuffer));gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);//重置观察矩阵gl.glLoadIdentity();//移动顶点,移动到右边gl.glTranslatef(1.5f, 0f, -6f);//绘制四边形gl.glVertexPointer(3, GL10.GL_FIXED, 0, getVertexBuffer(12, quaterBuffer));gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);//取消顶点设置gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);}public void onSurfaceChanged(GL10 gl, int width, int height) {float ratio = (float) width / height;gl.glViewport(0, 0, width, height);//设置投影矩阵gl.glMatrixMode(GL10.GL_PROJECTION);//重置投影矩阵gl.glLoadIdentity();gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);gl.glMatrixMode(GL10.GL_MODELVIEW);gl.glLoadIdentity();}public void onSurfaceCreated(GL10 gl, EGLConfig config) {//告诉 系统对透视进行修正gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);//白色背景gl.glClearColor(0, 0, 0, 0);//启用阴影平滑gl.glShadeModel(GL10.GL_SMOOTH);//设置深度缓存gl.glClearDepthf(1.0f);//启用深度测试gl.glEnable(GL10.GL_DEPTH_TEST);//所做深度测试 的类型gl.glDepthFunc(GL10.GL_LEQUAL);}}
package wu.demo.www;import java.nio.ByteBuffer;import java.nio.ByteOrder;import java.nio.FloatBuffer;import java.nio.IntBuffer;import java.nio.ShortBuffer;/** * A utility class to create buffers. * * All public methods are static. The Singleton pattern was avoided to avoid concerns about * threading and the Android life cycle. If needed, It can be implemented later given some research. */public class BufferFactory {// This class cannot and should not be instantiatedprivate BufferFactory() {}// We use Buffer.allocateDirect() to get memory outside of// the normal, garbage collected heap. I think this is done// because the buffer is subject to native I/O.// See http://download.oracle.com/javase/1.4.2/docs/api/java/nio/ByteBuffer.html#direct/** * Creates a buffer of floats using memory outside the normal, garbage collected heap * * @param capacityThe number of primitives to create in the buffer. */public static FloatBuffer createFloatBuffer(int capacity) {// 4 is the number of bytes in a floatByteBuffer vbb = ByteBuffer.allocateDirect(capacity * 4);vbb.order(ByteOrder.nativeOrder());return vbb.asFloatBuffer();}/** * Creates a buffer of shorts using memory outside the normal, garbage collected heap * * @param capacityThe number of primitives to create in the buffer. */public static ShortBuffer createShortBuffer(int capacity) {// 2 is the number of bytes in a shortByteBuffer vbb = ByteBuffer.allocateDirect(capacity * 2);vbb.order(ByteOrder.nativeOrder());return vbb.asShortBuffer();}public static IntBuffer createIntBuffer(int capacity) {ByteBuffer vbb = ByteBuffer.allocateDirect(capacity * 4);vbb.order(ByteOrder.nativeOrder());return vbb.asIntBuffer();}}
3、OpenGLTest1Activity.java
package wu.demo.www;import android.app.Activity;import android.opengl.GLSurfaceView;import android.opengl.GLSurfaceView.Renderer;import android.os.Bundle;public class OpenGLTest1Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Renderer render = new GLRender(); GLSurfaceView glView = new GLSurfaceView(this); glView.setRenderer(render); setContentView(glView); }}
- openGL ES 学习第一课(绘制三角形和四边形)
- android opengl es--三角形,四边形
- OpenGL ES学习001---绘制三角形
- OpenGL ES(一)绘制三角形
- Android 中使用OpenGL ES进行2D开发(绘制第一个三角形)
- Android 中使用OpenGL ES进行2D开发(绘制第一个三角形番外篇)
- openGL ES Android 三角形绘制
- 第一个OpenGL ES三角形
- Opengl ES 学习笔记 2:绘制一个旋转的三角形
- Android OpenGL ES(十):绘制三角形Triangle
- 用OpenGL ES绘制一个红色的三角形(一)
- 用OpenGL ES绘制一个红色三角形(二)
- Android OpenGL实战一——开发框架搭建、三角形和四边形的绘制
- OpenGL(二)第一个OpenGL程序绘制三角形
- opengl es绘制四边形的三种方法
- android opengl es绘制三角形+纹理
- Android OpenGL ES 基础:绘制三角形
- Android OpenGL ES 2.0绘制简单三角形
- 报表制作软件:finereport和raq的测试对比
- 优秀程序员的十个习惯
- 概率生产器
- 比onload更快获取图片尺寸的javascript图片预加载技术
- gdb调试启动调试程序
- openGL ES 学习第一课(绘制三角形和四边形)
- 启动android project时报resources.ap_ does not exist
- RIA
- plsql编程语法
- poj 2886(线段树+反素数)
- Json与Bean互转,Timestamp类型的问题
- HDU 1211 求逆元
- Fatal error: Call to a member function read() on a non-object in D:\wamp\www\include\dialog\select_i
- Rails部署特别好的文章