Project

General

Profile

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

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

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
  private Random mRnd = new Random(System.currentTimeMillis());
54 7589635e Leszek Koltunski
  private DynamicQuat mRot;
55 427ab7bf Leszek Koltunski
    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58 bc0a685b Leszek Koltunski
  public QuaternionRenderer(GLSurfaceView v) 
59
    {
60
    mView = v;
61
    mCube = new DistortedCubes( 3, "000010000", SIZE);
62 427ab7bf Leszek Koltunski
      
63 7589635e Leszek Koltunski
    mRot = new DynamicQuat();
64 427ab7bf Leszek Koltunski
      
65 bc0a685b Leszek Koltunski
    float x,y,z,w, len;
66 427ab7bf Leszek Koltunski
      
67 bc0a685b Leszek Koltunski
    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 427ab7bf Leszek Koltunski
    	 
74 bc0a685b Leszek Koltunski
      len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
75 427ab7bf Leszek Koltunski
    		
76 7589635e Leszek Koltunski
      mRot.add(new Static4D(x/len,y/len,z/len,w/len));
77 427ab7bf Leszek Koltunski
      }
78 bc0a685b Leszek Koltunski
    
79
    mRot.setCount(0);
80
    mRot.setDuration(8000);
81 7589635e Leszek Koltunski
    mRot.setMode(Dynamic.MODE_LOOP);
82 bc0a685b Leszek Koltunski
    }
83 427ab7bf Leszek Koltunski
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
   
86 bc0a685b Leszek Koltunski
  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 427ab7bf Leszek Koltunski
      
91 bc0a685b Leszek Koltunski
    mCube.draw(System.currentTimeMillis());
92
    }
93 427ab7bf Leszek Koltunski
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
    
96 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
97
    {
98
    mCube.abortEffects(EffectTypes.MATRIX);
99 427ab7bf Leszek Koltunski
100 bc0a685b Leszek Koltunski
    if( width > height )
101
      {
102 7589635e Leszek Koltunski
      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 bc0a685b Leszek Koltunski
      }  
106
    else
107 7589635e Leszek Koltunski
      {
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 bc0a685b Leszek Koltunski
      }
112 427ab7bf Leszek Koltunski
     
113 7589635e Leszek Koltunski
    mCube.quaternion( new Static3D(3*SIZE/2,3*SIZE/2,0), mRot);
114 427ab7bf Leszek Koltunski
       
115 bc0a685b Leszek Koltunski
    Distorted.onSurfaceChanged(width, height); 
116
    }
117 427ab7bf Leszek Koltunski
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
    
120 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
121
    {
122
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
123
    Bitmap bitmap;
124 427ab7bf Leszek Koltunski
        
125 bc0a685b Leszek Koltunski
    try 
126
      {
127
      bitmap = BitmapFactory.decodeStream(is);
128
      } 
129
    finally 
130
      {
131 427ab7bf Leszek Koltunski
      try 
132
        {
133 bc0a685b Leszek Koltunski
        is.close();
134 427ab7bf Leszek Koltunski
        } 
135 bc0a685b Leszek Koltunski
      catch(IOException e) { }
136
      }  
137 427ab7bf Leszek Koltunski
      
138 bc0a685b Leszek Koltunski
    mCube.setBitmap(bitmap);
139 427ab7bf Leszek Koltunski
      
140 bc0a685b Leszek Koltunski
    try
141
      {
142
      Distorted.onSurfaceCreated(mView.getContext());
143
      }
144
    catch(Exception ex)
145
      {
146
      android.util.Log.e("Quaternion", ex.getMessage() );
147 427ab7bf Leszek Koltunski
      }
148 bc0a685b Leszek Koltunski
    }
149
  }