Project

General

Profile

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

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

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 pLeft = new Static3D( 98, 183, 0);
66
      Static3D pRight= new Static3D(233, 140, 0);
67
      
68
      Static4D rLeft = new Static4D( -3,-33,47,47);
69
      Static4D rRight= new Static4D(-14,-33,47,47);
70
     
71
      Dynamic3D dLeft = new Dynamic3D(2000,0.0f);
72
      Dynamic3D dRight= new Dynamic3D(2000,0.0f);
73

    
74
      Static3D p1 = new Static3D(  0,  0, 0);
75
      Static3D p2 = new Static3D(-15,-30, 0);
76
      
77
      dLeft.add(p1);
78
      dLeft.add(p1);
79
      dLeft.add(p1);
80
      dLeft.add(p1);
81
      dLeft.add(p2);
82
      dLeft.add(p2);
83
      
84
      dRight.add(p1);
85
      dRight.add(p2);
86
      dRight.add(p2);
87
      dRight.add(p1);
88
      dRight.add(p1);
89
      dRight.add(p1);
90

    
91
      mMove = new Static3D(0,0,0);
92
      mScale= new Static3D(1,1,1);
93

    
94
      mEffects = new DistortedEffects();
95
      mEffects.apply( new VertexEffectDistort(dLeft , pLeft , rLeft) );
96
      mEffects.apply( new VertexEffectDistort(dRight, pRight, rRight));
97
      mEffects.apply(new MatrixEffectMove(mMove));
98
      mEffects.apply(new MatrixEffectScale(mScale));
99

    
100
      mScreen = new DistortedScreen(mView);
101
      }
102

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
    
112
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
113
     {
114
     if( (float)mObjHeight/mObjWidth > (float)height/width )
115
        {
116
        int w = (height*mObjWidth)/mObjHeight;
117
        float factor = (float)height/mObjHeight;
118
        mMove.set((width-w)/2,0,0);
119
        mScale.set(factor,factor,factor);
120
        }
121
      else
122
        {
123
        int h = (width*mObjHeight)/mObjWidth;
124
        float factor = (float)width/mObjWidth;
125
        mMove.set(0,(height-h)/2,0);
126
        mScale.set(factor,factor,factor);
127
        }
128

    
129
      mScreen.resize(width,height);
130
     }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
    
134
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
135
     {
136
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
137
     Bitmap bitmap;
138
        
139
     try
140
       {
141
       bitmap = BitmapFactory.decodeStream(is);
142
       }
143
     finally
144
       {
145
       try
146
         {
147
         is.close();
148
         }
149
       catch(IOException e) { }
150
       }
151
      
152
     mObjHeight = bitmap.getHeight();
153
     mObjWidth  = bitmap.getWidth();
154
      
155
     if( mTexture==null ) mTexture = new DistortedTexture(mObjWidth,mObjHeight);
156
     mTexture.setTexture(bitmap);
157
     if( mMesh==null ) mMesh = new MeshFlat(25,25*mObjHeight/mObjWidth);
158
     mScreen.detachAll();
159
     mScreen.attach(mTexture,mEffects,mMesh);
160

    
161
     DistortedEffects.enableEffect(EffectName.DISTORT);
162

    
163
     try
164
       {
165
       Distorted.onCreate(mView.getContext());
166
       }
167
     catch(Exception ex)
168
       {
169
       android.util.Log.e("Bean", ex.getMessage() );
170
       }
171
     }
172
}
(2-2/3)