Project

General

Profile

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

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