2012-04-21 115 views
0

enter image description here在Open GL的繪製文本不清晰

我使用這個font.png,但在遊戲中它看起來並不clear.How我可以在我的遊戲劃清文本?渲染文本到像Draw text in OpenGL ES這樣的紋理比我的解決方案更符合邏輯嗎?

+0

請發佈圖片或更好地描述您的問題。你的意思是它不透明?或者它的模糊? – Tim 2012-04-21 18:39:11

+0

@Tim我的意思是模糊不清。 – droidmachine 2012-04-21 19:06:16

+0

您可以通過遊戲中的問題圖像以及您用來繪製它的代碼獲得更多幫助。因爲沒有辦法回答這個問題。 – Tim 2012-04-21 22:48:07

回答

0

您是否爲OpenGL上下文啓用了混合?

嘗試OnSurfaceCreated()這樣的代碼:

gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 
gl.glEnable(GL10.GL_BLEND); 

編輯: 此代碼的工作與你的紋理: 只需重命名你的紋理爲 「text.png」,並把它在res /生/

////////////////////////////// 
// TextureTestActivity.java 

package com.TextureTest.TextureTest; 
import android.app.Activity; 
import android.os.Bundle; 
import android.content.Context; 
import android.opengl.GLSurfaceView; 

public class TextureTestActivity extends Activity { 
    private GLSurfaceView mGLView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mGLView = new TextureTestSurfaceView(this); 
     setContentView(mGLView); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     mGLView.onPause(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     mGLView.onResume(); 
    } 
} 

class TextureTestSurfaceView extends GLSurfaceView { 
    public TextureTestSurfaceView(Context context) { 
     super(context); 
     setEGLConfigChooser(false); 
     setRenderer(new TextureTestRenderer(context)); 
    } 
} 

////////////////////////////// 
// TextureTestRenderer.java 
package com.TextureTest.TextureTest; 
import java.io.InputStream; 
import java.nio.ByteBuffer; 
import java.nio.ByteOrder; 
import java.nio.FloatBuffer; 
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.GLES10; 
import android.opengl.GLSurfaceView; 
import android.opengl.GLU; 
import android.opengl.GLUtils; 
public class TextureTestRenderer implements GLSurfaceView.Renderer { 
    Context context; 
    int textureID; 

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

    float texcoords[] = { 
      0.0f, 0.0f, 
      0.0f, 1.0f, 
      1.0f, 0.0f, 
      1.0f, 1.0f, 
      }; 

    FloatBuffer texCoordBuffer; 
    FloatBuffer vertexBuffer; 

    TextureTestRenderer(Context context) { 
     this.context = context; 
    } 

    public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
     // set gl options 
     gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); 

     gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 
     gl.glEnable(GL10.GL_BLEND); 

     // create texture 
     int textures[] = new int[1]; 
     gl.glGenTextures(1, textures, 0); 
     textureID = textures[0]; 

     gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID); 
     gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); 
     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_CLAMP_TO_EDGE); 
     gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE); 

     InputStream is = context.getResources().openRawResource(R.raw.text); 
     Bitmap textBitmap = BitmapFactory.decodeStream(is); 

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

     // create vertex and texcoord buffers 
     ByteBuffer vbb = ByteBuffer.allocateDirect(4 * 3 * 4); 
     vbb.order(ByteOrder.nativeOrder()); 
     vertexBuffer = vbb.asFloatBuffer(); 

     ByteBuffer tbb = ByteBuffer.allocateDirect(4 * 2 * 4); 
     tbb.order(ByteOrder.nativeOrder()); 
     texCoordBuffer = tbb.asFloatBuffer(); 

     for (int v = 0; v < 4; v++) 
      for(int c = 0; c < 3; c++) 
       vertexBuffer.put(vertices[v * 3 + c]); 

     for (int v = 0; v < 4; v++) 
      for(int c = 0; c < 2; c++) 
       texCoordBuffer.put(texcoords[v * 2 + c]); 

     vertexBuffer.position(0); 
     texCoordBuffer.position(0); 

     // set up view matrices 
     gl.glMatrixMode(GL10.GL_MODELVIEW); 
     gl.glLoadIdentity(); 

     GLU.gluLookAt(gl, 0.0f, 0.0f, 5.0f, 
          0.0f, 0.0f, 0.0f, 
          0.0f, 1.0f, 0.0f); 
    } 

    public void onDrawFrame(GL10 gl) { 
     gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 

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

     gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID); 
     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); 
     gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texCoordBuffer); 

     gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); 
    } 

    public void onSurfaceChanged(GL10 gl, int width, int height) { 
     GLES10.glViewport(0, 0, width, height); 
    } 
} 
+0

對不起,但沒有change.Blending已啓用。我也啓用了四個通道,但仍然沒有change.I找不到解決方案。 – droidmachine 2012-04-25 21:04:29

+0

你接受了答案......這是否意味着你得到了它的工作? – bitwise 2012-04-25 21:33:28

+0

接受,因爲我認爲我們嘗試了所有的方法。我想我會嘗試繪製到畫布並使用此紋理。最後哪一個是有用的?畫到畫布或使用位圖? – droidmachine 2012-04-25 22:48:10

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_NEAREST); 

調用此渲染之前,然後你就可以再次調用這兩種方法繪製,但使用GL10.GL_LINEAR代替GL10.GL_NEAREST後重新設置爲「線性」的過濾。

你不需要兩個呼叫不過,第一個是當紋理渲染比它的實際分辨率,第二個是當它被渲染較大爲。 根據我的經驗,最好使文字的可讀性在渲染較小時使用「最接近」的濾鏡,渲染較大時使用「線性」濾鏡;但這是主觀的,所以你應該嘗試並找到最適合你的方法。