Project

General

Profile

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

examples / src / main / java / org / distorted / examples / glow / GlowRenderer.java @ 463f1f9c

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.MatrixEffectMove;
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.main.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 int mRootW, mRootH;
55
   private Static3D mMove, mScale;
56
   private Static1D mRadius;
57
   private Static4D mColor;
58

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

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

    
65
      mRootW = LEAF_SIZE;
66
      mRootH = LEAF_SIZE;
67
      mLeaf  = new DistortedTexture(LEAF_SIZE,LEAF_SIZE);
68
      mMove  = new Static3D(0,0,0);
69
      mScale = new Static3D(1,1,1);
70
      mRadius= new Static1D(25);
71

    
72
      mColor = new Static4D(1.0f,0.0f,0.0f,0.5f); // half-transparent red
73

    
74
      DistortedEffects effects = new DistortedEffects();
75
      effects.apply(new MatrixEffectMove(mMove));
76
      effects.apply(new MatrixEffectScale(mScale));
77
      effects.apply(new PostprocessEffectGlow(mRadius,mColor));
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
      }
83

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

    
86
   int setGlowRadius(int glow)
87
     {
88
     int radius = glow/2;
89
     mRadius.set(radius);
90
     return radius;
91
     }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
   float setGlowAlpha(float glow)
96
     {
97
     float alpha = glow/100.0f;
98
     mColor.set4(alpha);
99
     return alpha;
100
     }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
   public void onDrawFrame(GL10 glUnused)
105
     {
106
     mScreen.render(System.currentTimeMillis());
107
     }
108

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

    
119
     mMove.set((width-w)/2 ,(height-h)/2, 0);
120
     mScale.set( factor,factor,factor );
121
     mScreen.resize(width, height);
122
     }
123

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

    
146
     PostprocessEffectGlow.enable();
147

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