百度地图sdk为开发者开放了opengl绘制接口,以帮助开发者在地图上实现灵活的样式绘制,丰富地图的显示效果和使用体验。
自v4.2.0起,百度地图sdk支持opengles 2.0,请开发者调用onmapdrawframecallback接口的onmapdrawframe(mapstatus drawingmapstatus)方法来进行绘制,在地图渲染每一帧的过程中,以及每次需要重绘地图时(如添加覆盖物),该接口都会被调用。
自v4.2.0起,旧版的onmapdrawframe(gl10 gl, mapstatus drawingmapstatus)方法废弃。
下面代码以在地图上绘制3d立方体为例,介绍如何使用opengl绘制接口。
定义3d立方体着色器类
privateclasscubeshader{int mvertex;int mmvpmatrix;int mcolor;int mprogram;publiccubeshader(){}string vertexshader ="precision highp float;\n"" attribute vec3 mvertex;//顶点数组,三维坐标\n"" attribute vec4 mcolor;//颜色数组,三维坐标\n"" uniform mat4 mmvpmatrix;//mvp矩阵\n"" varying vec4 color;//\n"" void main(){\n"" gl_position = mmvpmatrix * vec4(mvertex, 1.0);\n"" color = mcolor;\n"" }";string fragmentshader ="//有颜色 没有纹理\n"" precision highp float;\n"" varying vec4 color;//\n"" void main(){\n"" gl_fragcolor = color;\n"" }";publicvoidinit(){int vertexlocation =gles20.glcreateshader(gles20.gl_vertex_shader);gles20.glshadersource(vertexlocation, vertexshader);gles20.glcompileshader(vertexlocation);int fragmentlocation =gles20.glcreateshader(gles20.gl_fragment_shader);gles20.glshadersource(fragmentlocation, fragmentshader);gles20.glcompileshader(fragmentlocation);mprogram =gles20.glcreateprogram();gles20.glattachshader(mprogram, vertexlocation);gles20.glattachshader(mprogram, fragmentlocation);gles20.gllinkprogram(mprogram);mvertex =gles20.glgetattriblocation(mprogram,"mvertex");mmvpmatrix =gles20.glgetuniformlocation(mprogram,"mmvpmatrix");mcolor =gles20.glgetattriblocation(mprogram,"mcolor");}}
准备立方体数据并初始化
1、声明所需数据及属性
// 3d立方体顶点绘制顺序列表private short[] mdrawindices ={0,4,5,0,5,1,1,5,6,1,6,2,2,6,7,2,7,3,3,7,4,3,4,0,4,7,6,4,6,5,3,0,1,3,1,2};// 3d立方体8个顶点颜色值private float[] mvertexcolors ={1f, 1f, 0f, 1f,0f, 1f, 1f, 1f,1f, 0f, 1f, 1f,0f, 0f, 0f, 1f,1f, 1f, 1f, 1f,1f, 0f, 0f, 1f,0f, 1f, 0f, 1f,0f, 0f, 1f, 1f};// 立方体顶点坐标bufferprivatefloatbuffer mvertextbuffer;// 顶点绘制顺序bufferprivateshortbuffer mindexbuffer;// 立方体顶点颜色bufferprivatefloatbuffer mcolorbuffer;// 3d立方体着色器privatecubeshader mcubeshader;
2、初始化数据
privatevoidinitcubemodeldata(float width, float height, float depth){// 对标墨卡托坐标width = width *10000/2;height = height *10000/2;depth = depth *10000/2;// 立方体8个顶点坐标float[] vertices ={-width,-height,-0,width,-height,-0,width, height,-0,-width, height,-0,-width,-height, depth,width,-height, depth,width, height, depth,-width, height, depth,};mvertextbuffer =bytebuffer.allocatedirect(vertices.length*4).order(byteorder.nativeorder()).asfloatbuffer();mvertextbuffer.put(vertices).position(0);// 立方体顶点绘制顺序bufferbytebuffer bytebuffer =bytebuffer.allocatedirect(mdrawindices.length*4);bytebuffer.order(byteorder.nativeorder());mindexbuffer = bytebuffer.asshortbuffer();mindexbuffer.put(mdrawindices);mindexbuffer.position(0);// 立方体顶点颜色bufferbytebuffer bytebuffer1 =bytebuffer.allocatedirect(mvertexcolors.length*4);bytebuffer1.order(byteorder.nativeorder());mcolorbuffer = bytebuffer1.asfloatbuffer();mcolorbuffer.put(mvertexcolors);mcolorbuffer.position(0);}
调用onmapdrawframecallback接口绘制
1、创建onmapdrawframecallback实例
baidumap.onmapdrawframecallback callback =newbaidumap.onmapdrawframecallback(){//废弃@overridepublicvoidonmapdrawframe(gl10 gl,mapstatus drawingmapstatus){}@overridepublicvoidonmapdrawframe(mapstatus drawingmapstatus){if(null== mbaidumap.getprojection()){return;}drawframefor3dcube(drawingmapstatus,0.2f,0.2f,0.3f);}};
2、设置onmapdrawframecallback接口
mbaidumap.setonmapdrawframecallback(callback);
3、绘制方法
/*** 绘制3d立方体* @param drawingmapstatus*/privatevoiddrawcube(mapstatus drawingmapstatus){if(null== mcubeshader ||null== drawingmapstatus){return;}// step1 初始化数据float[] mvpmatrix =newfloat[16];matrix.setidentitym(mvpmatrix,0);// 获取投影矩阵float[] projectmatrix = mbaidumap.getprojectionmatrix();// 获取视图矩阵float[] viewmatrix = mbaidumap.getviewmatrix();matrix.multiplymm(mvpmatrix,0, projectmatrix,0, viewmatrix,0);// 绑定地图移动pointf p1f = mbaidumap.getprojection().toopengllocation(latlng1, drawingmapstatus);matrix.translatem(mvpmatrix,0, p1f.x, p1f.y,0);// 设置缩放比例int scale =1;matrix.scalem(mvpmatrix,0, scale, scale, scale);// step2 开始绘制设置gles20.gluseprogram(mcubeshader.mprogram);gles20.glenable(gles20.gl_depth_test);// 顶点指针gles20.glenablevertexattribarray(mcubeshader.mvertex);gles20.glvertexattribpointer(mcubeshader.mvertex,3,gles20.gl_float,false,0, mvertextbuffer);// 颜色指针gles20.glenablevertexattribarray(mcubeshader.mcolor);gles20.glvertexattribpointer(mcubeshader.mcolor,4,gles20.gl_float,false,0, mcolorbuffer);gles20.gluniformmatrix4fv(mcubeshader.mmvpmatrix,1,false, mvpmatrix,0);// 开始画gles20.gldrawelements(gles20.gl_triangles, mdrawindices.length,gles20.gl_unsigned_short, mindexbuffer);gles20.gldisablevertexattribarray(mcubeshader.mvertex);gles20.gldisable(gles20.gl_depth_test);}
效果如图:
上一篇
下一篇
本篇文章对您是否有帮助?