Project

General

Profile

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

examples / src / main / java / org / distorted / examples / glow / GlowRenderer.java @ 16b22aab

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.Distorted;
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.MeshFlat;
35
import org.distorted.library.type.Static1D;
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

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

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

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

    
73
      mGlow  = new PostprocessEffectGlow(mRadius,mColor);
74

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

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

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

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

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
   int setGlowRadius(int glow)
95
     {
96
     int radius = glow/2;
97
     mRadius.set(radius);
98
     return radius;
99
     }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
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
     float qw = (float)width /mRootW;
122
     float qh = (float)height/mRootH;
123
     float factor = 0.8f* (qw<qh ? qw:qh);
124

    
125
     mScale.set( factor,factor,factor );
126
     mScreen.resize(width, height);
127
     }
128

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

    
151
     PostprocessEffectGlow.enable();
152

    
153
     try
154
       {
155
       Distorted.onCreate(mView.getContext());
156
       }
157
     catch(Exception ex)
158
       {
159
       android.util.Log.e("Glow", ex.getMessage() );
160
       }
161
     }
162
}
(2-2/3)