openGL es学习(1.画点与画线)

时间:2022-09-10 17:15:05

1.创建一个接口,好处在于直接在activity修改渲染操作,免得换来换去

package com.opengles;

import javax.microedition.khronos.opengles.GL10;

public interface IOpenGLDemo {
public void DrawScene(GL10 gl);
}


2.创建渲染类OpenGLRenderer

package com.opengles;

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

import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;

public class OpenGLRenderer implements Renderer {

private final IOpenGLDemo openGLDemo;

public OpenGLRenderer(IOpenGLDemo demo) {
openGLDemo
= demo;
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Set the background color to black ( rgba ).
//黑色背景
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
// Enable Smooth Shading, default not really needed.
// 启用阴影平滑(不是必须的)
gl.glShadeModel(GL10.GL_SMOOTH);
// Depth buffer setup.
// 设置深度缓存
gl.glClearDepthf(1.0f);
// Enables depth testing.
// 启用深度测试
gl.glEnable(GL10.GL_DEPTH_TEST);
// The type of depth testing to do.
// 所作深度测试的类型
gl.glDepthFunc(GL10.GL_LEQUAL);
// Really nice perspective calculations.
// 对透视进行修正
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

}

public void onDrawFrame(GL10 gl) {
if (openGLDemo != null) {
openGLDemo.DrawScene(gl);
}
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
// Sets the current view port to the new size.
// 设置画面的大小
gl.glViewport(0, 0, width, height);
// Select the projection matrix
// 设置投影矩阵
gl.glMatrixMode(GL10.GL_PROJECTION);
// Reset the projection matrix
// 重置投影矩阵
gl.glLoadIdentity();
// Calculate the aspect ratio of the window
// 设置画面比例
GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
100.0f);
// Select the modelview matrix
// 选择模型观察矩阵
gl.glMatrixMode(GL10.GL_MODELVIEW);
// Reset the modelview matrix
// 重置模型观察矩阵
gl.glLoadIdentity();
}
}

3.创建activity,实现上面的接口

package com.opengles;

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

import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class Main extends Activity implements IOpenGLDemo {

private GLSurfaceView mGLSurfaceView;

// 画点的坐标
float[] vertexArray = new float[] {
-0.8f, -0.4f * 1.732f, 0.0f,
0.8f,-0.4f * 1.732f, 0.0f,
0.0f, 0.4f * 1.732f, 0.0f, };

// 画线的坐标
float vertexArray2[] = {
-0.8f, -0.4f * 1.732f, 0.0f,
-0.4f, 0.4f * 1.732f,0.0f,
0.0f, -0.4f * 1.732f, 0.0f,
0.4f, 0.4f * 1.732f, 0.0f, };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

mGLSurfaceView
= new GLSurfaceView(this);
mGLSurfaceView.setRenderer(
new OpenGLRenderer(this));
setContentView(mGLSurfaceView);
}

//渲染操作就在DrawScene(GL10 gl)这个办法操作
// 画背景色
// public void DrawScene(GL10 gl) {
// gl.glClearColor(0f, 0f, 1.0f, 0.0f);//设置背景颜色
// // Clears the screen and depth buffer.
// gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// }

// 画点
// public void DrawScene(GL10 gl) {
// gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);//每次画页面把上次的缓存清除掉
// //System.out.println("这里已经执行");
// ByteBuffer vbb = ByteBuffer.allocateDirect(vertexArray.length * 4);
// vbb.order(ByteOrder.nativeOrder());
// FloatBuffer vertex = vbb.asFloatBuffer();
// vertex.put(vertexArray);
// vertex.position(0);
//
// gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);//点的颜色
// gl.glPointSize(18f);
// gl.glLoadIdentity();
// gl.glTranslatef(0, 0, -4);
//
// gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
//
// gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex);//3表示xyz3唯坐标
// gl.glDrawArrays(GL10.GL_POINTS, 0, 3);
//
// gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
//
// }

// 画线
public void DrawScene(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT
| GL10.GL_DEPTH_BUFFER_BIT);
ByteBuffer vbb
= ByteBuffer.allocateDirect(vertexArray2.length * 4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer vertex
= vbb.asFloatBuffer();
vertex.put(vertexArray);
vertex.position(
0);

gl.glLoadIdentity();
gl.glTranslatef(
0, 0, -4);

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glVertexPointer(
3, GL10.GL_FLOAT, 0, vertex);

int index = new Random().nextInt(4);
switch (index) {

case 1:
gl.glColor4f(
1.0f, 0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_LINES,
0, 4);
break;
case 2:
gl.glColor4f(
0.0f, 1.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_LINE_STRIP,
0, 4);
break;
case 3:
gl.glColor4f(
0.0f, 0.0f, 1.0f, 1.0f);
gl.glDrawArrays(GL10.GL_LINE_LOOP,
0, 4);
break;
}

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

}

@Override
protected void onResume() {
// Ideally a game should implement
// onResume() and onPause()
// to take appropriate action when the
// activity looses focus
super.onResume();
mGLSurfaceView.onResume();
}

@Override
protected void onPause() {
// Ideally a game should implement onResume()
// and onPause()
// to take appropriate action when the
// activity looses focus
super.onPause();
mGLSurfaceView.onPause();
}

}


参考于:http://www.apkbus.com/android-20427-1-1.html