Project

General

Profile

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

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

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