Project

General

Profile

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

examples / src / main / java / org / distorted / examples / starwars / StarWarsRenderer.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.starwars;
21 427ab7bf Leszek Koltunski
22
import java.io.IOException;
23
import java.io.InputStream;
24
import java.util.Random;
25
26
import javax.microedition.khronos.egl.EGLConfig;
27
import javax.microedition.khronos.opengles.GL10;
28
29 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
30
31 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
32 c5a28eb8 Leszek Koltunski
import org.distorted.library.EffectNames;
33 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
34
import org.distorted.library.type.Dynamic3D;
35 10b7e588 Leszek Koltunski
import org.distorted.library.DistortedObjectTree;
36 7589635e Leszek Koltunski
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39 13efa930 Leszek Koltunski
import org.distorted.library.message.EffectListener;
40
import org.distorted.library.message.EffectMessage;
41 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
42 e8b6aa95 Leszek Koltunski
import org.distorted.library.DistortedObject;
43 427ab7bf Leszek Koltunski
44
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
46
import android.graphics.Canvas;
47
import android.graphics.Paint;
48
import android.graphics.Typeface;
49
import android.opengl.GLES20;
50
import android.opengl.GLSurfaceView;
51
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54
class StarWarsRenderer implements GLSurfaceView.Renderer, EffectListener
55 bc0a685b Leszek Koltunski
  {
56
  private final String[] mGFFAString = 
57 427ab7bf Leszek Koltunski
         
58
            { "A long time ago, in a galaxy far,",
59
              "far away...." };                    // yes, there really was a four-dot ellipsis
60
61 bc0a685b Leszek Koltunski
  private final String[] mCRAWLString = 
62 427ab7bf Leszek Koltunski
      
63
            { "It is a period of civil war.",      // in the original 1977 version, there was no 
64
              "Rebel spaceships, striking",        // 'Episode IV New Hope' subtitle line before
65
              "from a hidden base, have",          // the crawl. Lets keep to the classic.
66
              "won their first victory",
67
              "against the evil Galactic",
68
              "Empire.",
69
              "",
70
              "During the battle, rebel",
71
              "spies managed to steal",
72
              "secret plans to the Empire's",
73
              "ultimate weapon, the",
74
              "DEATH STAR, an armored",
75
              "space station with enough",
76
              "power to destroy an entire",
77
              "planet.",
78
              "",
79
              "Pursued by the Empire's",
80
              "sinister agents, Princess",
81
              "Leia races home aboard her",
82
              "starship, custodian of the",
83
              "stolen plans that can save",
84
              "her people and restore",
85
              "freedom to the galaxy...." };      // four-dot.
86
   
87 bc0a685b Leszek Koltunski
  private final int NUM_STARS = 40;
88 427ab7bf Leszek Koltunski
   
89 bc0a685b Leszek Koltunski
  private final int CRAWL_COLOR = 0xffffe81f;
90
  private final int GFFA_COLOR  = 0xff0000ff;
91
  private final int FONT_HEIGHT      = 40;
92
  private final int VERTICAL_SPACING = 10;
93
  private final String CRAWL_TYPEFACE = "News Gothic";
94 427ab7bf Leszek Koltunski
   
95 bc0a685b Leszek Koltunski
  private final int CRAWL_WIDTH = 500;
96
  private final int CRAWL_HEIGHT= (FONT_HEIGHT+VERTICAL_SPACING)*(mCRAWLString.length+1);
97 427ab7bf Leszek Koltunski
   
98 bc0a685b Leszek Koltunski
  private final int GFFA_WIDTH = 600;
99
  private final int GFFA_HEIGHT= (FONT_HEIGHT+VERTICAL_SPACING)*(mGFFAString.length+1);
100 427ab7bf Leszek Koltunski
   
101 bc0a685b Leszek Koltunski
  private final float CRAWL_ANGLE = -30.0f;
102 427ab7bf Leszek Koltunski
   
103 bc0a685b Leszek Koltunski
  private GLSurfaceView mView;
104 e8b6aa95 Leszek Koltunski
  private DistortedObject mScreen, mGFFA, mLogo, mCrawl, mCrawlBackground;
105
  private DistortedObject[] mStars;
106 bc0a685b Leszek Koltunski
  private long gffaID, logoID, crawlID;
107 427ab7bf Leszek Koltunski
    
108 bc0a685b Leszek Koltunski
  private Random mRnd = new Random(0);
109 10b7e588 Leszek Koltunski
  private DistortedObjectTree mRoot, mBackground;
110
  private GridFlat mQuad;
111 e8b6aa95 Leszek Koltunski
112
113 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
114
115 e7a4ef16 Leszek Koltunski
  StarWarsRenderer(GLSurfaceView v)
116 bc0a685b Leszek Koltunski
    {
117
    mView = v;
118 e8b6aa95 Leszek Koltunski
119 10b7e588 Leszek Koltunski
    mQuad = new GridFlat(1,1);
120 10d53839 Leszek Koltunski
    Distorted.setProjection(60.0f, 0.0f, 0.0f);
121 bc0a685b Leszek Koltunski
    }
122 427ab7bf Leszek Koltunski
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
   
125 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused) 
126
    {
127
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
128
    mRoot.draw(System.currentTimeMillis());
129
    }
