Project

General

Profile

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

examples / src / main / java / org / distorted / examples / glow / GlowRenderer.java @ 678c391d

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
48
{
49
   private static final int LEAF_SIZE = 100;
50
   private static final float HALO_TO_RADIUS = 0.2f;
51

    
52
   private GLSurfaceView mView;
53
   private DistortedTexture mLeaf;
54
   private DistortedScreen mScreen;
55
   private PostprocessEffectGlow mGlow;
56
   private int mRootW, mRootH;
57
   private Static3D mScale;
58
   private Static2D mHaloRadius;
59
   private Static4D mColor;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
   GlowRenderer(GLSurfaceView v)
64
      {     
65
      mView = v;
66

    
67
      mRootW     = LEAF_SIZE;
68
      mRootH     = LEAF_SIZE;
69
      mLeaf      = new DistortedTexture();
70
      mScale     = new Static3D(1,1,1);
71
      mHaloRadius= new Static2D(25*HALO_TO_RADIUS,25);
72
      mColor     = new Static4D(1.0f,0.0f,0.0f,0.5f); // half-transparent red
73

    
74
      mGlow  = new PostprocessEffectGlow(mHaloRadius,mColor);
75

    
76
      DistortedEffects effects = new DistortedEffects();
77
      effects.apply(new MatrixEffectScale(mScale));
78
      effects.apply(mGlow);
79

    
80
      MeshQuad quad = new MeshQuad();
81
      quad.setStretch(mRootW,mRootH,0);
82

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

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
   void setQuality(EffectQuality quality)
92
      {
93
      mGlow.setQuality(quality);
94
      }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

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

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

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

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
   public void onDrawFrame(GL10 glUnused)
117
     {
118
     mScreen.render(System.currentTimeMillis());
119
     }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
    
123
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
124
     {
125
     float qw = (float)width /mRootW;
126
     float qh = (float)height/mRootH;
127
     float factor = 0.8f* (qw<qh ? qw:qh);
128

    
129
     mScale.set( factor,factor,factor );
130
     mScreen.resize(width, height);
131
     }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134
    
135
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
136
     {
137
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
138
     Bitmap leaf;
139
      
140
     try
141
       {
142
       leaf = BitmapFactory.decodeStream(is);
143
       }
144
     finally
145
       {
146
       try
147
         {
148
         is.close();
149
         }
150
       catch(IOException e) { }
151
       }
152
      
153
     mLeaf.setTexture(leaf);
154

    
155
     PostprocessEffectGlow.enable();
156

    
157
     try
158
       {
159
       DistortedLibrary.onCreate(mView.getContext());
160
       }
161
     catch(Exception ex)
162
       {
163
       android.util.Log.e("Glow", ex.getMessage() );
164
       }
165
     }
166
}
(2-2/3)