Project

General

Profile

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

examples / src / main / java / org / distorted / examples / glow / GlowRenderer.java @ 061449ed

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.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25

    
26
import org.distorted.examples.R;
27
import org.distorted.library.effect.EffectQuality;
28
import org.distorted.library.effect.MatrixEffectScale;
29
import org.distorted.library.effect.PostprocessEffectGlow;
30
import org.distorted.library.main.DistortedLibrary;
31
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedScreen;
33
import org.distorted.library.main.DistortedTexture;
34
import org.distorted.library.mesh.MeshQuad;
35
import org.distorted.library.type.Static2D;
36
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
class GlowRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
48
{
49
   private static final float HALO_TO_RADIUS = 0.2f;
50

    
51
   private GLSurfaceView mView;
52
   private DistortedTexture mLeaf;
53
   private DistortedScreen mScreen;
54
   private PostprocessEffectGlow mGlow;
55
   private Static3D mScale;
56
   private Static2D mHaloRadius;
57
   private Static4D mColor;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
   GlowRenderer(GLSurfaceView v)
62
      {     
63
      mView = v;
64

    
65
      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

    
70
      mGlow  = new PostprocessEffectGlow(mHaloRadius,mColor);
71

    
72
      DistortedEffects effects = new DistortedEffects();
73
      effects.apply(new MatrixEffectScale(mScale));
74
      effects.apply(mGlow);
75

    
76
      MeshQuad quad = new MeshQuad();
77

    
78
      mScreen = new DistortedScreen();
79
      mScreen.attach(mLeaf, effects, quad );
80
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
81
      mScreen.showFPS();
82
      }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
   void setQuality(EffectQuality quality)
87
      {
88
      mGlow.setQuality(quality);
89
      }
90

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

    
93
   int setGlowRadius(int glow)
94
     {
95
     int radius = glow/2;
96
     mHaloRadius.set(radius*HALO_TO_RADIUS,radius);
97
     return radius;
98
     }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
   float setGlowAlpha(float glow)
103
     {
104
     float alpha = glow/100.0f;
105
     mColor.set3(alpha);
106
     return alpha;
107
     }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
   public void onDrawFrame(GL10 glUnused)
112
     {
113
     mScreen.render(System.currentTimeMillis());
114
     }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
    
118
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
119
     {
120
     float factor = 0.8f* (Math.min(width, height));
121
     mScale.set( factor,factor,factor );
122
     mScreen.resize(width, height);
123
     }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
    
127
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
128
     {
129
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
130
     Bitmap leaf;
131
      
132
     try
133
       {
134
       leaf = BitmapFactory.decodeStream(is);
135
       }
136
     finally
137
       {
138
       try
139
         {
140
         is.close();
141
         }
142
       catch(IOException ignored) { }
143
       }
144
      
145
     mLeaf.setTexture(leaf);
146

    
147
     PostprocessEffectGlow.enable();
148

    
149
     DistortedLibrary.onCreate(mView.getContext(), this);
150
     }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
   public void distortedException(Exception ex)
155
     {
156
     android.util.Log.e("Glow", ex.getMessage() );
157
     }
158
}
(2-2/3)