问题描述:

Overview

I am having trouble rendering the texture on the sides of my cube. I have successfully rendered textures on the top and bottom of my cube, but am unable to render on the sides.

What I have

I have a texture buffer full of 48 elements, (4*2 elements per face and 6 faces is 48) and they are full will good coordinates.

The cube shape is drawing is drawing, but the sides are not being rendered.

The image I am drawing is simply a image with the numbers 1-9 as you can see from the top of the cube. The textureBuffer is the same patter over and over again...

 texture[0] = 0;

texture[1] = 0;

texture[2] = 1;

texture[3] = 0;

texture[4] = 1;

texture[5] = 1;

texture[6] = 0;

texture[7] = 1;

texture[8] = 0;

texture[9] = 0;

texture[10] = 1;

texture[11] = 0;

texture[12] = 1;

texture[13] = 1;

texture[14] = 0;

texture[15] = 1;

texture[16] = 0f;

texture[17] = 0f;

texture[18] = 1f;

texture[19] = 0f;

texture[20] = 1f;

texture[21] = 1f;

texture[22] = 0f;

texture[23] = 1f;

which simply loads the texture Buffer to render the full texture.

Possible Problem##

It appears that only the first 16 texture coordinates are being drawn and used because only the top and bottom surface of the rectangle are being textured. I've debugged it and when I populate the TextureBuffer the size is 48 though.

Render Code

@Override

public void draw(GL10 gl)

{

super.draw(gl);

//gl.glColor4f(255, 0, 0, 150);

gl.glEnable(GL10.GL_TEXTURE_2D);

gl.glEnable(GL10.GL_ALPHA_TEST);

gl.glAlphaFunc(GL10.GL_GREATER, 0.0f);

gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

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

gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID);

gl.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE_MINUS_SRC_ALPHA);

gl.glEnable(GL10.GL_BLEND);

gl.glFrontFace(GL10.GL_CCW);

gl.glEnable(GL10.GL_CULL_FACE);

gl.glCullFace(GL10.GL_BACK);

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

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

gl.glDrawElements(GL10.GL_TRIANGLES,indexBuffer.capacity(),GL10.GL_UNSIGNED_SHORT,indexBuffer);

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

gl.glDisable(GL10.GL_CULL_FACE);

gl.glDisable(GL10.GL_ALPHA_TEST);

gl.glDisable(GL10.GL_TEXTURE_2D);

gl.glColor4f(255, 255, 255, 255);

}

Creating the variable textureBuffer

The parameter variable texture that comes in contains 48 elements

public void constructTextureBuffer(float[] texture)

{

ByteBuffer vbb = ByteBuffer.allocateDirect(texture.length*4);

vbb.order(ByteOrder.nativeOrder());

textureBuffer = vbb.asFloatBuffer();

textureBuffer.put(texture);

textureBuffer.position(0);

}

The vertexBuffer is correctly setup and using the index buffer to render a cube. Do you know why the sides of the cube are not being rendered?

NEW!!

So I tried creating a shape by hand and I am running into the same situation with the texture buffer. I can render two faces but not the third! It appears anything past 8 texture verticies do not work.

This picture shows my new shape. Notice the horizontal extension. No matter what I do to those texture coordinates, that texture does not change. That is also the third face of my random object.

网友答案:

I have yet to get index buffers and textures to work together. I've tried almost everything! So instead (and unfortunately) I am building lots of triangles. In order to parse the .obj file generated by blender, I wrote this function which creates the vertex buffer and texture buffer for me.

