2011-06-11 18 views
0

您好我是Android和OpenGLES開發的新手。我正在嘗試使用OpenGL開發一個簡單的Android遊戲。我的遊戲的背景圖像是一個平鋪,我想用紋理貼圖包裝作爲GL_REPEAT。它在我的三星Galaxy S和平臺3.0的模擬器中工作正常。問題在於我的三星10.1 Galaxy Tab,我無法重複它的質感。它始終使用紋理夾邊緣。我找到了紋理教程,當我修改本教程的紋理貼圖和換行參數時,紋理按預期重複,因此我知道它不是平板電腦中的平板電腦錯誤。問題是我的代碼中出現的問題只是平板電腦的問題?我不能讓我的紋理使用GL_REPEAT工作在我的Android Galaxy Tab 10.1上,但它適用於我的手機和模擬器

我正在做一個實現GLSurfaceView.Renderer的類中的所有紋理設置。這是一個問題嗎?

package com.droidev.games.bubbilliards; 

import java.nio.ByteBuffer; 
import java.nio.ByteOrder; 
import java.nio.FloatBuffer; 
import java.nio.ShortBuffer; 

import javax.microedition.khronos.egl.EGLConfig; 
import javax.microedition.khronos.opengles.GL10; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.opengl.GLSurfaceView; 
import android.opengl.GLUtils; 

import android.util.Log; 

public class BBGLRenderer implements GLSurfaceView.Renderer { 

    private BBGame game; 
    private int width; 
    private int height; 
    private float ratio_w_h; 
    private ShortBuffer indicesBuffer; 
    private FloatBuffer vertexBuffer; 
    private FloatBuffer textureBuffer; 
    private FloatBuffer backgroundVertexBuffer; 
    private FloatBuffer backgroundTextureBuffer; 

    private Context context; 
    private BBGLSurfaceView glSurfaceView; 
    private int ball_textures[]; 
    private int hole_open_textures[]; 
    private int hole_closed_textures[]; 
    private int background_textures[]; 
    private float length; 

    private boolean texture_set; 
    private Bitmap green_ball; 
    private Bitmap orange_ball; 
    private Bitmap purple_ball; 
    private Bitmap green_hole_open; 
    private Bitmap orange_hole_open; 
    private Bitmap purple_hole_open; 
    private Bitmap green_hole_closed; 
    private Bitmap orange_hole_closed; 
    private Bitmap purple_hole_closed; 
    private Bitmap background; 



    public BBGLRenderer(Context context_, BBGLSurfaceView sv){ 
     super(); 
     context = context_; 
     glSurfaceView = sv; 
     game = new BBGame(sv); 
     texture_set = false; 
    } 



    public void onSurfaceCreated(GL10 gl, EGLConfig config){ 



     gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
     gl.glEnable(GL10.GL_BLEND); 
     gl.glDisable(GL10.GL_DEPTH_TEST); 
     gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA); 
     gl.glEnable(GL10.GL_TEXTURE_2D); 

     // reading images from resources   
     green_ball   = BitmapFactory.decodeResource(context.getResources(), R.drawable.green_ball); 
     orange_ball  = BitmapFactory.decodeResource(context.getResources(), R.drawable.orange_ball); 
     purple_ball  = BitmapFactory.decodeResource(context.getResources(), R.drawable.purple_ball); 

     green_hole_open = BitmapFactory.decodeResource(context.getResources(), R.drawable.green_hole_open); 
     orange_hole_open = BitmapFactory.decodeResource(context.getResources(), R.drawable.orange_hole_open); 
     purple_hole_open = BitmapFactory.decodeResource(context.getResources(), R.drawable.purple_hole_open); 

     green_hole_closed = BitmapFactory.decodeResource(context.getResources(), R.drawable.green_hole_closed); 
     orange_hole_closed = BitmapFactory.decodeResource(context.getResources(), R.drawable.orange_hole_closed); 
     purple_hole_closed = BitmapFactory.decodeResource(context.getResources(), R.drawable.purple_hole_closed); 

