Project

General

Profile

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

examples / src / main / java / org / distorted / examples / quaternion / QuaternionRenderer.java @ 16b22aab

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.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.mesh.MeshCubes;
37
import org.distorted.library.type.Dynamic;
38
import org.distorted.library.type.DynamicQuat;
39
import org.distorted.library.type.Static4D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.main.Distorted;
42

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

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

    
53
  private GLSurfaceView mView;
54
  private DistortedTexture mTexture;
55
  private DistortedScreen mScreen;
56
  private Static3D mScale;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  QuaternionRenderer(GLSurfaceView v)
61
    {
62
    mView    = v;
63
    mTexture = new DistortedTexture(1,1);
64

    
65
    DistortedEffects effects = new DistortedEffects();
66
    DynamicQuat rot = new DynamicQuat();
67

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

    
87
    mScale  = new Static3D(1,1,1);
88

    
89
    effects.apply(new MatrixEffectScale(mScale));
90
    effects.apply( new MatrixEffectQuaternion(rot,new Static3D(0,0,0)) );
91

    
92
    mScreen = new DistortedScreen();
93
    mScreen.attach(mTexture,effects,new MeshCubes(1,1,1));
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
   
98
  public void onDrawFrame(GL10 glUnused) 
99
    {
100
    mScreen.render( System.currentTimeMillis() );
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
    
105
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
106
    {
107
    float w = mTexture.getWidth();
108
    float h = mTexture.getHeight();
109
    float factor = 0.5f*(width>height ? height/h:width/w);
110

    
111
    mScale.set(factor,factor,factor);
112
    mScreen.resize(width, height);
113
    }
114

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