Project

General

Profile

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

examples / src / main / java / org / distorted / examples / olimpic / OlimpicRenderer.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 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.olimpic;
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 427ab7bf Leszek Koltunski
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 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedTree;
34 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
35 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
36 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static1D;
39 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
40 40eef4b9 Leszek Koltunski
import org.distorted.library.DistortedTexture;
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
class OlimpicRenderer implements GLSurfaceView.Renderer 
50
{
51
   private static final int LEAF_SIZE = 100;
52
   private static final int NUM_CIRCLES = 5;
53 e7a4ef16 Leszek Koltunski
   private static final int NUM_LEAVES  = 8;
54 427ab7bf Leszek Koltunski
   
55
   private GLSurfaceView mView;
56 d04a4886 Leszek Koltunski
   private DistortedTree mRoot;
57 40eef4b9 Leszek Koltunski
   private DistortedTexture mLeaf;
58 d218d64e leszek
   private DistortedScreen mScreen;
59 40eef4b9 Leszek Koltunski
   private int mScreenW, mScreenH;
60 880bdf97 Leszek Koltunski
61 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 508f22b5 Leszek Koltunski
   OlimpicRenderer(GLSurfaceView v)
64 427ab7bf Leszek Koltunski
      {     
65
      mView = v;
66 e8b6aa95 Leszek Koltunski
67 7451c98a Leszek Koltunski
      mLeaf = new DistortedTexture(LEAF_SIZE,LEAF_SIZE);
68 b01acdaf Leszek Koltunski
      MeshFlat mesh = new MeshFlat(1,1);
69 d04a4886 Leszek Koltunski
      DistortedEffects effects = new DistortedEffects();
70 40eef4b9 Leszek Koltunski
71
      mScreenW = 9*LEAF_SIZE;
72
      mScreenH = 9*LEAF_SIZE;
73 b01acdaf Leszek Koltunski
      mRoot = new DistortedTree(new DistortedTexture(mScreenW,mScreenH), effects, mesh);
74 427ab7bf Leszek Koltunski
     
75 880bdf97 Leszek Koltunski
      Dynamic1D rot = new Dynamic1D(5000,0.0f);
76
      rot.setMode(Dynamic1D.MODE_JUMP);
77
      rot.add(new Static1D(  0));
78
      rot.add(new Static1D(360));
79 427ab7bf Leszek Koltunski
      
80 d04a4886 Leszek Koltunski
      DistortedTree[] mCircleNode = new DistortedTree[NUM_CIRCLES];
81 427ab7bf Leszek Koltunski
    
82
      int[] colors    = new int[] {0,0,1,  0,0,0,  1,0,0,  1,1,0,  0,1,0}; // blue, black, red, yellow, green  
83 40eef4b9 Leszek Koltunski
      int[] positions = new int[] {0,2*LEAF_SIZE,  3*LEAF_SIZE,2*LEAF_SIZE,  6*LEAF_SIZE,2*LEAF_SIZE,  3*LEAF_SIZE/2,9*LEAF_SIZE/2,  9*LEAF_SIZE/2,9*LEAF_SIZE/2};
84 427ab7bf Leszek Koltunski
      
85 7589635e Leszek Koltunski
      Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
86
      Static3D axis   = new Static3D(0,0,1);
87
      Static3D moveVector = new Static3D(0,LEAF_SIZE,0);
88
89 427ab7bf Leszek Koltunski
      for(int i=0; i<NUM_CIRCLES; i++)
90
        {
91 3c8b1903 Leszek Koltunski
        if( i<=1 )
92 427ab7bf Leszek Koltunski
          {
93 d04a4886 Leszek Koltunski
          effects = new DistortedEffects();
94 b01acdaf Leszek Koltunski
          mCircleNode[i] = new DistortedTree(new DistortedTexture(3*LEAF_SIZE,3*LEAF_SIZE), effects, mesh);
95 427ab7bf Leszek Koltunski
        
96 3c8b1903 Leszek Koltunski
          for(int j=0; j<NUM_LEAVES-i; j++)
97 427ab7bf Leszek Koltunski
            {
98 d04a4886 Leszek Koltunski
            effects = new DistortedEffects();
99 b01acdaf Leszek Koltunski
            mCircleNode[i].attach(mLeaf, effects, mesh);
100 392e16fd Leszek Koltunski
            effects.rotate( new Static1D(j*(360/NUM_LEAVES)), axis, center );
101
            effects.move(moveVector);
102 427ab7bf Leszek Koltunski
            }
103
          }
104
        else
105
          {
106 772dee14 Leszek Koltunski
          mCircleNode[i] = new DistortedTree(mCircleNode[0], Distorted.CLONE_RENDERABLE |Distorted.CLONE_CHILDREN);
107 427ab7bf Leszek Koltunski
          }
108 3c8b1903 Leszek Koltunski
109 392e16fd Leszek Koltunski
        mRoot.attach(mCircleNode[i]);
110 d04a4886 Leszek Koltunski
        effects = mCircleNode[i].getEffects();
111 392e16fd Leszek Koltunski
        effects.move( new Static3D(positions[2*i], positions[2*i+1], 0) );
112
        effects.rotate( rot, axis, center );
113
        effects.chroma( new Static1D(0.5f), new Static3D(colors[3*i],colors[3*i+1], colors[3*i+2]) );
114 427ab7bf Leszek Koltunski
        }
115 392e16fd Leszek Koltunski
116 d218d64e leszek
      mScreen = new DistortedScreen();
117 427ab7bf Leszek Koltunski
      }
118
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
   
121
    public void onDrawFrame(GL10 glUnused) 
122
      {
123 41a81a14 Leszek Koltunski
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
124 bc29e409 Leszek Koltunski
      mScreen.renderTo(mRoot,System.currentTimeMillis());
125 427ab7bf Leszek Koltunski
      }
126
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
    
129
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
130
      {
131 d04a4886 Leszek Koltunski
      DistortedEffects effects = mRoot.getEffects();
132 40eef4b9 Leszek Koltunski
133 392e16fd Leszek Koltunski
      effects.abortEffects(EffectTypes.MATRIX);
134 427ab7bf Leszek Koltunski
      
135 40eef4b9 Leszek Koltunski
      if( (float)mScreenH/mScreenW > (float)height/width )
136 427ab7bf Leszek Koltunski
        {
137 40eef4b9 Leszek Koltunski
        int w = (height*mScreenW)/mScreenH;
138
        float factor = (float)height/mScreenH;
139 7589635e Leszek Koltunski
140 392e16fd Leszek Koltunski
        effects.move( new Static3D((width-w)/2 ,0, 0) );
141
        effects.scale( factor );
142 427ab7bf Leszek Koltunski
        }
143
      else
144
        {
145 40eef4b9 Leszek Koltunski
        int h = (width*mScreenH)/mScreenW;
146
        float factor = (float)width/mScreenW;
147 7589635e Leszek Koltunski
148 392e16fd Leszek Koltunski
        effects.move( new Static3D(0,(height-h)/2,0) );
149
        effects.scale( factor );
150 427ab7bf Leszek Koltunski
        }
151
      
152 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
153 427ab7bf Leszek Koltunski
      }
154
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
    
157
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
158
      {
159 41a81a14 Leszek Koltunski
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
160 e7a4ef16 Leszek Koltunski
161 427ab7bf Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
162
      Bitmap leaf;
163
      
164
      try 
165
        {
166
        leaf = BitmapFactory.decodeStream(is);
167
        } 
168
      finally 
169
        {
170
        try 
171
          {
172
          is.close();
173
          } 
174
        catch(IOException e) { }
175
        }  
176
      
177 e8b6aa95 Leszek Koltunski
      mLeaf.setTexture(leaf);
178 427ab7bf Leszek Koltunski
      
179
      try
180
        {
181 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
182 427ab7bf Leszek Koltunski
        }
183
      catch(Exception ex)
184
        {
185 ed1c0b33 Leszek Koltunski
        android.util.Log.e("Olympic", ex.getMessage() );
186 427ab7bf Leszek Koltunski
        }
187
      }
188
 
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
    
191
}