Project

General

Profile

Download (6.08 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / glow / GlowRenderer.java @ 698ad0a8

1 cb9d104d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 cb9d104d Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 cb9d104d Leszek Koltunski
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 cb9d104d Leszek Koltunski
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 cb9d104d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.glow;
21
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25
26
import org.distorted.examples.R;
27 3fee789f leszek
import org.distorted.library.effect.EffectQuality;
28 cb9d104d Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.effect.PostprocessEffectGlow;
31 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
32 cb9d104d Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.DistortedTexture;
35 698ad0a8 Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
36 cb9d104d Leszek Koltunski
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39
40
import java.io.IOException;
41
import java.io.InputStream;
42
43
import javax.microedition.khronos.egl.EGLConfig;
44
import javax.microedition.khronos.opengles.GL10;
45
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48
class GlowRenderer implements GLSurfaceView.Renderer
49
{
50
   private static final int LEAF_SIZE = 100;
51
52
   private GLSurfaceView mView;
53
   private DistortedTexture mLeaf;
54
   private DistortedScreen mScreen;
55 c658e742 Leszek Koltunski
   private PostprocessEffectGlow mGlow;
56 cb9d104d Leszek Koltunski
   private int mRootW, mRootH;
57
   private Static3D mMove, mScale;
58
   private Static1D mRadius;
59 3a4f3ae2 Leszek Koltunski
   private Static4D mColor;
60 cb9d104d Leszek Koltunski
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63
   GlowRenderer(GLSurfaceView v)
64
      {     
65
      mView = v;
66
67
      mRootW = LEAF_SIZE;
68
      mRootH = LEAF_SIZE;
69 687263cc Leszek Koltunski
      mLeaf  = new DistortedTexture();
70 cb9d104d Leszek Koltunski
      mMove  = new Static3D(0,0,0);
71
      mScale = new Static3D(1,1,1);
72
      mRadius= new Static1D(25);
73 babda153 Leszek Koltunski
      mColor = new Static4D(1.0f,0.0f,0.0f,0.5f); // half-transparent red
74 cb9d104d Leszek Koltunski
75 c658e742 Leszek Koltunski
      mGlow  = new PostprocessEffectGlow(mRadius,mColor);
76 3fee789f leszek
77 698ad0a8 Leszek Koltunski
      DistortedEffects effects = new DistortedEffects();
78 cb9d104d Leszek Koltunski
      effects.apply(new MatrixEffectScale(mScale));
79 dae661e9 Leszek Koltunski
      effects.apply(new MatrixEffectMove(mMove));
80 c658e742 Leszek Koltunski
      effects.apply(mGlow);
81 cb9d104d Leszek Koltunski
82 698ad0a8 Leszek Koltunski
      MeshQuad quad = new MeshQuad();
83
      quad.setStretch(mRootW,mRootH,0);
84
85 cb9d104d Leszek Koltunski
      mScreen = new DistortedScreen();
86 698ad0a8 Leszek Koltunski
      mScreen.attach(mLeaf, effects, quad );
87 babda153 Leszek Koltunski
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
88 e72c53e5 Leszek Koltunski
      mScreen.showFPS();
89 cb9d104d Leszek Koltunski
      }
90
91 c658e742 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
92
93
   void setQuality(EffectQuality quality)
94
      {
95
      mGlow.setQuality(quality);
96
      }
97
98 cb9d104d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
99
100 3a4f3ae2 Leszek Koltunski
   int setGlowRadius(int glow)
101 cb9d104d Leszek Koltunski
     {
102
     int radius = glow/2;
103
     mRadius.set(radius);
104
     return radius;
105
     }
106
107 3a4f3ae2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
108
109
   float setGlowAlpha(float glow)
110
     {
111
     float alpha = glow/100.0f;
112 bcbd5b45 Leszek Koltunski
     mColor.set3(alpha);
113 3a4f3ae2 Leszek Koltunski
     return alpha;
114
     }
115
116 cb9d104d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
117
118
   public void onDrawFrame(GL10 glUnused)
119
     {
120
     mScreen.render(System.currentTimeMillis());
121
     }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
    
125
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
126
     {
127
     float qw = (float)width /mRootW;
128
     float qh = (float)height/mRootH;
129 be169c10 Leszek Koltunski
     float factor = 0.8f* (qw<qh ? qw:qh);
130 cb9d104d Leszek Koltunski
     int w = (int)(factor*mRootW);
131
     int h = (int)(factor*mRootH);
132
133
     mMove.set((width-w)/2 ,(height-h)/2, 0);
134
     mScale.set( factor,factor,factor );
135
     mScreen.resize(width, height);
136
     }
137
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
    
140
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
141
     {
142
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
143
     Bitmap leaf;
144
      
145
     try
146
       {
147
       leaf = BitmapFactory.decodeStream(is);
148
       }
149
     finally
150
       {
151
       try
152
         {
153
         is.close();
154
         }
155
       catch(IOException e) { }
156
       }
157
      
158
     mLeaf.setTexture(leaf);
159
160
     PostprocessEffectGlow.enable();
161
162
     try
163
       {
164 e3900503 Leszek Koltunski
       DistortedLibrary.onCreate(mView.getContext());
165 cb9d104d Leszek Koltunski
       }
166
     catch(Exception ex)
167
       {
168
       android.util.Log.e("Glow", ex.getMessage() );
169
       }
170
     }
171
}