public static Mesh createMesh(int resourceID)
    {
        Mesh m = new Mesh();

        Scanner s = null;

        BufferedReader inputStream = null;

        ArrayList<Float> readInVerticies        = new ArrayList<Float>();
        ArrayList<Float> readInTextures         = new ArrayList<Float>();
        ArrayList<Short> readInVertexIndicies   = new ArrayList<Short>();
        ArrayList<Short> readInTextureIndicies  = new ArrayList<Short>();

        int numberFaces = 0;

        try
        {
            inputStream = new BufferedReader(new InputStreamReader(context.getResources().openRawResource(resourceID)));
            s = new Scanner(inputStream);
            String line = null;

            /*
             * Read the header part of the file
             */
            line = inputStream.readLine();
            line = inputStream.readLine();
            line = inputStream.readLine();
            line = inputStream.readLine();
            line = inputStream.readLine();

            while(line.charAt(0) == 'v' && line.charAt(1) != 't')
            {
                s = new Scanner(line);
                s.next();
                readInVerticies.add(s.nextFloat());
                readInVerticies.add(s.nextFloat());
                readInVerticies.add(s.nextFloat());
                line = inputStream.readLine();
            }
            while(line.charAt(0)=='v' && line.charAt(1)=='t')
            {
                s = new Scanner(line);
                s.next(); //read in "vt"
                readInTextures.add(s.nextFloat());
                readInTextures.add(s.nextFloat());
                line = inputStream.readLine();
            }
            line = inputStream.readLine();
            line = inputStream.readLine();

            while(line != null && line.charAt(0) == 'f')
            {
                s = new Scanner(line);
                s.useDelimiter("[ /\n]");
                String buffer = s.next();
                short vi1,vi2,vi3,vi4;
                short ti1,ti2,ti3,ti4;

                vi1 = s.nextShort();
                ti1 = s.nextShort();
                vi2 = s.nextShort();
                ti2 = s.nextShort();
                vi3 = s.nextShort();
                ti3 = s.nextShort();
                vi4 = s.nextShort();
                ti4 = s.nextShort();

                readInVertexIndicies.add(vi1);
                readInVertexIndicies.add(vi2);
                readInVertexIndicies.add(vi3);
                readInVertexIndicies.add(vi4);

                readInTextureIndicies.add(ti1);
                readInTextureIndicies.add(ti2);
                readInTextureIndicies.add(ti3);
                readInTextureIndicies.add(ti4);

                numberFaces = numberFaces + 1;
                line = inputStream.readLine();      
            }

            /*
             * constructing our verticies. Use the number of faces * 6 because
             * there are 2 triangles per face and 3 verticies on a triangle but there are
             * also 3 coordinates per vertex.
             * 
             * For the texture, the same number but there are only 2 coordinates per texture
             */
            float verticies[] = new float[numberFaces * 6 * 3];
            float textures[]  = new float[numberFaces * 6 * 2];
            for(int i=0;i<numberFaces;i++)
            {
                verticies[i*18+0]       =   readInVerticies.get((readInVertexIndicies.get(i*4+0)-1)*3+0);
                verticies[i*18+1]       =   readInVerticies.get((readInVertexIndicies.get(i*4+0)-1)*3+1);
                verticies[i*18+2]       =   readInVerticies.get((readInVertexIndicies.get(i*4+0)-1)*3+2);

                textures[i*12+0]        =   readInTextures.get((readInTextureIndicies.get(i*4+0)-1)*2+0);
                textures[i*12+1]        =   readInTextures.get((readInTextureIndicies.get(i*4+0)-1)*2+1);

                verticies[i*18+3]       =   readInVerticies.get((readInVertexIndicies.get(i*4+1)-1)*3+0);
                verticies[i*18+4]       =   readInVerticies.get((readInVertexIndicies.get(i*4+1)-1)*3+1);
                verticies[i*18+5]       =   readInVerticies.get((readInVertexIndicies.get(i*4+1)-1)*3+2);

                textures[i*12+2]        =   readInTextures.get((readInTextureIndicies.get(i*4+1)-1)*2+0);
                textures[i*12+3]        =   readInTextures.get((readInTextureIndicies.get(i*4+1)-1)*2+1);

                verticies[i*18+6]       =   readInVerticies.get((readInVertexIndicies.get(i*4+2)-1)*3+0);
                verticies[i*18+7]       =   readInVerticies.get((readInVertexIndicies.get(i*4+2)-1)*3+1);
                verticies[i*18+8]       =   readInVerticies.get((readInVertexIndicies.get(i*4+2)-1)*3+2);

                textures[i*12+4]        =   readInTextures.get((readInTextureIndicies.get(i*4+2)-1)*2+0);
                textures[i*12+5]        =   readInTextures.get((readInTextureIndicies.get(i*4+2)-1)*2+1);

                verticies[i*18+9]       =   readInVerticies.get((readInVertexIndicies.get(i*4+0)-1)*3+0);
                verticies[i*18+10]      =   readInVerticies.get((readInVertexIndicies.get(i*4+0)-1)*3+1);
                verticies[i*18+11]      =   readInVerticies.get((readInVertexIndicies.get(i*4+0)-1)*3+2);

                textures[i*12+6]        =   readInTextures.get((readInTextureIndicies.get(i*4+0)-1)*2+0);
                textures[i*12+7]        =   readInTextures.get((readInTextureIndicies.get(i*4+0)-1)*2+1);

                verticies[i*18+12]      =   readInVerticies.get((readInVertexIndicies.get(i*4+2)-1)*3+0);
                verticies[i*18+13]      =   readInVerticies.get((readInVertexIndicies.get(i*4+2)-1)*3+1);
                verticies[i*18+14]      =   readInVerticies.get((readInVertexIndicies.get(i*4+2)-1)*3+2);

                textures[i*12+8]        =   readInTextures.get((readInTextureIndicies.get(i*4+2)-1)*2+0);
                textures[i*12+9]        =   readInTextures.get((readInTextureIndicies.get(i*4+2)-1)*2+1);

                verticies[i*18+15]      =   readInVerticies.get((readInVertexIndicies.get(i*4+3)-1)*3+0);
                verticies[i*18+16]      =   readInVerticies.get((readInVertexIndicies.get(i*4+3)-1)*3+1);
                verticies[i*18+17]      =   readInVerticies.get((readInVertexIndicies.get(i*4+3)-1)*3+2);

                textures[i*12+10]       =   readInTextures.get((readInTextureIndicies.get(i*4+3)-1)*2+0);
                textures[i*12+11]       =   readInTextures.get((readInTextureIndicies.get(i*4+3)-1)*2+1);
            }

            m.constructVertexBuffer(verticies);
            m.constructTextureBuffer(textures);


        } 
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }

        return m;
    }

So if you ever have trouble parsing a .obj file, feel free to use this as a reference or guide! Blender gives you everything in faces with 4 vertices, and this turns everything into 3s and constructs 2 triangles per face.

相关阅读:
Top