我最近偶然發現了一個奇怪的問題(或者至少我是這麼認爲的)。我在Android的一個小opengl es應用程序中做的。問題是在它運行的模擬器上很好,但在電話上,它只是不顯示模擬器給我看的東西! 我測試了兩款設備上的應用程序:三星Ace和索尼Xperia x10,結果相同! 我只是不知道是什麼問題(我沒有與Android設備上的OpenGL太多的經驗),所以請,如果你有一些想法,......,點我出去 一些代碼:Android Opengl不會讓我的顯示器像模擬器
類實現渲染器
public class GLOrbitor implements Renderer{
OrbitorLayer layer;
TexFont text; //this is used to render 1-9 and A-Z
TexFont text1; //this one to render a-z
int x;
int y;
private int atomNumber;
private int width;
private int height;
private Context context;
private final static float consty = 0.15f;
GLOrbitor(Context context){
setContext(context);
}
@Override
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
gl.glDisable(GL10.GL_DITHER);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, 5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
gl.glPushMatrix();
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnable(GL10.GL_BLEND); // We know this is a 32bit font so set blending to suit
gl.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE_MINUS_SRC_ALPHA);
drawNumAndLetters(gl);
gl.glDisable(GL10.GL_BLEND);
gl.glDisable(GL10.GL_TEXTURE_2D);
gl.glPopMatrix();
gl.glPushMatrix();
//gl.glTranslatef(-0.8f, 0.8f, 0f);
layer.draw(gl,getAtomNumber());
gl.glPopMatrix();
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
gl.glViewport(0,0, width, height);
this.width = width;
this.height = height;
float ratio = (float)width/height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, 1, 1, 3, 7);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
layer = new OrbitorLayer();
text = new TexFont(getContext(),gl);
text1 = new TexFont(getContext(),gl);
try {
text.LoadFont("ubunturegular.bff", gl); // 0-9 and A-Z
text1.LoadFont("ubunturegular1.bff", gl); //a-z
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_NICEST);
gl.glClearColor(0.0f,0.0f,0.0f, 1f);
gl.glEnable(GL10.GL_DEPTH_TEST | GL10.GL_SMOOTH);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glFrontFace(GL10.GL_CCW);
//gl.glEnable(GL10.GL_CULL_FACE);
//gl.glCullFace(GL10.GL_BACK);
}
}`
//此代碼繪製一個微小的矩形
public GLRectangle(){
setFilled(false);
ByteBuffer vbb = ByteBuffer.allocateDirect(12 * 4);
vbb.order(ByteOrder.nativeOrder());
mFVertexBuffer = vbb.asFloatBuffer();
ByteBuffer ibb = ByteBuffer.allocateDirect(6 * 2);
ibb.order(ByteOrder.nativeOrder());
mIndexBuffer = ibb.asShortBuffer();
float[] coords = {
-poz, -poz, 0,
poz, -poz, 0,
poz, poz, 0,
-poz,poz,0
};
mFVertexBuffer.put(coords);
short[] myIndecesArray = {0,1,2,0,2,3};
mIndexBuffer.put(myIndecesArray);
mFVertexBuffer.position(0);
mIndexBuffer.position(0);
}
public void draw(GL10 gl)
{
if(isFilled())
gl.glColor4f(1f, 0f, 0f, 1f);
else gl.glColor4f(1f, 1f, 1f, 1f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mFVertexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, 6,GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
//而這一次繪製rectangl的陣列ES
public class OrbitorLayer {
GLRectangle[] rectangle;
GLMargin margin;
private final static float consty = 0.15f;
OrbitorLayer(){
margin = new GLMargin();
rectangle = new GLRectangle[118];
}
public void draw(GL10 gl,int atomNumber){
for(int i = 0;i<118;i++){
rectangle[i] = new GLRectangle();
if((i>=0) && (i<atomNumber))
rectangle[i].setFilled(true);
}
//there's some exception for chromium with z = 24 and copper with z=29
//they have 4s1 and 3d5 also 4s1 and 3d10
if(atomNumber == 24){
//4s2 is at 19 and 24
rectangle[19].setFilled(false);
rectangle[24].setFilled(true);
}
else if(atomNumber == 29){
//3d10 is at 29
rectangle[19].setFilled(false);
rectangle[29].setFilled(true);
}
gl.glPushMatrix();
margin.draw(gl);
gl.glPopMatrix();
int index = -1;
float startx = -0.9f;
float starty = 0.8f;
//start with 1s layer
for(int i=0;i<2;i++){
index++;
//rectangle[index] = new GLRectangle();
//translate and draw
gl.glPushMatrix();
gl.glTranslatef((float) (startx + (i * 0.05)), starty, 0.0f);
rectangle[index].draw(gl);
gl.glPopMatrix();
}
//2 s layer
startx = -0.9f;
starty = (float)(0.8f - (1 * consty));
for(int i=0;i<2;i++){
index++;
//rectangle[index] = new GLRectangle();
//translate and draw
gl.glPushMatrix();
gl.glTranslatef((float) (startx + (i * 0.05)), starty, 0.0f);
rectangle[index].draw(gl);
gl.glPopMatrix();
}
//2 p layer
startx = 0.7f;
starty = (float)(0.8f - (1 * consty));
for(int i=0;i<6;i++){
index++;
//rectangle[index] = new GLRectangle();
//translate and draw
gl.glPushMatrix();
gl.glTranslatef((float) (startx + (i * 0.05)), starty, 0.0f);
rectangle[index].draw(gl);
gl.glPopMatrix();
}
}
而且它如何呈現在模擬器 一個真正的Android智能手機,那些充滿矩形和藍色的圓形是不可見的只有被繪製這些字母和數字的截圖代碼 http://www.codehead.co.uk/cbfg/TexFont.java
如果您有一些想法,請不要猶豫!
更新:
謝謝Craigy和馬修
Craigy:在真正的手機,我不能看到這些紅色和白色的小矩形。順便說一句:只有填充藍色矩形的區域是一個GLSurfaceView其他所有與opengl沒有鏈接!對不起,如果我不能提供一個真實設備的屏幕截圖。我的應用程序已通過測試2他們的設備上的朋友。
馬修:我唯一使用紋理的地方是顯示:1,2,3,4,5,6,7和s,f,d,p.有趣的是,它們在一個真實device.My問題是爲什麼繪製紋理影響繪畫和翻譯那些rectangles.Anyway我會玩一點你的信息,你建議我,我會讓你們現在!
如果您對這些問題有任何新的認識......請求我請求你..這是驅動我堅果只是顯示幾個矩形和一些文本給我這麼多問題!
你可以發佈你的設備上看起來像什麼截圖? – Craigy