各位大侠好:
我在使用open gl es的做显示的时候,发现一个问题,请各位帮助一下,谢谢。
环境:opengl es 1.x,2D的模式显示纹理图片。
在LG-P990,HTC-C510E上显示附件代码可以正常显示图片,
在LG-P920,亚马逊平板上显示附件代码不能正常显示图片,显示为白屏的。
附件地址:
http://download.csdn.net/detail/sukeradrd/4685329
硬件方面
LG-P990使用Nvidia的Tegra2 芯片。
HTC-C510E使用高通的MsM7227。
LG-P920
亚马逊平板均使用的TI的Omap芯片。
// GLImage.java
package com.sk.tstoge1;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
class GLImage {
public static Bitmap mBitmap;
static final String TAG = "OpenGl";
public static int m_nScrnWid;
public static int m_nScrnHei;
public static int m_nBmpWid;
public static int m_nBmpHei;
public static void load(Resources resources) {
mBitmap = BitmapFactory.decodeResource(resources, R.drawable.wood);
Log.i(TAG, "bmp info:" + mBitmap.getWidth() + "," + mBitmap.getHeight());
m_nBmpWid = (mBitmap.getWidth() / 3) * 2;
m_nBmpHei = (mBitmap.getHeight() / 3) * 2;
}
public static void setScreen(int nWid, int nHei) {
m_nScrnWid = nWid;
m_nScrnHei = nHei;
}
}
// TstOpenGlEs1.java
package com.sk.tstoge1;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
public class TstOpenGlEs1 extends Activity {
final String TAG = "OpenGl"; /* suker OpenGl Es */
Renderer m_cRdr;
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate-start...");
super.onCreate(savedInstanceState);
String strPath1 = getApplicationContext().getFilesDir()
.getAbsolutePath();
Log.i(TAG, "path1:" + strPath1);
String strPath2 = strPath1.substring(0, strPath1.length() - 5);
Log.i(TAG, "path2:" + strPath2);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
GLImage.load(this.getResources());
WindowManager wm = getWindowManager();
Display display = wm.getDefaultDisplay();
GLImage.setScreen(display.getWidth(), display.getHeight());
GLSurfaceView cGlSfcV = new GLSurfaceView(this);
final ActivityManager cActtMng = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final ConfigurationInfo cCfgInfo = cActtMng
.getDeviceConfigurationInfo();
Log.i(TAG, "reqGlEsVersion:" + cCfgInfo.reqGlEsVersion);
m_cRdr = new OpenGLRenderer(cGlSfcV);
cGlSfcV.setRenderer(m_cRdr);
cGlSfcV.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
/* cGlSfcV.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); */
setContentView(cGlSfcV);
Log.i(TAG, "onCreate-end...");
}
public void onPause() {
Log.i(TAG, "onPause-start...");
super.onPause();
Log.i(TAG, "onPause-end...");
}
}
// OpenGLRenderer.java
package com.sk.tstoge1;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL;
import javax.microedition.khronos.opengles.GL10;
import android.graphics.Bitmap;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;
public class OpenGLRenderer implements Renderer {
static final String TAG = "OpenGl";
FloatBuffer vertices;
FloatBuffer texture;
ShortBuffer indices;
int textureId;
int m_nWid = 800;
int m_nHei = 480;
float m_fPosX;
float m_fPosY;
int nPicWid = 256;
int nPicHei = 256;
byte[] m_bitmapBuf = null;
public OpenGLRenderer(GLSurfaceView mGlSfcV) {
m_nWid = GLImage.m_nScrnWid;
m_nHei = GLImage.m_nScrnHei;
m_fPosX = m_nWid / 2;
m_fPosY = m_nHei / 2;
if (null != GLImage.mBitmap) {
nPicWid = GLImage.m_nBmpWid;
nPicHei = GLImage.m_nBmpHei;
}
Log.i(TAG, "Screen:" + m_nWid + "," + m_nHei + ", Pos:X" + m_fPosX
+ ", Y:" + m_fPosY);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * 2 * 4);
byteBuffer.order(ByteOrder.nativeOrder());
vertices = byteBuffer.asFloatBuffer();
vertices.put(new float[] { -m_fPosX, -m_fPosY,/* indices:0,1pos */
m_fPosX, -m_fPosY,/* indices:1,1pos */-m_fPosX, m_fPosY,/* indices:0,0pos */
m_fPosX, m_fPosY /* indices:1,0pos */});
ByteBuffer indicesBuffer = ByteBuffer.allocateDirect(6 * 2);
indicesBuffer.order(ByteOrder.nativeOrder());
indices = indicesBuffer.asShortBuffer();
indices.put(new short[] { 0, 1, 2, 1, 2, 3 });
ByteBuffer textureBuffer = ByteBuffer.allocateDirect(4 * 2 * 4);
textureBuffer.order(ByteOrder.nativeOrder());
texture = textureBuffer.asFloatBuffer();
texture.put(new float[] { 0, 1f, 1f, 1f, 0f, 0f, 1f, 0f });
indices.position(0);
vertices.position(0);
texture.position(0);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Log.d(TAG, "surface created");
}
int m_nW;
int m_nH;
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
Log.d(TAG, "surface changed: " + width + "x" + height);
int nPosX = 0;
int nPosY = 0;
int nWid = 0;
int nHei = 0;
if (width > height) {
nPosX = (int) (width - nPicWid) / 2;
nPosY = (int) (height - nPicHei) / 2;
} else {
nPosX = (int) (width - nPicHei) / 2;
nPosY = (int) (height - nPicWid) / 2;
}
Log.i(TAG, "pic pos:" + nPosX + "," + nPosY + "," + nPicWid + ","
+ nPicHei);
gl.glViewport(nPosX, nPosY, nPicWid, nPicHei);
/* gl.glViewport(0, 0, width, width); */
m_nW = width;
m_nH = height;
}
boolean drawflag = false;
public void onDrawFrame(GL10 gl) {
/* here why entry twice? */
if (!drawflag) {
drawflag = true;
return;
}
Log.d(TAG, "onDrawFrame ");
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
textureId = loadTexture("bobrgb888.png", gl);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(-m_fPosX, m_fPosX, -m_fPosY, m_fPosY, 1, -1);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertices);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texture);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
public int loadTexture(String fileName, GL10 gl) {
Log.d(TAG, "loadTexture ");
int textureIds[] = new int[1];
gl.glGenTextures(1, textureIds, 0);
int textureId = textureIds[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
if (null != GLImage.mBitmap) {
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap, 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);
return textureId;
}
}
7 个解决方案
#1
自己顶一下
#2
纹理白的原因,应该是纹理绑定不成功。 可能是你的代码把不小心纹理删除了,接着又画图。 但你说的是不同的板子用的同一个代码,可以验证下,是不是有些板必须要求图片的尺寸是2N(老板子的要求了,现在少见)次方的,而你的图却不是。
#3
http://www.cnblogs.com/shengdoushi/archive/2011/01/13/1934181.html
仅供参考
#4
谢谢楼上,问题已经解决
#5
引用 4 楼 sukeradrd 的回复:
谢谢楼上,问题已经解决
请问怎么解决的
#6
引用 4 楼 sukeradrd 的回复:
谢谢楼上,问题已经解决
同问,怎么解决的呀,小弟现在也遇到同样的问题~~
#7
部分手机只支持纹理图案大小为2的N次方*2的N次方,比如2*2,4*4,8*8,16*16*32*32