Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将UV纹理从blender加载到opengl android java_Java_Android_Opengl Es_Textures_Blender - Fatal编程技术网

如何将UV纹理从blender加载到opengl android java

如何将UV纹理从blender加载到opengl android java,java,android,opengl-es,textures,blender,Java,Android,Opengl Es,Textures,Blender,嗯 大家好 我尝试加载我在搅拌机中制作的UV纹理。在搅拌机里就可以了 图加载正常,物料加载正常。但是纹理的问题。。我不知道怎么了,因为我是3d纹理的新手。(opengl总是只使用2d四边形。我的意思是只使用2d方向) 这里是我的简单立方体的obj文件 # Blender v2.72 (sub 0) OBJ File: '' # www.blender.org mtllib ncube.mtl o Cube v 1.000000 -1.000000 -1.00

大家好 我尝试加载我在搅拌机中制作的UV纹理。在搅拌机里就可以了 图加载正常,物料加载正常。但是纹理的问题。。我不知道怎么了,因为我是3d纹理的新手。(opengl总是只使用2d四边形。我的意思是只使用2d方向)

这里是我的简单立方体的obj文件

   # Blender v2.72 (sub 0) OBJ File: ''
    # www.blender.org
    mtllib ncube.mtl
    o Cube
    v 1.000000 -1.000000 -1.000000
    v 1.000000 -1.000000 1.000000
    v -1.000000 -1.000000 1.000000
    v -1.000000 -1.000000 -1.000000
    v 1.000000 1.000000 -0.999999
    v 0.999999 1.000000 1.000001
    v -1.000000 1.000000 1.000000
    v -1.000000 1.000000 -1.000000
    vt 0.000100 0.250000
    vt 0.250000 0.250000
    vt 0.250000 0.499900
    vt 0.499900 0.499900
    vt 0.499900 0.250000
    vt 0.749800 0.250000
    vt 0.250000 0.750000
    vt 0.250000 0.999900
    vt 0.000100 0.999900
    vt 0.250000 0.000100
    vt 0.499900 0.000100
    vt 0.250000 0.749800
    vt 0.000100 0.499900
    vt 0.749800 0.499900
    vt 0.000100 0.750000
    vt 0.499900 0.749800
    usemtl Material
    s off
    f 2/1 3/2 4/3
    f 8/4 7/5 6/6
    f 1/7 5/8 6/9
    f 2/10 6/11 7/5
    f 7/5 8/4 4/3
    f 1/12 4/3 8/4
    f 1/13 2/1 4/3
    f 5/14 8/4 6/6
    f 2/15 1/7 6/9
    f 3/2 2/10 7/5
    f 3/2 7/5 4/3
    f 5/16 1/12 8/4
对于opengl,我以错误的方式加载了fix cos面,所以在解析时fix是f=f-1。如果不负,我就得不到正确的立方体。 如果立方体没有纹理而不是面看起来:F2 3 4;所以在修复之后,它看起来是正确的:f1 2 3

但是当我导出带有纹理的obj文件时,我不知道还有什么需要其他面。这是第一次

这是我的班上的装货图

public class TestUV {
    private FloatBuffer mVertexBuffer;
    public FloatBuffer mColorBuffer;
    public FloatBuffer mColorBufferKs;
    public FloatBuffer textureBuffer;
    private ShortBuffer  mIndexBuffer;
     private short indicesF[];
     private float vertices[];
    private float textureCoords[];
   private float matcolors[];
    private float matcolorsKs[];
     OBJLoader loader;
     public int tempo[];
     public float tempFloat[];

    public TestUV(Context context, String filename) {
            loader = new OBJLoader(context);
            loader.loadVertAndInd(filename);
            tempFloat=loader.indices;


            short indices[] = new short[tempFloat.length];
                for (int i = 0; i < tempFloat.length; i++) {
                        indices[i] = (short)tempFloat[i];
                }

            indicesF=indices;
            vertices=loader.vertices;

            matcolors=loader.materialsKd;
            matcolorsKs=loader.materialsKs;
            textureCoords=loader.Vtextures;
            String temp = ""+matcolors.length;
             String temp1 = ""+matcolorsKs.length;
            Log.e("","LENGTH OF MATCOmatcolors.length "+temp+"   KS = "+temp1);
            ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
            byteBuf.order(ByteOrder.nativeOrder());
            mVertexBuffer = byteBuf.asFloatBuffer();
            mVertexBuffer.put(vertices);
            mVertexBuffer.position(0);

        byteBuf = ByteBuffer.allocateDirect(matcolors.length * 4);
        byteBuf.order(ByteOrder.nativeOrder());
        mColorBuffer = byteBuf.asFloatBuffer();
        mColorBuffer.put(matcolors);
        mColorBuffer.position(0);

        byteBuf = ByteBuffer.allocateDirect(matcolorsKs.length * 4);
        byteBuf.order(ByteOrder.nativeOrder());
        mColorBufferKs = byteBuf.asFloatBuffer();
        mColorBufferKs.put(matcolorsKs);
        mColorBufferKs.position(0);

            ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length*4);
            ibb.order(ByteOrder.nativeOrder());
            mIndexBuffer = ibb.asShortBuffer();
            mIndexBuffer.put(indices);
            mIndexBuffer.position(0);

