Project

General

Profile

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

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

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

    
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
import org.distorted.examples.R;
29
import org.distorted.library.Distorted;
30
import org.distorted.library.DistortedEffects;
31
import org.distorted.library.DistortedScreen;
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.MeshFlat;
34
import org.distorted.library.EffectTypes;
35
import org.distorted.library.type.Dynamic1D;
36
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.Static1D;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40

    
41
import android.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43
import android.opengl.GLES30;
44
import android.opengl.GLSurfaceView;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
class CatAndDogRenderer implements GLSurfaceView.Renderer
49
{
50
    private GLSurfaceView mView;
51
    private DistortedEffects mEffects;
52
    private MeshFlat mMesh;
53
    private DistortedScreen mScreen;
54
    private int bmpHeight, bmpWidth;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
    CatAndDogRenderer(GLSurfaceView v)
59
      {   
60
      mView = v;
61

    
62
      mMesh = new MeshFlat(1,1);  // no vertex effects, grid can be a (1x1) quad.
63
      mEffects = new DistortedEffects();
64

    
65
      Static4D chromaRegion= new Static4D( 530, 200,100,100);
66
      Static4D alphaRegion = new Static4D( 230, 200,100,100);
67

    
68
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
69
      chromaDyn.add(new Static1D(1));
70
      chromaDyn.add(new Static1D(0));
71

    
72
      mEffects.chroma(chromaDyn, new Static3D(1,0,0), chromaRegion ,true);
73

    
74
      Dynamic1D alphaDyn = new Dynamic1D(3000,0.0f);
75
      alphaDyn.add(new Static1D(1));
76
      alphaDyn.add(new Static1D(0));
77

    
78
      mEffects.alpha( alphaDyn, alphaRegion, false );
79

    
80
      mScreen = new DistortedScreen();
81
      }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
   
85
    public void onDrawFrame(GL10 glUnused) 
86
      {
87
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
88
      mScreen.render( System.currentTimeMillis() );
89
      }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
    
93
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
94
      {
95
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
96

    
97
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat_and_dog);
98
      Bitmap bitmap;
99
        
100
      try 
101
        {
102
        bitmap = BitmapFactory.decodeStream(is);
103
        } 
104
      finally 
105
        {
106
        try 
107
          {
108
          is.close();
109
          } 
110
        catch(IOException e) { }
111
        }  
112
      
113
      bmpHeight = bitmap.getHeight();
114
      bmpWidth  = bitmap.getWidth();
115

    
116
      DistortedTexture texture = new DistortedTexture(bmpWidth,bmpHeight);
117
      texture.setTexture(bitmap);
118

    
119
      mScreen.detachAll();
120
      mScreen.attach(texture,mEffects,mMesh);
121

    
122
      try
123
        {
124
        Distorted.onCreate(mView.getContext());
125
        }
126
      catch(Exception ex)
127
        {
128
        android.util.Log.e("Renderer", ex.getMessage() );
129
        }
130
      }
131
    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
    
134
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
135
      {
136
      int duration = 10000;   // 10 seconds
137
      
138
      Dynamic3D diMove = new Dynamic3D(duration,0.0f);
139
      diMove.add(new Static3D(width-bmpWidth,height-bmpHeight,0));
140
      diMove.add(new Static3D(0,0,0));
141
      
142
      Dynamic3D diScale = new Dynamic3D(duration,0.0f);
143
      diScale.add(new Static3D(1,1,1));
144
      diScale.add(new Static3D(0.33f,0.33f,1));
145
      
146
      Dynamic1D diRotate = new Dynamic1D(duration,0.0f);
147
      diRotate.add(new Static1D(  0));
148
      diRotate.add(new Static1D(360));
149
      
150
      mEffects.abortEffects(EffectTypes.MATRIX);
151

    
152
      mEffects.move(diMove);
153
      mEffects.scale(diScale);
154
      mEffects.rotate( diRotate, new Static3D(0,0,1), new Static3D(bmpWidth/2,bmpHeight/2,0) );
155

    
156
      mScreen.resize(width, height);
157
      }
158
}
(2-2/3)