Project

General

Profile

« Previous | Next » 

Revision bc0a685b

Added by Leszek Koltunski almost 8 years ago

Add License

View differences:

src/main/java/org/distorted/examples/quaternion/QuaternionRenderer.java
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
///////////////////////////////////////////////////////////////////////////////////////////////////
1 19

  
2 20
package org.distorted.examples.quaternion;
3 21

  
......
26 44
///////////////////////////////////////////////////////////////////////////////////////////////////
27 45

  
28 46
class QuaternionRenderer implements GLSurfaceView.Renderer 
29
{
30
	private static final int NUM_QUATERNIONS = 5;
31
	private static final int SIZE = 100;
47
  {
48
  private static final int NUM_QUATERNIONS = 5;
49
  private static final int SIZE = 100;
32 50
	
33
    private GLSurfaceView mView;
34
    private DistortedCubes mCube;
35
    private Random mRnd = new Random(System.currentTimeMillis());
36
    private InterpolatorQuat mRot;
51
  private GLSurfaceView mView;
52
  private DistortedCubes mCube;
53
  private Random mRnd = new Random(System.currentTimeMillis());
54
  private InterpolatorQuat mRot;
37 55
    
38 56
///////////////////////////////////////////////////////////////////////////////////////////////////
39 57

  
40
    public QuaternionRenderer(GLSurfaceView v) 
41
      {
42
      mView = v;
43
      mCube = new DistortedCubes( 3, "000010000", SIZE);
58
  public QuaternionRenderer(GLSurfaceView v) 
59
    {
60
    mView = v;
61
    mCube = new DistortedCubes( 3, "000010000", SIZE);
44 62
      
45
      mRot = new InterpolatorQuat();
63
    mRot = new InterpolatorQuat();
46 64
      
47
      float x,y,z,w, len;
65
    float x,y,z,w, len;
48 66
      
49
      for(int i=0; i<NUM_QUATERNIONS; i++)
50
        {
51
        x = 2*mRnd.nextFloat()-1;  
52
        y = 2*mRnd.nextFloat()-1;  
53
        z = 2*mRnd.nextFloat()-1;  
54
        w = 2*mRnd.nextFloat()-1;  
67
    for(int i=0; i<NUM_QUATERNIONS; i++)
68
      {
69
      x = 2*mRnd.nextFloat()-1;  
70
      y = 2*mRnd.nextFloat()-1;  
71
      z = 2*mRnd.nextFloat()-1;  
72
      w = 2*mRnd.nextFloat()-1;  
55 73
    	 
56
        len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
74
      len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
57 75
    		
58
        mRot.add(new Float4D(x/len,y/len,z/len,w/len));
59
        }
60
    
61
      mRot.setCount(0);
62
      mRot.setDuration(8000);
63
      mRot.setMode(Interpolator.MODE_LOOP);
76
      mRot.add(new Float4D(x/len,y/len,z/len,w/len));
64 77
      }
78
    
79
    mRot.setCount(0);
80
    mRot.setDuration(8000);
81
    mRot.setMode(Interpolator.MODE_LOOP);
82
    }
65 83

  
66 84
///////////////////////////////////////////////////////////////////////////////////////////////////
67 85
   
68
    public void onDrawFrame(GL10 glUnused) 
69
      {
70
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
71
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
86
  public void onDrawFrame(GL10 glUnused) 
87
    {
88
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
89
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
72 90
      
73
      mCube.draw(System.currentTimeMillis());
74
      }
91
    mCube.draw(System.currentTimeMillis());
92
    }
75 93

  
76 94
///////////////////////////////////////////////////////////////////////////////////////////////////
77 95
    
78
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
79
      {
80
      mCube.abortEffects(EffectTypes.MATRIX);
96
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
97
    {
98
    mCube.abortEffects(EffectTypes.MATRIX);
81 99

  
82
      if( width > height )
83
        {
84
        mCube.move((width-height)/2 ,0, 0);
85
        mCube.scale(height/(3.0f*SIZE));
86
        }  
87
      else
88
        {   
89
        mCube.move(0 ,(height-width)/2, 0);
90
        mCube.scale(width/(3.0f*SIZE));
91
        }
100
    if( width > height )
101
      {
102
      mCube.move((width-height)/2 ,0, 0);
103
      mCube.scale(height/(3.0f*SIZE));
104
      }  
105
    else
106
      {   
107
      mCube.move(0 ,(height-width)/2, 0);
108
      mCube.scale(width/(3.0f*SIZE));
109
      }
92 110
     
93
      mCube.quaternion( new Float3D(3*SIZE/2,3*SIZE/2,0), mRot);
111
    mCube.quaternion( new Float3D(3*SIZE/2,3*SIZE/2,0), mRot);
94 112
       
95
      Distorted.onSurfaceChanged(width, height); 
96
      }
113
    Distorted.onSurfaceChanged(width, height); 
114
    }
97 115

  
98 116
///////////////////////////////////////////////////////////////////////////////////////////////////
99 117
    
100
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
101
      {
102
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
103
      Bitmap bitmap;
118
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
119
    {
120
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
121
    Bitmap bitmap;
104 122
        
123
    try 
124
      {
125
      bitmap = BitmapFactory.decodeStream(is);
126
      } 
127
    finally 
128
      {
105 129
      try 
106 130
        {
107
        bitmap = BitmapFactory.decodeStream(is);
131
        is.close();
108 132
        } 
109
      finally 
110
        {
111
        try 
112
          {
113
          is.close();
114
          } 
115
        catch(IOException e) { }
116
        }  
133
      catch(IOException e) { }
134
      }  
117 135
      
118
      mCube.setBitmap(bitmap);
136
    mCube.setBitmap(bitmap);
119 137
      
120
      try
121
        {
122
        Distorted.onSurfaceCreated(mView.getContext());
123
        }
124
      catch(Exception ex)
125
        {
126
        android.util.Log.e("Quaternion", ex.getMessage() );
127
        }
138
    try
139
      {
140
      Distorted.onSurfaceCreated(mView.getContext());
141
      }
142
    catch(Exception ex)
143
      {
144
      android.util.Log.e("Quaternion", ex.getMessage() );
128 145
      }
129
}
146
    }
147
  }

Also available in: Unified diff