2011-10-18 158 views
0

我目前沒有得到任何東西,只有一個彩色的正方形,我的紋理應該繪製。白色平方紋理Opengl-es Android

我已經確定大小的紋理2(128×128)

電源我已經把紋理的繪製,nodpi文件夾類似的建議,但我仍然得到一個白色(或其他顏色相應)正方形。

這裏是我的方形物體代碼:

public class MTestSquare { 




private float vertices[] = { 
     0.0f, 1.0f, 0.0f, 
     0.0f, 0.0f, 0.0f, 
     1.0f, 0.0f, 0.0f, 
     1.0f, 1.0f, 0.0f 
}; 

private short indices[] = {0, 1, 2, 0, 2, 3,}; 

private float texture[] = { 
     0.0f, 1.0f, 
     1.0f, 1.0f, 
     0.0f, 0.0f, 
     1.0f, 0.0f 
}; 
private FloatBuffer vertexBuffer; 

private FloatBuffer textureBuffer; 

private ShortBuffer indexBuffer; 

private int[] textures = new int[1]; 





public MTestSquare() { 

    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4); 
     vbb.order(ByteOrder.nativeOrder()); 
     vertexBuffer = vbb.asFloatBuffer(); 
     vertexBuffer.put(vertices); 
     vertexBuffer.position(0); 

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

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


} 


public void loadGLTexture(GL10 gl, Context context) { 

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.testtexture); 
    gl.glGenTextures(1, textures, 0); 
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); 
    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); 
    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); 
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); 
    bitmap.recycle(); 

} 

public void draw(GL10 gl) { 

    gl.glFrontFace(GL10.GL_CCW); 
    gl.glEnable(GL10.GL_CULL_FACE); 
    gl.glCullFace(GL10.GL_BACK); 
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); 

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); 
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); 
    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, 
         GL10.GL_UNSIGNED_SHORT, indexBuffer); 
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
    gl.glDisable(GL10.GL_CULL_FACE); 
} 

} 

我畫的廣場上繪製正方形

square.draw(gl) 

,我已經把這一行代碼onSurfaceCreated

square.loadGLTexture(gl, this.context); 

加載紋理

回答

0

也許您可以查看此內容。 OpenGL Texture Tutorial

int InitGL(GLvoid)        // All Setup For OpenGL Goes Here 
{ 
    if (!LoadGLTextures())       // Jump To Texture Loading Routine (NEW) 
    { 
     return FALSE;       // If Texture Didn't Load Return FALSE (NEW) 
    } 

    glEnable(GL_TEXTURE_2D);      // Enable Texture Mapping (NEW) 
    glShadeModel(GL_SMOOTH);      // Enable Smooth Shading 
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);     // Black Background 
    glClearDepth(1.0f);       // Depth Buffer Setup 
    glEnable(GL_DEPTH_TEST);      // Enables Depth Testing 
    glDepthFunc(GL_LEQUAL);       // The Type Of Depth Testing To Do 
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);   // Really Nice Perspective Calculations 
    return TRUE;        // Initialization Went OK 
} 
+0

這基本上是我在我的onSurfaceCreated中所擁有的。這是它應該是什麼?...我想不是因爲我的紋理加載不正確。 (我得到一個顯示對角線的半透明圖形)對象顏色是否影響紋理的顏色? – Jack

+0

哦,等等......那是我的紋理,它剛剛翻轉了90度,並且正方形右邊的拐角是錯誤的... – Jack

+0

只是一個問題float texture [] values – Jack

1

你真的啓用了紋理嗎?

gl.glEnable(GL10.GL_TEXTURE_2D);