2011-04-14 53 views
1

問候。我曾經在OpenGL中涉獵過,據我所知,用基於精靈的動畫完成2D遊戲的最佳方法是消除Z平面並設置正射投影,以便您可以使用笛卡爾座標進行紋理和繪製位置。如何在Android中的Ortho投影上渲染紋理

所以我一直在嘗試在android中實現這一點。我只是使用繪製函數中的所有構建函數,但使用vanilla drawbitmap函數分別渲染幾百個圖像會導致幀率下降。

一個矩形顯示在屏幕上,但紋理拒絕顯示。如果你可以看看,並讓我知道我要去哪裏錯了,我會非常感激。被警告我沒有像我想的那樣對opengl有全面的理解。我尊重任何可以圍繞這些東西的人。

這是精靈類含有結合的紋理繪製矩形:

public class Sprite { 

private FloatBuffer vertexBuffer; // buffer holding the vertices 
private FloatBuffer textureBuffer; 
private int[] textures = new int[1]; 


private float vertices[] = { 
     0.0f, 0.0f, 
     0.0f, 32.0f, 
     32.0f, 0.0f, 
     32.0f, 32.0f 

}; 

private float texture[] = { 
     0.0f, 0.0f, 
     0.0f, 16.0f, 
     16.0f, 0.0f, 
     16.0f, 16.0f 

}; 

public Sprite() { 
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4); 
    byteBuffer.order(ByteOrder.nativeOrder()); 
    vertexBuffer = byteBuffer.asFloatBuffer(); 
    vertexBuffer.put(vertices); 
    vertexBuffer.position(0); 

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

} 

public void loadGLTexture(GL10 gl, Context context) 
{ 
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), 
      R.drawable.charactersprites); 

    gl.glGenTextures(1, textures, 0); 
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); 

    //Not sure if I need these... 
    //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); 

    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); 
    bitmap.recycle(); 

} 

/** The draw method for the triangle with the GL context */ 
public void draw(GL10 gl) { 
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); 


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

    //gl.glFrontFace(GL10.GL_CW);//is this necessary? 

    // set the colour for the triangle 
    //gl.glColor4f(0.0f, 1.0f, 0.0f, 0.5f); 

    // Point to our vertex buffer 
    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer); 
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); 


    // Draw the vertices as triangle strip 
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length/2); 

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

} 
} 

這是渲染......我通過在srfaceview守住這樣我就可以得到高度和寬度投影

public class GlRenderer implements Renderer { 

private Sprite sprite; 
private GLSurfaceView surfaceView; 
private Context context; 

/** Constructor to set the handed over context */ 
public GlRenderer(GLSurfaceView surfaceView, Context context) { 
    this.sprite = new Sprite(); 
    this.surfaceView = surfaceView; 
    this.context = context; 
} 

@Override 
public void onDrawFrame(GL10 gl) { 
    // clear Screen and Depth Buffer 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
    gl.glMatrixMode(GL10.GL_PROJECTION); 

    // Reset the Modelview Matrix 
    gl.glLoadIdentity(); 
    gl.glOrthof(0.0f, surfaceView.getWidth(), surfaceView.getHeight(), 0.0f, -1.0f, 1.0f); 

    sprite.draw(gl);  

} 

@Override 
public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
    sprite.loadGLTexture(gl, this.context); 

    gl.glEnable(GL10.GL_TEXTURE_2D); 
    gl.glShadeModel(GL10.GL_SMOOTH); 
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); 
    gl.glClearDepthf(1.0f); 
    gl.glEnable(GL10.GL_DEPTH_TEST); 
    gl.glDepthFunc(GL10.GL_LEQUAL); 

    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);  
} 
} 

回答

0

你有沒有渲染的精靈嘗試過

glActiveTexture(GL10.GL_TEXTURE0); 

0

下面在我的銀河S上工作,但我很難讓它在我的朋友的HTC使用投影工作。雖然他們在模型空間中工作......你可以試試嗎?

public void draw(GL10 gl) { 
    gl.glActiveTexture(GL10.GL_TEXTURE0); 

    gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID); 
    gl.glEnable(GL10.GL_TEXTURE_2D); 
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mCoordinateBuffer); 

    // Draw the mesh 
    gl.glFrontFace(GL10.GL_CCW); 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer); 
    gl.glNormalPointer(GL10.GL_FLOAT, 0, mNormalBuffer); 
    gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, mIndexBuffer.limit(), GL10.GL_UNSIGNED_BYTE, mIndexBuffer); 

    // Disable texture 
    gl.glDisable(GL10.GL_TEXTURE_2D); 
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
} 

public void loadTexture(Context context, GL10 gl) { 
    Bitmap bmp; 
    try { 
      bmp = BitmapFactory.decodeStream(context.getResources().openRawResource(R.drawable.texture_loading)); 

      int[] textures = new int[1]; 
      gl.glGenTextures(1, textures, 0); 
      mTextureID = textures[0]; 
      gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID); 

      // Mendatory, tells openGL how to render the texture, nearest will look sharp, smooth will look blurry 
      gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST); 
      gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST); 

      // Not mendatory, tells openGL what to do when sprite is bigger than object 
      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE); 
      gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE); 

      // Mendatory, 
      // GL_REPLACE replaces all color info, 
      // GL_MODULATE modulates, texture will be affected by lightning 
      gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE); 

      bmp.recycle(); 
    } catch (Error e) { 

    } 

} 
0

嘗試(使用glTexEnv)的紋理環境設置爲GL_REPLACE。如果它設置爲GL_MODULATE,則紋理顏色將乘以當前顏色(如果啓用,則使用來自照明的顏色),如果此顏色爲黑色,則結果爲黑色。