Project

General

Profile

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

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

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.EffectName;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectSink;
34
import org.distorted.library.main.Distorted;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedScreen;
37
import org.distorted.library.main.MeshFlat;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.type.Dynamic1D;
40
import org.distorted.library.type.Static1D;
41
import org.distorted.library.type.Static3D;
42

    
43
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
45
import android.opengl.GLSurfaceView;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
class SinkRenderer implements GLSurfaceView.Renderer 
50
  {
51
  private GLSurfaceView mView;
52
  private DistortedEffects mEffects;
53
  private DistortedScreen mScreen;
54
  private DistortedTexture mTexture;
55
  private MeshFlat mMesh;
56
  private int bmpHeight, bmpWidth;
57
  private Static3D mScale, mMove;
58

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

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

    
65
    Dynamic1D sink = new Dynamic1D(2000,0.0f);
66
    sink.add(new Static1D(1.0f));
67
    sink.add(new Static1D(0.2f));
68

    
69
    mEffects = new DistortedEffects();
70
    VertexEffectSink sinkEffect = new VertexEffectSink(sink, new Static3D(297, 320, 0), null);
71
    mEffects.apply(sinkEffect);
72

    
73
    mScale = new Static3D(1,1,1);
74
    mMove  = new Static3D(0,0,0);
75
    mEffects.apply(new MatrixEffectMove(mMove));
76
    mEffects.apply(new MatrixEffectScale(mScale));
77

    
78
    mScreen = new DistortedScreen();
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
   
83
  public void onDrawFrame(GL10 glUnused) 
84
    {
85
    mScreen.render( System.currentTimeMillis() );
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
    
90
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
91
    { 
92
    if( (float)bmpHeight/bmpWidth > (float)height/width )
93
      {
94
      int w = (height*bmpWidth)/bmpHeight;
95
      float factor = (float)height/bmpHeight;
96
      mMove.set((width-w)/2,0,0);
97
      mScale.set(factor,factor,factor);
98
      }
99
    else
100
      {
101
      int h = (width*bmpHeight)/bmpWidth;
102
      float factor = (float)width/bmpWidth;
103
      mMove.set(0,(height-h)/2,0);
104
      mScale.set(factor,factor,factor);
105
      }
106
      
107
    mScreen.resize(width, height);
108
    }
109

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

    
133
    if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
134
    mTexture.setTexture(bitmap);
135
    if( mMesh==null ) mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
136

    
137
    mScreen.detachAll();
138
    mScreen.attach(mTexture,mEffects,mMesh);
139

    
140
    DistortedEffects.enableEffect(EffectName.SINK);
141

    
142
    try
143
      {
144
      Distorted.onCreate(mView.getContext());
145
      }
146
    catch(Exception ex)
147
      {
148
      android.util.Log.e("Sink", ex.getMessage() );
149
      }
150
    }
151
  }
(2-2/3)