Project

General

Profile

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

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

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 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
31 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
32 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.DistortedEffectQueues;
34 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
35 b5cc7760 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
36 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38 427ab7bf Leszek Koltunski
39
import android.graphics.Bitmap;
40
import android.graphics.BitmapFactory;
41
import android.opengl.GLES20;
42
import android.opengl.GLSurfaceView;
43
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46
class BeanRenderer implements GLSurfaceView.Renderer 
47
{
48
   private GLSurfaceView mView;
49 f6d884d5 Leszek Koltunski
   private DistortedTexture mTexture;
50
   private DistortedEffectQueues mQueues;
51 10b7e588 Leszek Koltunski
   private GridFlat mGrid;
52 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
53
    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 b56faf50 Leszek Koltunski
   BeanRenderer(GLSurfaceView v)
57 427ab7bf Leszek Koltunski
      {
58
      mView = v;
59
     
60 f6d884d5 Leszek Koltunski
      Static3D pLeft = new Static3D( 98, 183, 0);
61
      Static3D pRight= new Static3D(233, 140, 0);
62 427ab7bf Leszek Koltunski
      
63 f6d884d5 Leszek Koltunski
      Static4D rLeft = new Static4D( -3,-33,47,47);
64
      Static4D rRight= new Static4D(-14,-33,47,47);
65 427ab7bf Leszek Koltunski
     
66 f6d884d5 Leszek Koltunski
      Dynamic3D dLeft = new Dynamic3D(2000,0.0f);
67
      Dynamic3D dRight= new Dynamic3D(2000,0.0f);
68 f988589e Leszek Koltunski
69 b5cc7760 Leszek Koltunski
      Static3D p1 = new Static3D(  0,  0, 0);
70 cdc515b9 Leszek Koltunski
      Static3D p2 = new Static3D(-15,-30, 0);
71 427ab7bf Leszek Koltunski
      
72 b5cc7760 Leszek Koltunski
      dLeft.add(p1);
73
      dLeft.add(p1);
74
      dLeft.add(p1);
75
      dLeft.add(p1);
76
      dLeft.add(p2);
77
      dLeft.add(p2);
78 427ab7bf Leszek Koltunski
      
79 b5cc7760 Leszek Koltunski
      dRight.add(p1);
80
      dRight.add(p2);
81
      dRight.add(p2);
82
      dRight.add(p1);
83
      dRight.add(p1);
84
      dRight.add(p1);
85 f6d884d5 Leszek Koltunski
86
      mQueues = new DistortedEffectQueues();
87
      mQueues.distort(dLeft , pLeft , rLeft );
88
      mQueues.distort(dRight, pRight, rRight);
89 427ab7bf Leszek Koltunski
      }
90
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
   
93
    public void onDrawFrame(GL10 glUnused) 
94
      {
95
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
96 f6d884d5 Leszek Koltunski
      mQueues.draw(System.currentTimeMillis(), mTexture, mGrid);
97 427ab7bf Leszek Koltunski
      }
98
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
    
101
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
102
      { 
103 f6d884d5 Leszek Koltunski
      mQueues.abortEffects(EffectTypes.MATRIX);
104 427ab7bf Leszek Koltunski
         
105 5055b5d4 Leszek Koltunski
      if( (float)bmpHeight/bmpWidth > (float)height/width )
106 427ab7bf Leszek Koltunski
        {
107
        int w = (height*bmpWidth)/bmpHeight;
108 7589635e Leszek Koltunski
        float factor = (float)height/bmpHeight;
109
110 f6d884d5 Leszek Koltunski
        mQueues.move( new Static3D((width-w)/2,0,0) );
111
        mQueues.scale(factor);
112 427ab7bf Leszek Koltunski
        }
113
      else
114
        {
115
        int h = (width*bmpHeight)/bmpWidth;
116 7589635e Leszek Koltunski
        float factor = (float)width/bmpWidth;
117
118 f6d884d5 Leszek Koltunski
        mQueues.move( new Static3D(0,(height-h)/2,0) );
119
        mQueues.scale(factor);
120 427ab7bf Leszek Koltunski
        }
121
      
122
      Distorted.onSurfaceChanged(width, height); 
123
      }
124
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
    
127
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
128
      {
129 e7a4ef16 Leszek Koltunski
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
130
131 427ab7bf Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
132
      Bitmap bitmap;
133
        
134
      try 
135
        {
136
        bitmap = BitmapFactory.decodeStream(is);
137
        } 
138
      finally 
139
        {
140
        try 
141
          {
142
          is.close();
143
          } 
144
        catch(IOException e) { }
145
        }  
146
      
147
      bmpHeight = bitmap.getHeight();
148
      bmpWidth  = bitmap.getWidth();
149
      
150 f6d884d5 Leszek Koltunski
      mTexture = new DistortedTexture(bmpWidth, bmpHeight, 0);
151
      mTexture.setTexture(bitmap);
152 10b7e588 Leszek Koltunski
      mGrid = new GridFlat(25,25*bmpHeight/bmpWidth);
153 e8b6aa95 Leszek Koltunski
154 427ab7bf Leszek Koltunski
      try
155
        {
156 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
157 427ab7bf Leszek Koltunski
        }
158
      catch(Exception ex)
159
        {
160
        android.util.Log.e("Bean", ex.getMessage() );
161
        }
162
      }
163
}