Project

General

Profile

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

examples / src / main / java / org / distorted / examples / quaternion / QuaternionRenderer.java @ 39a0d81b

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 q= (float)width/height;
102
    float scale = 0.5f;
103

    
104
    mEffects.abortEffects(EffectTypes.MATRIX);
105
    mEffects.quaternion( mRot, new Static3D(0,0,0) );
106
    mEffects.scale(  q<1 ? (new Static3D(scale,scale*q,scale)) : (new Static3D(scale/q,scale,scale/q)) );
107

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

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