开发者

glDrawTexfOES draws black texture on phone, and correct in emulator

I'm writing a 2D game using OpenGL, using png images (64x64 pixels, with transparency) stored in my resources.

My code looks like this :

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

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

public class TestGLRenderer implements Renderer {

    private int mTexGLNames[];
    private Context mContext;

    public TestGLRenderer(Context ctx) {
        mContext = ctx;
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // setup the gl renderer
        gl.glClearColor(0.2f, 0.4f, 0.6f, 1.0f);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glShadeModel(GL10.GL_FLAT);
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);

        // reserve GL texture names
        mTexGLNames = new int[1];
        gl.glGenTextures(1, mTexGLNames, 0);

        // load image 开发者_运维问答from resources
        Bitmap b;
        b = BitmapFactory.decodeResource(mContext.getResources(),
                R.drawable.image);

        // load image in opengl
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexGLNames[0]);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, b, 0);
    }

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

        gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexGLNames[0]);
        int crop[] = new int[] { 0, 64, 64, -64 };

        ((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D,
                GL11Ext.GL_TEXTURE_CROP_RECT_OES, crop, 0);
        ((GL11Ext) gl).glDrawTexfOES(160, 240, 0, 64, 64);
    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {
    }

}

The result is working as expected in the emulator (running Android 2.2), but the image appears as a black square on my phone (LG-P500, Android 2.2). Attached are screenshots from both the emulator and my phone.

Is there something wrong with my code, or is it a problem with my phone (my phone can run other 3D games without problems) ?

glDrawTexfOES draws black texture on phone, and correct in emulator

glDrawTexfOES draws black texture on phone, and correct in emulator


I am not sure is it same reason than you but try it.

I loaded textures before (and same problem like you):

gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[i]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmaps[i], 0); 

Thats works fine in emulator but you can set more parameters to texture how its will calculate. My guess is that phone can't set these by default and thats why it not work (but that just guess). So to the solution:

gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[i]); //Same

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_REPEAT);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmaps[i], 0); //Same

That's it. You can google more information from those parameters (GL_LINEAR and GL_REPEAT) if you don't know.

I hope this will help =)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