2010-12-19 78 views
3

我是openGL的新手,我試圖將紋理映射到正方形。我遵循NeHe的紋理貼圖教程: http://insanitydesign.com/wp/wp-content/uploads/lesson06.zip映射正方形上的紋理(Android)

現在我看到了我的圖像......但它沒有正確映射。繼承人的原始圖像: http://ge.tt/2FzsdIx

...和繼承人什麼即時消息。 http://ge.tt/6y3cdIu

我用這個偉大的iPhone教程(鏈接下面)的頂點和紋理數組,所以即時通訊希望他們已被正確映射。以下是我在Square.java中的代碼鏈接,謝謝!

public class Square { 
// Our vertices. 
private float vertices[] = { 
      -1.0f, 1.0f, 0.0f, // 0, Top Left 
      -1.0f, -1.0f, 0.0f, // 1, Bottom Left 
      1.0f, -1.0f, 0.0f, // 2, Bottom Right 
      1.0f, 1.0f, 0.0f, // 3, Top Right 
    }; 

// The order we like to connect them. 
private short[] indices = { 0, 1, 2, 0, 2, 3 }; 

// Our vertex buffer. 
private FloatBuffer vertexBuffer; 

// Our index buffer. 
private ShortBuffer indexBuffer; 


/** The buffer holding the texture coordinates */ 
private FloatBuffer textureBuffer; 

//the texture pointer, holds the texture name which is actually a number. 
private int[] textures = new int[1]; 

public Square() { 
    // a float is 4 bytes, therefore we multiply the number if 
    // vertices with 4. 
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); 
    vbb.order(ByteOrder.nativeOrder()); 
    vertexBuffer = vbb.asFloatBuffer(); 
    vertexBuffer.put(vertices); 
    vertexBuffer.position(0); 

    // short is 2 bytes, therefore we multiply the number if 
    // vertices with 2. 
    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2); 
    ibb.order(ByteOrder.nativeOrder()); 
    indexBuffer = ibb.asShortBuffer(); 
    indexBuffer.put(indices); 
    indexBuffer.position(0); 

    //plot our texture 
    float textCoords[]={ 
      //Mapping coordinates for the vertices 
      0.0f, 1.0f, 
      1.0f, 1.0f, 
      0.0f, 0.0f, 
      1.0f, 0.0f 

           }; 
    ByteBuffer tbb = ByteBuffer.allocateDirect(textCoords.length * 4); tbb.order(ByteOrder.nativeOrder()); 
    textureBuffer = tbb.asFloatBuffer(); textureBuffer.put(textCoords); 
    textureBuffer.position(0); 

} 

//load our texture(s) 
static void loadTexture(GL10 gl, Context context, int resource) { 
    Bitmap bmp = BitmapFactory.decodeResource(context.getResources(),resource); 
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0); 
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, 
    GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); 
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, 
    GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); 
    bmp.recycle();  
} 


/** 
* This function draws our square on screen. 
* @param gl 
*/ 
public void draw(GL10 gl) { 
    //use our textures 
    gl.glEnable(GL10.GL_TEXTURE_2D); // workaround bug 3623 
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); 

    // Counter-clockwise winding. 
    gl.glFrontFace(GL10.GL_CCW); // OpenGL docs 
    // Enable face culling. 
    gl.glEnable(GL10.GL_CULL_FACE); // OpenGL docs 
    // What faces to remove with the face culling. 
    gl.glCullFace(GL10.GL_BACK); // OpenGL docs 

    // Enabled the vertices buffer for writing and to be used during 
    // rendering. 
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);// OpenGL docs. 
    // Specifies the location and data format of an array of vertex 
    // coordinates to use when rendering. 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, // OpenGL docs 
          vertexBuffer); 

    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,// OpenGL docs 
       GL10.GL_UNSIGNED_SHORT, indexBuffer); 

    // Disable the vertices buffer. 
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); // OpenGL docs 
    // Disable face culling. 
    gl.glDisable(GL10.GL_CULL_FACE); // OpenGL docs 

    } 

} 

iPhone教程: http://www.iphonemobilephones.com/opengl-es-from-the-ground-up-part-6-textures-and-texture-mapping.html

回答

5

您可以更快地繪製使用三角形扇,按以下順序在你的指數,更快。

01 
32 

那麼你不需要使用drawElements或指數,你可以將其提供給drawArrays,只需要4個元素。

你的錯誤在於,特克斯COORDS是錯誤的

tl is 0,0 
bl is 0,1 
br is 1,1 
tr is 1,0 

你有

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

所以你的紫外線是錯誤的。

-2

通常在OpenGL正方形的渲染看起來像這樣

gl.glLoadIdentity(); 
gl.glBindTexture(GL.GL_TEXTURE_2D,0); 
gl.glBegin(GL_QUADS) 
    glVertex(x,y,z); 
    glTexcoord2f(s,t); 
    glVertex(-x,y,z); 
    glTexcoord2f(-s,t); 
    glVertex(-x,-y,z); 
    glTexcoord2f(-s,-t); 
    glVertex(x,-y,z); 
    glTexcoord2f(s,-t); 
gl.glEnd(); 

我沒有看到這樣的事情,但我從來沒有做過GLES在Android之前,所以我可能會太老。

看到https://github.com/ChrisLundquist/Asteroids/blob/master/src/ScenePanel.java#L277

+3

沒有glBegin glVertex或任何在android中,你必須使用頂點緩衝區。 – HaMMeReD 2010-12-19 00:35:42

+0

相關提示謝謝。 :) – EnabrenTane 2010-12-19 00:38:12