Project

General

Profile

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

examples / src / main / java / org / distorted / examples / sink / SinkRenderer.java @ 30f337e5

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.sink;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
29 427ab7bf Leszek Koltunski
30 c9ff05c1 leszek
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectSink;
32 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
33 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedScreen;
35 f2085b96 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
36 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
37 b5cc7760 Leszek Koltunski
import org.distorted.library.type.Static1D;
38 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
39 513b2e9c Leszek Koltunski
import org.distorted.library.type.Static4D;
40 427ab7bf Leszek Koltunski
41 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
42
import android.content.Context;
43
import android.content.pm.ConfigurationInfo;
44
import android.content.res.Resources;
45 427ab7bf Leszek Koltunski
import android.graphics.Bitmap;
46
import android.graphics.BitmapFactory;
47
import android.opengl.GLSurfaceView;
48
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51 dc10a48d Leszek Koltunski
class SinkRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
52 bc0a685b Leszek Koltunski
  {
53 13fde129 Leszek Koltunski
  private final GLSurfaceView mView;
54 dc10a48d Leszek Koltunski
  private final Resources mResources;
55 13fde129 Leszek Koltunski
  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 f209a803 Leszek Koltunski
  private float mBmpRatio;
62 13fde129 Leszek Koltunski
  private MeshSquare mMesh;
63 c9ff05c1 leszek
64 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66 e7a4ef16 Leszek Koltunski
  SinkRenderer(GLSurfaceView v)
67 bc0a685b Leszek Koltunski
    { 
68 dc10a48d Leszek Koltunski
    mView = v;
69
    mResources = v.getResources();
70
71 4c42bc15 Leszek Koltunski
    mTexture = new DistortedTexture();
72
    mEffects = new DistortedEffects();
73
    mScreen  = new DistortedScreen();
74 13fde129 Leszek Koltunski
    mSinkStrength = new Static1D(1.0f);
75 f6d884d5 Leszek Koltunski
76 13fde129 Leszek Koltunski
    VertexEffectSink sinkEffect = new VertexEffectSink(mSinkStrength, new Static3D(0,0,0), new Static4D(0,0,0,0.5f) );
77 c9ff05c1 leszek
    mEffects.apply(sinkEffect);
78
79
    mScale = new Static3D(1,1,1);
80
    mEffects.apply(new MatrixEffectScale(mScale));
81 bc0a685b Leszek Koltunski
    }
82 427ab7bf Leszek Koltunski
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84 13fde129 Leszek Koltunski
// 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 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused) 
101
    {
102 fe59d375 Leszek Koltunski
    mScreen.render( System.currentTimeMillis() );
103 bc0a685b Leszek Koltunski
    }
104 427ab7bf Leszek Koltunski
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
    
107 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
108 d381aa80 Leszek Koltunski
    {
109 c7a31368 Leszek Koltunski
    if( width<height ) mScale.set( width,   width*mBmpRatio, 1 );
110
    else               mScale.set( height/mBmpRatio, height, 1 );
111 d381aa80 Leszek Koltunski
112 392e16fd Leszek Koltunski
    mScreen.resize(width, height);
113 bc0a685b Leszek Koltunski
    }
114 427ab7bf Leszek Koltunski
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
    
117 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
118
    {
119 13fde129 Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.gridlines);
120 bc0a685b Leszek Koltunski
    Bitmap bitmap;
121 427ab7bf Leszek Koltunski
        
122 bc0a685b Leszek Koltunski
    try 
123
      {
124
      bitmap = BitmapFactory.decodeStream(is);
125
      } 
126
    finally 
127
      {
128 427ab7bf Leszek Koltunski
      try 
129
        {
130 bc0a685b Leszek Koltunski
        is.close();
131 427ab7bf Leszek Koltunski
        } 
132 13fde129 Leszek Koltunski
      catch(IOException ignored) { }
133 bc0a685b Leszek Koltunski
      }  
134 427ab7bf Leszek Koltunski
      
135 f209a803 Leszek Koltunski
    mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
136 b5cc7760 Leszek Koltunski
137 b0ebdf5e Leszek Koltunski
    mTexture.setTexture(bitmap);
138 26c4e181 Leszek Koltunski
139 f2085b96 Leszek Koltunski
    if( mMesh==null ) mMesh = new MeshSquare(30,(int)(30*mBmpRatio));
140 698ad0a8 Leszek Koltunski
141 fe59d375 Leszek Koltunski
    mScreen.detachAll();
142 5f2a53b6 leszek
    mScreen.attach(mTexture,mEffects,mMesh);
143 b5cc7760 Leszek Koltunski
144 885b9cca leszek
    VertexEffectSink.enable();
145 dc10a48d Leszek Koltunski
    DistortedLibrary.onSurfaceCreated(this);
146 061449ed Leszek Koltunski
    }
147 6637d0f2 Leszek Koltunski
148 061449ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
149
150
  public void distortedException(Exception ex)
151
    {
152
    android.util.Log.e("Sink", ex.getMessage() );
153 bc0a685b Leszek Koltunski
    }
154 dc10a48d Leszek Koltunski
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
157
  public InputStream localFile(int fileID)
158
    {
159
    return mResources.openRawResource(fileID);
160
    }
161
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
164
  public void logMessage(String message)
165
    {
166
    android.util.Log.e("Sink", message );
167
    }
168
}