Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
835 chris 1
package com.gebauz.bauzoid.graphics.renderstates;
2
 
3
import java.nio.IntBuffer;
4
 
5
import com.badlogic.gdx.Gdx;
6
//import com.badlogic.gdx.graphics.GL10;
7
import com.badlogic.gdx.graphics.GL20;
8
import com.badlogic.gdx.graphics.Texture;
9
import com.badlogic.gdx.graphics.Texture.TextureFilter;
10
import com.badlogic.gdx.utils.BufferUtils;
11
import com.gebauz.bauzoid.app.BauzoidException;
12
 
13
/** Special kind of render state managing a specific texture stage. */
14
public class TextureStage
15
{
16
        private RenderStates mRenderStates = null;
17
        private Texture mBoundTexture = null;
18
        private int mIndex = 0;
19
 
20
        static int mMaxTextureUnits = -1;
21
 
22
        /** Constructor. */
23
        public TextureStage(RenderStates renderStates, int index)
24
        {              
25
                mIndex = index;
26
                mRenderStates = renderStates;          
27
        }
28
 
29
        /** Initialize render state. */
30
        void initialize() throws BauzoidException
31
        {
32
                if (mMaxTextureUnits == -1)
33
                {                      
34
                        IntBuffer numBuf = BufferUtils.newIntBuffer(16);
35
                        Gdx.gl20.glGetIntegerv(GL20.GL_MAX_TEXTURE_IMAGE_UNITS, numBuf);
36
                        mMaxTextureUnits = numBuf.get(0);
37
                }
38
 
39
                if (mIndex >= mMaxTextureUnits)
40
                        throw new BauzoidException("Not enough texture stages to run this game!");
41
 
42
                Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0 + mIndex);
43
 
44
                Gdx.gl20.glEnable(GL20.GL_TEXTURE_2D);
45
                //gl.Disable(GL10.GL_TEXTURE_CUBE_MAP);
46
                Gdx.gl20.glBindTexture(GL20.GL_TEXTURE_2D, 0);
47
                //gl.glBindTexture(GL10.GL_TEXTURE_CUBE_MAP);
48
 
49
                //mBoundTarget = GL_TEXTURE_2D;
50
                mBoundTexture = null;
51
        }
52
 
53
        public void bindTexture(Texture texture)
54
        {
55
                bindTexture(texture, false);
56
        }
57
 
58
        public void bindTexture(Texture texture, boolean force)
59
        {
60
                if (mIndex >= mMaxTextureUnits)
61
                        return;
62
 
63
                Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0 + mIndex);
64
 
65
                if ((mBoundTexture != texture) || (force))
66
                {
67
                        // Update the currently bound texture.
68
                        mBoundTexture = texture;
69
 
70
                        // Upload texture settings to GPU.
71
                        Gdx.gl20.glEnable(GL20.GL_TEXTURE_2D);
72
 
73
                        if (mBoundTexture != null)
74
                        {
75
                                //Gdx.gl20.glBindTexture(GL10.GL_TEXTURE_2D, mBoundTexture.getTextureObjectHandle());
76
                                mBoundTexture.bind();          
77
                        }
78
                        else
79
                        {
80
                                Gdx.gl20.glBindTexture(GL20.GL_TEXTURE_2D, 0);                         
81
                        }
82
                }
83
        }
84
 
85
        // HACK: should encapsulate Texture class instead
86
        public void setTextureFilter(Texture texture, TextureFilter minFilter, TextureFilter magFilter)
87
        {
88
                bindTexture(texture);
89
                texture.setFilter(minFilter, magFilter);
90
        }
91
 
92
        public final RenderStates getRenderStates()
93
        {
94
                return mRenderStates;
95
        }
96
 
97
 
98
}