Project

General

Profile

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

examples / src / main / java / org / distorted / examples / sink / SinkRenderer.java @ dc10a48d

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.sink;
21

    
22
import java.io.IOException;
23
import java.io.InputStream;
24

    
25
import javax.microedition.khronos.egl.EGLConfig;
26
import javax.microedition.khronos.opengles.GL10;
27

    
28
import org.distorted.examples.R;
29

    
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectSink;
32
import org.distorted.library.main.DistortedLibrary;
33
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.mesh.MeshSquare;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.type.Static1D;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40

    
41
import android.app.ActivityManager;
42
import android.content.Context;
43
import android.content.pm.ConfigurationInfo;
44
import android.content.res.Resources;
45
import android.graphics.Bitmap;
46
import android.graphics.BitmapFactory;
47
import android.opengl.GLSurfaceView;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class SinkRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
52
  {
53
  private final GLSurfaceView mView;
54
  private final Resources mResources;
55
  private final DistortedEffects mEffects;
56
  private final DistortedScreen mScreen;
57
  private final DistortedTexture mTexture;
58
  private final Static3D mScale;
59
  private final Static1D mSinkStrength;
60

    
61
  private float mBmpRatio;
62
  private MeshSquare mMesh;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  SinkRenderer(GLSurfaceView v)
67
    { 
68
    mView = v;
69
    mResources = v.getResources();
70

    
71
    mTexture = new DistortedTexture();
72
    mEffects = new DistortedEffects();
73
    mScreen  = new DistortedScreen();
74
    mSinkStrength = new Static1D(1.0f);
75

    
76
    VertexEffectSink sinkEffect = new VertexEffectSink(mSinkStrength, new Static3D(0,0,0), new Static4D(0,0,0,0.5f) );
77
    mEffects.apply(sinkEffect);
78

    
79
    mScale = new Static3D(1,1,1);
80
    mEffects.apply(new MatrixEffectScale(mScale));
81
    }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
// logarithmic: 50 -> 1.0, 40-> A^(-1), 30->A^(-2), 70-> A^2
85
// where A = 5 maybe?
86
// f(x) = A^((level-50)/10) = e^(logA*((level-50)/10))
87

    
88
  float setSink(int level)
89
    {
90
    final double logA = Math.log(5);
91
    final float exponent = (level-50)/10.0f;
92
    final float value = (float)Math.exp(logA*exponent);
93
    mSinkStrength.set(value);
94

    
95
    return value;
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  public void onDrawFrame(GL10 glUnused) 
101
    {
102
    mScreen.render( System.currentTimeMillis() );
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
    
107
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
108
    {
109
    if( width<height ) mScale.set( width,   width*mBmpRatio, 1 );
110
    else               mScale.set( height/mBmpRatio, height, 1 );
111

    
112
    mScreen.resize(width, height);
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
    
117
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
118
    {
119
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.gridlines);
120
    Bitmap bitmap;
121
        
122
    try 
123
      {
124
      bitmap = BitmapFactory.decodeStream(is);
125
      } 
126
    finally 
127
      {
128
      try 
129
        {
130
        is.close();
131
        } 
132
      catch(IOException ignored) { }
133
      }  
134
      
135
    mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
136

    
137
    mTexture.setTexture(bitmap);
138

    
139
    if( mMesh==null ) mMesh = new MeshSquare(30,(int)(30*mBmpRatio));
140

    
141
    mScreen.detachAll();
142
    mScreen.attach(mTexture,mEffects,mMesh);
143

    
144
    VertexEffectSink.enable();
145
    DistortedLibrary.onSurfaceCreated(this);
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  public void distortedException(Exception ex)
151
    {
152
    android.util.Log.e("Sink", ex.getMessage() );
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
  public int openGlVersion()
158
    {
159
    Context context = mView.getContext();
160
    final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
161
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
162
    int glESversion = configurationInfo.reqGlEsVersion;
163
    int major = glESversion >> 16;
164
    int minor = glESversion & 0xff;
165

    
166
    return 100*major + 10*minor;
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  public InputStream localFile(int fileID)
172
    {
173
    return mResources.openRawResource(fileID);
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  public void logMessage(String message)
179
    {
180
    android.util.Log.e("Sink", message );
181
    }
182
}
(2-2/3)