Chapter 4 (Lecture 3rd) Example 24 -- OpenGL rendering function, 24 -- opengl
Category: C #, Android, Baidu map application; date: 1. Introduction
The Baidu map SDK provides open OpenGL interfaces for developers to help developers achieve more flexible style painting on maps and enrich the experience of map use.
Ii. Running
Introduction: This section describes how to use OpenGL to implement custom plotting on a map.
Details:
(1) Draw a basic line using OpenGL;
(2) Use OpenGL to draw textures on a map;
This example runs as follows:
3. Design Steps
1. Add the demo24_opengl.xml file.
Add the file in the layout folder and change the code to the following content:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.baidu.mapapi.map.TextureMapView android:id="@+id/bmapView" android:layout_width="match_parent" android:layout_height="fill_parent" /></RelativeLayout>
2. Add the Demo24OpenGL. cs file.
Add the file in the SrcSdkDemos folder and change the code to the following content:
Using Android. app; using Android. OS; using Com. baidu. mapapi. map; using Com. baidu. mapapi. model; using Android. graphics; using Android. util; using System. collections. generic; using Javax. microedition. khronos. opengles; using Java. nio; using Android. opengl; namespace BdMapV371Demos. srcSdkDemos {/// <summary> /// this demo shows how to draw additional user content in each frame of the map. /// </summary> [Activity (label = "@ string/demo_name_opengl")] publ Ic class Demo24OpenGL: Activity, BaiduMap. IOnMapDrawFrameCallback {// map related private TextureMapView mMapView; private BaiduMap mBaiduMap; private Bitmap bitmap; private latlng1 = new LatLng (39.97923, 116.357428); private LatLng latlng2 = new LatLng (39.94923, 116.397428); private LatLng latlng3 = new LatLng (39.96923, 116.437428); private IList <LatLng> latLngPolygon; private float [] vertexs; Private FloatBuffer vertexBuffer; private int textureId =-1; private readonly string LTAG = "Demo24OpenGL"; protected override void OnCreate (Bundle savedInstanceState) {base. onCreate (savedInstanceState); SetContentView (Resource. layout. demo24_opengl); mMapView = FindViewById <TextureMapView> (Resource. id. bmapView); mBaiduMap = mMapView. map; latLngPolygon = new List <LatLng> () {latlng1, latlng2, latln G3}; mBaiduMap. setOnMapDrawFrameCallback (this); bitmap = BitmapFactory. decodeResource (Resources, Resource. drawable. ground_overlay);} protected override void OnPause () {mMapView. onPause (); base. onPause ();} protected override void OnResume () {mMapView. onResume (); textureId =-1; base. onResume ();} protected override void OnDestroy () {mMapView. onDestroy (); base. onDestroy ();} public void OnMapD RawFrame (IGL10 gl, MapStatus drawingMapStatus) {if (mBaiduMap. Projection! = Null) {calPolylinePoint (drawingMapStatus); drawPolyline (gl, Color. argb (255,255, 0, 0), vertexBuffer, 10, 3, drawingMapStatus); drawTexture (gl, bitmap, drawingMapStatus);} public void calPolylinePoint (MapStatus mspStatus) {PointF [] polyPoints = new PointF [latLngPolygon. count]; vertexs = new float [3 * latLngPolygon. count]; int I = 0; foreach (LatLng xy in latLngPolygon) {polyPoints [I] = mBaiduMap. projection. toOpenGLLocation (xy, mspStatus); vertexs [I * 3] = polyPoints [I]. x; vertexs [I * 3 + 1] = polyPoints [I]. y; vertexs [I * 3 + 2] = 0.0f; I ++;} for (int j = 0; j <vertexs. length; j ++) {Log. debug (LTAG, "vertexs [" + j + "]:" + vertexs [j]);} vertexBuffer = makeFloatBuffer (vertexs);} private FloatBuffer makeFloatBuffer (float [] fs) {ByteBuffer bb = ByteBuffer. allocateDirect (fs. length * 4); bb. order (ByteOrder. nativeOrder (); FloatBuffer fb = bb. asFloatBuffer (); fb. put (fs); fb. position (0); return fb;} private void drawPolyline (IGL10 gl, int color, FloatBuffer lineVertexBuffer, float lineWidth, int pointSize, MapStatus drawingMapStatus) {gl. glable (GL10.GlBlend); gl. glableclientstate (GL10.GlVertexArray); gl. glBlendFunc (GL10.GlSrcAlpha, GL10.GlOneMinusSrcAlpha); float colorA = Color. getAlphaComponent (color)/255f; float colorR = Color. getRedComponent (color)/255f; float colorG = Color. getGreenComponent (color)/255f; float colorB = Color. getBlueComponent (color)/255f; gl. glVertexPointer (3, GL10.GlFloat, 0, lineVertexBuffer); gl. glColor4f (colorR, colorG, colorB, colorA); gl. glLineWidth (lineWidth); gl. glDrawArrays (GL10.GlLineStrip, 0, pointSize); gl. glDisable (GL10.GlBlend); gl. glDisableClientState (GL10.GlVertexArray );} /// <summary> /// draw with opengl coordinates /// </summary> /// <param name = "gl"> </param> /// <param name = "bitmap"> </param> // <param name = "drawingMapStatus"> </param> public void drawTexture (IGL10 gl, bitmap bitmap, MapStatus drawingMapStatus) {PointF p1 = mBaiduMap. projection. toOpenGLLocation (latlng2, drawingMapStatus); PointF p2 = mBaiduMap. projection. toOpenGLLocation (latlng3, drawingMapStatus); ByteBuffer byteBuffer = ByteBuffer. allocateDirect (4*3*4); byteBuffer. order (ByteOrder. nativeOrder (); FloatBuffer vertices = byteBuffer. asFloatBuffer (); vertices. put (new float [] {p1.X, p1.Y, 0.0f, p2.X, p1.Y, 0.0f, p1.X, p2.Y, 0.0f, p2.X, p2.Y, 0.0f); ByteBuffer indicesBuffer = ByteBuffer. allocateDirect (6*2); indicesBuffer. order (ByteOrder. nativeOrder (); Protocol buffer indices = indicesBuffer. asShortBuffer (); indices. put (new short [] {0, 1, 2, 1, 2, 3}); ByteBuffer textureBuffer = ByteBuffer. allocateDirect (4*2*4); textureBuffer. order (ByteOrder. nativeOrder (); FloatBuffer texture = textureBuffer. asFloatBuffer (); texture. put (new float [] {0, 1f, 1f, 1f, 0f, 0f, 1f, 0f}); indices. position (0); vertices. position (0); texture. position (0); // generate the texture if (textureId =-1) {int [] textureIds = new int [1]; gl. glGenTextures (1, textureIds, 0); textureId = textureIds [0]; Log. debug (LTAG, "textureId:" + textureId); gl. glBindTexture (GL10.GlTexture2d, textureId); GLUtils. texImage2D (GL10.GlTexture2d, 0, bitmap, 0); gl. glTexParameterf (GL10.GlTexture2d, GL10.GlTextureMinFilter, GL10.GlNearest); gl. glTexParameterf (GL10.GlTexture2d, GL10.GlTextureMagFilter, GL10.GlNearest); gl. glBindTexture (GL10.GlTexture2d, 0);} gl. glable (GL10.GlTexture2d); gl. glableclientstate (GL10.GlVertexArray); gl. glableclientstate (GL10.GlTextureCoordArray); gl. glable (GL10.GlBlend); gl. glBlendFunc (GL10.GlSrcAlpha, GL10.GlOneMinusSrcAlpha); gl. glColor4f (1.0f, 1.0f, 1.0f, 1.0f); // bind the texture ID gl. glBindTexture (GL10.GlTexture2d, textureId); gl. glVertexPointer (3, GL10.GlFloat, 0, vertices); gl. glTexCoordPointer (2, GL10.GlFloat, 0, texture); gl. glDrawElements (GL10.GlTriangleStrip, 6, GL10.GlUnsignedShort, indices); gl. glDisable (GL10.GlTexture2d); gl. glDisableClientState (GL10.GlVertexArray); gl. glDisableClientState (GL10.GlTextureCoordArray); gl. glDisable (GL10.GlBlend );}}}
3. Modify the MainActivity. cs File
In the demos field definition of the MainActivity. cs file, remove the comment below [Example 24.
Observe the running effect.