0

概述

我在渲染立方体侧面的纹理时遇到问题。我已经成功地在立方体的顶部和底部渲染了纹理,但无法在侧面渲染。

是)我有的

我有一个充满 48 个元素的纹理缓冲区(每个面 4*2 个元素,6 个面是 48 个),它们充满了良好的坐标。

立方体形状正在绘制,但侧面没有被渲染。 立方体的屏幕截图

我正在绘制的图像只是一个带有数字 1-9 的图像,正如您从立方体顶部看到的那样。textureBuffer 是一遍又一遍的相同模式......

        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;

它只是加载纹理缓冲区来渲染完整的纹理。

可能的问题##

似乎只有前 16 个纹理坐标被绘制和使用,因为只有矩形的顶部和底部表面被纹理化。我已经调试过了,当我填充 TextureBuffer 时,大小是 48。

渲染代码

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

创建变量 textureBuffer

进来的参数变量texture包含48个元素

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

vertexBuffer 已正确设置并使用索引缓冲区来渲染立方体。你知道为什么立方体的侧面没有被渲染吗?

新的!!

所以我尝试手动创建一个形状,但我遇到了与纹理缓冲区相同的情况。我可以渲染两张脸,但不能渲染第三张脸!似乎任何超过 8 个纹理顶点的东西都不起作用。

这张照片显示了我的新形状。注意水平延伸。无论我对这些纹理坐标做什么,该纹理都不会改变。那也是我随机对象的第三张脸。

在此处输入图像描述

4

1 回答 1

0

我还没有让索引缓冲区和纹理一起工作。我几乎什么都试过了!所以相反(不幸的是)我正在构建很多三角形。为了解析 blender 生成的 .obj 文件,我编写了这个函数,它为我创建了顶点缓冲区和纹理缓冲区。

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

因此,如果您在解析 .obj 文件时遇到问题,请随时将其用作参考或指南!Blender 为您提供具有 4 个顶点的面中的所有内容,这会将所有内容变成 3 个并为每个面构造 2 个三角形。

于 2012-12-18T15:24:16.723 回答