Project

General

Profile

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

examples / src / main / java / org / distorted / examples / bean / BeanRenderer.java @ 849e0034

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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

    
20
package org.distorted.examples.bean;
21

    
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
import org.distorted.examples.R;
29

    
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectDistort;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.mesh.MeshRectangles;
37
import org.distorted.library.type.Dynamic3D;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40

    
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
   private DistortedEffects mEffects;
51
   private DistortedScreen mScreen;
52
   private DistortedTexture mTexture;
53
   private MeshRectangles mMesh;
54
   private Static3D mScale;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
   BeanRenderer(GLSurfaceView v)
59
      {
60
      mView = v;
61
     
62
      Static3D pointLeft  = new Static3D(-85,  57, 0);
63
      Static3D pointRight = new Static3D( 50, 100, 0);
64
      Static4D regionLeft = new Static4D( -3, 33, 0, 47);
65
      Static4D regionRight= new Static4D(-14, 33, 0, 47);
66
      Dynamic3D dynLeft   = new Dynamic3D(2000,0.0f);
67
      Dynamic3D dynRight  = new Dynamic3D(2000,0.0f);
68

    
69
      Static3D vect1 = new Static3D(  0,  0, 0);
70
      Static3D vect2 = new Static3D(-15, 30, 0);
71

    
72
      dynLeft.add(vect1);
73
      dynLeft.add(vect1);
74
      dynLeft.add(vect1);
75
      dynLeft.add(vect1);
76
      dynLeft.add(vect2);
77
      dynLeft.add(vect2);
78
      
79
      dynRight.add(vect1);
80
      dynRight.add(vect2);
81
      dynRight.add(vect2);
82
      dynRight.add(vect1);
83
      dynRight.add(vect1);
84
      dynRight.add(vect1);
85

    
86
      mScale= new Static3D(1,1,1);
87

    
88
      mEffects = new DistortedEffects();
89
      mEffects.apply( new VertexEffectDistort(dynLeft , pointLeft , regionLeft) );
90
      mEffects.apply( new VertexEffectDistort(dynRight, pointRight, regionRight));
91
      mEffects.apply( new MatrixEffectScale(mScale) );
92

    
93
      mScreen = new DistortedScreen();
94
      }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
   
98
   public void onDrawFrame(GL10 glUnused)
99
      {
100
      mScreen.render( System.currentTimeMillis() );
101
      }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
    
105
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
106
     {
107
     float horiRatio = (float)width / mTexture.getWidth();
108
     float vertRatio = (float)height/ mTexture.getHeight();
109
     float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
110

    
111
     mScale.set( factor,factor,factor );
112
     mScreen.resize(width, height);
113
     }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
    
117
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
118
     {
119
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
120
     Bitmap bitmap;
121
        
122
     try
123
       {
124
       bitmap = BitmapFactory.decodeStream(is);
125
       }
126
     finally
127
       {
128
       try
129
         {
130
         is.close();
131
         }
132
       catch(IOException e) { }
133
       }
134
      
135
     int bmpHeight = bitmap.getHeight();
136
     int bmpWidth  = bitmap.getWidth();
137

    
138
     if( mTexture==null ) mTexture = new DistortedTexture();
139
     mTexture.setTexture(bitmap);
140

    
141
     if( mMesh==null )
142
       {
143
       mMesh = new MeshRectangles(25,25*bmpHeight/bmpWidth);
144
       mMesh.setStretch(bmpWidth,bmpHeight,0);
145
       }
146

    
147
     mScreen.detachAll();
148
     mScreen.attach(mTexture,mEffects,mMesh);
149

    
150
     VertexEffectDistort.enable();
151

    
152
     try
153
       {
154
       DistortedLibrary.onCreate(mView.getContext());
155
       }
156
     catch(Exception ex)
157
       {
158
       android.util.Log.e("Bean", ex.getMessage() );
159
       }
160
     }
161
}
(2-2/3)