Subversion Repositories AndroidProjects

Rev

Rev 186 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.Bauzoid.graphics.renderstate;

import java.nio.IntBuffer;

import android.opengl.GLES20;

import com.gebauz.Bauzoid.app.BauzoidException;
import com.gebauz.Bauzoid.graphics.GLUtil;
import com.gebauz.Bauzoid.graphics.texture.Texture;

/** Special kind of render state managing a specific texture stage. */
public class TextureStage
{
        private RenderStates mRenderStates = null;
        private Texture mBoundTexture = null;
        private int mIndex = 0;
       
        static int mMaxTextureUnits = -1;
       
        /** Constructor. */
        public TextureStage(RenderStates renderStates, int index)
        {              
                mIndex = index;
                mRenderStates = renderStates;          
        }
       
        /** Initialize render state. */
        void initialize() throws BauzoidException
        {
                if (mMaxTextureUnits == -1)
                {
                        int num[] = {0};
                        IntBuffer numBuf = IntBuffer.wrap(num);
                        GLES20.glGetIntegerv(GLES20.GL_MAX_TEXTURE_IMAGE_UNITS, numBuf);
                        mMaxTextureUnits = numBuf.get(0);
                }
               
                if (mIndex >= mMaxTextureUnits)
                        throw new BauzoidException("Not enough texture stages to run this game!");
               
                GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + mIndex);
               
                GLES20.glEnable(GLES20.GL_TEXTURE_2D);
                //gl.Disable(GL10.GL_TEXTURE_CUBE_MAP);
                GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
                //gl.glBindTexture(GL10.GL_TEXTURE_CUBE_MAP);
               
                //mBoundTarget = GL_TEXTURE_2D;
                mBoundTexture = null;
        }
       
        public void bindTexture(Texture texture)
        {
                bindTexture(texture, false);
        }
       
        public void bindTexture(Texture texture, boolean force)
        {
                if (mIndex >= mMaxTextureUnits)
                        return;
               
                GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + mIndex);

                if ((mBoundTexture != texture) || (force))
                {
                        // Update the currently bound texture.
                        mBoundTexture = texture;
                       
                        // Upload texture settings to GPU.
                        GLES20.glEnable(GLES20.GL_TEXTURE_2D);

                        if (mBoundTexture != null)
                        {
                                GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mBoundTexture.getHandle());
                        }
                        else
                        {
                                GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);                         
                        }
                }
        }
       
        public final RenderStates getRenderStates()
        {
                return mRenderStates;
        }
               

}