130 427ab7bf Leszek Koltunski
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
    
133 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
134
    { 
135
    Distorted.onSurfaceChanged(width, height); 
136
    setupScreen(width,height);
137
    }
138 427ab7bf Leszek Koltunski
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
    
141 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
142
    {
143 e7a4ef16 Leszek Koltunski
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
144
145 bc0a685b Leszek Koltunski
    setupBitmaps();
146 427ab7bf Leszek Koltunski
         
147 bc0a685b Leszek Koltunski
    try
148
      {
149
      Distorted.onSurfaceCreated(mView.getContext());
150
      }
151
    catch(Exception ex)
152
      {
153
      android.util.Log.e("Star Wars", ex.getMessage() );
154 427ab7bf Leszek Koltunski
      }
155 bc0a685b Leszek Koltunski
    }
156 427ab7bf Leszek Koltunski
    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
159 bc0a685b Leszek Koltunski
  private void setupScreen(int w, int h)
160
    {
161 e8b6aa95 Leszek Koltunski
    mScreen = new DistortedObject(w,h,1);
162 10b7e588 Leszek Koltunski
    mRoot = new DistortedObjectTree(mScreen,mQuad);
163 427ab7bf Leszek Koltunski
      
164 e8b6aa95 Leszek Koltunski
    mCrawlBackground = new DistortedObject(w,(int)(Math.sqrt(3.0)*h),1);
165 427ab7bf Leszek Koltunski
       
166 bc0a685b Leszek Koltunski
    int randomA, randomX, randomY, randomTime;
167
    float randomS, randomAlpha1, randomAlpha2;
168 427ab7bf Leszek Koltunski
       
169 7589635e Leszek Koltunski
    Static3D center = new Static3D(0,0,0);
170
    Static3D axis   = new Static3D(0,0,1);
171
172 9ff0c8c3 Leszek Koltunski
    Static1D alphaNoise = new Static1D(0.4f);
173
174 bc0a685b Leszek Koltunski
    for(int i=0; i<NUM_STARS; i++)
175
      {
176
      randomX = mRnd.nextInt(w);
177
      randomY = mRnd.nextInt(h);
178
      randomS = 0.2f+ mRnd.nextFloat();
179
      randomA = (int)(180*mRnd.nextFloat());
180
      randomAlpha1 = 0.2f + 0.8f*mRnd.nextFloat();
181
      randomAlpha2 = 0.8f + 0.2f*mRnd.nextFloat();
182
      randomTime = 500+mRnd.nextInt(2000);
183 427ab7bf Leszek Koltunski
      
184 7589635e Leszek Koltunski
      mStars[i].move( new Static3D(randomX,randomY,0) );
185
      mStars[i].scale( new Static3D(randomS,randomS,randomS) );
186 59bbb86a Leszek Koltunski
      mStars[i].rotate( new Static1D(randomA), axis, center );
187 427ab7bf Leszek Koltunski
      
188 f988589e Leszek Koltunski
      Dynamic1D di = new Dynamic1D(randomTime,0.0f);
189 9ff0c8c3 Leszek Koltunski
      di.setNoise(alphaNoise);
190 7589635e Leszek Koltunski
      di.add(new Static1D(randomAlpha1));
191
      di.add(new Static1D(randomAlpha2));
192 427ab7bf Leszek Koltunski
      
193 59759251 Leszek Koltunski
      mStars[i].alpha(di);
194 427ab7bf Leszek Koltunski
      
195 e8b6aa95 Leszek Koltunski
      mRoot.attach(mStars[i], mQuad);
196 427ab7bf Leszek Koltunski
      }
197 bc0a685b Leszek Koltunski
      
198
    float scale = (0.5f*w/mGFFA.getWidth());
199
    
200 f988589e Leszek Koltunski
    Dynamic1D di = new Dynamic1D(6000,0.5f);
201 7589635e Leszek Koltunski
    di.add(new Static1D(1.0f));
202
    di.add(new Static1D(1.0f));
203
    di.add(new Static1D(0.0f));
204 bc0a685b Leszek Koltunski
    
205 7589635e Leszek Koltunski
    mGFFA.move( new Static3D(w/5,h/3,0) );
206
    mGFFA.scale( new Static3D(scale,scale,scale) );
207 59759251 Leszek Koltunski
    mGFFA.alpha(di);
208 bc0a685b Leszek Koltunski
      
209 e8b6aa95 Leszek Koltunski
    mRoot.attach(mGFFA, mQuad);
210 bc0a685b Leszek Koltunski
    mGFFA.addEventListener(this); 
211
    }
