java - 安卓OpenGL ES 2.0 : Text aren't showing up

标签 java android text textures opengl-es-2.0

我已经改编了 Draw Text in OpenGL ES Android 中的代码进入我的应用程序,但文本并未真正显示。

这是目前的样子:

Green background without any text.

绿色矩形框实际上是一个可绘制位图。

Drawable file in Android assets folder.

我期待某种文本,背景顶部可以是白色或黑色,但什么也没有显示。

这是Text类的代码。

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);
    }
}

有人可以告诉我哪里做错了吗?我应该怎么做才能解决这个问题?提前致谢。

<小时/>

更新1: 回答deathember:

这就是我将 Matrix.transformM() 更改为您建议的内容时发生的情况:

What happened afterwards...

这是显示我将 View 矩阵放置在何处的代码:

package gl.es;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.*;

public class MyRenderer implements GLSurfaceView.Renderer
{

    Context context;
    Text text;

    float eyeX;
    float eyeY;
    float eyeZ;


    private float[] modelMatrix;
    private float[] viewMatrix;
    private float[] projectionMatrix;
    private float[] modelViewProjectionMatrix;

    public MyRenderer(Context context) {
        this.context = context;
        modelMatrix = new float[16];
        projectionMatrix = new float[16];
        viewMatrix = new float[16];
        modelViewProjectionMatrix = new float[16];
    }

    @Override
    public void onSurfaceCreated(GL10 p1, EGLConfig config)
    {
        // TODO: Implement this method
        GLES20.glClearColor(0.4f, 0.8f, 1.0f, 1.0f);
        this.text = new Text(this.context);
    }

    @Override
    public void onSurfaceChanged(GL10 p1, int width, int height)
    {
        // TODO: Implement this method
        GLES20.glViewport(0, 0, width, height);
        this.setPerspectiveM(45f, (float) (width) / (float) (height), 1f, 200f);
        // Did not use view matrix at all for transformations of Text textures.
        Matrix.setLookAtM(viewMatrix, 0, 0, 0, 0, 0, 0, 0, 0, 1f, 0); //Unused.

        Matrix.setIdentityM(modelMatrix, 0);
        projectionMatrix, 0, modelMatrix, 0);
    }

    @Override
    public void onDrawFrame(GL10 p1)
    {

        // TODO: Implement this method
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

        text.transform(modelMatrix, projectionMatrix, modelViewProjectionMatrix);
        text.useProgram();
        text.setUniforms(modelViewProjectionMatrix);
        text.setVertexPointers();
        text.draw();
    }

    private void setPerspectiveM(float fov, float aspectRatio, float near, float far) {
        final float anglesInRadians = (float) (fov * Math.PI / 180f);
        final float angle = (float) (1f / Math.atan(anglesInRadians) / 2f);
        projectionMatrix[0] = angle / aspectRatio;
        projectionMatrix[1] = 0f;
        projectionMatrix[2] = 0f;
        projectionMatrix[3] = 0f;
        projectionMatrix[4] = 0f;
        projectionMatrix[5] = angle;
        projectionMatrix[6] = 0f;
        projectionMatrix[7] = 0f;
        projectionMatrix[8] = 0f;
        projectionMatrix[9] = 0f;
        projectionMatrix[10] = -((far + near) / (far - near));
        projectionMatrix[11] = -1f;
        projectionMatrix[12] = 0f;
        projectionMatrix[13] = 0f;
        projectionMatrix[14] = -((2f * far * near) / (far - near));
        projectionMatrix[15] = 0f;
    }
}

希望有帮助。

最佳答案

尝试改变

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 transform(float[] model, float[] projection, float[] mvp) {
    Matrix.setIdentityM(model, 0);
    Matrix.translateM(model, 0, 0f, 0f, 0f);
    Matrix.multiplyMM(mvp, 0, projection, 0, model, 0);

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

并将您的 View 矩阵设置为中心。也许你画了你的文字,但你看不到他。你在哪里设置 View 矩阵?

关于java - 安卓OpenGL ES 2.0 : Text aren't showing up,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21670931/

相关文章:

java - 在java xquery处理器中使用OSB功能bea

java - java中的多线程服务器..服务文件的线程

java - BlackBerry 数学实用程序(Pow & Round)

java - 如何使用 HTML 标签为 strings.xml 文件中的字符串着色?

java - 无法解析符号 Activity android studio

Css 文本定位

android:微调器的每个项目都有不同的颜色

java - 如何使用来自另一个内部类方法的内部类实例?

Android M - GoogleAccountCredential setSelectedAccount 不起作用 - 名称不能为空

android - 按钮文本在android中自动更改为大写字母