package com.gebauz.bauzoid.graphics.renderstates;
import java.nio.IntBuffer;
import com.badlogic.gdx.Gdx;
//import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.utils.BufferUtils;
import com.gebauz.bauzoid.app.BauzoidException;
/** 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)
{
IntBuffer numBuf = BufferUtils.
newIntBuffer(16);
Gdx.
gl20.
glGetIntegerv(GL20.
GL_MAX_TEXTURE_IMAGE_UNITS, numBuf
);
mMaxTextureUnits = numBuf.
get(0);
}
if (mIndex
>= mMaxTextureUnits
)
throw new BauzoidException
("Not enough texture stages to run this game!");
Gdx.
gl20.
glActiveTexture(GL20.
GL_TEXTURE0 + mIndex
);
Gdx.
gl20.
glEnable(GL20.
GL_TEXTURE_2D);
//gl.Disable(GL10.GL_TEXTURE_CUBE_MAP);
Gdx.
gl20.
glBindTexture(GL20.
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;
Gdx.
gl20.
glActiveTexture(GL20.
GL_TEXTURE0 + mIndex
);
if ((mBoundTexture
!= texture
) ||
(force
))
{
// Update the currently bound texture.
mBoundTexture = texture
;
// Upload texture settings to GPU.
Gdx.
gl20.
glEnable(GL20.
GL_TEXTURE_2D);
if (mBoundTexture
!=
null)
{
//Gdx.gl20.glBindTexture(GL10.GL_TEXTURE_2D, mBoundTexture.getTextureObjectHandle());
mBoundTexture.
bind();
}
else
{
Gdx.
gl20.
glBindTexture(GL20.
GL_TEXTURE_2D,
0);
}
}
}
// HACK: should encapsulate Texture class instead
public void setTextureFilter
(Texture texture, TextureFilter minFilter, TextureFilter magFilter
)
{
bindTexture
(texture
);
texture.
setFilter(minFilter, magFilter
);
}
public final RenderStates getRenderStates
()
{
return mRenderStates
;
}
}