Project

General

Profile

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

examples / src / main / java / org / distorted / examples / quaternion / QuaternionRenderer.java @ 8a99c681

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