我正在使用这个font.png,但在游戏中它看起来并不清晰。我如何在游戏中绘制一个明文?将文本渲染到Draw text in OpenGL ES中的纹理比我的解决方案更符合逻辑?
答案 0 :(得分:0)
您是否为OpenGL上下文启用了混合?
在OnSurfaceCreated()中尝试此代码:
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_BLEND);
修改强> 此代码适用于您的纹理: 只需将纹理重命名为“text.png”并将其放入res / raw /
//////////////////////////////
// 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);
}
}
答案 1 :(得分: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
。
你不需要两个调用,第一个用于纹理渲染小于而不是实际分辨率,第二个用于渲染时更大。 根据我的经验,文本的可读性最好在渲染较小时使用“最近”过滤,而在渲染较大时使用“线性”;但这是主观的,所以你应该试验并找到最适合你的东西。