Project

General

Profile

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

examples / src / main / java / org / distorted / examples / sink / SinkRenderer.java @ 107e4b72

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

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

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

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

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

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

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

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

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

    
77
    mScreen = new DistortedScreen();
78
    }
79

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

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

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

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

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

    
139
    VertexEffectSink.enable();
140

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