        byteBuf = ByteBuffer.allocateDirect(textureCoords.length * 4);
        byteBuf.order(ByteOrder.nativeOrder());
        textureBuffer = byteBuf.asFloatBuffer();
        textureBuffer.put(textureCoords);
        textureBuffer.position(0);
    }
    public float xPos=0;
    public float yPos=0;
    public float zPos=0;
    public void setX(float x){xPos=x;}
    public void setY(float y){yPos=y;}
    public void setXYZ(float  x,float y,float z)
    {
        xPos=x;
        yPos=y;
        zPos=z;
    }
// draw our object
    /** WORK WITH LIGHT **/
    //**********************************************************/
    //float[] pos = {0.0f, 0.0f, -3.0f, 1.0f}; //2
    float lightX = 0;
    float lightY =0;
    float[] red={1.0f, 0.0f, 1.0f, 1.0f};
    float[] lightpos = {0,0,-1,1};
    float[] lightpos1 = {0,0,0,0};
    float[] lightdir ={-0.5f,-0.6f,-0.7f};
    float[] lightdir1 ={0.5f,0.6f,0.7f};
    public final static int SS_SUNLIGHT = GL10.GL_LIGHT0;
    public final static int SS_SUNLIGHT1 = GL10.GL_LIGHT1;
    public final static int SS_SUNLIGHT2 = GL10.GL_LIGHT2;
    public FloatBuffer makeFloatBuffer(float p[]){
        ByteBuffer vbb = ByteBuffer.allocateDirect(p.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        FloatBuffer fb = vbb.asFloatBuffer();  // create a floating point buffer from the ByteBuffer
        fb.put(p);    // add the coordinates to the FloatBuffer
        fb.position(0);
        return fb;
    }

    public void draw(GL10 gl,float xR,float yR) {

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();


           gl.glFrontFace(GL10.GL_CW);

           gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
            gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);

           gl.glTranslatef(xPos, yPos, zPos);
           gl.glRotatef(xR, 0, 1, 0);  //rotate around y-axis

           gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, mColorBufferKs);

           gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_SPECULAR, mColorBufferKs);

         gl.glEnable(GL10.GL_LIGHTING);

           gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
                GL10.GL_NEAREST);
           gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
                GL10.GL_NEAREST);
           gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); //enable texture
            //ENABLES
           gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
           gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
           gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);



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

           gl.glDrawElements(GL10.GL_TRIANGLES, indicesF.length, GL10.GL_UNSIGNED_SHORT,mIndexBuffer);
        //ENABLE TEXTURE!!!!!
             gl.glEnable(GL10.GL_TEXTURE_2D);
        //DISABLES
             gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
           gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
          gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
   }



    private int[] textures = new int[1];
    //////=================================================================
    public void loadTexture(GL10 gl, Context context,int count) {
        // loading texture
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
                count);

        // generate one texture pointer
        gl.glGenTextures(1, textures, 0);
        // ...and bind it to our array
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        // create nearest filtered texture
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

        // Use Android GLUtils to specify a two-dimensional texture image from our bitmap
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

        // Clean up
        bitmap.recycle();
    }

}
公共类TestUV{
私有FloatBuffer mVertexBuffer;
公共浮动缓冲区McColorbuffer;
公共浮动缓冲区mColorBufferKs;
公共浮动缓冲器;
私有短缓冲区mIndexBuffer;
私人短指标f[];
私有浮动顶点[];
私有浮动纹理词[];
私人花色[];
私人浮点数【】;
装载机;
公共音速[];
公共浮动时间浮动[];
公共TestUV(上下文上下文,字符串文件名){
加载器=新对象加载器(上下文);
loader.loadVertAndInd(文件名);
tempFloat=loader.index;
短期指数[]=新的短期指数[tempFloat.length];
for(int i=0;i