Project

General

Profile

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

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