Project

General

Profile

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

examples / src / main / java / org / distorted / examples / glow / GlowRenderer.java @ dc10a48d

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// 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
// Distorted is distributed in the hope that it will be useful,                                  //
12
// 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
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.examples.glow;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28
import android.opengl.GLSurfaceView;
29

    
30
import org.distorted.examples.R;
31
import org.distorted.library.effect.EffectQuality;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.PostprocessEffectGlow;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedScreen;
37
import org.distorted.library.main.DistortedTexture;
38
import org.distorted.library.mesh.MeshQuad;
39
import org.distorted.library.type.Static2D;
40
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
class GlowRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
52
{
53
   private static final float HALO_TO_RADIUS = 0.2f;
54

    
55
   private final GLSurfaceView mView;
56
   private final Resources mResources;
57
   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

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
   GlowRenderer(GLSurfaceView v)
67
      {     
68
      mView = v;
69
      mResources = v.getResources();
70

    
71
      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

    
76
      mGlow  = new PostprocessEffectGlow(mHaloRadius,mColor);
77

    
78
      DistortedEffects effects = new DistortedEffects();
79
      effects.apply(new MatrixEffectScale(mScale));
80
      effects.apply(mGlow);
81

    
82
      MeshQuad quad = new MeshQuad();
83
      quad.setComponentCenter(0,0,0,-0.1f);
84

    
85
      mScreen = new DistortedScreen();
86
      mScreen.attach(mLeaf, effects, quad );
87
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
88
      mScreen.showFPS();
89
      }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
   void setQuality(EffectQuality quality)
94
      {
95
      mGlow.setQuality(quality);
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
   int setGlowRadius(int glow)
101
     {
102
     int radius = glow/2;
103
     mHaloRadius.set(radius*HALO_TO_RADIUS,radius);
104
     return radius;
105
     }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
   float setGlowAlpha(float glow)
110
     {
111
     float alpha = glow/100.0f;
112
     mColor.set3(alpha);
113
     return alpha;
114
     }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
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 factor = 0.8f* (Math.min(width, height));
128
     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
       catch(IOException ignored) { }
150
       }
151
      
152
     mLeaf.setTexture(leaf);
153

    
154
     PostprocessEffectGlow.enable();
155

    
156
     DistortedLibrary.onSurfaceCreated(this);
157
     }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
   public void distortedException(Exception ex)
162
     {
163
     android.util.Log.e("Glow", ex.getMessage() );
164
     }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
   public int openGlVersion()
169
      {
170
      Context context = mView.getContext();
171
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
172
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
173
      int glESversion = configurationInfo.reqGlEsVersion;
174
      int major = glESversion >> 16;
175
      int minor = glESversion & 0xff;
176

    
177
      return 100*major + 10*minor;
178
      }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
   public InputStream localFile(int fileID)
183
      {
184
      return mResources.openRawResource(fileID);
185
      }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
   public void logMessage(String message)
190
      {
191
      android.util.Log.e("Glow", message );
192
      }
193
}
(2-2/3)