Project

General

Profile

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

examples / src / main / java / org / distorted / examples / glow / GlowRenderer.java @ 32e96fff

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.MatrixEffectScale;
29
import org.distorted.library.effect.PostprocessEffectGlow;
30 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
31 cb9d104d Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedScreen;
33
import org.distorted.library.main.DistortedTexture;
34 698ad0a8 Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
35 678c391d Leszek Koltunski
import org.distorted.library.type.Static2D;
36 cb9d104d Leszek Koltunski
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38
39
import java.io.IOException;
40
import java.io.InputStream;
41
42
import javax.microedition.khronos.egl.EGLConfig;
43
import javax.microedition.khronos.opengles.GL10;
44
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
47 061449ed Leszek Koltunski
class GlowRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
48 cb9d104d Leszek Koltunski
{
49 678c391d Leszek Koltunski
   private static final float HALO_TO_RADIUS = 0.2f;
50 cb9d104d Leszek Koltunski
51 32e96fff Leszek Koltunski
   private final GLSurfaceView mView;
52
   private final DistortedTexture mLeaf;
53
   private final DistortedScreen mScreen;
54
   private final PostprocessEffectGlow mGlow;
55
   private final Static3D mScale;
56
   private final Static2D mHaloRadius;
57
   private final Static4D mColor;
58 cb9d104d Leszek Koltunski
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61
   GlowRenderer(GLSurfaceView v)
62
      {     
63
      mView = v;
64
65 678c391d Leszek Koltunski
      mLeaf      = new DistortedTexture();
66
      mScale     = new Static3D(1,1,1);
67
      mHaloRadius= new Static2D(25*HALO_TO_RADIUS,25);
68
      mColor     = new Static4D(1.0f,0.0f,0.0f,0.5f); // half-transparent red
69 cb9d104d Leszek Koltunski
70 678c391d Leszek Koltunski
      mGlow  = new PostprocessEffectGlow(mHaloRadius,mColor);
71 3fee789f leszek
72 698ad0a8 Leszek Koltunski
      DistortedEffects effects = new DistortedEffects();
73 cb9d104d Leszek Koltunski
      effects.apply(new MatrixEffectScale(mScale));
74 c658e742 Leszek Koltunski
      effects.apply(mGlow);
75 cb9d104d Leszek Koltunski
76 698ad0a8 Leszek Koltunski
      MeshQuad quad = new MeshQuad();
77 57a3f798 Leszek Koltunski
      quad.setComponentCenter(0,0,0,-0.1f);
78 698ad0a8 Leszek Koltunski
79 cb9d104d Leszek Koltunski
      mScreen = new DistortedScreen();
80 698ad0a8 Leszek Koltunski
      mScreen.attach(mLeaf, effects, quad );
81 babda153 Leszek Koltunski
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
82 e72c53e5 Leszek Koltunski
      mScreen.showFPS();
83 cb9d104d Leszek Koltunski
      }
84
85 c658e742 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87
   void setQuality(EffectQuality quality)
88
      {
89
      mGlow.setQuality(quality);
90
      }
91
92 cb9d104d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94 3a4f3ae2 Leszek Koltunski
   int setGlowRadius(int glow)
95 cb9d104d Leszek Koltunski
     {
96
     int radius = glow/2;
97 678c391d Leszek Koltunski
     mHaloRadius.set(radius*HALO_TO_RADIUS,radius);
98 cb9d104d Leszek Koltunski
     return radius;
99
     }
100
101 3a4f3ae2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103
   float setGlowAlpha(float glow)
104
     {
105
     float alpha = glow/100.0f;
106 bcbd5b45 Leszek Koltunski
     mColor.set3(alpha);
107 3a4f3ae2 Leszek Koltunski
     return alpha;
108
     }
109
110 cb9d104d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112
   public void onDrawFrame(GL10 glUnused)
113
     {
114
     mScreen.render(System.currentTimeMillis());
115
     }
116
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
    
119
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
120
     {
121 051f00eb Leszek Koltunski
     float factor = 0.8f* (Math.min(width, height));
122 cb9d104d Leszek Koltunski
     mScale.set( factor,factor,factor );
123
     mScreen.resize(width, height);
124
     }
125
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
129
     {
130
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
131
     Bitmap leaf;
132
      
133
     try
134
       {
135
       leaf = BitmapFactory.decodeStream(is);
136
       }
137
     finally
138
       {
139
       try
140
         {
141
         is.close();
142
         }
143 051f00eb Leszek Koltunski
       catch(IOException ignored) { }
144 cb9d104d Leszek Koltunski
       }
145
      
146
     mLeaf.setTexture(leaf);
147
148
     PostprocessEffectGlow.enable();
149
150 7c72e798 Leszek Koltunski
     DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
151 061449ed Leszek Koltunski
     }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
155
   public void distortedException(Exception ex)
156
     {
157
     android.util.Log.e("Glow", ex.getMessage() );
158 cb9d104d Leszek Koltunski
     }
159
}