简体   繁体   中英

OpenGL es glDrawTexfOES() 2D texture not rendering

First of all I know this is a duplicated question and I check out another questions but didn't helped! I'm trying to convert my android game from canvas system to the GL10. After lots of googling I decided to do that for performans. Anyway I tryed to build a class that loads and draws textures. The problem is textures showing up whole black over my blue screen. I'm not sure If I can draw texture without creating any mesh or something like that but this is what I'm trying to do. I'm only trying to draw 2D texture on their own sizes so I think no mesh needed. Here are my codes:

onSurfaceCreated

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glClearColor(0.0f, 0.0f, 1.0f, 0.2f);
        gl.glClearDepthf(1.0f);
        gl.glViewport(0, 0, 800,  480);

        gl.glShadeModel(GL10.GL_SMOOTH);
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glDepthFunc(GL10.GL_LEQUAL);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
        //gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        gl.glLoadIdentity();
        // This is the class where I load and draw my textures.
        textureStorage = new TextureStorage(gl);
        //I'm loading a texture.      **CODE A TEXTURE LOADER**
        bgNightID  = textureStorage.Load(BitmapFactory.decodeResource(context.getResources(),R.drawable.bg_game));
        //This is where I load my units variables. Nothing to do with it.
        RenderingFunctions.InitUnits(this);
    }

onDrawFrame

@Override
public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();
    //gl.glTranslatef(0, 0, -4);
        **CODE B DRAWING**
        // bgNightID is the ID of texture was loaded.
        textureStorage.DrawTexture(bgNightID, 50,50,0,(short)4000,(short)638);
        // Drawing background right here which is shows up black. 
}

CODE A TEXTURE LOADER

public int glLoad( Bitmap bitmap ){
    if ( gl == null ){
        Log.e(TAG, "Failed to load resource.  Context/GL is NULL");return -1;
    }
    int textureName = -1;
    gl.glGenTextures(1, TexIDStorage, 0);
    textureName = TexIDStorage[0];

    Log.d(TAG, "Generated texture: " + textureName);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureName);

    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_MIN_FILTER, GL10.GL_NEAREST);
    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);
    gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);

    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

    int error = gl.glGetError();
    if (error != GL10.GL_NO_ERROR){ 
        Log.e(TAG, "GL Texture Load Error: " + error);
    }else
        Log.d(TAG, "Loaded texture: " + textureName);
    return textureName;
}

CODE B DRAWING

public void DrawTexture(int TexutreID, float X, float Y, float Z, short Width, short Height){
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    //gl.glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, TexIDStorage[TexutreID]);
    ((GL11Ext) gl).glDrawTexfOES(X, Y, Z, Width, Height);
}

I'm totaly new to GL10 and can't figure out what is the problem and what is it I'm missing. So if anyone can help me that would be great. Thank you for givim time!

I'm pretty sure that textures width and height must be a power of 2 inorder to display propery. Try scaling your bitmap so it's a power of 2.

Well, I'm not a major OpenGL expert either, but from what I see in you code the only thing that's slightly strange is how you load you image, namely this part:

bgNightID  = textureStorage.Load(BitmapFactory.decodeResource(context.getResources(),R.drawable.bg_game));

What I do is just create a Bitmap instance like so:

InputStream is = appContext.getResources().openRawResource(R.drawable.whateverId);
Bitmap bitmap =  = BitmapFactory.decodeStream(is);

And then pass that to GLUtils:

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap , 0);

also I never make any call to glTexEnv:

//I don't do this:
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM