Project

General

Profile

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

examples / src / main / java / org / distorted / examples / glow / GlowRenderer.java @ 77e66c58

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 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26 cb9d104d Leszek Koltunski
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28
import android.opengl.GLSurfaceView;
29
30
import org.distorted.examples.R;
31 3fee789f leszek
import org.distorted.library.effect.EffectQuality;
32 cb9d104d Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.PostprocessEffectGlow;
34 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
35 cb9d104d Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedScreen;
37
import org.distorted.library.main.DistortedTexture;
38 698ad0a8 Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
39 678c391d Leszek Koltunski
import org.distorted.library.type.Static2D;
40 cb9d104d Leszek Koltunski
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42
43
import java.io.IOException;
44
import java.io.InputStream;
45
46
import javax.microedition.khronos.egl.EGLConfig;
47
import javax.microedition.khronos.opengles.GL10;
48
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51 dc10a48d Leszek Koltunski
class GlowRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
52 cb9d104d Leszek Koltunski
{
53 678c391d Leszek Koltunski
   private static final float HALO_TO_RADIUS = 0.2f;
54 cb9d104d Leszek Koltunski
55 32e96fff Leszek Koltunski
   private final GLSurfaceView mView;
56 dc10a48d Leszek Koltunski
   private final Resources mResources;
57 32e96fff Leszek Koltunski
   private final DistortedTexture mLeaf;
58
   private final DistortedScreen mScreen;
59
   private final PostprocessEffectGlow mGlow;
60
   private final Static3D mScale;
61
   private final Static2D mHaloRadius;
62
   private final Static4D mColor;
63 cb9d104d Leszek Koltunski
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66
   GlowRenderer(GLSurfaceView v)
67
      {     
68
      mView = v;
69 dc10a48d Leszek Koltunski
      mResources = v.getResources();
70 cb9d104d Leszek Koltunski
71 678c391d Leszek Koltunski
      mLeaf      = new DistortedTexture();
72
      mScale     = new Static3D(1,1,1);
73
      mHaloRadius= new Static2D(25*HALO_TO_RADIUS,25);
74
      mColor     = new Static4D(1.0f,0.0f,0.0f,0.5f); // half-transparent red
75 cb9d104d Leszek Koltunski
76 678c391d Leszek Koltunski
      mGlow  = new PostprocessEffectGlow(mHaloRadius,mColor);
77 3fee789f leszek
78 698ad0a8 Leszek Koltunski
      DistortedEffects effects = new DistortedEffects();
79 cb9d104d Leszek Koltunski
      effects.apply(new MatrixEffectScale(mScale));
80 c658e742 Leszek Koltunski
      effects.apply(mGlow);
81 cb9d104d Leszek Koltunski
82 698ad0a8 Leszek Koltunski
      MeshQuad quad = new MeshQuad();
83 57a3f798 Leszek Koltunski
      quad.setComponentCenter(0,0,0,-0.1f);
84 698ad0a8 Leszek Koltunski
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 678c391d Leszek Koltunski
     mHaloRadius.set(radius*HALO_TO_RADIUS,radius);
104 cb9d104d Leszek Koltunski
     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 051f00eb Leszek Koltunski
     float factor = 0.8f* (Math.min(width, height));
128 cb9d104d Leszek Koltunski
     mScale.set( factor,factor,factor );
129
     mScreen.resize(width, height);
130
     }
131
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
    
134
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
135
     {
136
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
137
     Bitmap leaf;
138
      
139
     try
140
       {
141
       leaf = BitmapFactory.decodeStream(is);
142
       }
143
     finally
144
       {
145
       try
146
         {
147
         is.close();
148
         }
149 051f00eb Leszek Koltunski
       catch(IOException ignored) { }
150 cb9d104d Leszek Koltunski
       }
151
      
152
     mLeaf.setTexture(leaf);
153
154
     PostprocessEffectGlow.enable();
155
156 dc10a48d Leszek Koltunski
     DistortedLibrary.onSurfaceCreated(this);
157 061449ed Leszek Koltunski
     }
158
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161
   public void distortedException(Exception ex)
162
     {
163
     android.util.Log.e("Glow", ex.getMessage() );
164 cb9d104d Leszek Koltunski
     }
165 dc10a48d Leszek Koltunski
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
168
   public InputStream localFile(int fileID)
169
      {
170
      return mResources.openRawResource(fileID);
171
      }
172
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
175
   public void logMessage(String message)
176
      {
177
      android.util.Log.e("Glow", message );
178
      }
179 cb9d104d Leszek Koltunski
}