Project

General

Profile

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

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

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
	  private static final int NUM  =   3;
52

    
53
    private GLSurfaceView mView;
54
    private static DistortedCubes mCube;
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
      mCube = new DistortedCubes( NUM, "111101111", SIZE);
125
      
126
      mCenterPoint = new Static2D(0,0);
127
      mDeformPoint = new Static3D(0,0,0);
128
      mDistortPoint= new Static3D(1,1,1);
129
      mSwirlPoint  = new Static1D(0);
130
      mSinkPoint   = new Static1D(1);
131

    
132
      mDeformInter  = new Dynamic3D();
133
      mDistortInter = new Dynamic3D();
134
      mSwirlInter   = new Dynamic1D();
135
      mSinkInter    = new Dynamic1D();
136

    
137
      mDeformInter.add(mDeformPoint);
138
      mDistortInter.add(mDistortPoint);
139
      mSwirlInter.add(mSwirlPoint);
140
      mSinkInter.add(mSinkPoint);
141
      }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
   
145
    public void onDrawFrame(GL10 glUnused) 
146
      {
147
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
148
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
149
      
150
      mCube.draw(System.currentTimeMillis());
151
      }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
    
155
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
156
      {
157
      mCube.abortEffects(EffectTypes.MATRIX);
158
      float factor = (width>height ? height:width)/((NUM+2)*SIZE);
159
      mCube.scale(factor);
160
      mCube.move( new Static3D( (width-factor*NUM*SIZE)/2,(height-factor*NUM*SIZE)/2,0) );
161

    
162
      setVertexEffects();
163

    
164
      Distorted.onSurfaceChanged(width, height); 
165
      }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
    
169
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
170
      {
171
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
172
      Bitmap bitmap;
173
        
174
      try 
175
        {
176
        bitmap = BitmapFactory.decodeStream(is);
177
        } 
178
      finally 
179
        {
180
        try 
181
          {
182
          is.close();
183
          } 
184
        catch(IOException e) { }
185
        }  
186
      
187
      mCube.setBitmap(bitmap);
188
      
189
      try
190
        {
191
        Distorted.onSurfaceCreated(mView.getContext());
192
        }
193
      catch(Exception ex)
194
        {
195
        android.util.Log.e("Vertex3D", ex.getMessage() );
196
        }
197
      }
198
}
(2-2/3)