Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 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.EffectName;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectSink;
34 01782e85 Leszek Koltunski
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 b5cc7760 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
40
import org.distorted.library.type.Static1D;
41 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
42 427ab7bf Leszek Koltunski
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 bc0a685b Leszek Koltunski
  {
51
  private GLSurfaceView mView;
52 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
53 d218d64e leszek
  private DistortedScreen mScreen;
54 b0ebdf5e Leszek Koltunski
  private DistortedTexture mTexture;
55 5f2a53b6 leszek
  private MeshFlat mMesh;
56 bc0a685b Leszek Koltunski
  private int bmpHeight, bmpWidth;
57 c9ff05c1 leszek
  private Static3D mScale, mMove;
58
59 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 e7a4ef16 Leszek Koltunski
  SinkRenderer(GLSurfaceView v)
62 bc0a685b Leszek Koltunski
    { 
63
    mView = v;
64 f6d884d5 Leszek Koltunski
65 13155c68 Leszek Koltunski
    Dynamic1D sink = new Dynamic1D(2000,0.0f);
66
    sink.add(new Static1D(1.0f));
67
    sink.add(new Static1D(0.2f));
68 f6d884d5 Leszek Koltunski
69 d04a4886 Leszek Koltunski
    mEffects = new DistortedEffects();
70 c9ff05c1 leszek
    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 392e16fd Leszek Koltunski
78 e4330c89 Leszek Koltunski
    mScreen = new DistortedScreen();
79 bc0a685b Leszek Koltunski
    }
80 427ab7bf Leszek Koltunski
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
   
83 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused) 
84
    {
85 fe59d375 Leszek Koltunski
    mScreen.render( System.currentTimeMillis() );
86 bc0a685b Leszek Koltunski
    }
87 427ab7bf Leszek Koltunski
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
    
90 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
91 630703d1 leszek
    { 
92
    if( (float)bmpHeight/bmpWidth > (float)height/width )
93
      {
94
      int w = (height*bmpWidth)/bmpHeight;
95
      float factor = (float)height/bmpHeight;
96 c9ff05c1 leszek
      mMove.set((width-w)/2,0,0);
97
      mScale.set(factor,factor,factor);
98 630703d1 leszek
      }
99
    else
100
      {
101
      int h = (width*bmpHeight)/bmpWidth;
102
      float factor = (float)width/bmpWidth;
103 c9ff05c1 leszek
      mMove.set(0,(height-h)/2,0);
104
      mScale.set(factor,factor,factor);
105 630703d1 leszek
      }
106
      
107 392e16fd Leszek Koltunski
    mScreen.resize(width, height);
108 bc0a685b Leszek Koltunski
    }
109 427ab7bf Leszek Koltunski
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
    
112 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
113
    {
114 13155c68 Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
115 bc0a685b Leszek Koltunski
    Bitmap bitmap;
116 427ab7bf Leszek Koltunski
        
117 bc0a685b Leszek Koltunski
    try 
118
      {
119
      bitmap = BitmapFactory.decodeStream(is);
120
      } 
121
    finally 
122
      {
123 427ab7bf Leszek Koltunski
      try 
124
        {
125 bc0a685b Leszek Koltunski
        is.close();
126 427ab7bf Leszek Koltunski
        } 
127 bc0a685b Leszek Koltunski
      catch(IOException e) { }
128
      }  
129 427ab7bf Leszek Koltunski
      
130 bc0a685b Leszek Koltunski
    bmpHeight = bitmap.getHeight();
131
    bmpWidth  = bitmap.getWidth();
132 b5cc7760 Leszek Koltunski
133 b0ebdf5e Leszek Koltunski
    if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
134
    mTexture.setTexture(bitmap);
135 5f2a53b6 leszek
    if( mMesh==null ) mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
136 fe59d375 Leszek Koltunski
137
    mScreen.detachAll();
138 5f2a53b6 leszek
    mScreen.attach(mTexture,mEffects,mMesh);
139 b5cc7760 Leszek Koltunski
140 c9ff05c1 leszek
    DistortedEffects.enableEffect(EffectName.SINK);
141 6637d0f2 Leszek Koltunski
142 bc0a685b Leszek Koltunski
    try
143
      {
144 76f9798b Leszek Koltunski
      Distorted.onCreate(mView.getContext());
145 bc0a685b Leszek Koltunski
      }
146
    catch(Exception ex)
147
      {
148
      android.util.Log.e("Sink", ex.getMessage() );
149 427ab7bf Leszek Koltunski
      }
150 bc0a685b Leszek Koltunski
    }
151
  }