Project

General

Profile

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

examples / src / main / java / org / distorted / examples / sink / SinkRenderer.java @ 13fde129

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
import android.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43
import android.opengl.GLSurfaceView;
44
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
47 061449ed Leszek Koltunski
class SinkRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
48 bc0a685b Leszek Koltunski
  {
49 13fde129 Leszek Koltunski
  private final GLSurfaceView mView;
50
  private final DistortedEffects mEffects;
51
  private final DistortedScreen mScreen;
52
  private final DistortedTexture mTexture;
53
  private final Static3D mScale;
54
  private final Static1D mSinkStrength;
55
56 f209a803 Leszek Koltunski
  private float mBmpRatio;
57 13fde129 Leszek Koltunski
  private MeshSquare mMesh;
58 c9ff05c1 leszek
59 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 e7a4ef16 Leszek Koltunski
  SinkRenderer(GLSurfaceView v)
62 bc0a685b Leszek Koltunski
    { 
63 4c42bc15 Leszek Koltunski
    mView    = v;
64
    mTexture = new DistortedTexture();
65
    mEffects = new DistortedEffects();
66
    mScreen  = new DistortedScreen();
67 13fde129 Leszek Koltunski
    mSinkStrength = new Static1D(1.0f);
68 f6d884d5 Leszek Koltunski
69 13fde129 Leszek Koltunski
    VertexEffectSink sinkEffect = new VertexEffectSink(mSinkStrength, new Static3D(0,0,0), new Static4D(0,0,0,0.5f) );
70 c9ff05c1 leszek
    mEffects.apply(sinkEffect);
71
72
    mScale = new Static3D(1,1,1);
73
    mEffects.apply(new MatrixEffectScale(mScale));
74 bc0a685b Leszek Koltunski
    }
75 427ab7bf Leszek Koltunski
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77 13fde129 Leszek Koltunski
// logarithmic: 50 -> 1.0, 40-> A^(-1), 30->A^(-2), 70-> A^2
78
// where A = 5 maybe?
79
// f(x) = A^((level-50)/10) = e^(logA*((level-50)/10))
80
81
  float setSink(int level)
82
    {
83
    final double logA = Math.log(5);
84
    final float exponent = (level-50)/10.0f;
85
    final float value = (float)Math.exp(logA*exponent);
86
    mSinkStrength.set(value);
87
88
    return value;
89
    }
90
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
93 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused) 
94
    {
95 fe59d375 Leszek Koltunski
    mScreen.render( System.currentTimeMillis() );
96 bc0a685b Leszek Koltunski
    }
97 427ab7bf Leszek Koltunski
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
    
100 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
101 d381aa80 Leszek Koltunski
    {
102 c7a31368 Leszek Koltunski
    if( width<height ) mScale.set( width,   width*mBmpRatio, 1 );
103
    else               mScale.set( height/mBmpRatio, height, 1 );
104 d381aa80 Leszek Koltunski
105 392e16fd Leszek Koltunski
    mScreen.resize(width, height);
106 bc0a685b Leszek Koltunski
    }
107 427ab7bf Leszek Koltunski
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
111
    {
112 13fde129 Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.gridlines);
113 bc0a685b Leszek Koltunski
    Bitmap bitmap;
114 427ab7bf Leszek Koltunski
        
115 bc0a685b Leszek Koltunski
    try 
116
      {
117
      bitmap = BitmapFactory.decodeStream(is);
118
      } 
119
    finally 
120
      {
121 427ab7bf Leszek Koltunski
      try 
122
        {
123 bc0a685b Leszek Koltunski
        is.close();
124 427ab7bf Leszek Koltunski
        } 
125 13fde129 Leszek Koltunski
      catch(IOException ignored) { }
126 bc0a685b Leszek Koltunski
      }  
127 427ab7bf Leszek Koltunski
      
128 f209a803 Leszek Koltunski
    mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
129 b5cc7760 Leszek Koltunski
130 b0ebdf5e Leszek Koltunski
    mTexture.setTexture(bitmap);
131 26c4e181 Leszek Koltunski
132 f2085b96 Leszek Koltunski
    if( mMesh==null ) mMesh = new MeshSquare(30,(int)(30*mBmpRatio));
133 698ad0a8 Leszek Koltunski
134 fe59d375 Leszek Koltunski
    mScreen.detachAll();
135 5f2a53b6 leszek
    mScreen.attach(mTexture,mEffects,mMesh);
136 b5cc7760 Leszek Koltunski
137 885b9cca leszek
    VertexEffectSink.enable();
138 7c72e798 Leszek Koltunski
    DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
139 061449ed Leszek Koltunski
    }
140 6637d0f2 Leszek Koltunski
141 061449ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
142
143
  public void distortedException(Exception ex)
144
    {
145
    android.util.Log.e("Sink", ex.getMessage() );
146 bc0a685b Leszek Koltunski
    }
147
  }