OpenGL学习(1)基本概述
(1)重写Rendar(渲染器), 重写三个方法:
a、onSurfaceCreated(GL10 gl, EGLConfig config);
b、onSurfaceChanged(GL10 gl, int width, int height);
c、 onDrawFrame(GL10 gl);
package com.example.mychapter2;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
public class GLRender implements Renderer
{
public void onDrawFrame(GL10 gl)
{
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl,
0,
0,
3,
0,
0,
0,
0,
1,
0);
}
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);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
gl.glClearColor(
0,
0,
0,
1);
gl.glEnable(GL10.GL_DEPTH_TEST);
}
}
(2)创建一个GLSurfaceView
(a)创建一个GLSurfaceView实例mGLSurfaceView, (b)设置渲染器mGLSurfaceView.setRenderer(Render);
package com.example.mychapter2;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class ChapterMain extends Activity
{
private GLSurfaceView mGLSurfaceView;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mGLSurfaceView =
new GLSurfaceView(
this);
mGLSurfaceView.setRenderer(
new GLRender());
setContentView(mGLSurfaceView);
}
protected void onResume()
{
super.onResume();
mGLSurfaceView.onResume();
}
protected void onPause()
{
super.onPause();
mGLSurfaceView.onPause();
}
}
转载请注明原文地址: https://ju.6miu.com/read-1300032.html