Project

General

Profile

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

examples / src / main / java / org / distorted / examples / catanddog / CatAndDogRenderer.java @ d218d64e

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
20 f26ab2fd Leszek Koltunski
package org.distorted.examples.catanddog;
21 427ab7bf Leszek Koltunski
22
import java.io.IOException;
23
import java.io.InputStream;
24
25
import javax.microedition.khronos.egl.EGLConfig;
26
import javax.microedition.khronos.opengles.GL10;
27
28 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
29
import org.distorted.library.Distorted;
30 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
31 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
32 d218d64e leszek
import org.distorted.library.DistortedScreen;
33 40eef4b9 Leszek Koltunski
import org.distorted.library.DistortedTexture;
34 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
35 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
36 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
37
import org.distorted.library.type.Dynamic3D;
38
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41 427ab7bf Leszek Koltunski
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
45 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 f26ab2fd Leszek Koltunski
class CatAndDogRenderer implements GLSurfaceView.Renderer
50 427ab7bf Leszek Koltunski
{
51
    private GLSurfaceView mView;
52 d04a4886 Leszek Koltunski
    private DistortedEffects mEffects;
53 40eef4b9 Leszek Koltunski
    private DistortedTexture mTexture;
54 b01acdaf Leszek Koltunski
    private MeshFlat mMesh;
55 d218d64e leszek
    private DistortedScreen mScreen;
56 427ab7bf Leszek Koltunski
    private int bmpHeight, bmpWidth;
57 59759251 Leszek Koltunski
58 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 6f3024ae Leszek Koltunski
    CatAndDogRenderer(GLSurfaceView v)
61 427ab7bf Leszek Koltunski
      {   
62
      mView = v;
63 40eef4b9 Leszek Koltunski
64 b01acdaf Leszek Koltunski
      mMesh = new MeshFlat(1,1);  // no vertex effects, grid can be a (1x1) quad.
65 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
66 40eef4b9 Leszek Koltunski
67
      Static4D chromaRegion= new Static4D( 530, 200,100,100);
68
      Static4D alphaRegion = new Static4D( 230, 200,100,100);
69
70
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
71
      chromaDyn.add(new Static1D(1));
72
      chromaDyn.add(new Static1D(0));
73
74 392e16fd Leszek Koltunski
      mEffects.chroma(chromaDyn, new Static3D(1,0,0), chromaRegion ,true);
75 40eef4b9 Leszek Koltunski
76
      Dynamic1D alphaDyn = new Dynamic1D(3000,0.0f);
77
      alphaDyn.add(new Static1D(1));
78
      alphaDyn.add(new Static1D(0));
79
80 392e16fd Leszek Koltunski
      mEffects.alpha( alphaDyn, alphaRegion, false );
81
82 d218d64e leszek
      mScreen = new DistortedScreen();
83 427ab7bf Leszek Koltunski
      }
84
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
   
87
    public void onDrawFrame(GL10 glUnused) 
88
      {
89 41a81a14 Leszek Koltunski
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
90 b01acdaf Leszek Koltunski
      mScreen.renderTo(mTexture, mMesh, mEffects, System.currentTimeMillis() );
91 427ab7bf Leszek Koltunski
      }
92
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
    
95
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
96 e7a4ef16 Leszek Koltunski
      {
97 41a81a14 Leszek Koltunski
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
98 e7a4ef16 Leszek Koltunski
99 427ab7bf Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat_and_dog);
100
      Bitmap bitmap;
101
        
102
      try 
103
        {
104
        bitmap = BitmapFactory.decodeStream(is);
105
        } 
106
      finally 
107
        {
108
        try 
109
          {
110
          is.close();
111
          } 
112
        catch(IOException e) { }
113
        }  
114
      
115
      bmpHeight = bitmap.getHeight();
116
      bmpWidth  = bitmap.getWidth();
117 e8b6aa95 Leszek Koltunski
118 7451c98a Leszek Koltunski
      mTexture = new DistortedTexture(bmpWidth,bmpHeight);
119 40eef4b9 Leszek Koltunski
      mTexture.setTexture(bitmap);
120 59759251 Leszek Koltunski
121 427ab7bf Leszek Koltunski
      try
122
        {
123 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
124 427ab7bf Leszek Koltunski
        }
125
      catch(Exception ex)
126
        {
127
        android.util.Log.e("Renderer", ex.getMessage() );
128
        }
129
      }
130
    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
    
133
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
134
      {
135 6f3024ae Leszek Koltunski
      int duration = 10000;   // 10 seconds
136 427ab7bf Leszek Koltunski
      
137 f988589e Leszek Koltunski
      Dynamic3D diMove = new Dynamic3D(duration,0.0f);
138 7589635e Leszek Koltunski
      diMove.add(new Static3D(width-bmpWidth,height-bmpHeight,0));
139
      diMove.add(new Static3D(0,0,0));
140 427ab7bf Leszek Koltunski
      
141 f988589e Leszek Koltunski
      Dynamic3D diScale = new Dynamic3D(duration,0.0f);
142 7589635e Leszek Koltunski
      diScale.add(new Static3D(1,1,1));
143
      diScale.add(new Static3D(0.33f,0.33f,1));
144 427ab7bf Leszek Koltunski
      
145 f988589e Leszek Koltunski
      Dynamic1D diRotate = new Dynamic1D(duration,0.0f);
146 7589635e Leszek Koltunski
      diRotate.add(new Static1D(  0));
147
      diRotate.add(new Static1D(360));
148 427ab7bf Leszek Koltunski
      
149 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
150 427ab7bf Leszek Koltunski
151 392e16fd Leszek Koltunski
      mEffects.move(diMove);
152
      mEffects.scale(diScale);
153
      mEffects.rotate( diRotate, new Static3D(0,0,1), new Static3D(bmpWidth/2,bmpHeight/2,0) );
154 7589635e Leszek Koltunski
155 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
156 427ab7bf Leszek Koltunski
      }
157
}