     background   = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile); 

     // creating textures IDS 
     int num_colors = BBColor.values().length; 

     ball_textures  = new int[num_colors]; 
     hole_open_textures = new int[num_colors]; 
     hole_closed_textures = new int[num_colors]; 
     background_textures = new int[1]; 

     gl.glGenTextures(num_colors, ball_textures  , 0); 


     // balls 
     initializeTexture(gl, green_ball ,  ball_textures,  (int)BBColor.GREEN.ordinal()); 
     initializeTexture(gl, orange_ball,  ball_textures,  (int)BBColor.ORANGE.ordinal()); 
     initializeTexture(gl, purple_ball,  ball_textures,  (int)BBColor.PURPLE.ordinal()); 

     gl.glGenTextures(num_colors, hole_open_textures , 0); 

     // holes open 
     initializeTexture(gl, green_hole_open , hole_open_textures, (int)BBColor.GREEN.ordinal()); 
     initializeTexture(gl, orange_hole_open, hole_open_textures, (int)BBColor.ORANGE.ordinal()); 
     initializeTexture(gl, purple_hole_open, hole_open_textures, (int)BBColor.PURPLE.ordinal()); 

     gl.glGenTextures(num_colors, hole_closed_textures, 0); 

     // holes closed 
     initializeTexture(gl, green_hole_closed, hole_closed_textures, (int)BBColor.GREEN.ordinal()); 
     initializeTexture(gl, orange_hole_closed, hole_closed_textures, (int)BBColor.ORANGE.ordinal()); 
     initializeTexture(gl, purple_hole_closed, hole_closed_textures, (int)BBColor.PURPLE.ordinal()); 

     gl.glGenTextures(1   , background_textures , 0);  
     initializeTexture(gl, background, background_textures, 0); 


//  initializeBuffers(); 

    } 

    public void initializeTextures(GL10 gl){ 
     int num_colors = BBColor.values().length; 
     gl.glGenTextures(num_colors, ball_textures  , 0); 
     gl.glGenTextures(num_colors, hole_open_textures , 0); 
     gl.glGenTextures(num_colors, hole_closed_textures, 0); 
     gl.glGenTextures(1   , background_textures , 0); 

     // balls 
     initializeTexture(gl, green_ball ,  ball_textures,  (int)BBColor.GREEN.ordinal()); 
     initializeTexture(gl, orange_ball,  ball_textures,  (int)BBColor.ORANGE.ordinal()); 
     initializeTexture(gl, purple_ball,  ball_textures,  (int)BBColor.PURPLE.ordinal()); 
     // holes open 
     initializeTexture(gl, green_hole_open , hole_open_textures, (int)BBColor.GREEN.ordinal()); 
     initializeTexture(gl, orange_hole_open, hole_open_textures, (int)BBColor.ORANGE.ordinal()); 
     initializeTexture(gl, purple_hole_open, hole_open_textures, (int)BBColor.PURPLE.ordinal()); 
     // holes closed 
     initializeTexture(gl, green_hole_closed, hole_closed_textures, (int)BBColor.GREEN.ordinal()); 
     initializeTexture(gl, orange_hole_closed, hole_closed_textures, (int)BBColor.ORANGE.ordinal()); 
     initializeTexture(gl, purple_hole_closed, hole_closed_textures, (int)BBColor.PURPLE.ordinal()); 

     initializeTexture(gl, background, background_textures, 0); 
     texture_set = true; 
    } 

    public void initializeTexture(GL10 gl, Bitmap bmp, int textures[], int color){ 
     gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[color]); 
     gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); 
     gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_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, bmp, 0); 

