3rd Chapter (24th Lecture) Example 24--OpenGL drawing function

Source: Internet
Author: User

Category: C #, Android, Baidu map application; Date: 2016-02-04 I. INTRODUCTION

Baidu Map SDK for the vast number of developers to open the OpenGL drawing interface, to help developers on the map to achieve more flexible style drawing, enrich the experience of the map use effect.

Second, the operation

Summary: Describes how to use OpenGL to implement custom drawing on a map.

Detailed:

(1) Using OpenGL to draw the basic polyline;

(2) using OpenGL to draw textures on the map;

This example runs as follows:

Third, design steps

1. Add Demo24_opengl.xml File

Add the file under the Layout folder, and then change the code to the following:

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns: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.TextureMapViewAndroid:id= "@+id/bmapview"Android:layout_width= "Match_parent"Android:layout_height= "Fill_parent" /></Relativelayout>

2. Add Demo24OpenGL.cs File

Add the file under the Srcsdkdemos folder, and then change the code to the following:

usingAndroid.app;usingAndroid.os;usingCom.Baidu.Mapapi.Map;usingCom.Baidu.Mapapi.Model;usingAndroid.graphics;usingAndroid.util;usingSystem.Collections.Generic;usingJavax.Microedition.Khronos.Opengles;usingJava.nio;usingAndroid.opengl;namespacebdmapv371demos.srcsdkdemos{/// <summary>    ///This demo is used to show how to draw additional users ' own content in each frame of the map drawing/// </summary>[Activity (Label ="@string/demo_name_opengl")]     Public classdemo24opengl:activity, Baidumap.ionmapdrawframecallback {//Map Related        PrivateTexturemapview Mmapview; PrivateBaidumap Mbaidumap; PrivateBitmap Bitmap; PrivateLATLNG latlng1 =NewLATLNG (39.97923,116.357428); PrivateLATLNG latlng2 =NewLATLNG (39.94923,116.397428); PrivateLATLNG Latlng3 =NewLATLNG (39.96923,116.437428); PrivateIlist<latlng>Latlngpolygon; Private float[] vertexs; PrivateFloatbuffer VertexBuffer; Private intTextureid =-1; Private ReadOnly stringLtag ="Demo24opengl"; protected Override voidOnCreate (Bundle savedinstancestate) {Base.            OnCreate (savedinstancestate);            Setcontentview (RESOURCE.LAYOUT.DEMO24_OPENGL); Mmapview= findviewbyid<texturemapview>(Resource.Id.bmapView); Mbaidumap=Mmapview.map; Latlngpolygon=NewList<latlng>() {latlng1,latlng2,latlng3}; Mbaidumap.setonmapdrawframecallback ( This); Bitmap=Bitmapfactory.decoderesource (Resources, Resource.Drawable.ground_overlay); }        protected Override voidOnPause () {mmapview.onpause (); Base.        OnPause (); }        protected Override voidOnresume () {mmapview.onresume (); Textureid= -1; Base.        Onresume (); }        protected Override voidOnDestroy () {Mmapview.ondestroy (); Base.        OnDestroy (); }         Public voidOnmapdrawframe (IGL10 gl, mapstatus drawingmapstatus) {if(Mbaidumap.projection! =NULL) {calpolylinepoint (drawingmapstatus); DrawPolyline (GL, Color.argb (255,255,0,0), VertexBuffer,Ten,3, Drawingmapstatus);            Drawtexture (GL, Bitmap, drawingmapstatus); }        }         Public voidcalpolylinepoint (Mapstatus mspstatus) {pointf[] polypoints=NewPointf[latlngpolygon.count]; Vertexs=New float[3*Latlngpolygon.count]; inti =0; foreach(LATLNG XYinchLatlngpolygon) {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(intj =0; J < Vertexs. Length; J + +) {log.debug (Ltag,"vertexs["+ j +"]: "+Vertexs[j]); } vertexbuffer=Makefloatbuffer (VERTEXS); }        PrivateFloatbuffer Makefloatbuffer (float[] FS) {Bytebuffer BB= Bytebuffer.allocatedirect (fs. Length *4); Dbh            Order (Byteorder.nativeorder ()); Floatbuffer FB=BB.            Asfloatbuffer (); Fb.            Put (FS); Fb. Position (0); returnFB; }        Private voidDrawPolyline (IGL10 GL,intcolor, Floatbuffer linevertexbuffer,floatLineWidth,intpointsize, Mapstatus drawingmapstatus) {gl. Glenable (GL10.            Glblend); Gl. Glenableclientstate (GL10.            Glvertexarray); Gl. Glblendfunc (GL10. Glsrcalpha, GL10.            Gloneminussrcalpha); floatColora = Color.getalphacomponent (Color)/255f; floatColorr = Color.getredcomponent (Color)/255f; floatcolorg = Color.getgreencomponent (Color)/255f; floatColorb = 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>        ///drawing with OpenGL coordinates/// </summary>        /// <param name= "GL" ></param>        /// <param name= "Bitmap" ></param>        /// <param name= "Drawingmapstatus" ></param>         Public voiddrawtexture (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. Y0.0f, p2. X, p1. Y0.0f, p1. X, p2. Y,0.0f, p2. X, p2. Y0.0f }); Bytebuffer Indicesbuffer= Bytebuffer.allocatedirect (6*2);            Indicesbuffer.order (Byteorder.nativeorder ()); Shortbuffer 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); //Creating Textures            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. Glenable (GL10.            GLTEXTURE2D); Gl. Glenableclientstate (GL10.            Glvertexarray); Gl. Glenableclientstate (GL10.            Gltexturecoordarray); Gl. Glenable (GL10.            Glblend); Gl. Glblendfunc (GL10. Glsrcalpha, GL10.            Gloneminussrcalpha); Gl. GLCOLOR4F (1.0f,1.0f,1.0f,1.0f); //Binding Texture IDGL. 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 under "Example 24".

Run the observation effect.

3rd Chapter (24th Lecture) Example 24--OpenGL drawing function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.