OpenGL ES (OpenGL for Embedded Systems) is a subset of the OpenGL three-dimensional graphics API designed for embedded devices such as mobile phones, PDAs, and game consoles.
The API is defined by the Khronos Group, Khronos is a graphic hardware and software industry association that focuses on open standards for graphics and multimedia.
Build a basic framework for 3D development in Android:
First, import OpenGL ES Library:
[Java]
Import Javax.microedition.khronos.egl.EGLConfig;
Import javax.microedition.khronos.opengles.GL10;
Import Javax.microedition.khronos.egl.EGLConfig;
Import javax.microedition.khronos.opengles.GL10;
Second, add render support:
In Android, Glsurfaceview contains an interface renderer designed to render the OpenGL view:
1, Import Support library:
[Java]
Import Android.opengl.GLSurfaceView;
Import Android.opengl.GLSurfaceView.Renderer;
Import Android.opengl.GLSurfaceView;
Import Android.opengl.GLSurfaceView.Renderer;
2, create a Glrender class implementation renderer interface:
[Java] View Plaincopyprint?public class Glrender implements Renderer
{
}
public class Glrender implements Renderer
{
}
3. Implement the 3 abstract methods in the Glrender class:
[Java]
public void Ondrawframe (GL10 gl)
public void onsurfacechanged (GL10 gl, int width, int height)
public void onsurfacecreated (GL10 gl, eglconfig config)
public void Ondrawframe (GL10 gl)
public void onsurfacechanged (GL10 gl, int width, int height)
public void onsurfacecreated (GL10 gl, eglconfig config)
①onsurfacecreated is invoked when a window is created, and needs to do some necessary initialization work:
[Java] View plaincopyprint?public void onsurfacecreated (GL10 gl, eglconfig config)
{
Enable Shadow Smoothing
Gl.glshademodel (Gl10.gl_smooth);
Black background, set the color used to clear the screen-->> (R, G, B, A) value: 0.0f-1.0f.
Gl.glclearcolor (0, 0, 0, 0);
Set depth cache-->> decide which object to draw first.
GL.GLCLEARDEPTHF (1.0f);
Enable depth Testing
Gl.glenable (gl10.gl_depth_test);
Type of depth test made
Gl.gldepthfunc (gl10.gl_lequal);
Tell the system to fix the perspective
Gl.glhint (Gl10.gl_perspective_correction_hint, gl10.gl_fastest);
}
public void onsurfacecreated (GL10 gl, eglconfig config)
{
Enable Shadow Smoothing
Gl.glshademodel (Gl10.gl_smooth);
Black background, set the color used to clear the screen-->> (R, G, B, A) value: 0.0f-1.0f.
Gl.glclearcolor (0, 0, 0, 0);
Set depth cache-->> decide which object to draw first.
GL.GLCLEARDEPTHF (1.0f);
Enable depth Testing
Gl.glenable (gl10.gl_depth_test);
Type of depth test made
Gl.gldepthfunc (gl10.gl_lequal);
Tell the system to fix the perspective
Gl.glhint (Gl10.gl_perspective_correction_hint, gl10.gl_fastest);
}②onsurfacechanged is invoked when the size of the window changes, regardless of whether the size of the window has changed, run at least once at the beginning of the program.
In this, we want to set the scene size for OpenGL:
[Java]
public void onsurfacechanged (GL10 gl, int width, int height)
{
float ratio = (float) width/height;
Set the size of an OpenGL scene
Gl.glviewport (0, 0, width, height);
Set the projection matrix-->> to increase the perspective.
Gl.glmatrixmode (gl10.gl_projection);
Resets the projection matrix-->> back into its original state.
Gl.glloadidentity ();
Set the size of the viewport-->> the first 4 parameters determine the size of the window, and the last 2 parameters are the starting and ending points of the depth in the scene.
GL.GLFRUSTUMF (-ratio, ratio,-1, 1, 1, 10);
Select Model Observation Matrix
Gl.glmatrixmode (Gl10.gl_modelview);
Reset Model Observation Matrix
Gl.glloadidentity ();
}
public void onsurfacechanged (GL10 gl, int width, int height)
{
float ratio = (float) width/height;
Set the size of an OpenGL scene
Gl.glviewport (0, 0, width, height);
Set the projection matrix-->> to increase the perspective.
Gl.glmatrixmode (gl10.gl_projection);
Resets the projection matrix-->> back into its original state.
Gl.glloadidentity ();
Set the size of the viewport-->> the first 4 parameters determine the size of the window, and the last 2 parameters are the starting and ending points of the depth in the scene.
GL.GLFRUSTUMF (-ratio, ratio,-1, 1, 1, 10);
Select Model Observation Matrix
Gl.glmatrixmode (Gl10.gl_modelview);
Reset Model Observation Matrix
Gl.glloadidentity ();
}③ondrawframe the drawing operation within the window. Before drawing, you need to clear the screen to the color you specified earlier, clear the depth cache, and reset the scene.
And then you can draw:
[Java]
public void Ondrawframe (GL10 gl)
{
Clear screen and depth caching
Gl.glclear (Gl10.gl_color_buffer_bit | Gl10.gl_depth_buffer_bit);
Resetting the current model observation matrix
Gl.glloadidentity ();
The concrete drawing operation begins ...
}
public void Ondrawframe (GL10 gl)
{
Clear screen and depth caching
Gl.glclear (Gl10.gl_color_buffer_bit | Gl10.gl_depth_buffer_bit);
Resetting the current model observation matrix
Gl.glloadidentity ();
The concrete drawing operation begins ...
}
Third, in the main program to invoke our written interface:
The Setrenderer method that calls the Glsurfaceview class sets our own built Glrender class as the default renderer,
The activity is then displayed as a Glsurfaceview by the Setcontentview method.
[Java]
Renderer render = new Glrender ();
Glsurfaceview Glview = new Glsurfaceview (this);
Glview.setrenderer (render);
Setcontentview (Glview);
Renderer render = new Glrender ();
Glsurfaceview Glview = new Glsurfaceview (this);
Glview.setrenderer (render);
Setcontentview (Glview);
Here, our basic framework is built.