
I'm using this font.png but in game it doesn't look clear.How can i draw a clear text in my game?Rendering text to a texture like in Draw text in OpenGL ES is more logical then my solution?

I'm using this font.png but in game it doesn't look clear.How can i draw a clear text in my game?Rendering text to a texture like in Draw text in OpenGL ES is more logical then my solution?
have you enabled blending for your OpenGL context?
try this code in OnSurfaceCreated():
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_BLEND);
edit: this code works with your texture: just rename your texture to "text.png" and put it in res/raw/
//////////////////////////////
// TextureTestActivity.java
package com.TextureTest.TextureTest;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.opengl.GLSurfaceView;
public class TextureTestActivity extends Activity {
private GLSurfaceView mGLView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLView = new TextureTestSurfaceView(this);
setContentView(mGLView);
}
@Override
protected void onPause() {
super.onPause();
mGLView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mGLView.onResume();
}
}
class TextureTestSurfaceView extends GLSurfaceView {
public TextureTestSurfaceView(Context context) {
super(context);
setEGLConfigChooser(false);
setRenderer(new TextureTestRenderer(context));
}
}
//////////////////////////////
// TextureTestRenderer.java
package com.TextureTest.TextureTest;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES10;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.opengl.GLUtils;
public class TextureTestRenderer implements GLSurfaceView.Renderer {
Context context;
int textureID;
float vertices[] = {
-1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f
};
float texcoords[] = {
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f,
};
FloatBuffer texCoordBuffer;
FloatBuffer vertexBuffer;
TextureTestRenderer(Context context) {
this.context = context;
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// set gl options
gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_BLEND);
// create texture
int textures[] = new int[1];
gl.glGenTextures(1, textures, 0);
textureID = textures[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
InputStream is = context.getResources().openRawResource(R.raw.text);
Bitmap textBitmap = BitmapFactory.decodeStream(is);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, textBitmap, 0);
textBitmap.recycle();
// create vertex and texcoord buffers
ByteBuffer vbb = ByteBuffer.allocateDirect(4 * 3 * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
ByteBuffer tbb = ByteBuffer.allocateDirect(4 * 2 * 4);
tbb.order(ByteOrder.nativeOrder());
texCoordBuffer = tbb.asFloatBuffer();
for (int v = 0; v < 4; v++)
for(int c = 0; c < 3; c++)
vertexBuffer.put(vertices[v * 3 + c]);
for (int v = 0; v < 4; v++)
for(int c = 0; c < 2; c++)
texCoordBuffer.put(texcoords[v * 2 + c]);
vertexBuffer.position(0);
texCoordBuffer.position(0);
// set up view matrices
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0.0f, 0.0f, 5.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texCoordBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES10.glViewport(0, 0, width, height);
}
}
I know this is a bit late...but this seems like a filtering issue. Try using "nearest" filtering (instead of "linear") for the texture. This can be done with:
gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST );
gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST );
Call this before rendering, and you can then set back to "linear" filtering after rendering by calling both methods again, but using GL10.GL_LINEAR instead of GL10.GL_NEAREST.
You do not need both calls though, the first one is for when the texture is rendered smaller than it's actual resolution, and the second is for when it is rendered larger. In my experience, it is best for the readability of text to use "nearest" filtering when rendering smaller, and "linear" when rendering larger; but that is subjective so you should experiment and find what works best for you.