简体   繁体   中英

OpenGl all models are with the same texture

I would like to know how to bind different textures to different models or how to improve my code.

My code:

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glLoadIdentity();
        GLU.gluPerspective(gl, 25.0f, (view.getWidth() * 1f) / view.getHeight(), 1, 100);
        GLU.gluLookAt(gl, 0f, 8f, 12f, 0.0f, 0.0f, 0.0f, 0.0f, 25.0f, 0.0f);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glEnable(GL10.GL_TEXTURE_2D);
    loadTexture(gl, context, R.drawable.grndf);
    loadTexture(gl, context, R.drawable.skintrip);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);
    }



private int[] mTexture = new int[2];

private void loadTexture(GL10 gl, Context mContext, int mTex) {
        gl.glGenTextures(1, mTexture, 0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexture[0]);

        Bitmap bitmap;
        bitmap = BitmapFactory.decodeResource(mContext.getResources(), mTex);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
        bitmap.recycle();}

As already said in the comment, there is no concept like a model or a object. OpenGL is a state machine, which means that at every draw-call the current state is used. So if you want to have two draw-calls using different textures, then you have to make sure that at every draw-call the correct texture is bound:

glBindTexture(GL_TEXTURE_2D, texture1);
glDraw*... //First draw call using texture1

glBindTexture(GL_TEXTURE_2D, texture2);
glDraw*... //Second draw call using texture2

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