Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 126393c8

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

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

    
26
import org.distorted.examples.R;
27
import org.distorted.library.effect.EffectType;
28
import org.distorted.library.effect.FragmentEffectChroma;
29
import org.distorted.library.effect.FragmentEffectContrast;
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectDistort;
32
import org.distorted.library.effect.VertexEffectPinch;
33
import org.distorted.library.effect.VertexEffectSink;
34
import org.distorted.library.effect.VertexEffectSwirl;
35
import org.distorted.library.main.DistortedLibrary;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.mesh.MeshRectangles;
39
import org.distorted.library.main.DistortedTexture;
40
import org.distorted.library.type.Static3D;
41

    
42
import java.io.IOException;
43
import java.io.InputStream;
44

    
45
import javax.microedition.khronos.egl.EGLConfig;
46
import javax.microedition.khronos.opengles.GL10;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class AroundTheWorldRenderer implements GLSurfaceView.Renderer
51
{
52
   private GLSurfaceView mView;
53
   private DistortedEffects mEffects;
54
   private DistortedTexture mTexture;
55
   private DistortedScreen mScreen;
56
   private MeshRectangles mMesh;
57
   private AroundTheWorldEffectsManager mManager;
58
   private Static3D mScale;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
   AroundTheWorldRenderer(GLSurfaceView view)
63
      {
64
      mScale= new Static3D(1,1,1);
65

    
66
      mView = view;
67
      mManager = new AroundTheWorldEffectsManager();
68
      mEffects = new DistortedEffects();
69
      mEffects.apply(new MatrixEffectScale(mScale));
70

    
71
      mManager.apply(mEffects);
72
      mScreen = new DistortedScreen();
73
      mScreen.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
74
      }
75

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

    
78
   AroundTheWorldEffectsManager getManager()
79
     {
80
     return mManager;
81
     }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
   public void onDrawFrame(GL10 glUnused) 
86
      {
87
      mScreen.render( System.currentTimeMillis() );
88
      }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
    
92
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
93
      {
94
      float horiRatio = (float)width / mMesh.getStretchX();
95
      float vertRatio = (float)height/ mMesh.getStretchY();
96
      float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
97

    
98
      mScale.set( factor,factor,factor );
99
      mScreen.resize(width,height);
100
      }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
    
104
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
105
      {
106
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.face);
107
      Bitmap bitmap;
108

    
109
      try
110
        {
111
        bitmap = BitmapFactory.decodeStream(is);
112
        }
113
      finally
114
        {
115
        try
116
          {
117
          is.close();
118
          }
119
        catch(IOException e) { }
120
        }
121

    
122
      int objWidth = bitmap.getWidth();
123
      int objHeight= bitmap.getHeight();
124

    
125
      if( mTexture==null ) mTexture = new DistortedTexture();
126
      mTexture.setTexture(bitmap);
127

    
128
      if( mMesh==null )
129
        {
130
        mMesh = new MeshRectangles(30,30*objHeight/objWidth);
131
        mMesh.setStretch(objWidth,objHeight,0);
132
        }
133

    
134
      mScreen.detachAll();
135
      mScreen.attach(mTexture, mEffects, mMesh);
136

    
137
      DistortedLibrary.setMax(EffectType.VERTEX  ,12);
138
      DistortedLibrary.setMax(EffectType.FRAGMENT, 9);
139

    
140
      VertexEffectDistort.enable();
141
      VertexEffectSink.enable();
142
      VertexEffectPinch.enable();
143
      VertexEffectSwirl.enable();
144
      FragmentEffectChroma.enable();
145
      FragmentEffectContrast.enable();
146

    
147
      try
148
        {
149
        DistortedLibrary.onCreate(mView.getContext());
150
        }
151
      catch(Exception ex)
152
        {
153
        android.util.Log.e("AroundTheWorld", ex.getMessage() );
154
        }
155
      }
156
}
(3-3/6)