Project

General

Profile

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

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

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