Project

General

Profile

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

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