2011-10-17 26 views
0

我需要了解如何在立方體的每個面上放置不同的紋理(OpenGL ES 1)。但我找不到怎麼辦呢有人有在每張臉上具有不同紋理的立方體的代碼示例? (OpenGL ES 1)

大氣壓我發現怎麼畫立方體的所有面相同的紋理的例子例子,但我不認爲需要

這是實際的代碼我有:

public class Cube { 

/** The buffer holding the vertices */ 
private FloatBuffer vertexBuffer; 
/** The buffer holding the texture coordinates */ 
private FloatBuffer textureBuffer; 
/** The buffer holding the indices */ 
private ByteBuffer indexBuffer; 

/** Our texture pointer */ 
private int[] textures = new int[1]; 

/** 
* The initial vertex definition 
* 
* Note that each face is defined, even 
* if indices are available, because 
* of the texturing we want to achieve 
*/ 
private float vertices[] = { 
        //Vertices according to faces 
        -1.0f, -1.0f, 1.0f, //Vertex 0 
        1.0f, -1.0f, 1.0f, //v1 
        -1.0f, 1.0f, 1.0f, //v2 
        1.0f, 1.0f, 1.0f, //v3 

        1.0f, -1.0f, 1.0f, //... 
        1.0f, -1.0f, -1.0f,   
        1.0f, 1.0f, 1.0f, 
        1.0f, 1.0f, -1.0f, 

        1.0f, -1.0f, -1.0f, 
        -1.0f, -1.0f, -1.0f,    
        1.0f, 1.0f, -1.0f, 
        -1.0f, 1.0f, -1.0f, 

        -1.0f, -1.0f, -1.0f, 
        -1.0f, -1.0f, 1.0f,   
        -1.0f, 1.0f, -1.0f, 
        -1.0f, 1.0f, 1.0f, 

        -1.0f, -1.0f, -1.0f, 
        1.0f, -1.0f, -1.0f,   
        -1.0f, -1.0f, 1.0f, 
        1.0f, -1.0f, 1.0f, 

        -1.0f, 1.0f, 1.0f, 
        1.0f, 1.0f, 1.0f,   
        -1.0f, 1.0f, -1.0f, 
        1.0f, 1.0f, -1.0f, 
             }; 

/** The initial texture coordinates (u, v) */ 
private float texture[] = {   
        //Mapping coordinates for the vertices 
        0.0f, 0.0f, 
        0.0f, 1.0f, 
        1.0f, 0.0f, 
        1.0f, 1.0f, 

        0.0f, 0.0f, 
        0.0f, 1.0f, 
        1.0f, 0.0f, 
        1.0f, 1.0f, 

        0.0f, 0.0f, 
        0.0f, 1.0f, 
        1.0f, 0.0f, 
        1.0f, 1.0f, 

        0.0f, 0.0f, 
        0.0f, 1.0f, 
        1.0f, 0.0f, 
        1.0f, 1.0f, 

        0.0f, 0.0f, 
        0.0f, 1.0f, 
        1.0f, 0.0f, 
        1.0f, 1.0f, 

        0.0f, 0.0f, 
        0.0f, 1.0f, 
        1.0f, 0.0f, 
        1.0f, 1.0f, 

             }; 

/** The initial indices definition */ 
private byte indices[] = { 
        //Faces definition 
        0,1,3, 0,3,2,   //Face front 
        4,5,7, 4,7,6,   //Face right 
        8,9,11, 8,11,10,  //... 
        12,13,15, 12,15,14,  
        16,17,19, 16,19,18,  
        20,21,23, 20,23,22,  
             }; 

/** 
* The Cube constructor. 
* 
* Initiate the buffers. 
*/ 
public Cube() { 
    // 
    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); 

    // 
    indexBuffer = ByteBuffer.allocateDirect(indices.length); 
    indexBuffer.put(indices); 
    indexBuffer.position(0); 
} 

/** 
* The object own drawing function. 
* Called from the renderer to redraw this instance 
* with possible changes in values. 
* 
* @param gl - The GL Context 
*/ 
public void draw(GL10 gl) { 
    //Bind our only previously generated texture in this case 
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); 

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

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

    //Enable the vertex and texture state 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); 
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); 

    //Draw the vertices as triangles, based on the Index Buffer information 
    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer); 

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

/** 
* Load the textures 
* 
* @param gl - The GL Context 
* @param context - The Activity context 
*/ 
public void loadGLTexture(GL10 gl, Context context) { 
    //Get the texture from the Android resource directory 
    InputStream is = context.getResources().openRawResource(R.drawable.nehe); 
    Bitmap bitmap = null; 
    try { 
     //BitmapFactory is an Android graphics utility for images 
     bitmap = BitmapFactory.decodeStream(is); 

    } finally { 
     //Always clear and close 
     try { 
      is.close(); 
      is = null; 
     } catch (IOException e) { 
     } 
    } 

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

    //Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE 
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT); 
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT); 

    //Use the 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(); 
} 
} 
+2

可能的重複[如何在OpenGL ES 1.1上填充具有不同紋理的立方體的每一面?](http://stackoverflow.com/questions/7767367/how-to-fill-each-side-of-a -cube-with-different-textures-on-opengl-es-1-1) –

回答

2

您正在繪製一個調用glDrawElements的立方體的所有12個三角形。在一次調用glDrawElements的過程中,GL渲染狀態保持不變,這包括綁定紋理。這意味着,在調用glDrawElements期間,不能更改用於渲染的紋理。因此,要爲每個立方體使用不同的紋理,您需要單獨調用glDrawElements。每次調用之前,您都需要更改綁定的紋理。

還有更高級的方法可以讓你只用一個電話來提高效率,但是現在我們不用擔心這些。

+0

嗨,謝謝你的解釋,但是對於我的實際opengl技能來說太過於模糊了。你有沒有一個例子或教程與代碼示例如何做到這一點? – NullPointerException

+2

@ AndroidUser99也許[這個問題](http://stackoverflow.com/q/7767367/743214)及其答案的幫助,因爲它似乎使用**完全相同的代碼**? –

相關問題