Auf Thema antworten

Hey! Danke für eure antworten


Was soll ich Sagen ChatGPT habe ich nebenbei am laufen. Ein Super Helferlei!

Aber an dieser stelle bekomme ich nichts mehr vernüftiges raus.


Die Idee ein Bitmap zu erstellen und anschließend mit Canvas die Schrift zu erzeugen stammt aus dem Chat.


Rein Logisch sollte meine Lösung funktionieren. Das Canvas Objekt erzeuge ich jetzt auch nur einmalig.


ich hab bestimmt irgend wo noch eine Zeile drin die den GL ärgert.


[CODE=java]

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.PorterDuff;

import android.graphics.Rect;

import android.opengl.GLUtils;

import javax.microedition.khronos.opengles.GL10;

import java.nio.ByteBuffer;

import java.nio.ByteOrder;

import java.nio.FloatBuffer;


public class OpenGLTextRenderer {

    private static final int TEXTURE_WIDTH = 80;

    private static final int TEXTURE_HEIGHT = 80;


    private Bitmap textBitmap;

    private int[] textures = new int[1];


    private Canvas mCanvas;


    public OpenGLTextRenderer() {

       textBitmap = Bitmap.createBitmap(TEXTURE_WIDTH, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);

        mCanvas = new Canvas(textBitmap);


    }


    public void loadTexture(GL10 gl) {

        gl.glGenTextures(1, textures, 0);

        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);


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


   //     GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, textBitmap, 0);

    }


 


    public void renderText(GL10 gl, String text, float x, float y, int textSize) {

        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, textBitmap, 0);

        if (textBitmap == null || textBitmap.isRecycled()) {

            textBitmap = Bitmap.createBitmap(TEXTURE_WIDTH, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);

            mCanvas = new Canvas(textBitmap);

        } else {

            textBitmap.eraseColor(Color.TRANSPARENT);

            mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);


        }


    //    Canvas canvas = new Canvas(textBitmap); wir behalten das Canvas objekt




        Paint paint = new Paint();

        paint.setColor(Color.WHITE);

        paint.setStyle(Paint.Style.FILL);

        mCanvas.drawPaint(paint);


        paint.setColor(Color.BLACK);

        paint.setTextSize(textSize * 10);


        Rect bounds = new Rect();

        paint.getTextBounds(text, 0, text.length(), bounds);

        int x1 = (textBitmap.getWidth() - bounds.width()) / 2;

        int y1 = (textBitmap.getHeight() + bounds.height()) / 2;


        mCanvas.drawText(text, x1, y1, paint);

        GLUtils.texSubImage2D(GL10.GL_TEXTURE_2D, 0, 0, 0, textBitmap);


        float[] vertices = {

                x, y, 0.0f,

                x + textBitmap.getWidth(), y, 0.0f,

                x, y + textBitmap.getHeight(), 0.0f,

                x + textBitmap.getWidth(), y + textBitmap.getHeight(), 0.0f

        };

      // 90 Grad

      //  float[] textureCoordinates = {

      //

      //          0.0f, 0.0f,

      //          0.0f, 1.0f,

      //          1.0f, 0.0f,

      //          1.0f, 1.0f

      //  };

        //0 Grad

        float[] textureCoordinates = {


                0.0f, 1.0f,

                1.0f, 1.0f,

                0.0f, 0.0f,

                1.0f, 0.0f

        };


        ByteBuffer vertexBuffer = ByteBuffer.allocateDirect(vertices.length * 4);

        vertexBuffer.order(ByteOrder.nativeOrder());

        FloatBuffer vertexFloatBuffer = vertexBuffer.asFloatBuffer();

        vertexFloatBuffer.put(vertices);

        vertexFloatBuffer.position(0);


        ByteBuffer textureBuffer = ByteBuffer.allocateDirect(textureCoordinates.length * 4);

        textureBuffer.order(ByteOrder.nativeOrder());

        FloatBuffer textureFloatBuffer = textureBuffer.asFloatBuffer();

        textureFloatBuffer.put(textureCoordinates);

        textureFloatBuffer.position(0);


        gl.glEnable(GL10.GL_TEXTURE_2D);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        gl.glColor4f(1f, 1f, 1f, 1f);


        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexFloatBuffer);

        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureFloatBuffer);

        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);


        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        gl.glDisable(GL10.GL_TEXTURE_2D);

      //  textBitmap.recycle();

    }



}


[/CODE]



Oben