//  gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, bmp.getWidth(), bmp.getHeight(), 0, GL10.GL_RGBA, GL10.GL_, pixels) 
    } 

    public void initializeBuffers(int w, int h){ 

     length = Math.min(ratio_w_h/(float)game.getWidth(), 1.0f/(float)game.getHeight()); 
     // Vertices Position ////////////////////////////////////////////////////////// 
     float vertices[] = {0.0f , 0.0f , 0.0f, 
          length, 0.0f , 0.0f,       
          0.0f , length, 0.0f, 
          length, length, 0.0f}; 

     ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(vertices.length * 4); 
     vertexByteBuffer.order(ByteOrder.nativeOrder()); 

     vertexBuffer = vertexByteBuffer.asFloatBuffer(); 
     vertexBuffer.put(vertices); 
     vertexBuffer.position(0); 

     // indices //////////////////////////////////////////////////////////////////// 

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

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

     // Texture Coords ///////////////////////////////////////////////////////////// 
     float coords[] = {0.0f, 1.0f, 
        1.0f, 1.0f,     
        0.0f, 0.0f, 
        1.0f, 0.0f}; 

     ByteBuffer textureByteBuffer = ByteBuffer.allocateDirect(coords.length * 4); 
     textureByteBuffer.order(ByteOrder.nativeOrder()); 

     textureBuffer = textureByteBuffer.asFloatBuffer(); 
     textureBuffer.put(coords); 
     textureBuffer.position(0); 


     ////////////////////////////////////////////////////////////////////// 
     // Vertices Position ////////////////////////////////////////////////////////// 
     float verticesBackground[] = {0.0f  , 0.0f, 0.0f, 
             game.getWidth()*length, 0.0f, 0.0f,          
             0.0f  , game.getHeight()*length, 0.0f, 
             game.getWidth()*length, game.getHeight()*length, 0.0f}; 

     ByteBuffer backgroundVertexByteBuffer = ByteBuffer.allocateDirect(verticesBackground.length * 4); 
     backgroundVertexByteBuffer.order(ByteOrder.nativeOrder()); 

     backgroundVertexBuffer = backgroundVertexByteBuffer.asFloatBuffer(); 
     backgroundVertexBuffer.put(verticesBackground); 
     backgroundVertexBuffer.position(0); 

     // Texture Coords ///////////////////////////////////////////////////////////// 
     float coordsBackground[] = {0.0f, game.getHeight(), 
            game.getWidth(), game.getHeight(), 
            0.0f, 0.0f, 
            game.getWidth(), 0.0f}; 

     ByteBuffer backgroundTextureByteBuffer = ByteBuffer.allocateDirect(coordsBackground.length * 4); 
     backgroundTextureByteBuffer.order(ByteOrder.nativeOrder()); 

     backgroundTextureBuffer = backgroundTextureByteBuffer.asFloatBuffer(); 
     backgroundTextureBuffer.put(coordsBackground); 
     backgroundTextureBuffer.position(0); 
     ////////////////////////////////////////////////////////////////////// 




     game.setResolution(w, h); 
     game.setRatio_W_H(ratio_w_h); 
     game.setSquareLength(length); 
    } 

    public void onSurfaceChanged(GL10 gl, int w, int h){ 
     width = w; 
     height = h; 
     ratio_w_h = (float)width/(float)height; 
     gl.glViewport(0, 0, w, h); 
     gl.glMatrixMode(GL10.GL_PROJECTION); 
     gl.glLoadIdentity(); 
     gl.glOrthof(0.0f, ratio_w_h*1.0f, 0.0f, 1.0f, -1.0f, 1.0f); 
     gl.glMatrixMode(GL10.GL_MODELVIEW); 
     initializeBuffers(w, h); 
    } 

    public void onDrawFrame(GL10 gl){ 

     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

     gl.glLoadIdentity(); 

     gl.glEnable(GL10.GL_TEXTURE_2D); 
     gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
     gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, backgroundTextureBuffer); 
     gl.glBindTexture(GL10.GL_TEXTURE_2D, background_textures[0]); 
     //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); 
//  if(!texture_set){ 
//   initializeTextures(gl); 
//   initializeBuffers(width, height); 
//  } 
     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);  
     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, backgroundVertexBuffer); 





//  gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); 
//  gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); 
//  gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE); 
//  gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE); 


//  gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4); 
     gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, indicesBuffer); 

     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); 
     gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); 

     for(BBHole h: game.getHoles()){ 
      gl.glPushMatrix(); 
      gl.glTranslatef(h.getX()*length, h.getY()*length, 0.0f); 
      gl.glBindTexture(GL10.GL_TEXTURE_2D, (h.isOpen())?hole_open_textures[h.getColor().ordinal()]:hole_closed_textures[h.getColor().ordinal()]); 
//   gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);  
      gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, indicesBuffer); 
      gl.glPopMatrix(); 
     } 


     for(BBBubble b: game.getBubbles()){ 
      synchronized (b) { 
       gl.glPushMatrix(); 
       gl.glTranslatef(b.getX()*length+b.getTx(), b.getY()*length+b.getTy(), 0.0f); 
       gl.glBindTexture(GL10.GL_TEXTURE_2D, ball_textures[b.getColor().ordinal()]); 
//    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);   
       gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, indicesBuffer); 
       gl.glPopMatrix(); 
       b.notifyAll(); 
      } 
     } 




//  gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f); 


     gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
     gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
     gl.glDisable(GL10.GL_TEXTURE_2D); 
//  gl.glFlush(); 
//  Log.i("GL_ERROR = ", Integer.toString(gl.glGetError())); 
     glSurfaceView.setDirty(false); 
    } 

    public BBGame getGame(){ 
     return game; 
    } 
} 

所以我基本上有一個Activity,它擴展了GLSurfaceView類,上面呈現的類實現了GLSurfaceView.Renderer,並且有遊戲規則類等 提前感謝!

回答

0

好的我找到了解決問題的方案,但對我來說沒有意義。在eclipse中創建的項目在res文件夾中只有文件夾drawable-hdpi,drawable-mdpi和drawable-ldpi,但沒有可繪製的文件夾,例如我之前提到的教程。

所以解決方案是創建這個「drawable」文件夾並將圖像放在那裏。然後,GL_REPEAT「神奇地」爲銀河標籤工作。我浪費2天因爲這個文件夾的問題:(。

我很好奇,如果它是有道理,還是沒有,如果我把我所有的紋理總是這些繪製文件夾內。

3

我覺得GL_REPEAT只用鍋工作(兩個冪次)紋理,並且您的drawable-xyz可能已經放大或縮小到非POT大小。

相關問題