Project

General

Profile

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

examples / src / main / java / org / distorted / examples / quaternion / QuaternionRenderer.java @ b695a86a

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.quaternion;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
30 427ab7bf Leszek Koltunski
31 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
32 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic;
33
import org.distorted.library.type.DynamicQuat;
34 5068fa06 Leszek Koltunski
import org.distorted.library.DistortedCubes;
35 7589635e Leszek Koltunski
import org.distorted.library.type.Static4D;
36
import org.distorted.library.type.Static3D;
37 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
38 427ab7bf Leszek Koltunski
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 bc0a685b Leszek Koltunski
  {
48
  private static final int NUM_QUATERNIONS = 5;
49
  private static final int SIZE = 100;
50 427ab7bf Leszek Koltunski
	
51 bc0a685b Leszek Koltunski
  private GLSurfaceView mView;
52
  private DistortedCubes mCube;
53 b695a86a Leszek Koltunski
54 7589635e Leszek Koltunski
  private DynamicQuat mRot;
55 427ab7bf Leszek Koltunski
    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58 b695a86a Leszek Koltunski
  QuaternionRenderer(GLSurfaceView v)
59 bc0a685b Leszek Koltunski
    {
60
    mView = v;
61
    mCube = new DistortedCubes( 3, "000010000", SIZE);
62 427ab7bf Leszek Koltunski
      
63 7589635e Leszek Koltunski
    mRot = new DynamicQuat();
64 b695a86a Leszek Koltunski
65
    Random rnd = new Random(System.currentTimeMillis());
66 bc0a685b Leszek Koltunski
    float x,y,z,w, len;
67 427ab7bf Leszek Koltunski
      
68 bc0a685b Leszek Koltunski
    for(int i=0; i<NUM_QUATERNIONS; i++)
69
      {
70 b695a86a Leszek Koltunski
      x = 2*rnd.nextFloat()-1;
71
      y = 2*rnd.nextFloat()-1;
72
      z = 2*rnd.nextFloat()-1;
73
      w = 2*rnd.nextFloat()-1;
74 427ab7bf Leszek Koltunski
    	 
75 bc0a685b Leszek Koltunski
      len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
76 427ab7bf Leszek Koltunski
    		
77 7589635e Leszek Koltunski
      mRot.add(new Static4D(x/len,y/len,z/len,w/len));
78 427ab7bf Leszek Koltunski
      }
79 bc0a685b Leszek Koltunski
    
80
    mRot.setCount(0);
81
    mRot.setDuration(8000);
82 7589635e Leszek Koltunski
    mRot.setMode(Dynamic.MODE_LOOP);
83 bc0a685b Leszek Koltunski
    }
84 427ab7bf Leszek Koltunski
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
   
87 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused) 
88
    {
89
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
90
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
91 427ab7bf Leszek Koltunski
      
92 bc0a685b Leszek Koltunski
    mCube.draw(System.currentTimeMillis());
93
    }
94 427ab7bf Leszek Koltunski
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
    
97 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
98
    {
99
    mCube.abortEffects(EffectTypes.MATRIX);
100 427ab7bf Leszek Koltunski
101 bc0a685b Leszek Koltunski
    if( width > height )
102
      {
103 7589635e Leszek Koltunski
      mCube.move( new Static3D((width-height)/2,0,0) );
104 e2d8ea1d Leszek Koltunski
      mCube.scale(height/(3.0f*SIZE));
105 bc0a685b Leszek Koltunski
      }  
106
    else
107 7589635e Leszek Koltunski
      {
108
      mCube.move( new Static3D(0,(height-width)/2,0) );
109 e2d8ea1d Leszek Koltunski
      mCube.scale(width/(3.0f*SIZE));
110 bc0a685b Leszek Koltunski
      }
111 427ab7bf Leszek Koltunski
     
112 59bbb86a Leszek Koltunski
    mCube.quaternion( mRot, new Static3D(3*SIZE/2,3*SIZE/2,0) );
113 427ab7bf Leszek Koltunski
       
114 bc0a685b Leszek Koltunski
    Distorted.onSurfaceChanged(width, height); 
115
    }
116 427ab7bf Leszek Koltunski
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
    
119 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
120
    {
121
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
122
    Bitmap bitmap;
123 427ab7bf Leszek Koltunski
        
124 bc0a685b Leszek Koltunski
    try 
125
      {
126
      bitmap = BitmapFactory.decodeStream(is);
127
      } 
128
    finally 
129
      {
130 427ab7bf Leszek Koltunski
      try 
131
        {
132 bc0a685b Leszek Koltunski
        is.close();
133 427ab7bf Leszek Koltunski
        } 
134 bc0a685b Leszek Koltunski
      catch(IOException e) { }
135
      }  
136 427ab7bf Leszek Koltunski
      
137 bc0a685b Leszek Koltunski
    mCube.setBitmap(bitmap);
138 427ab7bf Leszek Koltunski
      
139 bc0a685b Leszek Koltunski
    try
140
      {
141
      Distorted.onSurfaceCreated(mView.getContext());
142
      }
143
    catch(Exception ex)
144
      {
145
      android.util.Log.e("Quaternion", ex.getMessage() );
146 427ab7bf Leszek Koltunski
      }
147 bc0a685b Leszek Koltunski
    }
148
  }