android地图sdk | 百度地图api sdk-凯发k8官方旗舰厅

浏览器版本低!无法浏览完整内容,建议升级或更换浏览器。
opengl绘制
下载开发文档
opengl绘制(绘制3d模型)

百度地图sdk为开发者开放了opengl绘制接口,以帮助开发者在地图上实现灵活的样式绘制,丰富地图的显示效果和使用体验。
自v4.2.0起,百度地图sdk支持opengles 2.0,请开发者调用onmapdrawframecallback接口的onmapdrawframe(mapstatus drawingmapstatus)方法来进行绘制,在地图渲染每一帧的过程中,以及每次需要重绘地图时(如添加覆盖物),该接口都会被调用。
自v4.2.0起,旧版的onmapdrawframe(gl10 gl, mapstatus drawingmapstatus)方法废弃。

绘制3d立方体

下面代码以在地图上绘制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
};
// 立方体顶点坐标buffer
privatefloatbuffer mvertextbuffer;
// 顶点绘制顺序buffer
privateshortbuffer mindexbuffer;
// 立方体顶点颜色buffer
privatefloatbuffer 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);
// 立方体顶点绘制顺序buffer
bytebuffer bytebuffer =bytebuffer.allocatedirect(mdrawindices.length*4);
bytebuffer.order(byteorder.nativeorder());
mindexbuffer = bytebuffer.asshortbuffer();
mindexbuffer.put(mdrawindices);
mindexbuffer.position(0);
// 立方体顶点颜色buffer
bytebuffer 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(){
//废弃
@override
publicvoidonmapdrawframe(gl10 gl,mapstatus drawingmapstatus){
}
@override
publicvoidonmapdrawframe(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);
}

效果如图:

设置onmapdrawframecallback接口

上一篇

自定义瓦片图层

下一篇

绘制3d棱柱

本篇文章对您是否有帮助?

网站地图