Project

General

Profile

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

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

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 10b7e588 Leszek Koltunski
import org.distorted.library.GridCubes;
35 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
36
import org.distorted.library.DistortedEffectQueues;
37 7589635e Leszek Koltunski
import org.distorted.library.type.Static4D;
38
import org.distorted.library.type.Static3D;
39 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
40 427ab7bf Leszek Koltunski
41
import android.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43
import android.opengl.GLES20;
44
import android.opengl.GLSurfaceView;
45
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48
class QuaternionRenderer implements GLSurfaceView.Renderer 
49 bc0a685b Leszek Koltunski
  {
50
  private static final int NUM_QUATERNIONS = 5;
51 251e574e Leszek Koltunski
52 bc0a685b Leszek Koltunski
  private GLSurfaceView mView;
53 f6d884d5 Leszek Koltunski
  private DistortedTexture mTexture;
54
  private DistortedEffectQueues mQueues;
55 10b7e588 Leszek Koltunski
  private GridCubes mGrid;
56 7589635e Leszek Koltunski
  private DynamicQuat mRot;
57 427ab7bf Leszek Koltunski
    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 b695a86a Leszek Koltunski
  QuaternionRenderer(GLSurfaceView v)
61 bc0a685b Leszek Koltunski
    {
62 f6d884d5 Leszek Koltunski
    mView    = v;
63
    mGrid    = new GridCubes(1,1,false);
64
    mTexture = new DistortedTexture(1,1,1);
65
    mQueues  = new DistortedEffectQueues();
66
    mRot     = new DynamicQuat();
67 b695a86a Leszek Koltunski
68
    Random rnd = new Random(System.currentTimeMillis());
69 bc0a685b Leszek Koltunski
    float x,y,z,w, len;
70 427ab7bf Leszek Koltunski
      
71 bc0a685b Leszek Koltunski
    for(int i=0; i<NUM_QUATERNIONS; i++)
72
      {
73 b695a86a Leszek Koltunski
      x = 2*rnd.nextFloat()-1;
74
      y = 2*rnd.nextFloat()-1;
75
      z = 2*rnd.nextFloat()-1;
76
      w = 2*rnd.nextFloat()-1;
77 427ab7bf Leszek Koltunski
    	 
78 bc0a685b Leszek Koltunski
      len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
79 427ab7bf Leszek Koltunski
    		
80 7589635e Leszek Koltunski
      mRot.add(new Static4D(x/len,y/len,z/len,w/len));
81 427ab7bf Leszek Koltunski
      }
82 bc0a685b Leszek Koltunski
    
83
    mRot.setCount(0);
84
    mRot.setDuration(8000);
85 7589635e Leszek Koltunski
    mRot.setMode(Dynamic.MODE_LOOP);
86 bc0a685b Leszek Koltunski
    }
87 427ab7bf Leszek Koltunski
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
   
90 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused) 
91
    {
92
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
93 f6d884d5 Leszek Koltunski
    mQueues.draw(System.currentTimeMillis(), mTexture,mGrid);
94 bc0a685b Leszek Koltunski
    }
95 427ab7bf Leszek Koltunski
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
    
98 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
99
    {
100 251e574e Leszek Koltunski
    float scaleFactor = width>height ? height/3:width/3;
101 427ab7bf Leszek Koltunski
102 f6d884d5 Leszek Koltunski
    mQueues.abortEffects(EffectTypes.MATRIX);
103
    mQueues.move( new Static3D( (width-scaleFactor)/2 , (height-scaleFactor)/2 , 0) );
104
    mQueues.scale(scaleFactor);
105
    mQueues.quaternion( mRot, new Static3D(0.5f,0.5f,0) );
106 427ab7bf Leszek Koltunski
       
107 bc0a685b Leszek Koltunski
    Distorted.onSurfaceChanged(width, height); 
108
    }
109 427ab7bf Leszek Koltunski
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
    
112 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
113
    {
114 e7a4ef16 Leszek Koltunski
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
115
116 bc0a685b Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
117
    Bitmap bitmap;
118 427ab7bf Leszek Koltunski
        
119 bc0a685b Leszek Koltunski
    try 
120
      {
121
      bitmap = BitmapFactory.decodeStream(is);
122
      } 
123
    finally 
124
      {
125 427ab7bf Leszek Koltunski
      try 
126
        {
127 bc0a685b Leszek Koltunski
        is.close();
128 427ab7bf Leszek Koltunski
        } 
129 bc0a685b Leszek Koltunski
      catch(IOException e) { }
130
      }  
131 427ab7bf Leszek Koltunski
      
132 f6d884d5 Leszek Koltunski
    mTexture.setTexture(bitmap);
133 427ab7bf Leszek Koltunski
      
134 bc0a685b Leszek Koltunski
    try
135
      {
136
      Distorted.onSurfaceCreated(mView.getContext());
137
      }
138
    catch(Exception ex)
139
      {
140
      android.util.Log.e("Quaternion", ex.getMessage() );
141 427ab7bf Leszek Koltunski
      }
142 bc0a685b Leszek Koltunski
    }
143
  }