1

enter image description here

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?

Community
  • 1
  • 1
droidmachine
  • 577
  • 2
  • 8
  • 24
  • Post image or describe your problem better please. Do you mean it's not transparent? Or that its blurry? – Tim Apr 21 '12 at 18:39
  • @Tim i mean it's blurry.It's not clear. – droidmachine Apr 21 '12 at 19:06
  • You'll get more help with an in-game image of the problem, and your code that you use to draw it. As is there's no way to answer this question. – Tim Apr 21 '12 at 22:48
  • what format are you using to create the texture? it should be GL_RGBA – CuriousGeorge Apr 22 '12 at 01:06
  • @Nick I'm working with opengl 1.1.How can i set it to GL_RGBA? – droidmachine Apr 22 '12 at 18:45
  • Depends if you are using a 3rd party graphics system, or if you're using raw opengl apis. In the case of raw opengl, you would pass in a value of GL_RGBA as the format AND internal format arguments of glTexImage2D(). If you are using a 3rd party library, you'll have to check their documentatation about how to enable the alpha channel in textures. – CuriousGeorge Apr 22 '12 at 20:39
  • @Nick I pass as an internal format of glTexImage2D but i can't understand where i will pass GL_RGBA in values.In vertices?And where? – droidmachine Apr 23 '12 at 11:05
  • glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); - the first GL_RGBA specifies the format this texture will be stored as inside the video card, second GL_RGBA argument specifies the format of the pixel data you are uploading. In your case, since you are using a 32bit png with alpha, and you want alpha when you render, you would use GL_RGBA for both. – CuriousGeorge Apr 23 '12 at 14:20
  • @Nick I'm sorry but in my GLUtils i have only this : GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0,GL10.GL_RGBA, bitmap, 0);.I can't find this method.What is the wrong ? – droidmachine Apr 23 '12 at 17:44
  • sorry, I am used to using the C++ API. The one GL_RGBA is all you should need with that function, so long as your source texture(the png file) has an alpha channel. – CuriousGeorge Apr 23 '12 at 20:58

2 Answers2

0

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);
    }
}
CuriousGeorge
  • 7,120
  • 6
  • 42
  • 74
  • I'm sorry but no change.Blending is enabled.I also enabled four channels but still no change.I can't find solution. – droidmachine Apr 25 '12 at 21:04
  • Accepted because i think that we tried all of the ways.I suppose i will try draw to canvas and use this texture.Lastly which one is useful?Draw to canvas or using a bitmap? – droidmachine Apr 25 '12 at 22:48
0

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.

free3dom
  • 18,729
  • 7
  • 52
  • 51