Project

General

Profile

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

examples / src / main / java / org / distorted / examples / quaternion / QuaternionRenderer.java @ 19938eb7

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.DistortedEffects;
32
import org.distorted.library.DistortedScreen;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.Dynamic;
35
import org.distorted.library.type.DynamicQuat;
36
import org.distorted.library.MeshCubes;
37
import org.distorted.library.DistortedTexture;
38
import org.distorted.library.type.Static4D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.Distorted;
41

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

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

    
48
class QuaternionRenderer implements GLSurfaceView.Renderer 
49
  {
50
  private static final int NUM_QUATERNIONS = 5;
51

    
52
  private GLSurfaceView mView;
53
  private DistortedTexture mTexture;
54
  private DistortedEffects mEffects;
55
  private DistortedScreen mScreen;
56
  private DynamicQuat mRot;
57
    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  QuaternionRenderer(GLSurfaceView v)
61
    {
62
    mView    = v;
63
    mTexture = new DistortedTexture(1,1);
64
    mEffects = new DistortedEffects();
65
    mRot     = new DynamicQuat();
66

    
67
    Random rnd = new Random(System.currentTimeMillis());
68
    float x,y,z,w, len;
69
      
70
    for(int i=0; i<NUM_QUATERNIONS; i++)
71
      {
72
      x = 2*rnd.nextFloat()-1;
73
      y = 2*rnd.nextFloat()-1;
74
      z = 2*rnd.nextFloat()-1;
75
      w = 2*rnd.nextFloat()-1;
76
    	 
77
      len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
78
    		
79
      mRot.add(new Static4D(x/len,y/len,z/len,w/len));
80
      }
81
    
82
    mRot.setCount(0);
83
    mRot.setDuration(8000);
84
    mRot.setMode(Dynamic.MODE_LOOP);
85

    
86
    mScreen = new DistortedScreen();
87
    mScreen.attach(mTexture,mEffects,new MeshCubes(1,1,false));
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
   
92
  public void onDrawFrame(GL10 glUnused) 
93
    {
94
    mScreen.render( System.currentTimeMillis() );
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
    
99
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
100
    {
101
    float qx = (float)width ;
102
    float qy = (float)height;
103
    float scale = 0.5f;
104

    
105
    mEffects.abortEffects(EffectTypes.MATRIX);
106
    mEffects.quaternion( mRot, new Static3D(0,0,0) );
107
    mEffects.scale(  qx<qy ? (new Static3D(scale,scale*qx/qy,scale)) : (new Static3D(scale*qy/qx,scale,scale)) );
108

    
109
    mScreen.resize(width, height);
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
115
    {
116
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
117
    Bitmap bitmap;
118
        
119
    try 
120
      {
121
      bitmap = BitmapFactory.decodeStream(is);
122
      } 
123
    finally 
124
      {
125
      try 
126
        {
127
        is.close();
128
        } 
129
      catch(IOException e) { }
130
      }  
131
      
132
    mTexture.setTexture(bitmap);
133
      
134
    try
135
      {
136
      Distorted.onCreate(mView.getContext());
137
      }
138
    catch(Exception ex)
139
      {
140
      android.util.Log.e("Quaternion", ex.getMessage() );
141
      }
142
    }
143
  }
(2-2/3)