Project

General

Profile

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

examples / src / main / java / org / distorted / examples / vertex3d / Vertex3DRenderer.java @ d40cfeb2

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.vertex3d;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLES20;
25
import android.opengl.GLSurfaceView;
26

    
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29
import org.distorted.library.DistortedCubes;
30
import org.distorted.library.EffectNames;
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.Dynamic1D;
33
import org.distorted.library.type.Dynamic3D;
34
import org.distorted.library.type.Dynamic4D;
35
import org.distorted.library.type.Static1D;
36
import org.distorted.library.type.Static2D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39

    
40
import java.io.IOException;
41
import java.io.InputStream;
42

    
43
import javax.microedition.khronos.egl.EGLConfig;
44
import javax.microedition.khronos.opengles.GL10;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
class Vertex3DRenderer implements GLSurfaceView.Renderer
49
{
50
    private static final int SIZE = 100;
51

    
52
    private GLSurfaceView mView;
53
    private static DistortedCubes mCube;
54
    private int mCols, mRows;
55

    
56
    private static EffectNames[] order;
57
    
58
    private static Dynamic3D mDeformInter, mDistortInter;
59
    private static Dynamic1D mSinkInter, mSwirlInter;
60

    
61
    private static Static2D mCenterPoint;
62
    private static Static3D mDeformPoint, mDistortPoint;
63
    private static Static1D mSinkPoint, mSwirlPoint;
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
    public static void setDeform(float x, float y, float z)
68
      {
69
      mDeformPoint.set(x, y, z);
70
      }
71
    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
    public static void setDistort(float x, float y, float z)
75
      {
76
      mDistortPoint.set(x, y, z);
77
      }
78
     
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
    public static void setSink(float s)
82
      {
83
      mSinkPoint.set(s);
84
      }
85
    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
    public static void setSwirl(float s)
89
      {
90
      mSwirlPoint.set(s);
91
      }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
    public static void setOrder(EffectNames[] effects)
96
      {
97
      order = effects;
98
      setVertexEffects();
99
      }
100
      
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
    public static void setVertexEffects()
104
      {
105
      mCube.abortEffects(EffectTypes.VERTEX);
106
	
107
      for( int i=0; i<=order.length-1 ; i++ )
108
        {
109
        switch(order[i])
110
          {
111
          case DEFORM : mCube.deform( mDeformInter , mCenterPoint) ; break;
112
          case DISTORT: mCube.distort(mDistortInter, mCenterPoint) ; break;
113
          case SINK   : mCube.sink(   mSinkInter   , mCenterPoint) ; break;
114
          case SWIRL  : mCube.swirl(  mSwirlInter  , mCenterPoint) ; break;
115
          }
116
        }
117
      }
118
    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
    public Vertex3DRenderer(GLSurfaceView v)
122
      {
123
      mView = v;
124

    
125
      String shape = Vertex3DActivity.getShape();
126

    
127
      mCols = Vertex3DActivity.getCols();
128
      mRows = shape.length() / mCols;
129

    
130
      mCube = new DistortedCubes( mCols, shape, SIZE);
131
      
132
      mCenterPoint = new Static2D(0,0);
133
      mDeformPoint = new Static3D(0,0,0);
134
      mDistortPoint= new Static3D(1,1,1);
135
      mSwirlPoint  = new Static1D(0);
136
      mSinkPoint   = new Static1D(1);
137

    
138
      mDeformInter  = new Dynamic3D();
139
      mDistortInter = new Dynamic3D();
140
      mSwirlInter   = new Dynamic1D();
141
      mSinkInter    = new Dynamic1D();
142

    
143
      mDeformInter.add(mDeformPoint);
144
      mDistortInter.add(mDistortPoint);
145
      mSwirlInter.add(mSwirlPoint);
146
      mSinkInter.add(mSinkPoint);
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
   
151
    public void onDrawFrame(GL10 glUnused) 
152
      {
153
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
154
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
155
      
156
      mCube.draw(System.currentTimeMillis());
157
      }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
    
161
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
162
      {
163
      mCube.abortEffects(EffectTypes.MATRIX);
164
      float factor;
165

    
166
      if( width*mRows > height*mCols ) // screen is more 'horizontal' than the shape
167
        {
168
        factor = height/((mRows+2)*SIZE);
169
        }
170
      else
171
        {
172
        factor = width/((mCols+2)*SIZE);
173
        }
174

    
175
      mCube.scale(factor);
176
      mCube.move( new Static3D( (width-factor*mCols*SIZE)/2 , (height-factor*mRows*SIZE)/2 , 0) );
177

    
178
      setVertexEffects();
179

    
180
      Distorted.onSurfaceChanged(width, height); 
181
      }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
    
185
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
186
      {
187
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
188
      Bitmap bitmap;
189
        
190
      try 
191
        {
192
        bitmap = BitmapFactory.decodeStream(is);
193
        } 
194
      finally 
195
        {
196
        try 
197
          {
198
          is.close();
199
          } 
200
        catch(IOException e) { }
201
        }  
202
      
203
      mCube.setBitmap(bitmap);
204
      
205
      try
206
        {
207
        Distorted.onSurfaceCreated(mView.getContext());
208
        }
209
      catch(Exception ex)
210
        {
211
        android.util.Log.e("Vertex3D", ex.getMessage() );
212
        }
213
      }
214
}
(2-2/3)