Project

General

Profile

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

examples / src / main / java / org / distorted / examples / bean / BeanRenderer.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.bean;
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 fa9b6494 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectDistort;
32 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedScreen;
34 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
35 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
36 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
37 b5cc7760 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
38 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40 427ab7bf Leszek Koltunski
41
import android.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43
import android.opengl.GLSurfaceView;
44
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
47
class BeanRenderer implements GLSurfaceView.Renderer 
48
{
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 26c4e181 Leszek Koltunski
   private Static3D mScale, mBrowL, mBrowR;
55 fa9b6494 Leszek Koltunski
56 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58 b56faf50 Leszek Koltunski
   BeanRenderer(GLSurfaceView v)
59 427ab7bf Leszek Koltunski
      {
60
      mView = v;
61
     
62 26c4e181 Leszek Koltunski
63 a4d59c0b Leszek Koltunski
      Static4D regionLeft = new Static4D( -3, 33, 0, 47);
64
      Static4D regionRight= new Static4D(-14, 33, 0, 47);
65 dfae5d27 Leszek Koltunski
      Dynamic3D dynLeft   = new Dynamic3D(2000,0.0f);
66
      Dynamic3D dynRight  = new Dynamic3D(2000,0.0f);
67 f988589e Leszek Koltunski
68 a4d59c0b Leszek Koltunski
      Static3D vect1 = new Static3D(  0,  0, 0);
69
      Static3D vect2 = new Static3D(-15, 30, 0);
70 dfae5d27 Leszek Koltunski
71 a4d59c0b Leszek Koltunski
      dynLeft.add(vect1);
72
      dynLeft.add(vect1);
73
      dynLeft.add(vect1);
74
      dynLeft.add(vect1);
75
      dynLeft.add(vect2);
76
      dynLeft.add(vect2);
77 427ab7bf Leszek Koltunski
      
78 a4d59c0b Leszek Koltunski
      dynRight.add(vect1);
79
      dynRight.add(vect2);
80
      dynRight.add(vect2);
81
      dynRight.add(vect1);
82
      dynRight.add(vect1);
83
      dynRight.add(vect1);
84 f6d884d5 Leszek Koltunski
85 fa9b6494 Leszek Koltunski
      mScale= new Static3D(1,1,1);
86 26c4e181 Leszek Koltunski
      mBrowL= new Static3D(0,0,0);
87
      mBrowR= new Static3D(0,0,0);
88 fa9b6494 Leszek Koltunski
89 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
90 26c4e181 Leszek Koltunski
      mEffects.apply( new VertexEffectDistort(dynLeft , mBrowL, regionLeft) );
91
      mEffects.apply( new VertexEffectDistort(dynRight, mBrowR, regionRight));
92 dfae5d27 Leszek Koltunski
      mEffects.apply( new MatrixEffectScale(mScale) );
93 392e16fd Leszek Koltunski
94 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
95 427ab7bf Leszek Koltunski
      }
96
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
   
99 fa9b6494 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
100 427ab7bf Leszek Koltunski
      {
101 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
102 427ab7bf Leszek Koltunski
      }
103
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
    
106 fa9b6494 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
107
     {
108 d381aa80 Leszek Koltunski
     float horiRatio = (float)width / mTexture.getWidth();
109
     float vertRatio = (float)height/ mTexture.getHeight();
110
     float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
111
112
     mScale.set( factor,factor,factor );
113
     mScreen.resize(width, height);
114 fa9b6494 Leszek Koltunski
     }
115 427ab7bf Leszek Koltunski
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
    
118 fa9b6494 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
119
     {
120
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
121
     Bitmap bitmap;
122 427ab7bf Leszek Koltunski
        
123 fa9b6494 Leszek Koltunski
     try
124
       {
125
       bitmap = BitmapFactory.decodeStream(is);
126
       }
127
     finally
128
       {
129
       try
130
         {
131
         is.close();
132
         }
133
       catch(IOException e) { }
134
       }
135 427ab7bf Leszek Koltunski
      
136 d381aa80 Leszek Koltunski
     int bmpHeight = bitmap.getHeight();
137
     int bmpWidth  = bitmap.getWidth();
138 687263cc Leszek Koltunski
139 26c4e181 Leszek Koltunski
     mBrowL.set( 98 - bmpWidth/2, 297 - bmpHeight/2, 0);
140
     mBrowR.set(233 - bmpWidth/2, 340 - bmpHeight/2, 0);
141
142 687263cc Leszek Koltunski
     if( mTexture==null ) mTexture = new DistortedTexture();
143 fa9b6494 Leszek Koltunski
     mTexture.setTexture(bitmap);
144 26c4e181 Leszek Koltunski
145 698ad0a8 Leszek Koltunski
     if( mMesh==null )
146
       {
147 386fd702 Leszek Koltunski
       mMesh = new MeshRectangles(25,25*bmpHeight/bmpWidth);
148
       mMesh.setStretch(bmpWidth,bmpHeight,0);
149 698ad0a8 Leszek Koltunski
       }
150 a4d59c0b Leszek Koltunski
151 fa9b6494 Leszek Koltunski
     mScreen.detachAll();
152
     mScreen.attach(mTexture,mEffects,mMesh);
153
154 885b9cca leszek
     VertexEffectDistort.enable();
155 fa9b6494 Leszek Koltunski
156
     try
157
       {
158 e3900503 Leszek Koltunski
       DistortedLibrary.onCreate(mView.getContext());
159 fa9b6494 Leszek Koltunski
       }
160
     catch(Exception ex)
161
       {
162
       android.util.Log.e("Bean", ex.getMessage() );
163
       }
164
     }
165 427ab7bf Leszek Koltunski
}