2

I'm new to open gl in android and I need to draw some text in my GLSurfaceView. I found only one solution - to create bitmap with text and display it like texture (like this, for example; http://giantandroid.blogspot.ru/2011/03/draw-text-in-opengl-es.html). I tried to do like this, but it didn't work for me. What is textures array in this code? And is there any simplier way to display text?

Mary Ryllo
  • 2,321
  • 7
  • 34
  • 53

1 Answers1

1

openGL by itself doesn't offer any "simpler" way to render text - it even doesn't "know" anything about the way glyphs may be represented in bitmap or outline font sets.

Using some other library "knowing" how to handle different font sets and how to rasterize them to directly let them paint into an openGL texture doesn't seem so complicated that any other approach may claim to be a lot easier.

You might want to take a look at both this (see: Draw text in OpenGL ES) article here on stackoverflow as well as into the linked pages to get an overview of other methods available to choose the one that seems best to you.

Regarding your second question about the textures array in the code sample you linked the array is just used to fulfill the API requirements of the call to glGenTextures() as it expects an array as the second argument.

In fact just a single texture id is allocated in this line:

gl.glGenTextures(1, textures, 0);

Taking a look at the spec (see: http://docs.oracle.com/javame/config/cldc/opt-pkgs/api/jb/jsr239/javax/microedition/khronos/opengles/GL10.html) it turns out there is just a single texture id allocated stored at index 0 in the textures array.

Why then a (pseudo) array at all?

The seemingly complex signature of the java method

public void glGenTextures(int n, int[] textures, int offset)

is due to the fact that there are no pointers in java while the C function that gets wrappped requires a pointer as it's second argument:

void glGenTextures(GLsizei n, GLuint *textures);

As in C code allocating a single texture can be done with:

  GLuint texture_id;
  glGenTextures (1, &texture_id);

there is no need for a special function just returning a single texture id, so there's only a single glGenTextures() function available.

To allow for allocations that can be done with pointer arithmetic like:

  GLuint textures[10];
  glGenTextures (3, textures + 5);

in C the java wrapper allows for a third parameter specifying the starting index from which to assign allocated texture ids.

The corresponding method invocation in java would look like this:

   int[] textures = new int[10];
   glGenTextures (3, textures, 5);

Thus one more parameter in java wrapper and the requirement to use an array even if only a single id is needed.

Community
  • 1
  • 1
mikyra
  • 10,077
  • 1
  • 40
  • 41