Project

General

Profile

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

examples / src / main / java / org / distorted / examples / bean / BeanRenderer.java @ 107e4b72

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.MatrixEffectMove;
31
import org.distorted.library.effect.MatrixEffectScale;
32
import org.distorted.library.effect.VertexEffectDistort;
33
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
import org.distorted.library.mesh.MeshFlat;
38
import org.distorted.library.type.Dynamic3D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41

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

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

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

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

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

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

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

    
97
      mScreen = new DistortedScreen();
98
      }
99

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

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

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

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

    
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
}
(2-2/3)