Project

General

Profile

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

examples / src / main / java / org / distorted / examples / quaternion / QuaternionRenderer.java @ 698ad0a8

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.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.main.DistortedEffects;
35
import org.distorted.library.main.DistortedLibrary;
36
import org.distorted.library.main.DistortedScreen;
37
import org.distorted.library.main.DistortedTexture;
38
import org.distorted.library.mesh.MeshCubes;
39
import org.distorted.library.type.Dynamic;
40
import org.distorted.library.type.DynamicQuat;
41
import org.distorted.library.type.Static4D;
42
import org.distorted.library.type.Static3D;
43

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

    
54
  private GLSurfaceView mView;
55
  private DistortedTexture mTexture;
56
  private DistortedScreen mScreen;
57
  private Static3D mMove, mScale, mCenter;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

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

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

    
88
    mMove   = new Static3D(0,0,0);
89
    mScale  = new Static3D(1,1,1);
90
    mCenter = new Static3D(0,0,0);
91

    
92
    effects.apply( new MatrixEffectQuaternion(rot,mCenter) );
93
    effects.apply( new MatrixEffectScale(mScale));
94
    effects.apply( new MatrixEffectMove(mMove));
95

    
96
    mScreen = new DistortedScreen();
97
    mScreen.attach(mTexture,effects,new MeshCubes(1,1,1));
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
   
102
  public void onDrawFrame(GL10 glUnused) 
103
    {
104
    mScreen.render( System.currentTimeMillis() );
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
110
    {
111
    float factor = 0.5f*(width>height ? height:width);
112

    
113
    mCenter.set(0.5f,0.5f,0.5f);
114
    mMove.set( (width-factor)/2 , (height-factor)/2 , -factor/2 );
115
    mScale.set(factor,factor,factor);
116

    
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 e) { }
138
      }  
139
      
140
    mTexture.setTexture(bitmap);
141
      
142
    try
143
      {
144
      DistortedLibrary.onCreate(mView.getContext());
145
      }
146
    catch(Exception ex)
147
      {
148
      android.util.Log.e("Quaternion", ex.getMessage() );
149
      }
150
    }
151
  }
(2-2/3)