Project

General

Profile

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

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

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.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43
import android.opengl.GLSurfaceView;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
class SinkRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
48
  {
49
  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
  private float mBmpRatio;
57
  private MeshSquare mMesh;
58

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

    
61
  SinkRenderer(GLSurfaceView v)
62
    { 
63
    mView    = v;
64
    mTexture = new DistortedTexture();
65
    mEffects = new DistortedEffects();
66
    mScreen  = new DistortedScreen();
67
    mSinkStrength = new Static1D(1.0f);
68

    
69
    VertexEffectSink sinkEffect = new VertexEffectSink(mSinkStrength, new Static3D(0,0,0), new Static4D(0,0,0,0.5f) );
70
    mEffects.apply(sinkEffect);
71

    
72
    mScale = new Static3D(1,1,1);
73
    mEffects.apply(new MatrixEffectScale(mScale));
74
    }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
// 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
  public void onDrawFrame(GL10 glUnused) 
94
    {
95
    mScreen.render( System.currentTimeMillis() );
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
    
100
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
101
    {
102
    if( width<height ) mScale.set( width,   width*mBmpRatio, 1 );
103
    else               mScale.set( height/mBmpRatio, height, 1 );
104

    
105
    mScreen.resize(width, height);
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
111
    {
112
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.gridlines);
113
    Bitmap bitmap;
114
        
115
    try 
116
      {
117
      bitmap = BitmapFactory.decodeStream(is);
118
      } 
119
    finally 
120
      {
121
      try 
122
        {
123
        is.close();
124
        } 
125
      catch(IOException ignored) { }
126
      }  
127
      
128
    mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
129

    
130
    mTexture.setTexture(bitmap);
131

    
132
    if( mMesh==null ) mMesh = new MeshSquare(30,(int)(30*mBmpRatio));
133

    
134
    mScreen.detachAll();
135
    mScreen.attach(mTexture,mEffects,mMesh);
136

    
137
    VertexEffectSink.enable();
138
    DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
  public void distortedException(Exception ex)
144
    {
145
    android.util.Log.e("Sink", ex.getMessage() );
146
    }
147
  }
(2-2/3)