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

<<Android.游戏开发入门](美)Mario.Zechner>>读书笔记-----OpenGL ES概览

2012-06-05 22:01 711 查看
<<Android.游戏开发入门>>

CHAPTER 7: OpenGL ES: A Gentle Introduction

This means that the memory is

not allocated in the virtual machine’s heap memory, but in native heap memory.

NIO直接从本地堆内存分配 ,而不是VM 堆内存

float[] vertices = { ... definitions of vertex positions etc ...;//定义顶点
floatBuffer.clear();//重置buffer sets the position to zero and the limit to the capacity.
floatBuffer.put(vertices);//把顶点数据存入buffer
floatBuffer.flip();//切换模式 从写入模式进入读出模式 just swaps the position and limit.

we can only access OpenGL ES on the rendering
thread.
http://insanitydesign.com/wp/projects/nehe-android-ports/
2012-6-5

NOTE: While this method looks like it sets up a 2D coordinate system for us to render to, it
actually does not. It only defines the portion of the framebuffer OpenGL ES uses to output the
final image. Our coordinate system is defined via the projection and model-view matrices.

GL10.glViewport(int x, int y, int width, int height) 看起来像设置显示,其实不是

opengl es 追踪三个矩阵:projection matrix ,model-view matrix,texture matrix

在操作他们的数据前,先要告诉openglES 要操作哪个,使用:

GL10.glMatrixMode(int mode)

mode: 可选值 GL10.GL_PROJECTION, GL10.GL_MODELVIEW, or

GL10.GL_TEXTURE.

This matrix mode is one of OpenGL ES’s states (which will get lost when we
lose the context if our application is paused and resumed)

矩阵模式是opengl es的状态之一(会随应用程序状态变化发生丢失)

gl.glClearColor(0,0,0,1);//用RGBA颜色清除屏幕
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);//执行清除
gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
gl.glMatrixMode(GL10.GL_PROJECTION);//设置matrix mode
gl.glLoadIdentity();//恢复单位矩阵
gl.glOrthof(0, 320, 0, 480, 1, -1);//projection matrix produced by glOrthof()

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//要绘制顶点位置了
gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertices);//一旦执行,顶点信息被存储供后续使用
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);

package com.test.opengles;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.Random;

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

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
/**
* OpenGL ES 绘制三角形测试
* 按照作者所说,不要直接复制粘贴代码了事,要明白每一步在做什么,也不辜负作者用六页详细通俗苦心的解释了:)
* "
* Now compare that to the six pages it took me to explain this to you. I
* could have of course left out the details and used coarser language. The problem is that
* OpenGL ES is a pretty complex beast at times, and to avoid getting an empty screen, it’s
* best to learn what it is all about rather than just copying and pasting code.
* "
* @author Administrator
*
*/
public class MainActivity extends Activity {
GLSurfaceView glView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
glView = new GLSurfaceView(this);
glView.setRenderer(new SimpleRenderer());
setContentView(glView);
}

@Override
public void onResume() {
super.onPause();

glView.onResume();
}

@Override
public void onPause() {
super.onPause();
glView.onPause();
}
/**
* render
* @author Administrator
*
*/
private class SimpleRenderer implements Renderer {
Random rand = new Random();

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Log.d("GLSurfaceViewTest", "surface created");
gl.glViewport(0, 0, glView.getWidth(), glView.getHeight());
gl.glMatrixMode(GL10.GL_PROJECTION);//设置要操作的matrix mode
gl.glLoadIdentity();//单位矩阵
gl.glOrthof(0, 320, 0, 480, 1, -1);//设置projection
/**
* 依赖NIO ByteBuffer来从本地堆分配内存
*/
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3 * 2 * 4);//或最好写成float[].length*4
byteBuffer.order(ByteOrder.nativeOrder());
FloatBuffer vertices = byteBuffer.asFloatBuffer();
vertices.put(new float[] { 0.0f, 0.0f,
319.0f, 0.0f,
160.0f, 479.0f });
vertices.flip();//buffer模式切换,由写变成读
gl.glColor4f(1, 0, 0, 1);//设置RGBA颜色
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertices);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
Log.d("GLSurfaceViewTest", "surface changed: " + width + "x"
+ height);
}
private long start;

@Override
public void onDrawFrame(GL10 gl) {
/**
* The 10 in GL10
* indicates that it offers us all the functions defined in
*  the OpenGL ES 1.0 standard.
* OpenGL ES 1.0标准
*/

gl.glClearColor(0,0,0,1);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);//和setViewPort间无先后关系

gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
}
}

}


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