Project

General

Profile

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

examples / src / main / java / org / distorted / examples / sink / SinkRenderer.java @ 26c4e181

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectSink;
32 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
33 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedScreen;
35 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
36 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
37 b5cc7760 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
38
import org.distorted.library.type.Static1D;
39 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
40 427ab7bf Leszek Koltunski
41
import android.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43
import android.opengl.GLSurfaceView;
44
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
47
class SinkRenderer implements GLSurfaceView.Renderer 
48 bc0a685b Leszek Koltunski
  {
49
  private GLSurfaceView mView;
50 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
51 d218d64e leszek
  private DistortedScreen mScreen;
52 b0ebdf5e Leszek Koltunski
  private DistortedTexture mTexture;
53 f3ca895f Leszek Koltunski
  private MeshRectangles mMesh;
54 d381aa80 Leszek Koltunski
  private Static3D mScale;
55 c9ff05c1 leszek
56 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58 e7a4ef16 Leszek Koltunski
  SinkRenderer(GLSurfaceView v)
59 bc0a685b Leszek Koltunski
    { 
60
    mView = v;
61 f6d884d5 Leszek Koltunski
62 13155c68 Leszek Koltunski
    Dynamic1D sink = new Dynamic1D(2000,0.0f);
63
    sink.add(new Static1D(1.0f));
64
    sink.add(new Static1D(0.2f));
65 f6d884d5 Leszek Koltunski
66 698ad0a8 Leszek Koltunski
    mEffects = new DistortedEffects();
67 26c4e181 Leszek Koltunski
    VertexEffectSink sinkEffect = new VertexEffectSink(sink, new Static3D(0,0,0), null);
68 c9ff05c1 leszek
    mEffects.apply(sinkEffect);
69
70
    mScale = new Static3D(1,1,1);
71
    mEffects.apply(new MatrixEffectScale(mScale));
72 392e16fd Leszek Koltunski
73 e4330c89 Leszek Koltunski
    mScreen = new DistortedScreen();
74 bc0a685b Leszek Koltunski
    }
75 427ab7bf Leszek Koltunski
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
   
78 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused) 
79
    {
80 fe59d375 Leszek Koltunski
    mScreen.render( System.currentTimeMillis() );
81 bc0a685b Leszek Koltunski
    }
82 427ab7bf Leszek Koltunski
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
    
85 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
86 d381aa80 Leszek Koltunski
    {
87
    float horiRatio = (float)width / mTexture.getWidth();
88
    float vertRatio = (float)height/ mTexture.getHeight();
89
    float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
90
91
    mScale.set( factor,factor,factor );
92 392e16fd Leszek Koltunski
    mScreen.resize(width, height);
93 bc0a685b Leszek Koltunski
    }
94 427ab7bf Leszek Koltunski
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
    
97 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
98
    {
99 13155c68 Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
100 bc0a685b Leszek Koltunski
    Bitmap bitmap;
101 427ab7bf Leszek Koltunski
        
102 bc0a685b Leszek Koltunski
    try 
103
      {
104
      bitmap = BitmapFactory.decodeStream(is);
105
      } 
106
    finally 
107
      {
108 427ab7bf Leszek Koltunski
      try 
109
        {
110 bc0a685b Leszek Koltunski
        is.close();
111 427ab7bf Leszek Koltunski
        } 
112 bc0a685b Leszek Koltunski
      catch(IOException e) { }
113
      }  
114 427ab7bf Leszek Koltunski
      
115 d381aa80 Leszek Koltunski
    int bmpHeight = bitmap.getHeight();
116
    int bmpWidth  = bitmap.getWidth();
117 b5cc7760 Leszek Koltunski
118 687263cc Leszek Koltunski
    if( mTexture==null ) mTexture = new DistortedTexture();
119 b0ebdf5e Leszek Koltunski
    mTexture.setTexture(bitmap);
120 26c4e181 Leszek Koltunski
121 698ad0a8 Leszek Koltunski
    if( mMesh==null )
122
      {
123
      mMesh = new MeshRectangles(30,30*bmpHeight/bmpWidth);
124
      mMesh.setStretch(bmpWidth,bmpHeight,0);
125
      }
126
127 fe59d375 Leszek Koltunski
    mScreen.detachAll();
128 5f2a53b6 leszek
    mScreen.attach(mTexture,mEffects,mMesh);
129 b5cc7760 Leszek Koltunski
130 885b9cca leszek
    VertexEffectSink.enable();
131 6637d0f2 Leszek Koltunski
132 bc0a685b Leszek Koltunski
    try
133
      {
134 e3900503 Leszek Koltunski
      DistortedLibrary.onCreate(mView.getContext());
135 bc0a685b Leszek Koltunski
      }
136
    catch(Exception ex)
137
      {
138
      android.util.Log.e("Sink", ex.getMessage() );
139 427ab7bf Leszek Koltunski
      }
140 bc0a685b Leszek Koltunski
    }
141
  }