2012-10-31 68 views
1

我有麻烦,为什么我的加载imagenot完美工作。我有一个渲染器类,一个模型类和一个类。在rendere类中,我生成模型,并且每个模型都有一个piece属性。在rendere类android图像加载动态openGL

@Override 
public void onSurfaceCreated(GL10 gl, EGLConfig config) { 


    // preparation 
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
    loadGLTexture(gl, context); 
    models=new HashMap<Identity,Model>(); 
    for(int i=-9;i<10;i+=2){ 
     for(int j=-8;j<10;j+=4){ 
      models.put(new Identity(i,j), new Model(j,i,0f,w,h)); 
      models.get(new Identity(i,j)).getPiece().setImageBuffer(imageBuffer); 
      models.get(new Identity(i,j)).getPiece().setTextures(textures); 
     } 
    } 

     // Load the texture for the square 
    gl.glEnable(GL10.GL_TEXTURE_2D);   //Enable Texture Mapping (NEW) 
    gl.glShadeModel(GL10.GL_SMOOTH);   //Enable Smooth Shading 
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //Black Background 
    gl.glClearDepthf(1.0f);      //Depth Buffer Setup 
    gl.glEnable(GL10.GL_DEPTH_TEST);   //Enables Depth Testing 
    gl.glDepthFunc(GL10.GL_LEQUAL);    //The Type Of Depth Testing To Do 
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); 

} 
public void loadGLTexture(GL10 gl, Context context) { 
     Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), 
       R.drawable.tree2); 
     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); 

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

     imageBuffer = ByteBuffer.allocateDirect(bitmap.getHeight() * bitmap.getWidth() * 4); 
     imageBuffer.order(ByteOrder.nativeOrder()); 
     byte buffer[] = new byte[4]; 
     for(int i = 0; i < bitmap.getHeight(); i++) 
     { 
      for(int j = 0; j < bitmap.getWidth(); j++) 
      { 
       int color = bitmap.getPixel(j, i); 
       buffer[0] = (byte)Color.red(color); 
       buffer[1] = (byte)Color.green(color); 
       buffer[2] = (byte)Color.blue(color); 
       buffer[3] = (byte)Color.alpha(color); 
       imageBuffer.put(buffer); 
      } 
     } 
     imageBuffer.position(0); 
     gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, bitmap.getWidth(), bitmap.getHeight(), 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, imageBuffer);  
     // Recycle the bitmap, since its data has been loaded into OpenGL. 
     bitmap.recycle(); 
    } 

绘制函数:

@Override 
public void onDrawFrame(GL10 gl) { 
    // define the color we want to be displayed as the "clipping wall" 
    gl.glClearColor(_red, _green, _blue, 1.0f); 
    // reset the matrix - good to fix the rotation to a static angle 

    gl.glLoadIdentity(); 
    // clear the color buffer to show the ClearColor we called above... 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
    gl.glEnable(GL10.GL_BLEND); 
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 
    for(Model model:models.values()){ 
     model.draw(gl);  
    } 

} 

在模型类我画彩色六角形;

public void draw(GL10 gl){ 

    gl.glLoadIdentity(); 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer); 
    gl.glColor4f(0.5f, 0.5f, 0.5f, 1f); 
    gl.glDrawElements(GL10.GL_TRIANGLE_FAN, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer); 
    if(piece!=null){ 
     piece.draw(gl); 
    } 
} 

在片类

public void draw(GL10 gl){ 

    gl.glLoadIdentity(); 
    // gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
      // bind the previously generated texture 
      gl.glActiveTexture(GL10.GL_TEXTURE0); 
    // gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA); 
      gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); 
      gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, 32, 32, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, imageBuffer); 
      // gl.glTexSubImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, 32, 32, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, imageBuffer); 
      gl.glVertexPointer(3, 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,4); 

} 

我设置vertexBuffer和textureBuffer在构造。 我不知道巫婆的方法在哪里使用,在大多数教程中只使用一次图片。 为什么我的PNG失去了透明性,为什么我看不到模型的六边形。

它开始是复杂的,我:(

回答

0

在OpenGL ES 2.0的我的图像加载代码如下所示:

final int[] textureObjectIds = new int[1]; 
    glGenTextures(1, textureObjectIds, 0); 

    if (textureObjectIds[0] == 0) { 
      Log.w(TAG, "Could not generate a new OpenGL texture object."); 
     return 0; 
    } 

    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inScaled = false; 
    options.inPreferredConfig = Bitmap.Config.ARGB_8888; 

    // Read in the resource 
    final Bitmap bitmap = BitmapFactory.decodeResource(
     context.getResources(), resourceId, options); 

    if (bitmap == null) { 
     if(GameLogger.ON) 
     { 
      Log.w(TAG, "Resource ID " + resourceId + " could not be decoded."); 
     } 
     glDeleteTextures(1, textureObjectIds, 0); 
     return 0; 
    } 

    // Bind to the texture in OpenGL 
    glBindTexture(GL_TEXTURE_2D, textureObjectIds[0]); 

    // Set filtering: a default must be set, or the texture will be 
    // black. 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 

    texImage2D(GL_TEXTURE_2D, 0, bitmap, 0); 

    glGenerateMipmap(GL_TEXTURE_2D); 

    // Recycle the bitmap, since its data has been loaded into OpenGL. 
    bitmap.recycle(); 

    // Unbind from the texture. 
    glBindTexture(GL_TEXTURE_2D, 0); 

希望可以帮助你