Project

General

Profile

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

examples / src / main / java / org / distorted / examples / quaternion / QuaternionRenderer.java @ 7589635e

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.EffectTypes;
32
import org.distorted.library.type.Dynamic;
33
import org.distorted.library.type.DynamicQuat;
34
import org.distorted.library.DistortedCubes;
35
import org.distorted.library.type.Static4D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.Distorted;
38

    
39
import android.graphics.Bitmap;
40
import android.graphics.BitmapFactory;
41
import android.opengl.GLES20;
42
import android.opengl.GLSurfaceView;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
class QuaternionRenderer implements GLSurfaceView.Renderer 
47
  {
48
  private static final int NUM_QUATERNIONS = 5;
49
  private static final int SIZE = 100;
50
	
51
  private GLSurfaceView mView;
52
  private DistortedCubes mCube;
53
  private Random mRnd = new Random(System.currentTimeMillis());
54
  private DynamicQuat mRot;
55
    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  public QuaternionRenderer(GLSurfaceView v) 
59
    {
60
    mView = v;
61
    mCube = new DistortedCubes( 3, "000010000", SIZE);
62
      
63
    mRot = new DynamicQuat();
64
      
65
    float x,y,z,w, len;
66
      
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;  
73
    	 
74
      len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
75
    		
76
      mRot.add(new Static4D(x/len,y/len,z/len,w/len));
77
      }
78
    
79
    mRot.setCount(0);
80
    mRot.setDuration(8000);
81
    mRot.setMode(Dynamic.MODE_LOOP);
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
   
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);
90
      
91
    mCube.draw(System.currentTimeMillis());
92
    }
93

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

    
100
    if( width > height )
101
      {
102
      float factor = height/(3.0f*SIZE);
103
      mCube.move( new Static3D((width-height)/2,0,0) );
104
      mCube.scale( new Static3D(factor,factor,factor) );
105
      }  
106
    else
107
      {
108
      float factor = width/(3.0f*SIZE);
109
      mCube.move( new Static3D(0,(height-width)/2,0) );
110
      mCube.scale( new Static3D(factor,factor,factor) );
111
      }
112
     
113
    mCube.quaternion( new Static3D(3*SIZE/2,3*SIZE/2,0), mRot);
114
       
115
    Distorted.onSurfaceChanged(width, height); 
116
    }
117

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