Project

General

Profile

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

examples / src / main / java / org / distorted / examples / quaternion / QuaternionRenderer.java @ dc10a48d

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

    
22
import java.io.IOException;
23
import java.io.InputStream;
24
import java.util.Random;
25

    
26
import javax.microedition.khronos.egl.EGLConfig;
27
import javax.microedition.khronos.opengles.GL10;
28

    
29
import org.distorted.examples.R;
30

    
31
import org.distorted.library.effect.MatrixEffectQuaternion;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.mesh.MeshCubes;
38
import org.distorted.library.type.Dynamic;
39
import org.distorted.library.type.DynamicQuat;
40
import org.distorted.library.type.Static4D;
41
import org.distorted.library.type.Static3D;
42

    
43
import android.app.ActivityManager;
44
import android.content.Context;
45
import android.content.pm.ConfigurationInfo;
46
import android.content.res.Resources;
47
import android.graphics.Bitmap;
48
import android.graphics.BitmapFactory;
49
import android.opengl.GLSurfaceView;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
class QuaternionRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
54
  {
55
  private static final int NUM_QUATERNIONS = 5;
56

    
57
  private final GLSurfaceView mView;
58
  private final Resources mResources;
59
  private final DistortedTexture mTexture;
60
  private final DistortedScreen mScreen;
61
  private final Static3D mScale;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  QuaternionRenderer(GLSurfaceView v)
66
    {
67
    mView = v;
68
    mResources = v.getResources();
69

    
70
    mTexture = new DistortedTexture();
71

    
72
    DistortedEffects effects = new DistortedEffects();
73
    DynamicQuat rot = new DynamicQuat();
74

    
75
    Random rnd = new Random(System.currentTimeMillis());
76
    float x,y,z,w, len;
77
      
78
    for(int i=0; i<NUM_QUATERNIONS; i++)
79
      {
80
      x = 2*rnd.nextFloat()-1;
81
      y = 2*rnd.nextFloat()-1;
82
      z = 2*rnd.nextFloat()-1;
83
      w = 2*rnd.nextFloat()-1;
84
    	 
85
      len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
86
    		
87
      rot.add(new Static4D(x/len,y/len,z/len,w/len));
88
      }
89
    
90
    rot.setCount(0);
91
    rot.setDuration(8000);
92
    rot.setMode(Dynamic.MODE_LOOP);
93

    
94
    mScale  = new Static3D(1,1,1);
95

    
96
    effects.apply(new MatrixEffectScale(mScale));
97
    effects.apply( new MatrixEffectQuaternion(rot,new Static3D(0,0,0)) );
98

    
99
    mScreen = new DistortedScreen();
100
    mScreen.attach(mTexture,effects,new MeshCubes(1,1,1));
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
   
105
  public void onDrawFrame(GL10 glUnused) 
106
    {
107
    mScreen.render( System.currentTimeMillis() );
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
    
112
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
113
    {
114
    float factor = 0.5f*Math.min(width,height);
115

    
116
    mScale.set(factor,factor,factor);
117
    mScreen.resize(width, height);
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
123
    {
124
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
125
    Bitmap bitmap;
126
        
127
    try 
128
      {
129
      bitmap = BitmapFactory.decodeStream(is);
130
      } 
131
    finally 
132
      {
133
      try 
134
        {
135
        is.close();
136
        } 
137
      catch(IOException ignored) { }
138
      }  
139
      
140
    mTexture.setTexture(bitmap);
141

    
142
    DistortedLibrary.onSurfaceCreated(this);
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  public void distortedException(Exception ex)
148
    {
149
    android.util.Log.e("Quaternion", ex.getMessage() );
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  public int openGlVersion()
155
    {
156
    Context context = mView.getContext();
157
    final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
158
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
159
    int glESversion = configurationInfo.reqGlEsVersion;
160
    int major = glESversion >> 16;
161
    int minor = glESversion & 0xff;
162

    
163
    return 100*major + 10*minor;
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  public InputStream localFile(int fileID)
169
    {
170
    return mResources.openRawResource(fileID);
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  public void logMessage(String message)
176
    {
177
    android.util.Log.e("Quaternion", message );
178
    }
179
  }
(2-2/3)