Project

General

Profile

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

examples / src / main / java / org / distorted / examples / wind / WindRenderer.java @ 14122c52

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.wind;
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.DistortedObject;
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.DynamicQuat;
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35

    
36
import java.io.IOException;
37
import java.io.InputStream;
38

    
39
import javax.microedition.khronos.egl.EGLConfig;
40
import javax.microedition.khronos.opengles.GL10;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
class WindRenderer implements GLSurfaceView.Renderer
45
{
46
   private GLSurfaceView mView;
47
   private DistortedObject mObject;
48
   private DynamicQuat mQuatInt1, mQuatInt2;
49
   private int mWind;
50
   private int mObjWidth, mObjHeight;
51

    
52
   Static4D mQuat1, mQuat2;
53
   int mScreenMin;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
   WindRenderer(GLSurfaceView view)
58
      { 
59
      mView = view;
60

    
61
      mObject = new DistortedCubes(50,30,10,false);
62

    
63
      mObjWidth = mObject.getWidth();
64
      mObjHeight= mObject.getHeight();
65

    
66
      mQuat1 = new Static4D(           0,         0,           0,          1);  // unity quaternion
67
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);  // something semi-random that looks good
68

    
69
      mQuatInt1 = new DynamicQuat(0,0.5f);
70
      mQuatInt2 = new DynamicQuat(0,0.5f);
71

    
72
      mQuatInt1.add(mQuat1);
73
      mQuatInt2.add(mQuat2);
74
      }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
   void setWind(int wind)
79
      {
80
      mWind = wind;
81
      }
82
   
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
   
85
   public void onDrawFrame(GL10 glUnused) 
86
      {
87
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
88
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
89
     
90
      mObject.draw(System.currentTimeMillis());
91
      }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
    
95
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
96
      {
97
      mScreenMin = width<height ? width:height;
98

    
99
      mObject.abortEffects(EffectTypes.MATRIX);
100
      float factor;
101

    
102
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
103
        {
104
        factor = (0.8f*height)/mObjHeight;
105
        }
106
      else
107
        {
108
        factor = (0.8f*width)/mObjWidth;
109
        }
110

    
111
      mObject.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
112
      mObject.scale(factor);
113
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
114

    
115
      mObject.quaternion(mQuatInt1, center);
116
      mObject.quaternion(mQuatInt2, center);
117

    
118
      Distorted.onSurfaceChanged(width, height);
119
      }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
    
123
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
124
      {  
125
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
126
      Bitmap bitmap;
127

    
128
      try
129
        {
130
        bitmap = BitmapFactory.decodeStream(is);
131
        }
132
      finally
133
        {
134
        try
135
          {
136
          is.close();
137
          }
138
        catch(IOException e) { }
139
        }
140

    
141
      mObject.setBitmap(bitmap);
142

    
143
      try
144
        {
145
        Distorted.onSurfaceCreated(mView.getContext());
146
        }
147
      catch(Exception ex)
148
        {
149
        android.util.Log.e("Wind", ex.getMessage() );
150
        }
151
      }
152
}
(2-2/3)