Project

General

Profile

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

examples / src / main / java / org / distorted / examples / bean / BeanRenderer.java @ 10b7e588

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