Project

General

Profile

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

examples / src / main / java / org / distorted / examples / glow / GlowRenderer.java @ 3fee789f

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.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.effect.PostprocessEffectGlow;
31
import org.distorted.library.main.Distorted;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.DistortedTexture;
35
import org.distorted.library.main.MeshFlat;
36
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39

    
40
import java.io.IOException;
41
import java.io.InputStream;
42

    
43
import javax.microedition.khronos.egl.EGLConfig;
44
import javax.microedition.khronos.opengles.GL10;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
class GlowRenderer implements GLSurfaceView.Renderer
49
{
50
   private static final int LEAF_SIZE = 100;
51

    
52
   private GLSurfaceView mView;
53
   private DistortedTexture mLeaf;
54
   private DistortedScreen mScreen;
55
   private int mRootW, mRootH;
56
   private Static3D mMove, 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(LEAF_SIZE,LEAF_SIZE);
69
      mMove  = new Static3D(0,0,0);
70
      mScale = new Static3D(1,1,1);
71
      mRadius= new Static1D(25);
72
      mColor = new Static4D(1.0f,0.0f,0.0f,0.5f); // half-transparent red
73

    
74
      PostprocessEffectGlow glow = new PostprocessEffectGlow(mRadius,mColor);
75
      glow.setQuality(EffectQuality.HIGH);
76

    
77
      DistortedEffects effects = new DistortedEffects();
78
      effects.apply(new MatrixEffectMove(mMove));
79
      effects.apply(new MatrixEffectScale(mScale));
80
      effects.apply(glow);
81

    
82
      mScreen = new DistortedScreen();
83
      mScreen.attach(mLeaf, effects, new MeshFlat(1,1) );
84
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
85
      }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
   int setGlowRadius(int glow)
90
     {
91
     int radius = glow/2;
92
     mRadius.set(radius);
93
     return radius;
94
     }
95

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

    
98
   float setGlowAlpha(float glow)
99
     {
100
     float alpha = glow/100.0f;
101
     mColor.set4(alpha);
102
     return alpha;
103
     }
104

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

    
107
   public void onDrawFrame(GL10 glUnused)
108
     {
109
     mScreen.render(System.currentTimeMillis());
110
     }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
115
     {
116
     float qw = (float)width /mRootW;
117
     float qh = (float)height/mRootH;
118
     float factor = 0.5f* (qw<qh ? qw:qh);
119
     int w = (int)(factor*mRootW);
120
     int h = (int)(factor*mRootH);
121

    
122
     mMove.set((width-w)/2 ,(height-h)/2, 0);
123
     mScale.set( factor,factor,factor );
124
     mScreen.resize(width, height);
125
     }
126

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

    
149
     PostprocessEffectGlow.enable();
150

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