212 427ab7bf Leszek Koltunski
    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
215 bc0a685b Leszek Koltunski
  private void setupBitmaps()
216
    {
217
    InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.starwars);
218
    InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.star);
219 427ab7bf Leszek Koltunski
    
220 bc0a685b Leszek Koltunski
    Bitmap bitmapStar, bitmapGFFA, bitmapLogo, bitmapText;
221
      
222
    try 
223
      {
224
      bitmapLogo = BitmapFactory.decodeStream(is1);
225
      bitmapStar = BitmapFactory.decodeStream(is2);
226
      } 
227
    finally 
228
      {
229 427ab7bf Leszek Koltunski
      try 
230
        {
231 bc0a685b Leszek Koltunski
        is1.close();
232
        is2.close();
233 427ab7bf Leszek Koltunski
        } 
234 bc0a685b Leszek Koltunski
      catch(IOException e) { }
235
      } 
236 427ab7bf Leszek Koltunski
      
237 bc0a685b Leszek Koltunski
    Paint paint = new Paint();
238
    paint.setAntiAlias(true);
239
    paint.setTextAlign(Paint.Align.LEFT);
240
    paint.setTextSize(FONT_HEIGHT);
241
    paint.setColor(GFFA_COLOR);
242 427ab7bf Leszek Koltunski
      
243 bc0a685b Leszek Koltunski
    Typeface tf = Typeface.create(CRAWL_TYPEFACE, Typeface.BOLD);
244
    paint.setTypeface(tf);     
245 427ab7bf Leszek Koltunski
 
246 bc0a685b Leszek Koltunski
    ///// create GFFA ///////////////////
247 e8b6aa95 Leszek Koltunski
    mGFFA  = new DistortedObject(GFFA_WIDTH, GFFA_HEIGHT, 1);
248 bc0a685b Leszek Koltunski
    bitmapGFFA = Bitmap.createBitmap(GFFA_WIDTH,GFFA_HEIGHT,Bitmap.Config.ARGB_8888);
249
    bitmapGFFA.eraseColor(0x00000000);
250
    Canvas gffaCanvas = new Canvas(bitmapGFFA);
251 427ab7bf Leszek Koltunski
      
252 bc0a685b Leszek Koltunski
    for(int i=0; i<mGFFAString.length; i++)
253
      {
254
      gffaCanvas.drawText(mGFFAString[i], 0, (i+1)*(FONT_HEIGHT+VERTICAL_SPACING), paint);  
255
      }
256 427ab7bf Leszek Koltunski
  
257 e8b6aa95 Leszek Koltunski
    mGFFA.setTexture(bitmapGFFA);
258 427ab7bf Leszek Koltunski
      
259 bc0a685b Leszek Koltunski
    ///// create Logo ///////////////////
260 e8b6aa95 Leszek Koltunski
    mLogo  = new DistortedObject(bitmapLogo.getWidth(),bitmapLogo.getHeight(),1);
261
    mLogo.setTexture(bitmapLogo);
262
263 bc0a685b Leszek Koltunski
    ///// create CRAWL //////////////////
264 e8b6aa95 Leszek Koltunski
    mCrawl = new DistortedObject(CRAWL_WIDTH, CRAWL_HEIGHT, 1);
265 bc0a685b Leszek Koltunski
    bitmapText = Bitmap.createBitmap(CRAWL_WIDTH,CRAWL_HEIGHT,Bitmap.Config.ARGB_8888);
