Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.bean;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
29 427ab7bf Leszek Koltunski
30 fa9b6494 Leszek Koltunski
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 01782e85 Leszek Koltunski
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 b5cc7760 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
40 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42 427ab7bf Leszek Koltunski
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 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
53 d218d64e leszek
   private DistortedScreen mScreen;
54 b0ebdf5e Leszek Koltunski
   private DistortedTexture mTexture;
55 5f2a53b6 leszek
   private MeshFlat mMesh;
56 fa9b6494 Leszek Koltunski
   private int mObjHeight, mObjWidth;
57
   private Static3D mMove, mScale;
58
59 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 b56faf50 Leszek Koltunski
   BeanRenderer(GLSurfaceView v)
62 427ab7bf Leszek Koltunski
      {
63
      mView = v;
64
     
65 f6d884d5 Leszek Koltunski
      Static3D pLeft = new Static3D( 98, 183, 0);
66
      Static3D pRight= new Static3D(233, 140, 0);
67 427ab7bf Leszek Koltunski
      
68 f6d884d5 Leszek Koltunski
      Static4D rLeft = new Static4D( -3,-33,47,47);
69
      Static4D rRight= new Static4D(-14,-33,47,47);
70 427ab7bf Leszek Koltunski
     
71 f6d884d5 Leszek Koltunski
      Dynamic3D dLeft = new Dynamic3D(2000,0.0f);
72
      Dynamic3D dRight= new Dynamic3D(2000,0.0f);
73 f988589e Leszek Koltunski
74 b5cc7760 Leszek Koltunski
      Static3D p1 = new Static3D(  0,  0, 0);
75 cdc515b9 Leszek Koltunski
      Static3D p2 = new Static3D(-15,-30, 0);
76 427ab7bf Leszek Koltunski
      
77 b5cc7760 Leszek Koltunski
      dLeft.add(p1);
78
      dLeft.add(p1);
79
      dLeft.add(p1);
80
      dLeft.add(p1);
81
      dLeft.add(p2);
82
      dLeft.add(p2);
83 427ab7bf Leszek Koltunski
      
84 b5cc7760 Leszek Koltunski
      dRight.add(p1);
85
      dRight.add(p2);
86
      dRight.add(p2);
87
      dRight.add(p1);
88
      dRight.add(p1);
89
      dRight.add(p1);
90 f6d884d5 Leszek Koltunski
91 fa9b6494 Leszek Koltunski
      mMove = new Static3D(0,0,0);
92
      mScale= new Static3D(1,1,1);
93
94 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
95 fa9b6494 Leszek Koltunski
      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 392e16fd Leszek Koltunski
100 1f9a52f1 leszek
      mScreen = new DistortedScreen(mView);
101 427ab7bf Leszek Koltunski
      }
102
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
   
105 fa9b6494 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
106 427ab7bf Leszek Koltunski
      {
107 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
108 427ab7bf Leszek Koltunski
      }
109
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
    
112 fa9b6494 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
113
     {
114
     if( (float)mObjHeight/mObjWidth > (float)height/width )
115 630703d1 leszek
        {
116 fa9b6494 Leszek Koltunski
        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 630703d1 leszek
        }
121
      else
122
        {
123 fa9b6494 Leszek Koltunski
        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 630703d1 leszek
        }
128 fa9b6494 Leszek Koltunski
129
      mScreen.resize(width,height);
130
     }
131 427ab7bf Leszek Koltunski
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
    
134 fa9b6494 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
135
     {
136
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
137
     Bitmap bitmap;
138 427ab7bf Leszek Koltunski
        
139 fa9b6494 Leszek Koltunski
     try
140
       {
141
       bitmap = BitmapFactory.decodeStream(is);
142
       }
143
     finally
144
       {
145
       try
146
         {
147
         is.close();
148
         }
149
       catch(IOException e) { }
150
       }
151 427ab7bf Leszek Koltunski
      
152 fa9b6494 Leszek Koltunski
     mObjHeight = bitmap.getHeight();
153
     mObjWidth  = bitmap.getWidth();
154 427ab7bf Leszek Koltunski
      
155 fa9b6494 Leszek Koltunski
     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 427ab7bf Leszek Koltunski
}