• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
tom_mai78101

OpenGL ES 2.0: Text from TextView isn't showing up

5 posts in this topic

I have adapted the code from Draw Text in OpenGL ES Android into my app, but the text isn't really showing up.

 

Here's what it currently looks like:

 

nKc5V.png

 

The green rectangle box is actually a Drawable bitmap.

 

pBVVs.png

 

I am expecting some sort of text, either white or black on top of the background, but nothing is ever showing up.

 

Here's the code for the Text class.

package gl.es;

import java.nio.*;

import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.opengl.*;
import android.opengl.Matrix;

public class Text {

    public int textTexture;
    public FloatBuffer textFloatBuffer;
    public FloatBuffer textureFloatBuffer;
    public int bufferSize;

    public int program;

    private final int textWidth = 112;
    private final int textHeight = 16;

    //private float posX;

    private int aTextPositionLocation;
    private int aTexturePositionLocation;
    private int uMatrixLocation;
    private int uTextureUnitLocation;

    public Text(Context context) {

        program = Shader.buildProgram(context, R.raw.text_vert, R.raw.text_frag);

        aTextPositionLocation = GLES20.glGetAttribLocation(program, "a_textPosition");
        aTexturePositionLocation = GLES20.glGetAttribLocation(program, "a_texturePosition");
        uMatrixLocation = GLES20.glGetUniformLocation(program, "u_matrix");
        uTextureUnitLocation = GLES20.glGetUniformLocation(program, "u_textureUnit");

        textTexture = createTexture(context);
        setBuffer();

        //posX = 0f;
    }

    public int createTexture(Context context) {
        //Create empty mutable bitmap.
        Bitmap bitmap = Bitmap.createBitmap(textWidth, textHeight, Bitmap.Config.ARGB_8888);
        //Use Canvas to paint over it.
        Canvas canvas = new Canvas(bitmap);
        bitmap.eraseColor(0);

        //Draw background
        Drawable background = context.getResources().getDrawable(R.drawable.text_bg);
        background.setBounds(0, 0, 112, 16);
        background.draw(canvas);

        //Draw text
        Paint textPaint = new Paint();
        textPaint.setTextSize(6f);
        textPaint.setAntiAlias(false);
        textPaint.setARGB(0xff, 0, 0, 0);
        //Draw text centered.
        canvas.drawText("Text.", 0, 0, textPaint);

        int[] texture = new int[1];
        GLES20.glGenTextures(1, texture, 0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture[0]);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

        //Recycle.
        bitmap.recycle();
        return texture[0];
    }

    public void setBuffer() {
        final float[] vertexData = {
                0f, 0f,
                textWidth, 0f,
                0f, textHeight,
                0f, textHeight,
                textWidth, 0f,
                textWidth, textHeight
        };

        final float[] texData = {
                0f, 1f,
                1f, 1f,
                0f, 0f,
                0f, 0f,
                1f, 1f,
                1f, 0f
        };

        textFloatBuffer = ByteBuffer.allocateDirect(vertexData.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
        textFloatBuffer.put(vertexData);
        textureFloatBuffer = ByteBuffer.allocateDirect(texData.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
        textureFloatBuffer.put(texData);
        bufferSize = vertexData.length / 2;
    }

    public void transform(float[] model, float[] projection, float[] mvp) {
        Matrix.setIdentityM(model, 0);
        Matrix.translateM(model, 0, 0f, 0f, -60f);
        Matrix.translateM(model, 0, -60f, 0f, 0f);
        Matrix.multiplyMM(mvp, 0, projection, 0, model, 0);

        //posX = (posX - 0.3f) % 112f;
    }

    public void setVertexPointers() {
        textFloatBuffer.position(0);
        GLES20.glVertexAttribPointer(aTextPositionLocation, 2, GLES20.GL_FLOAT, false, 2 * 4, textFloatBuffer);
        GLES20.glEnableVertexAttribArray(aTextPositionLocation);
        textFloatBuffer.position(0);
        textureFloatBuffer.position(0);
        GLES20.glVertexAttribPointer(aTexturePositionLocation, 2, GLES20.GL_FLOAT, false, 2 * 4, textureFloatBuffer);
        GLES20.glEnableVertexAttribArray(aTexturePositionLocation);
        textureFloatBuffer.position(0);
    }

    public void draw() {
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, bufferSize);
    }

    public void useProgram() {
        GLES20.glUseProgram(program);
    }

    public void setUniforms(float[] matrix) {
        GLES20.glUniformMatrix4fv(uMatrixLocation, 1, false, matrix, 0);
        GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textTexture);
        GLES20.glUniform1i(uTextureUnitLocation, 1);
    }
}

Could someone tell me where I'm doing wrong? What should I do in order to fix this? Thanks in advance.

0

Share this post


Link to post
Share on other sites

Could be:

You're painting the text outside of the box! The "Canvas.drawText" thing uses the y-parameter as the baseline for the text. 

Please try using a different value, say "canvas.drawText("Text.", 50, 16, textPaint);"

Edited by KoMaXX
1

Share this post


Link to post
Share on other sites
Finally have time to get back to you.

No text.

But when I revert your suggestion and just add a Boolean value, true, to the method, view.getDrawingCache() as a parameter, I get text showing up.

Text has been cut from the middle, but there is Text!

I don't know why the same piece of code returns a black rectangle (void of any colors) on my Asus Transformer Pad though. It is like somebug in the TextView that were not compatible with something.

Attached are two photos of my Android phone and my tablet. Both are running the same Android app.
HTC Desire S:
[attachment=20083:2014-02-20_20-45-44.png]

Asus Transformer Pad TF701T:
[attachment=20084:Screenshot_2014-02-20-20-27-38.png]
0

Share this post


Link to post
Share on other sites

Okay, for the Asus Transformer Pad, I have finally found the cause:

 

Stack Overflow Q&A tells me that because of the generated bitmap, on some devices, the bitmap size is a power of two, therefore the texture is drawn to the screen. On other devices, such as the tablet, due to its sheer resolution scale, the generated bitmap is not a power of two, therefore it shows up as nothing.

 

In order to combat this issue, I need to use GL_CLAMP_TO_EDGE or use a source texture from the /res/drawable-noapi folder. The latter prevents Android from density-based scaling from occurring.

 

Thank you for your time.

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0