how to render a map in android opengl
I'm trying to show a bitmap in android opengl-es3.1.
The bitmap is created with Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
and I filled the bitmap with pixels.
Here is the bitmap I created:
But the emulator show a strange thing:
Here is the main render code:
// generate map
private void initMap(OccupancyGrid occupancyGrid) {
int height = occupancyGrid.info.height;
int width = occupancyGrid.info.width;
Bitmap newMap = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
int[] pixels;
int bytePixel;
int curr = 0;
for (int y = 0; y < height; y++) {
pixels = new int[width];
for (int x = 0; x < width; x++) {
// Pixels are ARGB packed ints.
bytePixel = occupancyGrid.data[curr++];
if (bytePixel == -1) {
// black color
pixels[x] = gradient[101];
} else {
// gray color
pixels[x] = gradient[bytePixel];
}
}
newMap.setPixels(pixels, 0, width, 0, y, width, 1);
}
bitmap = newMap;
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
}
@Override
public void onDrawFrame(GL10 gl) {
// enable
GLES31.glEnable(GLES31.GL_TEXTURE_2D);
final int[] textureIds = new int[1];
//create
GLES31.glGenTextures(1, textureIds, 0);
// bind
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, textureIds[0]);
//set default param
GLES31.glTexParameterf(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MIN_FILTER, GLES31.GL_NEAREST);
GLES31.glTexParameterf(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MAG_FILTER, GLES31.GL_NEAREST);
GLES31.glTexParameteri(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_BASE_LEVEL, 0);
GLES31.glTexParameteri(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MAX_LEVEL, 0);
// load bitmap
GLUtils.texImage2D(GLES31.GL_TEXTURE_2D, 0, bitmap, 0);
// vertex
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, surfaceVertices);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureVertices);
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
}
View the full code in repository in repository link
And by the way, I'm a beginner of both ROS
and opengl
, I read the code of VisualizationView
and OccupyGridLayer
in ros-anroid
project, I can't understand the Tile
class clearly.
Does it(a tile) represent a point in map data?
A Tile corresponds A TexureBitmap
, and the size of TextureBitmap is 1024, I'm confused.
Any way, my purpose is to show a ros map in android app. I can handle the ros websocket bridge, but I really need some help of opengl.
Just a quick guess, but the map is white and everything else transparent. If your render settings paint a white background (might be default) you will not "see" the map. I would suggest checking "background color".