Project

General

Profile

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

examples / src / main / java / org / distorted / examples / bean / BeanRenderer.java @ e4330c89

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.EffectName;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectDistort;
34
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
import org.distorted.library.type.Dynamic3D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42

    
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
   private DistortedEffects mEffects;
53
   private DistortedScreen mScreen;
54
   private DistortedTexture mTexture;
55
   private MeshFlat mMesh;
56
   private int mObjHeight, mObjWidth;
57
   private Static3D mMove, mScale;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
   BeanRenderer(GLSurfaceView v)
62
      {
63
      mView = v;
64
     
65
      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

    
72
      Static3D p1 = new Static3D(  0,  0, 0);
73
      Static3D p2 = new Static3D(-15,-30, 0);
74

    
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
      
82
      dynRight.add(p1);
83
      dynRight.add(p2);
84
      dynRight.add(p2);
85
      dynRight.add(p1);
86
      dynRight.add(p1);
87
      dynRight.add(p1);
88

    
89
      mMove = new Static3D(0,0,0);
90
      mScale= new Static3D(1,1,1);
91

    
92
      mEffects = new DistortedEffects();
93
      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

    
98
      mScreen = new DistortedScreen();
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
   
103
   public void onDrawFrame(GL10 glUnused)
104
      {
105
      mScreen.render( System.currentTimeMillis() );
106
      }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
111
     {
112
     if( (float)mObjHeight/mObjWidth > (float)height/width )
113
        {
114
        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
        }
119
     else
120
        {
121
        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
        }
126

    
127
     mScreen.resize(width,height);
128
     }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
    
132
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
133
     {
134
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
135
     Bitmap bitmap;
136
        
137
     try
138
       {
139
       bitmap = BitmapFactory.decodeStream(is);
140
       }
141
     finally
142
       {
143
       try
144
         {
145
         is.close();
146
         }
147
       catch(IOException e) { }
148
       }
149
      
150
     mObjHeight = bitmap.getHeight();
151
     mObjWidth  = bitmap.getWidth();
152
      
153
     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
}
(2-2/3)