266
    bitmapText.eraseColor(0x00000000);
267
    Canvas textCanvas = new Canvas(bitmapText);
268
    paint.setColor(CRAWL_COLOR);
269 427ab7bf Leszek Koltunski
  
270 bc0a685b Leszek Koltunski
    for(int i=0; i<mCRAWLString.length; i++)
271
      {
272
      displayJustified(mCRAWLString[i], 0, (i+1)*(FONT_HEIGHT+VERTICAL_SPACING), CRAWL_WIDTH, textCanvas, paint);  
273
      }
274 427ab7bf Leszek Koltunski
      
275 e8b6aa95 Leszek Koltunski
    mCrawl.setTexture(bitmapText);
276 427ab7bf Leszek Koltunski
      
277 bc0a685b Leszek Koltunski
    ///// create Stars ///////////////////
278 427ab7bf Leszek Koltunski
      
279 e8b6aa95 Leszek Koltunski
    mStars = new DistortedObject[NUM_STARS];
280 427ab7bf Leszek Koltunski
      
281 e8b6aa95 Leszek Koltunski
    mStars[0] = new DistortedObject(bitmapStar.getWidth(),bitmapStar.getHeight(),1);
282
    mStars[0].setTexture(bitmapStar);
283
284 bc0a685b Leszek Koltunski
    for(int i=1; i<NUM_STARS; i++)
285
      {
286 e8b6aa95 Leszek Koltunski
      mStars[i] = new DistortedObject(mStars[0], Distorted.CLONE_BITMAP|Distorted.CLONE_VERTEX);
287 427ab7bf Leszek Koltunski
      }
288 bc0a685b Leszek Koltunski
      
289
    gffaID = mGFFA.getID();
290
    logoID = mLogo.getID();
291
    crawlID= mCrawl.getID();
292
    }
293 427ab7bf Leszek Koltunski
 
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
296 bc0a685b Leszek Koltunski
  private void displayJustified(String str, int x, int y, int length, Canvas c, Paint paint)
297
    { 
298
    int len       = str.length();
299
    int numspaces = retNumSpaces(str);
300 427ab7bf Leszek Koltunski
    
301 bc0a685b Leszek Koltunski
    if( len>0 && str.charAt(len-1) == ' ' ) numspaces--;
302 427ab7bf Leszek Koltunski
303 bc0a685b Leszek Koltunski
    float left=x,w = (numspaces>0 ? (length-paint.measureText(str))/numspaces : 0);
304
    String tmp;
305
    int begin,end=0;
306 427ab7bf Leszek Koltunski
307 bc0a685b Leszek Koltunski
    while( end<len )
308
      {
309
      begin=end;
310
      end = str.indexOf(' ', begin)+1;
311
      if( end<=0 ) end = len;
312 427ab7bf Leszek Koltunski
313 bc0a685b Leszek Koltunski
      tmp = str.substring(begin,end);
314
      c.drawText( tmp, left, y, paint);
315
      left+= (paint.measureText(tmp)+w);
316
      }  
317
    }
318 427ab7bf Leszek Koltunski
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320
   
321 bc0a685b Leszek Koltunski
  private int retNumSpaces(String str)
322
    {
323
    int begin=0, ret=0;
324 427ab7bf Leszek Koltunski
325 bc0a685b Leszek Koltunski
    while( true )
326
      {
327
      begin = str.indexOf(' ', begin) +1;
328 427ab7bf Leszek Koltunski
329 bc0a685b Leszek Koltunski
      if( begin>0 ) ret++;
330
      else break;
331 427ab7bf Leszek Koltunski
      }
332 bc0a685b Leszek Koltunski
333
    return ret;
334
    }
335 427ab7bf Leszek Koltunski
    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337 c5a28eb8 Leszek Koltunski
// the library sending messages to us. This is running on a library 'MessageSender' thread.
338 427ab7bf Leszek Koltunski
339 c5a28eb8 Leszek Koltunski
  public void effectMessage(final EffectMessage em, final long effectID, final EffectNames effectName, final long objectID, final String message)
