Рисование текста в виде текстуры на квадратах ничего не показывает

Я новичок в OpenGL и разрабатываю приложение дополненной реальности для Android.

До сих пор я рисовал белые квадраты перпендикулярно камере, указывая пользователю направление, в котором будет находиться «точка интереса».

Теперь я пытаюсь отобразить какой-нибудь текст в квадратах.

Я много читал, и мне кажется, что создание текстуры с текстом - самый простой и прямой подход, поэтому я создаю текстуры, как только получаю данные о точках интереса и прикрепляю их к их квадратам. Для этого я использую растровые изображения.

Посмотрим код. В моем методе onDrawFrame я делаю что-то вроде этого:

public void onDrawFrame(GL10 gl) {

        // Get sensors matrix
                ...


        //Clear Screen And Depth Buffer
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        // Load remapped matrix
        gl.glMatrixMode(GL10.GL_MODELVIEW);



        // List of Points of interest (modified when some data is downloaded)
        synchronized (poiList) {

            if(mNewData){  // True if new data has been dowloaded)
                if(textures != null)  // Delete old textures
                    gl.glDeleteTextures(textures.length, textures, 0);

                textures = loadGLTexture(gl, soapPoiList.getPoiList());
                mNewData = false;
            }

            int i = 0;
            // Iterate the list
            for (PointOfInterest c : poiList) {

                         gl.glLoadIdentity();
                 gl.glLoadMatrixf(remappedRotationMatrix, 0);

                 // Get bearing
                             ...

                 // Place polygon in the right place
                 gl.glRotatef(-bearing, 0, 0, 1);
                 gl.glTranslatef(0, ICONS_SIZE_RATIO, 0);

                         // Actually draws the polygon with text
                 c.draw(gl, textures[i]);

                 i++;
            }
        }
    }

Где loadGLTextures:

protected int[] loadGLTexture(GL10 gl, List<PointOfInterest> l){
    int res[] = new int[l.size()];
    gl.glGenTextures(res.length, res, 0);
    int i = 0;

    for(PointOfInterest p : l) {
        Bitmap bitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.RGB_565);
        bitmap.eraseColor(Color.BLACK);

        Canvas canvas = new Canvas(bitmap);

        Paint textPaint = new Paint();
        textPaint.setTextSize(35);
        textPaint.setFakeBoldText(true);
        textPaint.setAntiAlias(true);
        textPaint.setARGB(255, 255, 255, 255);
        // Draw the text centered
        canvas.drawText(Float.toString(p.getDinstanceToOrigin()) + " m.", 10,35, textPaint);

        // Bind the texture to our array
        gl.glBindTexture(GL10.GL_TEXTURE_2D, res[i]);

        //  Create Nearest Filtered Texture
        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, bitmap, 0);

        bitmap.recycle();

        i++;
    }
    return res;
}

По сути, он создает растровое изображение для каждой точки интереса и генерирует на его основе текстуру. Текстура будет применена к белому квадрату позже, как показано в этом классе:

    public class PointOfInterest {

        // MEMBERS ----------------------------------------------------------------
            ....
            ....

        // OpenGL necessary variables
        /** The buffer holding the vertices */
        private FloatBuffer vertexBuffer;

        /** The initial vertex definition */
        private float vertices[] = { 
                                    -1.0f, 0.0f, -1.0f, //Bottom Left   V1
                                    -1.0f, 0.0f,  1.0f, //Top Left      V2
                                     1.0f, 0.0f, -1.0f, //Bottom Right  V3
                                     1.0f, 0.0f,  1.0f, //Top Right     V4
                                   };

        private FloatBuffer textureBuffer;
        private float texture[] = {
                                    0.0f, 0.0f,     // V1
                                    1.0f, 0.0f,     // V3
                                    0.0f, 1.0f,     // V2
                                    1.0f, 1.0f      // V4
        };

        // CONSTRUCTORS -----------------------------------------------------------

        public PointOfInterest(Location originLocation){
            currentLocation = originLocation;

            mPoiLocation = new Location(LocationManager.GPS_PROVIDER);

            ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
            byteBuf.order(ByteOrder.nativeOrder());

            vertexBuffer = byteBuf.asFloatBuffer();
            vertexBuffer.put(vertices);
            vertexBuffer.position(0);

            byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
            byteBuf.order(ByteOrder.nativeOrder());

            textureBuffer = byteBuf.asFloatBuffer();
            textureBuffer.put(texture);
            textureBuffer.position(0);
        }

        // PUBLIC METHODS ---------------------------------------------------------

        public void draw(GL10 gl, int textureId){
            // Bind the previously generated texture
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);

            // Point to our buffers
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

            // set the colour for the square
            //gl.glColor4f(0.0f, 0.1f, 0.0f, 0.5f);

            //Set the face rotation
            gl.glFrontFace(GL10.GL_CW);

            //Point to our vertex buffer
            gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
            gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

            //Draw the vertices as triangle strip
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

            //Disable the client state before leaving
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        }

        ....
        ....
}

Я попытался отобразить текстуру, как ее учат здесь и здесь безуспешно. Я действительно не знаю, что делать, чтобы нарисовать какие-то буквы на квадратах, и я действительно потерялся здесь ... Может быть, текст рисуется на другой стороне квадрата, может быть, текстуры не генерируются ... Я не знаю.

Любая помощь будет очень признательна.


person Pedriyoo    schedule 14.09.2011    source источник
comment
Вы откуда-то скопировали код? Вы говорите, что новичок в openGL. Если вы его скопировали, то, вероятно, вы что-то упустили в процессе.   -  person Ronnie    schedule 14.09.2011
comment
Я сделал очень простой пример, который работает. Просто статический квадрат с текстурой на нем. Теперь я адаптирую этот пример к своему приложению. Я сотни раз проверял, не упустил ли что-то, но ничего не вижу   -  person Pedriyoo    schedule 15.09.2011


Ответы (1)


Хорошо, я забыл включить наложение текстуры. Вы можете сделать это в любом методе, использующем объект GL10. Я предпочитаю делать это с помощью метода рисования моих объектов, чтобы текстура не влияла на любой другой объект. Это так просто (просто изменили 2 строки, те, которые говорят NEW !!):

public void draw(GL10 gl, int textureId){
    gl.glEnable(GL10.GL_TEXTURE_2D);    //NEW !!! Enable Texture Mapping

    // Bind the previously generated texture
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);

    // Point to our buffers
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    // set the colour for the square
    //gl.glColor4f(0.0f, 0.1f, 0.0f, 0.5f);

    //Set the face rotation
    gl.glFrontFace(GL10.GL_CW);

    //Point to our vertex buffer
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

    //Draw the vertices as triangle strip
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

    //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    gl.glDisable(GL10.GL_TEXTURE_2D);   ////NEW !!! Disable texture mapping
}
person Pedriyoo    schedule 16.09.2011