Subversion Repositories AndroidProjects

Rev

Rev 88 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
87 chris 1
package com.gebauz.pingK.game;
2
 
3
import javax.microedition.khronos.opengles.GL10;
4
 
5
import com.gebauz.framework.util.GLUtil;
6
import com.gebauz.framework.util.MathUtil;
7
import com.gebauz.framework.util.Mesh;
8
import com.gebauz.framework.util.RenderImmediate;
9
import com.gebauz.framework.util.RenderTexture;
89 chris 10
import com.gebauz.framework.util.RenderUtil;
87 chris 11
import com.gebauz.framework.util.renderstates.RenderStates;
12
import com.gebauz.framework.util.renderstates.BlendingStates.BlendingMode;
13
 
14
public class MosaicEffect
15
{
16
        private GameLogic mGameLogic;
17
 
18
        private RenderTexture mOffscreen = null;
19
        private Mesh mMesh = null;
20
 
21
        private float mCurrentAlpha = 0.0f;
22
 
23
        public MosaicEffect(GameLogic gameLogic)
24
        {
25
                mGameLogic = gameLogic;
26
                mOffscreen = RenderTexture.create(GameConsts.POWERUP_MOSAIC_OFFSCREEN_WIDTH, GameConsts.POWERUP_MOSAIC_OFFSCREEN_HEIGHT);
27
 
89 chris 28
                mMesh = RenderUtil.createQuad(0.0f, 0.0f, GameConsts.VIRTUAL_SCREEN_WIDTH, mGameLogic.getVirtualPlayFieldHeight());
29
 
30
                /*mMesh = new Mesh();
31
 
87 chris 32
                Mesh.AttributeArray vertices = new Mesh.AttributeArray(Mesh.COLOR_ELEMENTS_COUNT * 6);
33
                vertices.fill(0.0f, 0.0f, 0.0f);
34
                vertices.fill(GameConsts.VIRTUAL_SCREEN_WIDTH, 0.0f, 0.0f);
35
                vertices.fill(0.0f, mGameLogic.getVirtualPlayFieldHeight(), 0.0f);
36
 
37
                vertices.fill(0.0f, mGameLogic.getVirtualPlayFieldHeight(), 0.0f);
38
                vertices.fill(GameConsts.VIRTUAL_SCREEN_WIDTH, 0.0f, 0.0f);
39
                vertices.fill(GameConsts.VIRTUAL_SCREEN_WIDTH, mGameLogic.getVirtualPlayFieldHeight(), 0.0f);
40
 
41
                Mesh.AttributeArray colors = new Mesh.AttributeArray(Mesh.COLOR_ELEMENTS_COUNT * 6);
42
                colors.fill(1.0f, 1.0f, 1.0f, 1.0f);
43
                colors.fill(1.0f, 1.0f, 1.0f, 1.0f);
44
                colors.fill(1.0f, 1.0f, 1.0f, 1.0f);
45
                colors.fill(1.0f, 1.0f, 1.0f, 1.0f);
46
                colors.fill(1.0f, 1.0f, 1.0f, 1.0f);
47
                colors.fill(1.0f, 1.0f, 1.0f, 1.0f);
48
 
49
                Mesh.AttributeArray texCoords = new Mesh.AttributeArray(Mesh.TEX_COORD_ELEMENTS_COUNT * 6);
50
                texCoords.fill(0.0f, 1.0f);
51
                texCoords.fill(1.0f, 1.0f);
52
                texCoords.fill(0.0f, 0.0f);
53
 
54
                texCoords.fill(0.0f, 0.0f);
55
                texCoords.fill(1.0f, 1.0f);
56
                texCoords.fill(1.0f, 0.0f);
57
 
58
                mMesh.setVertices(vertices.getAttributeArray());
59
                mMesh.setColors(colors.getAttributeArray());
60
                mMesh.setTexCoords(texCoords.getAttributeArray());             
89 chris 61
                mMesh.setPrimitiveType(GL10.GL_TRIANGLES);*/
87 chris 62
 
63
                mCurrentAlpha = 0.0f;
64
        }
65
 
66
        public void update(float deltaTime)
67
        {
68
                if (mGameLogic.getPowerUpEffect().isMosaicActive())
69
                {
70
                        float mosaicTime = mGameLogic.getPowerUpEffect().getMoasicTime();
71
                        float alpha = 1.0f;
72
                        if (mosaicTime < GameConsts.POWERUP_MOSAIC_FADE_TIME)
73
                        {
74
                                alpha = MathUtil.clamp(mosaicTime / GameConsts.POWERUP_MOSAIC_FADE_TIME, 0.0f, 1.0f);
75
                        }                              
76
                        else if (mosaicTime > (GameConsts.POWERUP_MOSAIC_TIME - GameConsts.POWERUP_MOSAIC_FADE_TIME))
77
                        {
78
                                float v = mosaicTime - (GameConsts.POWERUP_MOSAIC_TIME - GameConsts.POWERUP_MOSAIC_FADE_TIME);
79
                                alpha = 1.0f - MathUtil.clamp(v / GameConsts.POWERUP_MOSAIC_FADE_TIME, 0.0f, 1.0f);
80
                        }
81
 
82
                        // go to target
83
                        float diff = alpha - mCurrentAlpha;
84
                        mCurrentAlpha += (diff * deltaTime * GameConsts.POWERUP_MOSAIC_FADE_TIME);
85
 
86
                        float colors[] = {
87
                                1.0f, 1.0f, 1.0f, mCurrentAlpha,
88
                                1.0f, 1.0f, 1.0f, mCurrentAlpha,
89
                                1.0f, 1.0f, 1.0f, mCurrentAlpha,
90
                                1.0f, 1.0f, 1.0f, mCurrentAlpha,
91
                                1.0f, 1.0f, 1.0f, mCurrentAlpha,
92
                                1.0f, 1.0f, 1.0f, mCurrentAlpha
93
                        };
94
 
95
                        mMesh.setColors(colors);                       
96
                }
97
        }
98
 
99
        public void render()
100
        {
101
                // pass-through if effect not active
102
                if (mGameLogic.getPowerUpEffect().isMosaicActive())
103
                {
104
                        // fade out effect
105
 
106
                        RenderStates rs = GLUtil.getRenderStates();
107
 
108
                        rs.blending.setEnabled(true);
109
                        rs.blending.setBlendingMode(BlendingMode.ALPHABLEND);
110
                        rs.culling.setEnabled(false);
111
                        rs.getTextureStage(0).bindTexture(mOffscreen);
112
 
113
                        rs.activate();                 
114
                        mMesh.render();
115
                        rs.deactivate();
116
 
117
                        rs.getTextureStage(0).bindTexture(null);
118
                }
119
        }
120
 
121
        /** Called before the game is rendered. */
88 chris 122
        public void beginRenderToTexture()
87 chris 123
        {
124
                // pass-through if effect not active
125
                if (mGameLogic.getPowerUpEffect().isMosaicActive())
126
                {
88 chris 127
                        mOffscreen.activate();
128
 
129
                        GL10 gl = GLUtil.getGL();
130
                        gl.glClearColor(0.05f, 0.05f, 0.05f, 1.0f);
131
                        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
132
 
133
                        gl.glViewport(0, 0, GameConsts.POWERUP_MOSAIC_OFFSCREEN_WIDTH, GameConsts.POWERUP_MOSAIC_OFFSCREEN_HEIGHT);
134
 
135
                        gl.glMatrixMode(GL10.GL_PROJECTION);
136
                        gl.glLoadIdentity();
137
                        gl.glOrthof(0, GameConsts.VIRTUAL_SCREEN_WIDTH-1.0f, mGameLogic.getVirtualPlayFieldHeight()-1.0f, 0, 0, 1);
138
 
87 chris 139
                }              
140
        }
141
 
142
        /** Called after the game is rendered. */
88 chris 143
        public void endRenderToTexture()
87 chris 144
        {
145
                // pass-through if effect not active
146
                if (mGameLogic.getPowerUpEffect().isMosaicActive())
147
                {
88 chris 148
                        mOffscreen.deactivate();
149
                        GL10 gl = GLUtil.getGL();
150
                        gl.glViewport(0, 0, GLUtil.getInstance().getWidth(), GLUtil.getInstance().getHeight());
151
                        gl.glMatrixMode(GL10.GL_PROJECTION);
152
                        gl.glLoadIdentity();
153
                        gl.glOrthof(0, GameConsts.VIRTUAL_SCREEN_WIDTH-1.0f, GameConsts.VIRTUAL_SCREEN_HEIGHT-1.0f, 0, 0, 1);
87 chris 154
                }              
155
        }
156
}
157