340 bc0a685b Leszek Koltunski
    {
341
    if( em==EffectMessage.EFFECT_FINISHED )
342 427ab7bf Leszek Koltunski
      {
343 c5a28eb8 Leszek Koltunski
      if( objectID == gffaID )
344 427ab7bf Leszek Koltunski
        {
345 bc0a685b Leszek Koltunski
        mRoot.detach(mGFFA);   
346
        mGFFA.removeEventListener(this);
347 427ab7bf Leszek Koltunski
       
348 bc0a685b Leszek Koltunski
        int screenW=mScreen.getWidth();
349
        int screenH=mScreen.getHeight();
350 427ab7bf Leszek Koltunski
        
351 bc0a685b Leszek Koltunski
        int logoW = mLogo.getWidth();
352
        int logoH = mLogo.getHeight();
353 427ab7bf Leszek Koltunski
      
354 bc0a685b Leszek Koltunski
        int initSize= (int)(3.0f*screenW/logoW);
355
        int finaSize= (int)(0.1f*screenW/logoW);
356 427ab7bf Leszek Koltunski
      
357 f988589e Leszek Koltunski
        Dynamic3D di = new Dynamic3D(10000,0.5f);
358 7589635e Leszek Koltunski
        di.add(new Static3D(initSize,initSize,1));
359
        di.add(new Static3D(finaSize,finaSize,1));
360 f988589e Leszek Koltunski
361 7589635e Leszek Koltunski
        mLogo.move( new Static3D(screenW/2,screenH/2,0) );
362 bc0a685b Leszek Koltunski
        mLogo.scale(di);
363 7589635e Leszek Koltunski
        mLogo.move( new Static3D(-logoW/2,-logoH/2,0) );
364 427ab7bf Leszek Koltunski
      
365 e8b6aa95 Leszek Koltunski
        mRoot.attach(mLogo,mQuad);
366 bc0a685b Leszek Koltunski
        mLogo.addEventListener(this);
367
        }
368 c5a28eb8 Leszek Koltunski
      else if( objectID==logoID )
369 bc0a685b Leszek Koltunski
        {
370
        mRoot.detach(mLogo);   
371
        mLogo.removeEventListener(this);
372 427ab7bf Leszek Koltunski
        
373 bc0a685b Leszek Koltunski
        int crawlW = mCrawl.getWidth();
374
        int crawlH = mCrawl.getHeight();
375
        int screenW= mScreen.getWidth();
376
        int screenH= mScreen.getHeight();
377
        int backH  = mCrawlBackground.getHeight();
378
        float scale= (float)screenW/crawlW;
379
      
380 f988589e Leszek Koltunski
        Dynamic3D di = new Dynamic3D(60000,0.5f);
381 7589635e Leszek Koltunski
        di.add(new Static3D(screenW/2,+backH       , 0));
382
        di.add(new Static3D(screenW/2,-scale*crawlH, 0));
383 427ab7bf Leszek Koltunski
        
384 7589635e Leszek Koltunski
        mCrawlBackground.move( new Static3D(0,screenH-backH,0) );
385 59bbb86a Leszek Koltunski
        mCrawlBackground.rotate( new Static1D(CRAWL_ANGLE), new Static3D(1,0,0), new Static3D(screenW/2,backH,0) );
386 427ab7bf Leszek Koltunski
        
387 59759251 Leszek Koltunski
        final int transpDist = 5;
388
        Static4D region = new Static4D(screenW/2,(1-transpDist)*backH,transpDist*backH,transpDist*backH);
389
        mCrawlBackground.alpha(new Static1D(1-transpDist/2), region, true);
390 427ab7bf Leszek Koltunski
        
391 bc0a685b Leszek Koltunski
        mCrawl.move(di);
392 7589635e Leszek Koltunski
        mCrawl.scale( new Static3D(scale,scale,scale) );
393
        mCrawl.move( new Static3D(-crawlW/2,0,0) );
394 427ab7bf Leszek Koltunski
        
395 e8b6aa95 Leszek Koltunski
        mBackground = mRoot.attach(mCrawlBackground,mQuad);
396
        mBackground.attach(mCrawl,mQuad);
397 bc0a685b Leszek Koltunski
        mCrawl.addEventListener(this);
398
        }
399 c5a28eb8 Leszek Koltunski
      else if( objectID==crawlID )
400 bc0a685b Leszek Koltunski
        {
401
        mRoot.detach(mBackground);
402
        mBackground.detach(mCrawl);
403
        mCrawl.removeEventListener(this);
404 427ab7bf Leszek Koltunski
        }
405
      }
406 bc0a685b Leszek Koltunski
    }
407
  }