Project

General

Profile

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

examples / src / main / java / org / distorted / examples / bean / BeanRenderer.java @ 26c4e181

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, mBrowL, mBrowR;
55

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

    
58
   BeanRenderer(GLSurfaceView v)
59
      {
60
      mView = v;
61
     
62

    
63
      Static4D regionLeft = new Static4D( -3, 33, 0, 47);
64
      Static4D regionRight= new Static4D(-14, 33, 0, 47);
65
      Dynamic3D dynLeft   = new Dynamic3D(2000,0.0f);
66
      Dynamic3D dynRight  = new Dynamic3D(2000,0.0f);
67

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

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

    
85
      mScale= new Static3D(1,1,1);
86
      mBrowL= new Static3D(0,0,0);
87
      mBrowR= new Static3D(0,0,0);
88

    
89
      mEffects = new DistortedEffects();
90
      mEffects.apply( new VertexEffectDistort(dynLeft , mBrowL, regionLeft) );
91
      mEffects.apply( new VertexEffectDistort(dynRight, mBrowR, regionRight));
92
      mEffects.apply( new MatrixEffectScale(mScale) );
93

    
94
      mScreen = new DistortedScreen();
95
      }
96

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

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

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

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

    
139
     mBrowL.set( 98 - bmpWidth/2, 297 - bmpHeight/2, 0);
140
     mBrowR.set(233 - bmpWidth/2, 340 - bmpHeight/2, 0);
141

    
142
     if( mTexture==null ) mTexture = new DistortedTexture();
143
     mTexture.setTexture(bitmap);
144

    
145
     if( mMesh==null )
146
       {
147
       mMesh = new MeshRectangles(25,25*bmpHeight/bmpWidth);
148
       mMesh.setStretch(bmpWidth,bmpHeight,0);
149
       }
150

    
151
     mScreen.detachAll();
152
     mScreen.attach(mTexture,mEffects,mMesh);
153

    
154
     VertexEffectDistort.enable();
155

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