Project

General

Profile

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

examples / src / main / java / org / distorted / examples / quaternion / QuaternionRenderer.java @ 8a99c681

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.MeshObject;
35
import org.distorted.library.type.Dynamic;
36
import org.distorted.library.type.DynamicQuat;
37
import org.distorted.library.MeshCubes;
38
import org.distorted.library.DistortedTexture;
39
import org.distorted.library.type.Static4D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.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 DistortedEffects mEffects;
56
  private MeshObject mMesh;
57
  private DistortedScreen mScreen;
58
  private DynamicQuat mRot;
59
    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  QuaternionRenderer(GLSurfaceView v)
63
    {
64
    mView    = v;
65
    mTexture = new DistortedTexture(1,1);
66
    mEffects = new DistortedEffects();
67
    mMesh    = new MeshCubes(1,1,false);
68
    mRot     = new DynamicQuat();
69

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

    
89
    mScreen = new DistortedScreen();
90
    mScreen.attach(mTexture,mEffects,mMesh);
91
    }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
   
95
  public void onDrawFrame(GL10 glUnused) 
96
    {
97
    mScreen.render( System.currentTimeMillis() );
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
    
102
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
103
    {
104
    float w = mTexture.getWidth();
105
    float h = mTexture.getHeight();
106
    float d = mTexture.getDepth(mMesh);
107

    
108
    float scaleFactor = 0.5f*(width>height ? height/h:width/w);
109
    Static3D center = new Static3D(w/2,h/2,-d/2);
110

    
111
    mEffects.abortEffects(EffectTypes.MATRIX);
112
    mEffects.move( new Static3D( (width-scaleFactor*w)/2 , (height-scaleFactor*h)/2 , 0) );
113
    mEffects.scale(scaleFactor);
114
    mEffects.quaternion( mRot,center );
115
       
116
    mScreen.resize(width, height);
117
    }
118

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