广场上没有显示任何内容绘制文本的纹理 [英] Drawing text as textures on squares does not show anything

查看:88
本文介绍了广场上没有显示任何内容绘制文本的纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的OpenGL和我正在开发针对Android的增强现实应用程序。

I'm new to OpenGL and I'm developing an Augmented-reality application for Android.

到现在为止,我在画白格子,垂直于相机,指向用户,其中一个兴趣点将是方向。

Until now, I was drawing white squares, perpendicular to the camera, pointing the user to the direction where a "Point of interest" would be.

现在,我想展示一些文本的平方。

Now, I'm trying to show some text into the squares.

我已经读了很多,似乎与文字创造一个质感是最直接和最简单的方法,所以我只要我得到的兴趣点数据,并将其坚持创建自己的贴图广场。对于这一点,我使用位图。

I've read a lot and it seems that creating a texture with the text is the most direct and easiest approach, so I'm creating the textures as soon as I get data of the Points of interest and stick them to their squares. For that, I use bitmaps.

让我们来看看一些code。在我的onDrawFrame的方法,我做这样的事情:

Let's see some code. Within my onDrawFrame method, I do something like this:

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是:

Where loadGLTextures is:

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

有基本上创建兴趣的每个点位图,并且产生与它的质地。纹理将在白色正方形以后被应用,因为它是在这个类中所示:

It basically creates a bitmap for each Point of Interest, and generate a texture with it. The texture will be applied over a white square later, as it is shown in this class:

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

        ....
        ....
}

我试图映射贴图,因为它是教<一个href=\"http://obviam.net/index.php/texture-mapping-opengl-android-displaying-images-using-opengl-and-squares/\"相对=nofollow>这里和的这里没有成功的。我真的不知道该怎么做才能得到的平方得出一些字母和我真的在这里失去了...也许文本正在绘制在广场的另一面,也许是纹理不会生成...我不知道。

I have tried to map the texture as it is taught here and here with no success. I really don't know what to do to get some letters drawn on the squares and am really lost here... Maybe the text is being drawn on the other face of the square, maybe the textures are not being generated... I don't know.

任何形式的帮助将是非常美联社preciated。

Any kind of help would be very appreciated.

推荐答案

好吧,我已经忘了,使纹理贴图。你可以做到这一点,它使用GL10对象的任何方法中。我preFER到我的对象的绘制方法中做到这一点,因此,任何其他对象不受纹理。它是如此简单(只是换2号线时,那些说NEW !!):

Okay, I had forgotten to enable texture mapping. You can do that within any method that uses the GL10 object. I prefer to do it within the draw method of my objects, so any other object is not affected by the texture. It's as simple as this (just changed 2 lines, the ones that say 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
}

这篇关于广场上没有显示任何内容绘制文本的纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